[Pyrex] Migration to Py 2.5: Py_ssize_t and exceptions

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Tue Apr 25 14:00:49 CEST 2006


Hi again,

two remarks and one fix.

First remark:
> Index: Pyrex/Compiler/PyrexTypes.py
> ===================================================================
> --- Pyrex/Compiler/PyrexTypes.py	(Revision 131)
> +++ Pyrex/Compiler/PyrexTypes.py	(Arbeitskopie)
> @@ -293,8 +293,8 @@
>      default_value = "0"
>      
>      parsetuple_formats = ( # rank -> format
> -        "?HIkK???", # unsigned
> -        "chilLfd?", # signed
> +        "?HIkK????", # unsigned
> +        "chilL?fd?", # signed
>      )
>      
>      def __init__(self, rank, signed = 1, pymemberdef_typecode = None):

This means you can't tuple-parse Py_ssize_t values, although there is a new
"n" type code in Python 2.5. Since Python 2.2-2.4 can't handle this, I left
that out. You'd have to go for conditional compilation to allow the generated
code to run both on Python 2.2-2.4 and Python 2.5, so that's quite some
effort. All you loose is being able to say:

def myfunc(Py_ssize_t l):
  return l + 1

Instead, you have to write

def myfunc(py_l):
  cdef Py_ssize_t l
  l = py_l
  return l + 1

Note that this is a Python functions, C functions and slot functions do not
have this restrition. You can happily write

def __setitem__(Py_ssize_t index, value):
  ...

and the like. I think that's an acceptable trade-off.


Second thing:
The above example looks somewhat ugly due to the unwieldy look of
"Py_ssize_t". I could imagine having a Pyrex-built-in type "size" instead.
This would make the example:

cdef __setitem__(size index, value)

Looks a bit nicer, but may break more code, as "size" is more likely to be
already in use than "Py_ssize_t". An alternative name could be "pysize" or
"py_size".


And the fix:
> @@ -699,6 +705,7 @@
>  c_int_type =      CIntType(2, 1, "T_INT")
>  c_long_type =     CIntType(3, 1, "T_LONG")
>  c_longlong_type = CLongLongType(4, 1, "T_LONGLONG")
> +c_py_ssize_t_type = CPySSizeTType(5, 1, "T_PY_SSIZE_T")
>  
>  c_uchar_type =     CIntType(0, 0, "T_UBYTE")
>  c_ushort_type =    CIntType(1, 0, "T_USHORT")

This is actually not quite correct, "T_PY_SSIZE_T" does not exist, it just
slipped through on my side. The correct line is

+c_py_ssize_t_type = CPySSizeTType(5, 1)

Regards,
Stefan



More information about the Pyrex mailing list