From: Robert Bradshaw Date: Sat, 12 Dec 2009 06:11:44 +0000 (-0800) Subject: Streamline writing module-level compatability macros. X-Git-Tag: 0.12.1~40 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5109df243090ec49b3f6cdc5254feaacda464afd;p=cython.git Streamline writing module-level compatability macros. --- diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 25b25b4c..5a45ad71 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -414,154 +414,163 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): self.generate_cfunction_predeclarations(module, modulecode, defined_here) def generate_module_preamble(self, env, cimported_modules, code): - code.putln('/* Generated by Cython %s on %s */' % ( + code.putln("/* Generated by Cython %s on %s */" % ( Version.version, time.asctime())) - code.putln('') - code.putln('#define PY_SSIZE_T_CLEAN') + 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_PYTHON_H") code.putln(" #error Python headers needed to compile C extensions, please install development version of Python.") code.putln("#else") code.globalstate["end"].putln("#endif /* Py_PYTHON_H */") - code.putln("#ifndef PY_LONG_LONG") - code.putln(" #define PY_LONG_LONG LONG_LONG") - code.putln("#endif") - code.putln("#ifndef DL_EXPORT") - code.putln(" #define DL_EXPORT(t) t") - code.putln("#endif") - code.putln("#if PY_VERSION_HEX < 0x02040000") - code.putln(" #define METH_COEXIST 0") - code.putln(" #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)") - code.putln(" #define PyDict_Contains(d,o) PySequence_Contains(d,o)") - code.putln("#endif") + + code.put(""" +#ifndef PY_LONG_LONG + #define PY_LONG_LONG LONG_LONG +#endif +#ifndef DL_EXPORT + #define DL_EXPORT(t) t +#endif +#if PY_VERSION_HEX < 0x02040000 + #define METH_COEXIST 0 + #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type) + #define PyDict_Contains(d,o) PySequence_Contains(d,o) +#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 PY_FORMAT_SIZE_T \"\"") - code.putln(" #define PyInt_FromSsize_t(z) PyInt_FromLong(z)") - code.putln(" #define PyInt_AsSsize_t(o) PyInt_AsLong(o)") - code.putln(" #define PyNumber_Index(o) PyNumber_Int(o)") - code.putln(" #define PyIndex_Check(o) PyNumber_Check(o)") - code.putln(" #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)") - code.putln("#endif") +#if PY_VERSION_HEX < 0x02050000 + typedef int Py_ssize_t; + #define PY_SSIZE_T_MAX INT_MAX + #define PY_SSIZE_T_MIN INT_MIN + #define PY_FORMAT_SIZE_T \"\" + #define PyInt_FromSsize_t(z) PyInt_FromLong(z) + #define PyInt_AsSsize_t(o) PyInt_AsLong(o) + #define PyNumber_Index(o) PyNumber_Int(o) + #define PyIndex_Check(o) PyNumber_Check(o) + #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message) +#endif - code.putln("#if PY_VERSION_HEX < 0x02060000") - code.putln(" #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)") - code.putln(" #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)") - code.putln(" #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)") - code.putln(" #define PyVarObject_HEAD_INIT(type, size) \\") - code.putln(" PyObject_HEAD_INIT(type) size,") - code.putln(" #define PyType_Modified(t)") - code.putln("") - code.putln(" typedef struct {") - code.putln(" void *buf;") - code.putln(" PyObject *obj;") - code.putln(" Py_ssize_t len;") - code.putln(" Py_ssize_t itemsize;") - code.putln(" int readonly;") - code.putln(" int ndim;") - code.putln(" char *format;") - code.putln(" Py_ssize_t *shape;") - code.putln(" Py_ssize_t *strides;") - code.putln(" Py_ssize_t *suboffsets;") - code.putln(" void *internal;") - code.putln(" } Py_buffer;") - code.putln("") - code.putln(" #define PyBUF_SIMPLE 0") - code.putln(" #define PyBUF_WRITABLE 0x0001") - code.putln(" #define PyBUF_FORMAT 0x0004") - code.putln(" #define PyBUF_ND 0x0008") - code.putln(" #define PyBUF_STRIDES (0x0010 | PyBUF_ND)") - code.putln(" #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)") - code.putln(" #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)") - code.putln(" #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)") - code.putln(" #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)") - code.putln("") - code.putln("#endif") +#if PY_VERSION_HEX < 0x02060000 + #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt) + #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) + #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size) + #define PyVarObject_HEAD_INIT(type, size) \\ + PyObject_HEAD_INIT(type) size, + #define PyType_Modified(t) - code.put(builtin_module_name_utility_code.proto) + typedef struct { + void *buf; + PyObject *obj; + Py_ssize_t len; + Py_ssize_t itemsize; + int readonly; + int ndim; + char *format; + Py_ssize_t *shape; + Py_ssize_t *strides; + Py_ssize_t *suboffsets; + void *internal; + } Py_buffer; + + #define PyBUF_SIMPLE 0 + #define PyBUF_WRITABLE 0x0001 + #define PyBUF_FORMAT 0x0004 + #define PyBUF_ND 0x0008 + #define PyBUF_STRIDES (0x0010 | PyBUF_ND) + #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES) + #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES) + #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES) + #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES) - code.putln("#if PY_MAJOR_VERSION >= 3") - code.putln(" #define Py_TPFLAGS_CHECKTYPES 0") - code.putln(" #define Py_TPFLAGS_HAVE_INDEX 0") - code.putln("#endif") +#endif - code.putln("#if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)") - code.putln(" #define Py_TPFLAGS_HAVE_NEWBUFFER 0") - code.putln("#endif") +#if PY_MAJOR_VERSION < 3 + #define __Pyx_BUILTIN_MODULE_NAME "__builtin__" +#else + #define __Pyx_BUILTIN_MODULE_NAME "builtins" +#endif - code.putln("#if PY_MAJOR_VERSION >= 3") - code.putln(" #define PyBaseString_Type PyUnicode_Type") - code.putln(" #define PyString_Type PyUnicode_Type") - code.putln(" #define PyString_CheckExact PyUnicode_CheckExact") - code.putln("#else") - code.putln(" #define PyBytes_Type PyString_Type") - code.putln(" #define PyBytes_CheckExact PyString_CheckExact") - code.putln("#endif") +#if PY_MAJOR_VERSION >= 3 + #define Py_TPFLAGS_CHECKTYPES 0 + #define Py_TPFLAGS_HAVE_INDEX 0 +#endif - code.putln("#if PY_MAJOR_VERSION >= 3") - code.putln(" #define PyInt_Type PyLong_Type") - code.putln(" #define PyInt_Check(op) PyLong_Check(op)") - code.putln(" #define PyInt_CheckExact(op) PyLong_CheckExact(op)") - code.putln(" #define PyInt_FromString PyLong_FromString") - code.putln(" #define PyInt_FromUnicode PyLong_FromUnicode") - code.putln(" #define PyInt_FromLong PyLong_FromLong") - code.putln(" #define PyInt_FromSize_t PyLong_FromSize_t") - code.putln(" #define PyInt_FromSsize_t PyLong_FromSsize_t") - code.putln(" #define PyInt_AsLong PyLong_AsLong") - code.putln(" #define PyInt_AS_LONG PyLong_AS_LONG") - code.putln(" #define PyInt_AsSsize_t PyLong_AsSsize_t") - code.putln(" #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask") - code.putln(" #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask") - code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)") - code.putln(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y)") - code.putln("#else") +#if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3) + #define Py_TPFLAGS_HAVE_NEWBUFFER 0 +#endif + +#if PY_MAJOR_VERSION >= 3 + #define PyBaseString_Type PyUnicode_Type + #define PyString_Type PyUnicode_Type + #define PyString_CheckExact PyUnicode_CheckExact +#else + #define PyBytes_Type PyString_Type + #define PyBytes_CheckExact PyString_CheckExact +#endif + +#if PY_MAJOR_VERSION >= 3 + #define PyInt_Type PyLong_Type + #define PyInt_Check(op) PyLong_Check(op) + #define PyInt_CheckExact(op) PyLong_CheckExact(op) + #define PyInt_FromString PyLong_FromString + #define PyInt_FromUnicode PyLong_FromUnicode + #define PyInt_FromLong PyLong_FromLong + #define PyInt_FromSize_t PyLong_FromSize_t + #define PyInt_FromSsize_t PyLong_FromSsize_t + #define PyInt_AsLong PyLong_AsLong + #define PyInt_AS_LONG PyLong_AS_LONG + #define PyInt_AsSsize_t PyLong_AsSsize_t + #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask + #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask + #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y) + #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y) +#else +""") if Future.division in env.context.future_directives: code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)") code.putln(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y)") else: code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y)") code.putln(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceDivide(x,y)") - code.putln("#endif") + code.put(""" +#endif - code.putln("#if PY_MAJOR_VERSION >= 3") - code.putln(" #define PyMethod_New(func, self, klass) PyInstanceMethod_New(func)") - code.putln("#endif") +#if PY_MAJOR_VERSION >= 3 + #define PyMethod_New(func, self, klass) PyInstanceMethod_New(func) +#endif - code.putln("#if !defined(WIN32) && !defined(MS_WINDOWS)") - code.putln(" #ifndef __stdcall") - code.putln(" #define __stdcall") - code.putln(" #endif") - code.putln(" #ifndef __cdecl") - code.putln(" #define __cdecl") - code.putln(" #endif") - code.putln(" #ifndef __fastcall") - code.putln(" #define __fastcall") - code.putln(" #endif") - code.putln("#else") - code.putln(" #define _USE_MATH_DEFINES") - code.putln("#endif") +#if !defined(WIN32) && !defined(MS_WINDOWS) + #ifndef __stdcall + #define __stdcall + #endif + #ifndef __cdecl + #define __cdecl + #endif + #ifndef __fastcall + #define __fastcall + #endif +#else + #define _USE_MATH_DEFINES +#endif - code.putln("#if PY_VERSION_HEX < 0x02050000") - code.putln(" #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),((char *)(n)))") - code.putln(" #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))") - code.putln(" #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),((char *)(n)))") - code.putln("#else") - code.putln(" #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),(n))") - code.putln(" #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))") - code.putln(" #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),(n))") - code.putln("#endif") +#if PY_VERSION_HEX < 0x02050000 + #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),((char *)(n))) + #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a)) + #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),((char *)(n))) +#else + #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),(n)) + #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a)) + #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),(n)) +#endif - code.putln("#if PY_VERSION_HEX < 0x02050000") - code.putln(" #define __Pyx_NAMESTR(n) ((char *)(n))") - code.putln(" #define __Pyx_DOCSTR(n) ((char *)(n))") - code.putln("#else") - code.putln(" #define __Pyx_NAMESTR(n) (n)") - code.putln(" #define __Pyx_DOCSTR(n) (n)") - code.putln("#endif") +#if PY_VERSION_HEX < 0x02050000 + #define __Pyx_NAMESTR(n) ((char *)(n)) + #define __Pyx_DOCSTR(n) ((char *)(n)) +#else + #define __Pyx_NAMESTR(n) (n) + #define __Pyx_DOCSTR(n) (n) +#endif +""") self.generate_extern_c_macro_definition(code) code.putln("#include ") @@ -2100,17 +2109,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): # #------------------------------------------------------------------------------------ -builtin_module_name_utility_code = UtilityCode( -proto = """\ -#if PY_MAJOR_VERSION < 3 - #define __Pyx_BUILTIN_MODULE_NAME "__builtin__" -#else - #define __Pyx_BUILTIN_MODULE_NAME "builtins" -#endif -""") - -#------------------------------------------------------------------------------------ - streq_utility_code = UtilityCode( proto = """ static INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/