ctypedef extern class without importing

81 views
Skip to first unread message

Jeroen Demeyer

unread,
Jun 16, 2017, 9:56:36 AM6/16/17
to cython-users
One thing which annoys me about the syntax

cdef extern from "complexobject.h":
ctypedef class __builtin__.complex [object PyComplexObject]:
cdef Py_complex cval

is that it requires the type (__builtin__.complex in this case) to be
importable. Unfortunately, some types in Python are exposed through the
C/API but are not available as Python object in some standard module (I
am in particular interested in PyWrapperDescr_Type). The above syntax
also doesn't work for __builtin__.long because "long" in Cython means
something else.

Would it be possible to allow specifying the PyTypeObject name instead
of an importable name? So something like

cdef extern from "complexobject.h":
ctypedef class CUSTOM_NAME [object PyComplexObject, type
PyComplex_Type]:
cdef Py_complex cval

Where CUSTOM_NAME is simply an identifier used to refer to the type.

Robert Bradshaw

unread,
Jun 16, 2017, 6:46:00 PM6/16/17
to cython...@googlegroups.com
Even if a PyTypeObject name is exposed (e.g. via a header), it may not
be initialized until the corresponding module is imported. Any ideas
on how best to solve that?
> --
>
> --- You received this message because you are subscribed to the Google
> Groups "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jeroen Demeyer

unread,
Jun 18, 2017, 5:35:01 AM6/18/17
to cython...@googlegroups.com
On 2017-06-17 00:45, Robert Bradshaw wrote:
> Even if a PyTypeObject name is exposed (e.g. via a header), it may not
> be initialized until the corresponding module is imported. Any ideas
> on how best to solve that?

Of course, there is no solution to that. But I am thinking about
standard Python types, which would be initialized when Python starts up.

Jeroen Demeyer

unread,
Jul 7, 2017, 11:21:03 AM7/7/17
to cython...@googlegroups.com
Moreover, PyType_Ready() on a type calls PyType_Ready() on the base type
tp_base if needed.

I'm still interested in this feature, do you think it could work?

Robert Bradshaw

unread,
Jul 7, 2017, 6:59:23 PM7/7/17
to cython...@googlegroups.com
Worth considering. On the other hand, how bad would it be to create a
module (.py files) that simply enumerates these types. (e.g. PyLong =
long, ...) and then use that as the name?

Jeroen Demeyer

unread,
Jul 10, 2017, 4:25:38 AM7/10/17
to cython...@googlegroups.com
On 2017-07-08 00:58, Robert Bradshaw wrote:
> Worth considering. On the other hand, how bad would it be to create a
> module (.py files)

Would it be possible to create a module in the "cython" package, say
cython.types defining a few of these?

Robert Bradshaw

unread,
Jul 11, 2017, 1:28:38 AM7/11/17
to cython...@googlegroups.com
Yes, that would also be possible. (Maybe cpython.builtin_types?)
Reply all
Reply to author
Forward
0 new messages