Commit 5109df24 authored by Robert Bradshaw's avatar Robert Bradshaw

Streamline writing module-level compatability macros.

parent feee1a22
...@@ -414,154 +414,163 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -414,154 +414,163 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
self.generate_cfunction_predeclarations(module, modulecode, defined_here) self.generate_cfunction_predeclarations(module, modulecode, defined_here)
def generate_module_preamble(self, env, cimported_modules, code): 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())) Version.version, time.asctime()))
code.putln('') code.putln("")
code.putln('#define PY_SSIZE_T_CLEAN') code.putln("#define PY_SSIZE_T_CLEAN")
for filename in env.python_include_files: for filename in env.python_include_files:
code.putln('#include "%s"' % filename) code.putln('#include "%s"' % filename)
code.putln("#ifndef Py_PYTHON_H") code.putln("#ifndef Py_PYTHON_H")
code.putln(" #error Python headers needed to compile C extensions, please install development version of Python.") code.putln(" #error Python headers needed to compile C extensions, please install development version of Python.")
code.putln("#else") code.putln("#else")
code.globalstate["end"].putln("#endif /* Py_PYTHON_H */") code.globalstate["end"].putln("#endif /* Py_PYTHON_H */")
code.putln("#ifndef PY_LONG_LONG")
code.putln(" #define PY_LONG_LONG LONG_LONG") code.put("""
code.putln("#endif") #ifndef PY_LONG_LONG
code.putln("#ifndef DL_EXPORT") #define PY_LONG_LONG LONG_LONG
code.putln(" #define DL_EXPORT(t) t") #endif
code.putln("#endif") #ifndef DL_EXPORT
code.putln("#if PY_VERSION_HEX < 0x02040000") #define DL_EXPORT(t) t
code.putln(" #define METH_COEXIST 0") #endif
code.putln(" #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)") #if PY_VERSION_HEX < 0x02040000
code.putln(" #define PyDict_Contains(d,o) PySequence_Contains(d,o)") #define METH_COEXIST 0
code.putln("#endif") #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") #if PY_VERSION_HEX < 0x02050000
code.putln(" typedef int Py_ssize_t;") typedef int Py_ssize_t;
code.putln(" #define PY_SSIZE_T_MAX INT_MAX") #define PY_SSIZE_T_MAX INT_MAX
code.putln(" #define PY_SSIZE_T_MIN INT_MIN") #define PY_SSIZE_T_MIN INT_MIN
code.putln(" #define PY_FORMAT_SIZE_T \"\"") #define PY_FORMAT_SIZE_T \"\"
code.putln(" #define PyInt_FromSsize_t(z) PyInt_FromLong(z)") #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
code.putln(" #define PyInt_AsSsize_t(o) PyInt_AsLong(o)") #define PyInt_AsSsize_t(o) PyInt_AsLong(o)
code.putln(" #define PyNumber_Index(o) PyNumber_Int(o)") #define PyNumber_Index(o) PyNumber_Int(o)
code.putln(" #define PyIndex_Check(o) PyNumber_Check(o)") #define PyIndex_Check(o) PyNumber_Check(o)
code.putln(" #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)") #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)
code.putln("#endif") #endif
code.putln("#if PY_VERSION_HEX < 0x02060000") #if PY_VERSION_HEX < 0x02060000
code.putln(" #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)") #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
code.putln(" #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)") #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
code.putln(" #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)") #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
code.putln(" #define PyVarObject_HEAD_INIT(type, size) \\") #define PyVarObject_HEAD_INIT(type, size) \\
code.putln(" PyObject_HEAD_INIT(type) size,") PyObject_HEAD_INIT(type) size,
code.putln(" #define PyType_Modified(t)") #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")
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") #endif
code.putln(" #define Py_TPFLAGS_CHECKTYPES 0")
code.putln(" #define Py_TPFLAGS_HAVE_INDEX 0")
code.putln("#endif")
code.putln("#if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)") #if PY_MAJOR_VERSION < 3
code.putln(" #define Py_TPFLAGS_HAVE_NEWBUFFER 0") #define __Pyx_BUILTIN_MODULE_NAME "__builtin__"
code.putln("#endif") #else
#define __Pyx_BUILTIN_MODULE_NAME "builtins"
#endif
code.putln("#if PY_MAJOR_VERSION >= 3") #if PY_MAJOR_VERSION >= 3
code.putln(" #define PyBaseString_Type PyUnicode_Type") #define Py_TPFLAGS_CHECKTYPES 0
code.putln(" #define PyString_Type PyUnicode_Type") #define Py_TPFLAGS_HAVE_INDEX 0
code.putln(" #define PyString_CheckExact PyUnicode_CheckExact") #endif
code.putln("#else")
code.putln(" #define PyBytes_Type PyString_Type")
code.putln(" #define PyBytes_CheckExact PyString_CheckExact")
code.putln("#endif")
code.putln("#if PY_MAJOR_VERSION >= 3") #if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)
code.putln(" #define PyInt_Type PyLong_Type") #define Py_TPFLAGS_HAVE_NEWBUFFER 0
code.putln(" #define PyInt_Check(op) PyLong_Check(op)") #endif
code.putln(" #define PyInt_CheckExact(op) PyLong_CheckExact(op)")
code.putln(" #define PyInt_FromString PyLong_FromString") #if PY_MAJOR_VERSION >= 3
code.putln(" #define PyInt_FromUnicode PyLong_FromUnicode") #define PyBaseString_Type PyUnicode_Type
code.putln(" #define PyInt_FromLong PyLong_FromLong") #define PyString_Type PyUnicode_Type
code.putln(" #define PyInt_FromSize_t PyLong_FromSize_t") #define PyString_CheckExact PyUnicode_CheckExact
code.putln(" #define PyInt_FromSsize_t PyLong_FromSsize_t") #else
code.putln(" #define PyInt_AsLong PyLong_AsLong") #define PyBytes_Type PyString_Type
code.putln(" #define PyInt_AS_LONG PyLong_AS_LONG") #define PyBytes_CheckExact PyString_CheckExact
code.putln(" #define PyInt_AsSsize_t PyLong_AsSsize_t") #endif
code.putln(" #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask")
code.putln(" #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask") #if PY_MAJOR_VERSION >= 3
code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)") #define PyInt_Type PyLong_Type
code.putln(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y)") #define PyInt_Check(op) PyLong_Check(op)
code.putln("#else") #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: 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_Divide(x,y) PyNumber_TrueDivide(x,y)")
code.putln(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y)") code.putln(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y)")
else: else:
code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y)") 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(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceDivide(x,y)")
code.putln("#endif") code.put("""
#endif
code.putln("#if PY_MAJOR_VERSION >= 3") #if PY_MAJOR_VERSION >= 3
code.putln(" #define PyMethod_New(func, self, klass) PyInstanceMethod_New(func)") #define PyMethod_New(func, self, klass) PyInstanceMethod_New(func)
code.putln("#endif") #endif
code.putln("#if !defined(WIN32) && !defined(MS_WINDOWS)") #if !defined(WIN32) && !defined(MS_WINDOWS)
code.putln(" #ifndef __stdcall") #ifndef __stdcall
code.putln(" #define __stdcall") #define __stdcall
code.putln(" #endif") #endif
code.putln(" #ifndef __cdecl") #ifndef __cdecl
code.putln(" #define __cdecl") #define __cdecl
code.putln(" #endif") #endif
code.putln(" #ifndef __fastcall") #ifndef __fastcall
code.putln(" #define __fastcall") #define __fastcall
code.putln(" #endif") #endif
code.putln("#else") #else
code.putln(" #define _USE_MATH_DEFINES") #define _USE_MATH_DEFINES
code.putln("#endif") #endif
code.putln("#if PY_VERSION_HEX < 0x02050000") #if PY_VERSION_HEX < 0x02050000
code.putln(" #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),((char *)(n)))") #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),((char *)(n)))
code.putln(" #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))") #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))
code.putln(" #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),((char *)(n)))") #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),((char *)(n)))
code.putln("#else") #else
code.putln(" #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),(n))") #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),(n))
code.putln(" #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))") #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))
code.putln(" #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),(n))") #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),(n))
code.putln("#endif") #endif
code.putln("#if PY_VERSION_HEX < 0x02050000") #if PY_VERSION_HEX < 0x02050000
code.putln(" #define __Pyx_NAMESTR(n) ((char *)(n))") #define __Pyx_NAMESTR(n) ((char *)(n))
code.putln(" #define __Pyx_DOCSTR(n) ((char *)(n))") #define __Pyx_DOCSTR(n) ((char *)(n))
code.putln("#else") #else
code.putln(" #define __Pyx_NAMESTR(n) (n)") #define __Pyx_NAMESTR(n) (n)
code.putln(" #define __Pyx_DOCSTR(n) (n)") #define __Pyx_DOCSTR(n) (n)
code.putln("#endif") #endif
""")
self.generate_extern_c_macro_definition(code) self.generate_extern_c_macro_definition(code)
code.putln("#include <math.h>") code.putln("#include <math.h>")
...@@ -2100,17 +2109,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -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( streq_utility_code = UtilityCode(
proto = """ proto = """
static INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/ static INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment