Index: Python/bltinmodule.c =================================================================== --- Python/bltinmodule.c (revision 55704) +++ Python/bltinmodule.c (working copy) @@ -1142,17 +1142,14 @@ static PyObject * builtin_intern(PyObject *self, PyObject *args) { - PyObject *s; - if (!PyArg_ParseTuple(args, "S:intern", &s)) + PyObject *internfunc; + if (Py_Py3kWarningFlag && + PyErr_Warn(PyExc_DeprecationWarning, + "intern() is moving to sys.intern() in 3.x") < 0) return NULL; - if (!PyString_CheckExact(s)) { - PyErr_SetString(PyExc_TypeError, - "can't intern subclass of string"); - return NULL; - } - Py_INCREF(s); - PyString_InternInPlace(&s); - return s; + + internfunc = PySys_GetObject("intern"); + return PyEval_CallObject(internfunc, args); } PyDoc_STRVAR(intern_doc, Index: Python/sysmodule.c =================================================================== --- Python/sysmodule.c (revision 55704) +++ Python/sysmodule.c (working copy) @@ -578,6 +578,31 @@ ); #endif +static PyObject * +sys_intern(PyObject *self, PyObject *args) +{ + PyObject *s; + if (!PyArg_ParseTuple(args, "S:intern", &s)) + return NULL; + if (!PyString_CheckExact(s)) { + PyErr_SetString(PyExc_TypeError, + "can't intern subclass of string"); + return NULL; + } + Py_INCREF(s); + PyString_InternInPlace(&s); + return s; +} + +PyDoc_STRVAR(intern_doc, +"intern(string) -> string\n\ +\n\ +``Intern'' the given string. This enters the string in the (global)\n\ +table of interned strings whose purpose is to speed up dictionary lookups.\n\ +Return the string itself or the previously interned string object with the\n\ +same value."); + + #ifdef USE_MALLOPT /* Link with -lmalloc (or -lmpc) on an SGI */ #include @@ -776,6 +801,7 @@ {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, getwindowsversion_doc}, #endif /* MS_WINDOWS */ + {"intern", sys_intern, METH_VARARGS, intern_doc}, #ifdef USE_MALLOPT {"mdebug", sys_mdebug, METH_VARARGS}, #endif Index: Doc/lib/libsys.tex =================================================================== --- Doc/lib/libsys.tex (revision 55704) +++ Doc/lib/libsys.tex (working copy) @@ -340,6 +340,22 @@ \versionadded{1.5.2} \end{datadesc} +\begin{funcdesc}{intern}{string} + Enter \var{string} in the table of ``interned'' strings and return + the interned string -- which is \var{string} itself or a copy. + Interning strings is useful to gain a little performance on + dictionary lookup -- if the keys in a dictionary are interned, and + the lookup key is interned, the key comparisons (after hashing) can + be done by a pointer compare instead of a string compare. Normally, + the names used in Python programs are automatically interned, and + the dictionaries used to hold module, class or instance attributes + have interned keys. \versionchanged[Interned strings are not + immortal (like they used to be in Python 2.2 and before); + you must keep a reference to the return value of \function{intern()} + around to benefit from it]{2.3} \versionchanged{intern was moved to sys + in Python 2.6 - it will disappear from the builtin module in 3.0}{2.6} +\end{funcdesc} + \begin{datadesc}{last_type} \dataline{last_value} \dataline{last_traceback}