From b33b582142b9eb3d89284c2b37e462de46ed3b5f Mon Sep 17 00:00:00 2001 From: William Stein Date: Thu, 19 Oct 2006 21:30:50 -0700 Subject: [PATCH] Version of Pyrex distributed with SAGE on October 19, 2006 --- Cython/Compiler/ExprNodes.py | 8 ++++---- Cython/Compiler/Main.py | 5 ++++- Cython/Compiler/Nodes.py | 33 ++++++++++++++++++++------------- Cython/Compiler/Parsing.py | 2 +- Cython/Compiler/PyrexTypes.py | 34 ++++++++++++++++++++++------------ Cython/Compiler/Symtab.py | 8 ++++++++ Cython/Compiler/TypeSlots.py | 27 ++++++++++++++++++++++----- 7 files changed, 81 insertions(+), 36 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index d194335c..22f421c3 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1035,7 +1035,7 @@ class IndexNode(ExprNode): self.type = PyrexTypes.error_type if self.index.type.is_pyobject: self.index = self.index.coerce_to( - PyrexTypes.c_int_type, env) + PyrexTypes.c_py_ssize_t_type, env) if not self.index.type.is_int: error(self.pos, "Invalid index type '%s'" % @@ -1107,7 +1107,7 @@ class SliceIndexNode(ExprNode): if self.stop: self.stop.analyse_types(env) self.base = self.base.coerce_to_pyobject(env) - c_int = PyrexTypes.c_int_type + c_int = PyrexTypes.c_py_ssize_t_type if self.start: self.start = self.start.coerce_to(c_int, env) if self.stop: @@ -3101,7 +3101,7 @@ static void __Pyx_UnpackError(void) { PyErr_SetString(PyExc_ValueError, "unpack sequence of wrong size"); } -static PyObject *__Pyx_UnpackItem(PyObject *seq, int i) { +static PyObject *__Pyx_UnpackItem(PyObject *seq, Py_ssize_t i) { PyObject *item; if (!(item = PySequence_GetItem(seq, i))) { if (PyErr_ExceptionMatches(PyExc_IndexError)) @@ -3110,7 +3110,7 @@ static PyObject *__Pyx_UnpackItem(PyObject *seq, int i) { return item; } -static int __Pyx_EndUnpack(PyObject *seq, int i) { +static int __Pyx_EndUnpack(PyObject *seq, Py_ssize_t i) { PyObject *item; if (item = PySequence_GetItem(seq, i)) { Py_DECREF(item); diff --git a/Cython/Compiler/Main.py b/Cython/Compiler/Main.py index 254c8037..39912622 100644 --- a/Cython/Compiler/Main.py +++ b/Cython/Compiler/Main.py @@ -91,7 +91,10 @@ class Context: def find_pxd_file(self, module_name, pos): # Search include directories for the .pxd file # corresponding to the given (full) module name. - pxd_filename = "%s.pxd" % module_name + if "." in module_name: + pxd_filename = "%s.pxd" % os.path.join(*module_name.split('.')) + else: + pxd_filename = "%s.pxd" % module_name return self.search_include_directories(pxd_filename, pos) def find_include_file(self, filename, pos): diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index a70f7f72..e1e7ddd0 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -199,11 +199,19 @@ class ModuleNode(Node, BlockNode): code.putln('/* Generated by Pyrex %s on %s */' % ( Version.version, time.asctime())) code.putln('') + code.putln('#define PY_SSIZE_T_CLEAN') for filename in env.python_include_files: code.putln('#include "%s"' % filename) code.putln("#ifndef PY_LONG_LONG") code.putln(" #define PY_LONG_LONG LONG_LONG") code.putln("#endif") + code.putln("#if PY_VERSION_HEX < 0x02050000") + code.putln(" typedef int Py_ssize_t;") + code.putln(" #define PY_SSIZE_T_MAX INT_MAX") + code.putln(" #define PY_SSIZE_T_MIN INT_MIN") + code.putln(" #define PyInt_FromSsize_t(z) PyInt_FromLong(z)") + code.putln(" #define PyInt_AsSsize_t(o) PyInt_AsLong(o)") + code.putln("#endif") self.generate_extern_c_macro_definition(code) code.putln("%s double pow(double, double);" % Naming.extern_c_macro) self.generate_includes(env, cimported_modules, code) @@ -624,12 +632,12 @@ class ModuleNode(Node, BlockNode): # a __getitem__ method is present. It converts its # argument to a Python integer and calls mp_subscript. code.putln( - "static PyObject *%s(PyObject *o, int i) {" % + "static PyObject *%s(PyObject *o, Py_ssize_t i) {" % scope.mangle_internal("sq_item")) code.putln( "PyObject *r;") code.putln( - "PyObject *x = PyInt_FromLong(i); if(!x) return 0;") + "PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0;") code.putln( "r = o->ob_type->tp_as_mapping->mp_subscript(o, x);") code.putln( @@ -715,7 +723,7 @@ class ModuleNode(Node, BlockNode): del_entry = scope.lookup_here("__delslice__") code.putln("") code.putln( - "static int %s(PyObject *o, int i, int j, PyObject *v) {" % + "static int %s(PyObject *o, Py_ssize_t i, Py_ssize_t j, PyObject *v) {" % scope.mangle_internal("sq_ass_slice")) code.putln( "if (v) {") @@ -3542,8 +3550,8 @@ utility_function_predeclarations = \ """ typedef struct {PyObject **p; char *s;} __Pyx_InternTabEntry; /*proto*/ typedef struct {PyObject **p; char *s; long n;} __Pyx_StringTabEntry; /*proto*/ -static PyObject *__Pyx_UnpackItem(PyObject *, int); /*proto*/ -static int __Pyx_EndUnpack(PyObject *, int); /*proto*/ +static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t); /*proto*/ +static int __Pyx_EndUnpack(PyObject *, Py_ssize_t); /*proto*/ static int __Pyx_PrintItem(PyObject *); /*proto*/ static int __Pyx_PrintNewline(void); /*proto*/ static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ @@ -3553,7 +3561,7 @@ static PyObject *__Pyx_GetExcValue(void); /*proto*/ static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, char *name); /*proto*/ static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/ static int __Pyx_GetStarArgs(PyObject **args, PyObject **kwds,\ - char *kwd_list[], int nargs, PyObject **args2, PyObject **kwds2); /*proto*/ + char *kwd_list[], Py_ssize_t nargs, PyObject **args2, PyObject **kwds2); /*proto*/ static void __Pyx_WriteUnraisable(char *name); /*proto*/ static void __Pyx_AddTraceback(char *funcname); /*proto*/ static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name, long size); /*proto*/ @@ -3595,7 +3603,7 @@ static int __Pyx_PrintItem(PyObject *v) { return -1; if (PyString_Check(v)) { char *s = PyString_AsString(v); - int len = PyString_Size(v); + Py_ssize_t len = PyString_Size(v); if (len > 0 && isspace(Py_CHARMASK(s[len-1])) && s[len-1] != ' ') @@ -3650,7 +3658,8 @@ static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) { } if (PyString_Check(type)) ; - else if (PyClass_Check(type)) +/* else if (PyClass_Check(type)) */ + else if (PyType_Check(type) || PyClass_Check(type)) ; /*PyErr_NormalizeException(&type, &value, &tb);*/ else if (PyInstance_Check(type)) { /* Raising an instance. The value should be a dummy. */ @@ -3739,7 +3748,7 @@ static int __Pyx_GetStarArgs( PyObject **args, PyObject **kwds, char *kwd_list[], - int nargs, + Py_ssize_t nargs, PyObject **args2, PyObject **kwds2) { @@ -3799,10 +3808,8 @@ static int __Pyx_GetStarArgs( bad: Py_XDECREF(args1); Py_XDECREF(kwds1); - if (*args2) - Py_XDECREF(*args2); - if (*kwds2) - Py_XDECREF(*kwds2); + Py_XDECREF(*args2); + Py_XDECREF(*kwds2); return -1; } """ diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index 827c2559..909223de 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -1257,7 +1257,7 @@ def looking_at_dotted_name(s): # "void", "signed", "unsigned" #) -basic_c_type_names = ("void", "char", "int", "float", "double") +basic_c_type_names = ("void", "char", "int", "float", "double", "Py_ssize_t") sign_and_longness_words = ("short", "long", "signed", "unsigned") diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 88576642..128f41d4 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -293,8 +293,8 @@ class CNumericType(CType): 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): @@ -340,6 +340,12 @@ class CIntType(CNumericType): self.is_returncode = is_returncode +class CPySSizeTType(CIntType): + + to_py_function = "PyInt_FromSsize_t" + from_py_function = "PyInt_AsSsize_t" + + class CUIntType(CIntType): to_py_function = "PyLong_FromUnsignedLong" @@ -699,6 +705,7 @@ c_short_type = CIntType(1, 1, "T_SHORT") 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) c_uchar_type = CIntType(0, 0, "T_UBYTE") c_ushort_type = CIntType(1, 0, "T_USHORT") @@ -706,9 +713,9 @@ c_uint_type = CUIntType(2, 0, "T_UINT") c_ulong_type = CULongType(3, 0, "T_ULONG") c_ulonglong_type = CULongLongType(4, 0, "T_ULONGLONG") -c_float_type = CFloatType(5, "T_FLOAT") -c_double_type = CFloatType(6, "T_DOUBLE") -c_longdouble_type = CFloatType(7) +c_float_type = CFloatType(6, "T_FLOAT") +c_double_type = CFloatType(7, "T_DOUBLE") +c_longdouble_type = CFloatType(8) c_null_ptr_type = CNullPtrType(c_void_type) c_char_array_type = CCharArrayType(None) @@ -720,7 +727,7 @@ c_returncode_type = CIntType(2, 1, "T_INT", is_returncode = 1) error_type = ErrorType() -lowest_float_rank = 5 +lowest_float_rank = 6 rank_to_type_name = ( "char", # 0 @@ -728,9 +735,10 @@ rank_to_type_name = ( "int", # 2 "long", # 3 "PY_LONG_LONG", # 4 - "float", # 5 - "double", # 6 - "long double", # 7 + "Py_ssize_t", # 5 + "float", # 6 + "double", # 7 + "long double", # 8 ) sign_and_rank_to_type = { @@ -745,9 +753,10 @@ sign_and_rank_to_type = { (1, 2): c_int_type, (1, 3): c_long_type, (1, 4): c_longlong_type, - (1, 5): c_float_type, - (1, 6): c_double_type, - (1, 7): c_longdouble_type, + (1, 5): c_py_ssize_t_type, + (1, 6): c_float_type, + (1, 7): c_double_type, + (1, 8): c_longdouble_type, } modifiers_and_name_to_type = { @@ -763,6 +772,7 @@ modifiers_and_name_to_type = { (1, 0, "int"): c_int_type, (1, 1, "int"): c_long_type, (1, 2, "int"): c_longlong_type, + (1, 0, "Py_ssize_t"): c_py_ssize_t_type, (1, 0, "float"): c_float_type, (1, 0, "double"): c_double_type, (1, 1, "double"): c_longdouble_type, diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index 6ae4e3bf..ab111299 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -566,6 +566,14 @@ class ModuleScope(Scope): entry = self.lookup_here(name) if entry: if not (entry.is_pyglobal and not entry.as_module): + # SAGE -- I put this here so Pyrex + # cimport's work across directories. + # Currently it tries to multiply define + # every module appearing in an import list. + # It shouldn't be an error for a module + # name to appear again, and indeed the generated + # code compiles fine. + return entry error(pos, "'%s' redeclared" % name) return None else: diff --git a/Cython/Compiler/TypeSlots.py b/Cython/Compiler/TypeSlots.py index c173710a..b8a05d73 100644 --- a/Cython/Compiler/TypeSlots.py +++ b/Cython/Compiler/TypeSlots.py @@ -26,6 +26,7 @@ class Signature: # 'i' int # 'I' int * # 'l' long + # 'Z' Py_ssize_t # 's' char * # 'S' char ** # 'r' int used only to signal exception @@ -42,6 +43,7 @@ class Signature: 'i': PyrexTypes.c_int_type, 'I': PyrexTypes.c_int_ptr_type, 'l': PyrexTypes.c_long_type, + 'Z': PyrexTypes.c_py_ssize_t_type, 's': PyrexTypes.c_char_ptr_type, 'S': PyrexTypes.c_char_ptr_ptr_type, 'r': PyrexTypes.c_returncode_type, @@ -354,18 +356,30 @@ ternaryfunc = Signature("OOO", "O") # typedef PyObject * (*ternaryfunc)(P iternaryfunc = Signature("TOO", "O") # typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); callfunc = Signature("T*", "O") # typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); inquiry = Signature("T", "i") # typedef int (*inquiry)(PyObject *); +lenfunc = Signature("T", "Z") # typedef Py_ssize_t (*lenfunc)(PyObject *); + # typedef int (*coercion)(PyObject **, PyObject **); intargfunc = Signature("Ti", "O") # typedef PyObject *(*intargfunc)(PyObject *, int); +ssizeargfunc = Signature("TZ", "O") # typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t); intintargfunc = Signature("Tii", "O") # typedef PyObject *(*intintargfunc)(PyObject *, int, int); +ssizessizeargfunc = Signature("TZZ", "O") # typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t); intobjargproc = Signature("TiO", 'r') # typedef int(*intobjargproc)(PyObject *, int, PyObject *); +ssizeobjargproc = Signature("TZO", 'r') # typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *); intintobjargproc = Signature("TiiO", 'r') # typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *); +ssizessizeobjargproc = Signature("TZZO", 'r') # typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *); + intintargproc = Signature("Tii", 'r') +ssizessizeargproc = Signature("TZZ", 'r') objargfunc = Signature("TO", "O") objobjargproc = Signature("TOO", 'r') # typedef int (*objobjargproc)(PyObject *, PyObject *, PyObject *); getreadbufferproc = Signature("TiP", 'i') # typedef int (*getreadbufferproc)(PyObject *, int, void **); getwritebufferproc = Signature("TiP", 'i') # typedef int (*getwritebufferproc)(PyObject *, int, void **); getsegcountproc = Signature("TI", 'i') # typedef int (*getsegcountproc)(PyObject *, int *); getcharbufferproc = Signature("TiS", 'i') # typedef int (*getcharbufferproc)(PyObject *, int, const char **); +readbufferproc = Signature("TZP", "Z") # typedef Py_ssize_t (*readbufferproc)(PyObject *, Py_ssize_t, void **); +writebufferproc = Signature("TZP", "Z") # typedef Py_ssize_t (*writebufferproc)(PyObject *, Py_ssize_t, void **); +segcountproc = Signature("TZ", "Z") # typedef Py_ssize_t (*segcountproc)(PyObject *, Py_ssize_t *); +writebufferproc = Signature("TZS", "Z") # typedef Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, char **); objargproc = Signature("TO", 'r') # typedef int (*objobjproc)(PyObject *, PyObject *); # typedef int (*visitproc)(PyObject *, void *); # typedef int (*traverseproc)(PyObject *, visitproc, void *); @@ -454,14 +468,17 @@ PyNumberMethods = ( MethodSlot(binaryfunc, "nb_true_divide", "__truediv__"), MethodSlot(ibinaryfunc, "nb_inplace_floor_divide", "__ifloordiv__"), MethodSlot(ibinaryfunc, "nb_inplace_true_divide", "__itruediv__"), + + # Added in release 2.5 + MethodSlot(unaryfunc, "nb_index", "__index__"), ) PySequenceMethods = ( - MethodSlot(inquiry, "sq_length", "__len__"), # EmptySlot("sq_length"), # mp_length used instead + MethodSlot(lenfunc, "sq_length", "__len__"), # EmptySlot("sq_length"), # mp_length used instead EmptySlot("sq_concat"), # nb_add used instead EmptySlot("sq_repeat"), # nb_multiply used instead SyntheticSlot("sq_item", ["__getitem__"], "0"), #EmptySlot("sq_item"), # mp_subscript used instead - MethodSlot(intintargfunc, "sq_slice", "__getslice__"), + MethodSlot(ssizessizeargfunc, "sq_slice", "__getslice__"), EmptySlot("sq_ass_item"), # mp_ass_subscript used instead SyntheticSlot("sq_ass_slice", ["__setslice__", "__delslice__"], "0"), MethodSlot(cmpfunc, "sq_contains", "__contains__"), @@ -470,7 +487,7 @@ PySequenceMethods = ( ) PyMappingMethods = ( - MethodSlot(inquiry, "mp_length", "__len__"), + MethodSlot(lenfunc, "mp_length", "__len__"), MethodSlot(objargfunc, "mp_subscript", "__getitem__"), SyntheticSlot("mp_ass_subscript", ["__setitem__", "__delitem__"], "0"), ) @@ -565,8 +582,8 @@ MethodSlot(initproc, "", "__new__") MethodSlot(destructor, "", "__dealloc__") MethodSlot(objobjargproc, "", "__setitem__") MethodSlot(objargproc, "", "__delitem__") -MethodSlot(intintobjargproc, "", "__setslice__") -MethodSlot(intintargproc, "", "__delslice__") +MethodSlot(ssizessizeobjargproc, "", "__setslice__") +MethodSlot(ssizessizeargproc, "", "__delslice__") MethodSlot(getattrofunc, "", "__getattr__") MethodSlot(setattrofunc, "", "__setattr__") MethodSlot(delattrofunc, "", "__delattr__") -- 2.26.2