diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2012-12-09 11:35:02 +0100 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2012-12-09 11:35:02 +0100 |
commit | 3b67ea4ba509e9554969417d1d4c743c0c8fa974 (patch) | |
tree | b8c61e486dd75dbdbe7be67b490dbdad48632def | |
parent | 5827f4c9bcfe6b9030358727c1ca3197425e576e (diff) | |
download | cython-3b67ea4ba509e9554969417d1d4c743c0c8fa974.tar.gz |
moved Py_UCS4/Py_UNICODE type conversion helper functions to utility code file
--HG--
extra : transplant_source : %7C%3E%C7x%E7%A8s%CEF%B5%90%C5%DC%AD%CD%5E%1Fg%F9%7E
-rwxr-xr-x | Cython/Compiler/PyrexTypes.py | 101 | ||||
-rw-r--r-- | Cython/Utility/TypeConversion.c | 95 |
2 files changed, 97 insertions, 99 deletions
diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 9fc3b460f..f4ecbaa77 100755 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -1561,65 +1561,13 @@ class CPyUCS4IntType(CIntType): from_py_function = "__Pyx_PyObject_AsPy_UCS4" def create_from_py_utility_code(self, env): - env.use_utility_code(pyobject_as_py_ucs4_utility_code) + env.use_utility_code(UtilityCode.load_cached("ObjectAsUCS4", "TypeConversion.c")) return True def sign_and_name(self): return "Py_UCS4" -pyobject_as_py_ucs4_utility_code = UtilityCode( -proto=''' -static CYTHON_INLINE Py_UCS4 __Pyx_PyObject_AsPy_UCS4(PyObject*); -''', -impl=''' -static CYTHON_INLINE Py_UCS4 __Pyx_PyObject_AsPy_UCS4(PyObject* x) { - long ival; - if (PyUnicode_Check(x)) { - Py_ssize_t length; - #if CYTHON_PEP393_ENABLED - length = PyUnicode_GET_LENGTH(x); - if (likely(length == 1)) { - return PyUnicode_READ_CHAR(x, 0); - } - #else - length = PyUnicode_GET_SIZE(x); - if (likely(length == 1)) { - return PyUnicode_AS_UNICODE(x)[0]; - } - #if Py_UNICODE_SIZE == 2 - else if (PyUnicode_GET_SIZE(x) == 2) { - Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0]; - if (high_val >= 0xD800 && high_val <= 0xDBFF) { - Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1]; - if (low_val >= 0xDC00 && low_val <= 0xDFFF) { - return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1))); - } - } - } - #endif - #endif - PyErr_Format(PyExc_ValueError, - "only single character unicode strings can be converted to Py_UCS4, " - "got length %" CYTHON_FORMAT_SSIZE_T "d", length); - return (Py_UCS4)-1; - } - ival = __Pyx_PyInt_AsLong(x); - if (unlikely(ival < 0)) { - if (!PyErr_Occurred()) - PyErr_SetString(PyExc_OverflowError, - "cannot convert negative value to Py_UCS4"); - return (Py_UCS4)-1; - } else if (unlikely(ival > 1114111)) { - PyErr_SetString(PyExc_OverflowError, - "value too large to convert to Py_UCS4"); - return (Py_UCS4)-1; - } - return (Py_UCS4)ival; -} -''') - - class CPyUnicodeIntType(CIntType): # Py_UNICODE @@ -1634,57 +1582,12 @@ class CPyUnicodeIntType(CIntType): from_py_function = "__Pyx_PyObject_AsPy_UNICODE" def create_from_py_utility_code(self, env): - env.use_utility_code(pyobject_as_py_unicode_utility_code) + env.use_utility_code(UtilityCode.load_cached("ObjectAsPyUnicode", "TypeConversion.c")) return True def sign_and_name(self): return "Py_UNICODE" -pyobject_as_py_unicode_utility_code = UtilityCode( -proto=''' -static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject*); -''', -impl=''' -static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) { - long ival; - #if CYTHON_PEP393_ENABLED - const long maxval = 1114111; - #else - static long maxval = 0; - #endif - if (PyUnicode_Check(x)) { - if (unlikely(__Pyx_PyUnicode_GET_LENGTH(x) != 1)) { - PyErr_Format(PyExc_ValueError, - "only single character unicode strings can be converted to Py_UNICODE, " - "got length %" CYTHON_FORMAT_SSIZE_T "d", __Pyx_PyUnicode_GET_LENGTH(x)); - return (Py_UNICODE)-1; - } - #if CYTHON_PEP393_ENABLED - ival = PyUnicode_READ_CHAR(x, 0); - #else - return PyUnicode_AS_UNICODE(x)[0]; - #endif - } else { - #if !CYTHON_PEP393_ENABLED - if (unlikely(!maxval)) - maxval = (long)PyUnicode_GetMax(); - #endif - ival = __Pyx_PyInt_AsLong(x); - } - if (unlikely(ival < 0)) { - if (!PyErr_Occurred()) - PyErr_SetString(PyExc_OverflowError, - "cannot convert negative value to Py_UNICODE"); - return (Py_UNICODE)-1; - } else if (unlikely(ival > maxval)) { - PyErr_SetString(PyExc_OverflowError, - "value too large to convert to Py_UNICODE"); - return (Py_UNICODE)-1; - } - return (Py_UNICODE)ival; -} -''') - class CPyHashTType(CIntType): diff --git a/Cython/Utility/TypeConversion.c b/Cython/Utility/TypeConversion.c index 8049e19aa..c2bb6f11f 100644 --- a/Cython/Utility/TypeConversion.c +++ b/Cython/Utility/TypeConversion.c @@ -34,3 +34,98 @@ bad: return result; } +/////////////// ObjectAsUCS4.proto /////////////// + +static CYTHON_INLINE Py_UCS4 __Pyx_PyObject_AsPy_UCS4(PyObject*); + +/////////////// ObjectAsUCS4 /////////////// + +static CYTHON_INLINE Py_UCS4 __Pyx_PyObject_AsPy_UCS4(PyObject* x) { + long ival; + if (PyUnicode_Check(x)) { + Py_ssize_t length; + #if CYTHON_PEP393_ENABLED + length = PyUnicode_GET_LENGTH(x); + if (likely(length == 1)) { + return PyUnicode_READ_CHAR(x, 0); + } + #else + length = PyUnicode_GET_SIZE(x); + if (likely(length == 1)) { + return PyUnicode_AS_UNICODE(x)[0]; + } + #if Py_UNICODE_SIZE == 2 + else if (PyUnicode_GET_SIZE(x) == 2) { + Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0]; + if (high_val >= 0xD800 && high_val <= 0xDBFF) { + Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1]; + if (low_val >= 0xDC00 && low_val <= 0xDFFF) { + return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1))); + } + } + } + #endif + #endif + PyErr_Format(PyExc_ValueError, + "only single character unicode strings can be converted to Py_UCS4, " + "got length %" CYTHON_FORMAT_SSIZE_T "d", length); + return (Py_UCS4)-1; + } + ival = __Pyx_PyInt_AsLong(x); + if (unlikely(ival < 0)) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_OverflowError, + "cannot convert negative value to Py_UCS4"); + return (Py_UCS4)-1; + } else if (unlikely(ival > 1114111)) { + PyErr_SetString(PyExc_OverflowError, + "value too large to convert to Py_UCS4"); + return (Py_UCS4)-1; + } + return (Py_UCS4)ival; +} + +/////////////// ObjectAsPyUnicode.proto /////////////// + +static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject*); + +/////////////// ObjectAsPyUnicode /////////////// + +static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) { + long ival; + #if CYTHON_PEP393_ENABLED + const long maxval = 1114111; + #else + static long maxval = 0; + #endif + if (PyUnicode_Check(x)) { + if (unlikely(__Pyx_PyUnicode_GET_LENGTH(x) != 1)) { + PyErr_Format(PyExc_ValueError, + "only single character unicode strings can be converted to Py_UNICODE, " + "got length %" CYTHON_FORMAT_SSIZE_T "d", __Pyx_PyUnicode_GET_LENGTH(x)); + return (Py_UNICODE)-1; + } + #if CYTHON_PEP393_ENABLED + ival = PyUnicode_READ_CHAR(x, 0); + #else + return PyUnicode_AS_UNICODE(x)[0]; + #endif + } else { + #if !CYTHON_PEP393_ENABLED + if (unlikely(!maxval)) + maxval = (long)PyUnicode_GetMax(); + #endif + ival = __Pyx_PyInt_AsLong(x); + } + if (unlikely(ival < 0)) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_OverflowError, + "cannot convert negative value to Py_UNICODE"); + return (Py_UNICODE)-1; + } else if (unlikely(ival > maxval)) { + PyErr_SetString(PyExc_OverflowError, + "value too large to convert to Py_UNICODE"); + return (Py_UNICODE)-1; + } + return (Py_UNICODE)ival; +} |