Which methods implement the buffer interface in Python?

buffer, python

Solution

Peculiarly, there are no special methods that a pure-Python coded class can directly implement to support the `buffer` interface -- that would have been PEP 298, but it was withdrawn.

I'm afraid you'll have to use some explicit attribute or method (or a built-in like `str` which internally calls a special method) to pass your class's instances to `file.write` &c:-(.

Problem

I have a custom class with a `serialize` method, and I want to be able to write this class directly to files and have the return value of the `serialize` method get written, in Python 2.6. (I'm not trying to pickle my objects, this is something totally different.) For example: ``` class Foo(object): def serialize(self): return "Hello World!" __str__ = serialize foo = Foo() f = open("foo.dat", "wb") f.write(foo) ``` However, when I run this code, I get the following exception ``` Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: argument 1 must be convertible to a buffer, not Foo ``` Okay, so I need my `Foo` class to implement the `buffer` interface. I even see in the buffer documentation that it says: "An example user of the buffer interface is the file object’s write() method. Any object that can export a series of bytes through the buffer interface can be written to a file." So apparently I can do what I want, but the docs don't actually say which methods I need to implement in order to have implemented the buffer interface. I've tried implementing `__str__`, `__unicode__`, `__len__`, and even `__sizeof__`. I've implemented `__getitem__`, `__setitem__`, and `__delitem__`, accepting both `int` and `slice` arguments. I've even tried implementing the deprecated `__getslice__`, `__setslice__`, and `__delslice__` methods just to be safe. No matter what I try, I still get exactly the same exception. For reference, here are the methods of the `buffer` builtin class: ``` >>> dir(buffer) ['__add__', '__class__', '__cmp__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__getslice__', '__hash__', '__init__', '__len__', '__mul__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__'] ``` I'd like to avoid implementing all of them one by one, and I'd especially like to find the documentation on exactly which methods are necessary. Is this something that can only be implemented in C extension classes? Or am I missing something obvious?

Original source