Edgewall Software

Changeset 1138


Ignore:
Timestamp:
Oct 24, 2010, 11:16:11 PM (13 years ago)
Author:
hodgestar
Message:

py3k branch: add python 3 support to _speedups C extension

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/experimental/py3k/genshi/_speedups.c

    r995 r1138  
    1515#include <structmember.h>
    1616
    17 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
    18 typedef int Py_ssize_t;
    19 #define PY_SSIZE_T_MAX INT_MAX
    20 #define PY_SSIZE_T_MIN INT_MIN
     17#if PY_MAJOR_VERSION > 2
     18#   define IS_PY3K
     19#elif PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
     20    typedef int Py_ssize_t;
     21#   define PY_SSIZE_T_MAX INT_MAX
     22#   define PY_SSIZE_T_MIN INT_MIN
     23#endif
     24
     25/* We only use Unicode Strings in this module */
     26#ifndef IS_PY3K
     27#   define PyObject_Str PyObject_Unicode
    2128#endif
    2229
     
    7481        return ret;
    7582    }
    76     in = (PyUnicodeObject *) PyObject_Unicode(text);
     83    in = (PyUnicodeObject *) PyObject_Str(text);
    7784    if (in == NULL) {
    7885        return NULL;
     
    391398
    392399    if (PyObject_TypeCheck(self, &MarkupType)) {
    393         unicode = PyObject_Unicode(self);
     400        unicode = PyObject_Str(self);
    394401        if (unicode == NULL) return NULL;
    395402        result = PyNumber_Multiply(unicode, num);
    396403    } else { // __rmul__
    397         unicode = PyObject_Unicode(num);
     404        unicode = PyObject_Str(num);
    398405        if (unicode == NULL) return NULL;
    399406        result = PyNumber_Multiply(unicode, self);
     
    419426    PyObject *format, *result, *args;
    420427
     428#ifdef IS_PY3K
     429    format = PyUnicode_FromString("<Markup %r>");
     430#else
    421431    format = PyString_FromString("<Markup %r>");
     432#endif
    422433    if (format == NULL) return NULL;
    423     result = PyObject_Unicode(self);
     434    result = PyObject_Str(self);
    424435    if (result == NULL) {
    425436        Py_DECREF(format);
     
    433444    }
    434445    PyTuple_SET_ITEM(args, 0, result);
     446#ifdef IS_PY3K
     447    result = PyUnicode_Format(format, args);
     448#else
    435449    result = PyString_Format(format, args);
     450#endif
    436451    Py_DECREF(format);
    437452    Py_DECREF(args);
     
    554569    0, /*nb_subtract*/
    555570    Markup_mul, /*nb_multiply*/
     571#ifndef IS_PY3K
    556572    0, /*nb_divide*/
     573#endif
    557574    Markup_mod, /*nb_remainder*/
    558575};
    559576
    560577PyTypeObject MarkupType = {
     578#ifdef IS_PY3K
     579    PyVarObject_HEAD_INIT(NULL, 0)
     580#else
    561581    PyObject_HEAD_INIT(NULL)
    562582    0,
     583#endif
    563584    "genshi._speedups.Markup",
    564585    sizeof(MarkupObject),
     
    568589    0,          /*tp_getattr*/
    569590    0,          /*tp_setattr*/
     591#ifdef IS_PY3K
     592    0,          /*tp_reserved*/
     593#else
    570594    0,          /*tp_compare*/
     595#endif
    571596    Markup_repr, /*tp_repr*/
    572597    &Markup_as_number, /*tp_as_number*/
     
    581606    0,          /*tp_as_buffer*/
    582607
     608#ifdef IS_PY3K
     609    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /*tp_flags*/
     610#elif defined(Py_TPFLAGS_UNICODE_SUBCLASS)
     611    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES | Py_TPFLAGS_UNICODE_SUBCLASS, /*tp_flags*/
     612#else
    583613    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
     614#endif
     615
    584616    Markup__doc__,/*tp_doc*/
    585617
     
    617649};
    618650
     651#ifdef IS_PY3K
     652struct PyModuleDef module_def = {
     653    PyModuleDef_HEAD_INIT, /*m_base*/
     654    "_speedups",           /*m_name*/
     655    NULL,                  /*m_doc*/
     656    -1,                    /*m_size*/
     657    NULL,                  /*m_methods*/
     658    NULL,                  /*m_reload*/
     659    NULL,                  /*m_traverse*/
     660    NULL,                  /*m_clear*/
     661    NULL                   /*m_free*/
     662};
     663
     664PyObject *
     665PyInit__speedups(void)
     666#else
    619667PyMODINIT_FUNC
    620668init_speedups(void)
     669#endif
    621670{
    622671    PyObject *module;
     
    627676
    628677    if (PyType_Ready(&MarkupType) < 0)
     678#ifdef IS_PY3K
     679        return NULL;
     680#else
    629681        return;
     682#endif
    630683
    631684    init_constants();
    632685
     686#ifdef IS_PY3K
     687    module = PyModule_Create(&module_def);
     688#else
    633689    module = Py_InitModule("_speedups", NULL);
     690#endif
    634691    Py_INCREF(&MarkupType);
    635692    PyModule_AddObject(module, "Markup", (PyObject *) &MarkupType);
    636 }
     693
     694#ifdef IS_PY3K
     695    return module;
     696#endif
     697}
Note: See TracChangeset for help on using the changeset viewer.