Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which methods implement the buffer interface in Python?

Tags:

python

buffer

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?

like image 495
Eli Courtwright Avatar asked Jan 16 '10 23:01

Eli Courtwright


People also ask

How do you implement a buffer in Python?

In Python, you don't have to care about memory management. You don't need to reserve "buffers", just assign the variables you want, and use them. If you assign too many (for your RAM), you might run into MemoryError s, indicating you don't have enough memory. You could use a Queue as some kind of buffer, though, e.g.

What is a buffer interface in Python?

Buffer structures (or simply “buffers”) are useful as a way to expose the binary data from another object to the Python programmer. They can also be used as a zero-copy slicing mechanism. Using their ability to reference a block of memory, it is possible to expose any data to the Python programmer quite easily.

What is file buffer in Python?

Buffering is the process of storing a chunk of a file in a temporary memory until the file loads completely. In python there are different values can be given. If the buffering is set to 0 , then the buffering is off. The buffering will be set to 1 when we need to buffer the file.

What is buffer size Python?

The buffer size tells how much data can be held at a time until it is used. io. DEFAULT_BUFFER_SIZE can tell the default buffer size of your platform. Optionally, you can pass an integer to buffering to set the buffering policy: 0 to switch off buffering (only allowed in binary mode)


1 Answers

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:-(.

like image 166
Alex Martelli Avatar answered Sep 22 '22 08:09

Alex Martelli