[Python-Dev] Python's C interface for types
Nick Maclaren
nmm1 at cus.cam.ac.uk
Fri Jan 26 17:37:08 CET 2007
Thanks very much! That answers most things. Yes, I had got many
of my answers from searching the source, but there is clearly some
history there, and it isn't always clear what is current. Here are
a few responses to the areas of confusion:
> nb_invert is used for bitwise inversion (~) and PyNumber_Invert(). It's not
> historical, it's actual.
Ah! So it's NOT 1/x! No relevant to floating-point, then.
> I don't recall ever seeing useful documentation on coerce() and nb_coerce.
> I suggest not to use it; it's gone in Python 3.0 anyway.
Excellent! Task completed :-)
> Which numeric property? the fact that it returns a C long? Or that, for
> natural numbers, it *seems* to return self?
The latter. hash(123) == hash(123.0) for example. It is a real
pain for advanced formats. Making it the same for things that compare
equal isn't a problem.
> [inplace ] I assume your floating-point type is
> immutable, so you won't have to implement them.
I haven't done anything special to flag it as such, but it is.
> Where do you allocate this space, and how do you allocate it? If it's space
> you malloc() and store somewhere in the type struct, yecchh. You should not
> just allocate stuff at the end of the type struct, as the type struct's
> layout is not under your control (we actually extend the type struct as
> needed, which is why newer features end up in less logical places at the end
> of the struct ;) I would suggest using attributes of the type instead, with
> the normal Python refcounting. That means the 'extra space' has to be an
> actual Python object, though.
PyMem_Malloc. I can certainly make it an attribute, as the overhead
isn't large for a per-class object. It is just a block of mutable
memory, opaque to the Python layer, and NOT containing any pointers!
> I don't you can make your own type marshallable. For pickle it's more or
> less the same as for Python types. The pickle docs (and maybe
> http://www.python.org/dev/peps/pep-0307/) probably cover what you want to
> know. You can also look at one of the complexer builtin types that support
> pickling, like the datetime types.
The only documentation I have found is how to do it in Python. Is
that what you mean? I will look at the datetime types.
> You can use PyGetSetDef to get 'easy' attributes with getters and setters.
> http://docs.python.org/api/type-structs.html#l2h-1020
I was put off by some of the warnings. I will revisit it.
> There's nothing I can think of that is a natural match for that in standard
> Python methods. I would suggest just making it a classmethod.
> (dict.fromkeysis a good example of a classmethod in C.)
Thanks. That is a useful reference. Reductions are a problem in
many languages.
Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1 at cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
More information about the Python-Dev
mailing list