PySequence_SetItem

Jack Diederich jack at psynchronous.com
Wed Aug 16 20:49:11 EDT 2006


On Wed, Aug 16, 2006 at 05:13:23PM -0700, John Machin wrote:
> 
> Jack Diederich wrote:
> > On Wed, Aug 16, 2006 at 03:25:39PM -0700, John Machin wrote:
> 
> > > > >
> > > > > Not the OP's problem, but a bug in the manual: example in the chapter
> > > > > that the OP was reading acts as though the 2nd arg to PyObject_SetItem
> > > > > is a C int (as it is for the List and Sequence varieties) -- it is in
> > > > > fact a (PyObject *), which explains the SystemError that I got.
> 
> Please don't forget this which is definitely a bug in the docs.
> 

You should file a bug (or patch) against the docs.  I haven't ever
updated them myself so I won't be the guy that ends up fixing it.

<snip>
> > Since this had been the same going all the way back to 2.2 I assumed it was
> > common.  It isn't, simlar functions in other objects guard against the
> > possibility.
> 
> I'm a bit curious here: tuples naturally don't fill in the sq_ass_item
> slot and I couldn't think of another object that would be initialised
> with NULL(s) in the expectation of being filled in later or maybe not
> ... enlightenment, please.
> 
> > I'll check in the change assuming no one else has already (and assuming
> > the trunk isn't currently frozen, I'll have to check).

Nothing else is quite the same.  As you point out almost nothing in core even 
supports the sq_ass_item slot.  Those that do aren't initialized with a fixed
size (deque takes an iterator) so they can't have NULLs in them.  Tuples have 
no ass_item so you _have to_ use the concrete API.  Mappings which have their 
own ass slot (like dicts) are sparse so they are used to handling nulls.

None of them allow you to segfault the interpreter.

The generic Sequence API isn't useful when you are initializing a sequence
object because it is either a list, a tuple, or some extension of your own
creation.  Because you know what you are making it is easiest to use the
concrete API.  Heck, if it is a tuple you have to.  No one noticed the
bug because no one uses that API for initializing objects in real life.
It is handy for functions that take a mutable list as an argument.

> For avoidance of doubt: the change is to use Py_XDECREF, yes/no?

Yes.

-Jack



More information about the Python-list mailing list