From: Georg Brandl Date: Sun, 21 Feb 2010 16:50:32 +0000 (+0100) Subject: Make the _speedups extension compatible with Python 3. X-Git-Tag: 2.4~25 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=05be95a359b9213edfa1fb521c1d7684a10b33c8;p=jinja2.git Make the _speedups extension compatible with Python 3. --HG-- branch : trunk --- diff --git a/CHANGES b/CHANGES index 0491b6c..863ec14 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,8 @@ Version 2.4 makes it possible to import or extend from a template object that was passed to the template. +- the _speedups C extension now supports Python 3. + Version 2.3.1 ------------- (bugfix release, released on February 19th 2010) diff --git a/jinja2/_speedups.c b/jinja2/_speedups.c index c8ef9af..3b2d71c 100644 --- a/jinja2/_speedups.c +++ b/jinja2/_speedups.c @@ -123,7 +123,10 @@ escape(PyObject *self, PyObject *text) PyObject *s = NULL, *rv = NULL, *html; /* we don't have to escape integers, bools or floats */ - if (PyInt_CheckExact(text) || PyLong_CheckExact(text) || + if (PyLong_CheckExact(text) || +#if PY_MAJOR_VERSION < 3 + PyInt_CheckExact(text) || +#endif PyFloat_CheckExact(text) || PyBool_Check(text) || text == Py_None) return PyObject_CallFunctionObjArgs(markup, text, NULL); @@ -139,7 +142,11 @@ escape(PyObject *self, PyObject *text) /* otherwise make the object unicode if it isn't, then escape */ PyErr_Clear(); if (!PyUnicode_Check(text)) { +#if PY_MAJOR_VERSION < 3 PyObject *unicode = PyObject_Unicode(text); +#else + PyObject *unicode = PyObject_Str(text); +#endif if (!unicode) return NULL; s = escape_unicode((PyUnicodeObject*)unicode); @@ -159,7 +166,11 @@ static PyObject* soft_unicode(PyObject *self, PyObject *s) { if (!PyUnicode_Check(s)) +#if PY_MAJOR_VERSION < 3 return PyObject_Unicode(s); +#else + return PyObject_Str(s); +#endif Py_INCREF(s); return s; } @@ -208,6 +219,8 @@ static PyMethodDef module_methods[] = { }; +#if PY_MAJOR_VERSION < 3 + #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ #define PyMODINIT_FUNC void #endif @@ -219,3 +232,28 @@ init_speedups(void) Py_InitModule3("jinja2._speedups", module_methods, ""); } + +#else /* Python 3.x module initialization */ + +static struct PyModuleDef module_definition = { + PyModuleDef_HEAD_INIT, + "jinja2._speedups", + NULL, + -1, + module_methods, + NULL, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC +PyInit__speedups(void) +{ + if (!init_constants()) + return NULL; + + return PyModule_Create(&module_definition); +} + +#endif