[Python-Dev] Assertion in _PyManagedBuffer_FromObject()

Stefan Krah stefan at bytereef.org
Sat Mar 3 12:08:38 CET 2012


Stefan Behnel <stefan_ml at behnel.de> wrote:
> > 1. assert() is the wrong tool for this job
> 
> Absolutely.

I disagree. This assert() is meant for extension authors and not end users. I
can't see how a reasonable release procedure would fail to trigger the assert().

My procedure as a C extension author is to test against a new Python version
and *then* set the PyPI classifier for that version.


If I download a C extension that doesn't have the 3.3 classifier set,
then as a user I would not be upset if the extension throws an assert or,
as Thomas Wouters pointed out, continues to work as before if not compiled
in debug mode.



> > 2. the current check is too strict (it should just check for obj !=
> > NULL, not obj == &exporter)
> 
> I don't know. The documentation isn't very clear on the cases where obj may
> be NULL. Definitely on error, ok, but otherwise, the bf_getbuffer() docs do
> not explicitly say that it must not be NULL (they just mention a "standard"
> case):
> 
> http://docs.python.org/dev/c-api/typeobj.html#buffer-object-structures

How about this:

"The value of view.obj is the equivalent of the return value of any C-API
 function that returns a new reference. The value must be NULL on error
 or a valid new reference to an exporting object.

 For a chain or a tree of views, there are two possible schemes:

   1) Re-export: Each member of the tree pretends to be the exporting
      object and sets view.obj to a new reference to itself.

   2) Redirect: The buffer request is redirected to the root object
      of the tree. Here view.obj will be a reference to the root object."



I think it's better not to complicate this familiar scheme of owning
a reference by allowing view.obj==NULL for the general case.


view.obj==NULL was introduced for temporary wrapping of ad-hoc memoryviews
via PyBuffer_FillInfo() and now also PyMemoryView_FromMemory().

That's why I explicitly wrote the following in the documentation of
PyBuffer_FillInfo():

"If this function is used as part of a getbufferproc, exporter MUST be
 set to the exporting object. Otherwise, exporter MUST be NULL."


Stefan Krah





More information about the Python-Dev mailing list