Commit b67f30be authored by Stefan Behnel's avatar Stefan Behnel

fix intern() optimisation

parent 63402a50
...@@ -28,7 +28,7 @@ builtin_function_table = [ ...@@ -28,7 +28,7 @@ builtin_function_table = [
#('hex', "", "", ""), #('hex', "", "", ""),
#('id', "", "", ""), #('id', "", "", ""),
#('input', "", "", ""), #('input', "", "", ""),
#('intern', "s", "O", "__Pyx_InternFromString"), # Doesn't work for Python str objects with null characters. ('intern', "O", "O", "__Pyx_Intern"),
('isinstance', "OO", "b", "PyObject_IsInstance"), ('isinstance', "OO", "b", "PyObject_IsInstance"),
('issubclass', "OO", "b", "PyObject_IsSubclass"), ('issubclass', "OO", "b", "PyObject_IsSubclass"),
('iter', "O", "O", "PyObject_GetIter"), ('iter', "O", "O", "PyObject_GetIter"),
...@@ -237,12 +237,23 @@ bad: ...@@ -237,12 +237,23 @@ bad:
intern_utility_code = UtilityCode( intern_utility_code = UtilityCode(
proto = """ proto = """
#if PY_MAJOR_VERSION >= 3 static PyObject* __Pyx_Intern(PyObject* s); /* proto */
# define __Pyx_InternFromString(s) PyUnicode_InternFromString(s) """,
#else impl = '''
# define __Pyx_InternFromString(s) PyString_InternFromString(s) static PyObject* __Pyx_Intern(PyObject* s) {
#endif if (!(likely(PyString_CheckExact(s)))) {
""") PyErr_Format(PyExc_TypeError, "Expected str, got %s", Py_TYPE(s)->tp_name);
return 0;
}
Py_INCREF(s);
#if PY_MAJOR_VERSION >= 3
PyUnicode_InternInPlace(&s);
#else
PyString_InternInPlace(&s);
#endif
return s;
}
''')
def put_py23_set_init_utility_code(code, pos): def put_py23_set_init_utility_code(code, pos):
code.putln("#if PY_VERSION_HEX < 0x02040000") code.putln("#if PY_VERSION_HEX < 0x02040000")
......
__doc__ = u"""
>>> s == s_interned
True
>>> intern(s) is s_interned
True
>>> intern('abc') is s_interned
True
>>> intern('abc') is s_interned_dynamic
True
"""
s = 'abc'
s_interned = intern(s)
s_interned_dynamic = intern('a'+'b'+'c')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment