Unpacking a python list in C?
Michael Hudson
mwh at python.net
Fri May 3 10:39:24 EDT 2002
Ken Seehof <kseehof at neuralintegrator.com> writes:
> Michael Hudson wrote:
> > Ken Seehof <kseehof at neuralintegrator.com> writes:
> >
> > > Here's some real code that's pretty close to what you want. You can
> > > use PyList_* instead of PySequence_* if speed is more important than
> > > flexibility (PySequence works with any sequence object).
> > > This example is a little different than what you requested (it's a
> > > working python extension function). But it does show how to iterate
> > > a python list.
> > >
> > > /////////////////////////
> > > // syntax: obj = manifold([seq])
> > > // returns: new manifold object
> > > //
> > > // Creates a manifold object, and initializes with sequence
> >
> > Are you not doing any error checking for clarity?
>
> Nah, just plain laziness :-)
Fair enough.
> I think the only error checking I missed is the sequence type check.
You miss stuff that's relavent if you pass a sequence implemented in
Python in; PySequence_GetItem can fail in arbitrary ways in that
case (beyond the Devious class I posted).
> > > static PyObject *module_manifold(PyObject *self, PyObject *args)
> > > {
> > > PyObject *seq = NULL;
> > > if (!PyArg_ParseTuple(args, "|O", &seq)) return NULL;
> >
> > It would be wise to check that "seq" is indeed a sequence at some
> > point...
>
> That's a good idea. Thanks.
>
> > Also, if you call this function with no argument it will return NULL
> > but with no exception set... don't do that.
>
> Not true. Actually, calling with no argument is valid for "|O", and
> in this case leaves seq=NULL (which skips the sequence initialization
> as intended).
Yeah, you're right of course. I'd have written the same logic in a
different way, but there's no reason to prefer my way over yours.
[...stuff...]
> Thanks, most of your suggestions are very helpful and all your
> suggestions are appreciated.
Glad to hear it! I should really write some docs for the extending
and embedding manual...
> I look forward to putting this stuff on SourceForge to get more
> feedback, when I get some free time.
>
> In case you're wondering, "manifold" is a new kind of list.
So why's it called manifold then? To me manifold means "topological
space locally isomorphic to Euclidean space"...
> - safe deletion during iteration
> - O(constant) fast __contains__ (in) operator
> - global explicit object removal (fast)
> - python thread safe
>
> Basically it solves the problem of explicit removal of an object from
> all "lists" of which it is a member, especially in situations where
> object removal of any kind would normally be hazardous. I've been using
> it for a couple weeks, and it seems stable. The sequence initialization
> that I posted is one of a few recent additions for completeness.
Not sure I really understand... can you show me how it's used?
Cheers,
M.
--
QNX... the OS that walks like a duck, quacks like a duck, but is,
in fact, a platypus. ... the adventures of porting duck software
to the platypus were avoidable this time.
-- Chris Klein, alt.sysadmin.recovery
More information about the Python-list
mailing list