[Python-3000] bytes & Py_TPFLAGS_BASETYPE

Mathieu Fenniak mathieu.fenniak at gmail.com
Mon Sep 17 18:44:18 CEST 2007


On 17-Sep-07, at 9:00 AM, Guido van Rossum wrote:
> Maybe I should apologize for pushing back so hard, but in my
> experience most people who subclass a built-in type do it because they
> can, not because they should -- the lamented "path" module being a
> prime example in my view.
>
> I'm still not convinced of the usefulness in your case -- what would
> you lose if you just passed a bytes instance around instead of an
> instance of the subclass you'd like to have?

The builtin type subclasses in pyPdf (including the would-be bytes  
subclass) add additional methods that every pdf object is expected to  
support.  All the PDF object types have two additional methods  
(writeToStream and getObject) that have varying behavior for each  
class: (relatively inconsequential PDF information follows)

"writeToStream" method that serializes the object -- a byte string  
would write out <68656c6c6f>, a text string (hello), and so on for  
other more complex types (dictionaries, labels, arrays, PDF data  
streams).  The type is also responsible for encrypting itself when  
applicable.

PDF files also have an ability to reference objects elsewhere in the  
file.  For example, the length of a content stream can be a simple  
"500 bytes", or it can be "read this length at offset X in the  
file".  Since almost any object can be an indirect object reference,  
the library objects support a "getObject" method that returns self --  
excluding PDF "indirect object reference" objects, which read an  
object from a table in a PDF file.

If you decide that bytes should be subclassable, I've included with  
this e-mail a patch that adds the basetype bit, adds some unit tests  
for bytes subclasses, and includes __dict__ in the bytes_reduce  
method (for pickling subclass instances).  I was going to upload this  
to the SF patch manager, but it appears to be closed to permit only  
project members access.

Mathieu Fenniak

-------------- next part --------------
A non-text attachment was scrubbed...
Name: bytes-subclass-patch.diff
Type: application/octet-stream
Size: 4933 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-3000/attachments/20070917/317ebd40/attachment.obj 


More information about the Python-3000 mailing list