moved Py_UCS4/Py_UNICODE type conversion helper functions to utility code file
authorStefan Behnel <stefan_ml@behnel.de>
Sun, 9 Dec 2012 10:35:02 +0000 (11:35 +0100)
committerStefan Behnel <stefan_ml@behnel.de>
Sun, 9 Dec 2012 10:35:02 +0000 (11:35 +0100)
--HG--
extra : transplant_source : %7C%3E%C7x%E7%A8s%CEF%B5%90%C5%DC%AD%CD%5E%1Fg%F9%7E

Cython/Compiler/PyrexTypes.py
Cython/Utility/TypeConversion.c

index 9fc3b46..f4ecbaa 100755 (executable)
@@ -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):
 
index 8049e19..c2bb6f1 100644 (file)
@@ -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;
+}