cython-devel
changeset 2552:e2532920c40c
fix intern() optimisation
| author | Stefan Behnel <scoder@users.berlios.de> |
|---|---|
| date | Thu Oct 22 11:24:48 2009 +0200 (2 years ago) |
| parents | 1cfd0ad66710 |
| children | 01d130c6671b |
| files | Cython/Compiler/Builtin.py tests/run/intern_T431.pyx |
line diff
1.1 --- a/Cython/Compiler/Builtin.py Thu Oct 22 08:12:32 2009 +0200
1.2 +++ b/Cython/Compiler/Builtin.py Thu Oct 22 11:24:48 2009 +0200
1.3 @@ -28,7 +28,7 @@
1.4 #('hex', "", "", ""),
1.5 #('id', "", "", ""),
1.6 #('input', "", "", ""),
1.7 - #('intern', "s", "O", "__Pyx_InternFromString"), # Doesn't work for Python str objects with null characters.
1.8 + ('intern', "O", "O", "__Pyx_Intern"),
1.9 ('isinstance', "OO", "b", "PyObject_IsInstance"),
1.10 ('issubclass', "OO", "b", "PyObject_IsSubclass"),
1.11 ('iter', "O", "O", "PyObject_GetIter"),
1.12 @@ -237,12 +237,23 @@
1.13
1.14 intern_utility_code = UtilityCode(
1.15 proto = """
1.16 -#if PY_MAJOR_VERSION >= 3
1.17 -# define __Pyx_InternFromString(s) PyUnicode_InternFromString(s)
1.18 -#else
1.19 -# define __Pyx_InternFromString(s) PyString_InternFromString(s)
1.20 -#endif
1.21 -""")
1.22 +static PyObject* __Pyx_Intern(PyObject* s); /* proto */
1.23 +""",
1.24 +impl = '''
1.25 +static PyObject* __Pyx_Intern(PyObject* s) {
1.26 + if (!(likely(PyString_CheckExact(s)))) {
1.27 + PyErr_Format(PyExc_TypeError, "Expected str, got %s", Py_TYPE(s)->tp_name);
1.28 + return 0;
1.29 + }
1.30 + Py_INCREF(s);
1.31 + #if PY_MAJOR_VERSION >= 3
1.32 + PyUnicode_InternInPlace(&s);
1.33 + #else
1.34 + PyString_InternInPlace(&s);
1.35 + #endif
1.36 + return s;
1.37 +}
1.38 +''')
1.39
1.40 def put_py23_set_init_utility_code(code, pos):
1.41 code.putln("#if PY_VERSION_HEX < 0x02040000")
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tests/run/intern_T431.pyx Thu Oct 22 11:24:48 2009 +0200
2.3 @@ -0,0 +1,16 @@
2.4 +__doc__ = u"""
2.5 +>>> s == s_interned
2.6 +True
2.7 +>>> intern(s) is s_interned
2.8 +True
2.9 +>>> intern('abc') is s_interned
2.10 +True
2.11 +>>> intern('abc') is s_interned_dynamic
2.12 +True
2.13 +"""
2.14 +
2.15 +s = 'abc'
2.16 +
2.17 +s_interned = intern(s)
2.18 +
2.19 +s_interned_dynamic = intern('a'+'b'+'c')
