Alex Rivera | Logout

Which methods implement the buffer interface in Python?

Asked 2010-01-16T23:08:40.993
13

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:

>>
Edit
Report

1 Answer

1

The documentation says,

Python objects implemented in C can export a group of functions called the “buffer interface.”

Your Foo class is not implemented in C, is it...?

answered 2010-01-16T23:18:03.983

Your Answer