Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66a796e

Browse files
committed
Patch #1601678: move intern() to sys.intern().
1 parent 376446d commit 66a796e

12 files changed

Lines changed: 81 additions & 73 deletions

File tree

Doc/lib/libfuncs.tex

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,17 +1262,3 @@ \section{Non-essential Built-in Functions \label{non-essential-built-in-funcs}}
12621262
argument).
12631263
\end{funcdesc}
12641264

1265-
\begin{funcdesc}{intern}{string}
1266-
Enter \var{string} in the table of ``interned'' strings and return
1267-
the interned string -- which is \var{string} itself or a copy.
1268-
Interning strings is useful to gain a little performance on
1269-
dictionary lookup -- if the keys in a dictionary are interned, and
1270-
the lookup key is interned, the key comparisons (after hashing) can
1271-
be done by a pointer compare instead of a string compare. Normally,
1272-
the names used in Python programs are automatically interned, and
1273-
the dictionaries used to hold module, class or instance attributes
1274-
have interned keys. \versionchanged[Interned strings are not
1275-
immortal (like they used to be in Python 2.2 and before);
1276-
you must keep a reference to the return value of \function{intern()}
1277-
around to benefit from it]{2.3}
1278-
\end{funcdesc}

Doc/lib/libsys.tex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,21 @@ \section{\module{sys} ---
340340
\versionadded{1.5.2}
341341
\end{datadesc}
342342

343+
\begin{funcdesc}{intern}{string}
344+
Enter \var{string} in the table of ``interned'' strings and return
345+
the interned string -- which is \var{string} itself or a copy.
346+
Interning strings is useful to gain a little performance on
347+
dictionary lookup -- if the keys in a dictionary are interned, and
348+
the lookup key is interned, the key comparisons (after hashing) can
349+
be done by a pointer compare instead of a string compare. Normally,
350+
the names used in Python programs are automatically interned, and
351+
the dictionaries used to hold module, class or instance attributes
352+
have interned keys. \versionchanged[Interned strings are not
353+
immortal (like they used to be in Python 2.2 and before);
354+
you must keep a reference to the return value of \function{intern()}
355+
around to benefit from it]{2.3}
356+
\end{funcdesc}
357+
343358
\begin{datadesc}{last_type}
344359
\dataline{last_value}
345360
\dataline{last_traceback}

Doc/tut/tut.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2700,7 +2700,7 @@ \section{The \function{dir()} Function \label{dir}}
27002700
'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod',
27012701
'enumerate', 'eval', 'exec', 'execfile', 'exit', 'file', 'filter', 'float',
27022702
'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex',
2703-
'id', 'int', 'intern', 'isinstance', 'issubclass', 'iter',
2703+
'id', 'int', 'isinstance', 'issubclass', 'iter',
27042704
'len', 'license', 'list', 'locals', 'long', 'map', 'max', 'min',
27052705
'object', 'oct', 'open', 'ord', 'pow', 'property', 'quit', 'range',
27062706
'reload', 'repr', 'reversed', 'round', 'set',

Include/stringobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ functions should be applied to nil objects.
2828
Interning strings (ob_sstate) tries to ensure that only one string
2929
object with a given value exists, so equality tests can be one pointer
3030
comparison. This is generally restricted to strings that "look like"
31-
Python identifiers, although the intern() builtin can be used to force
31+
Python identifiers, although the sys.intern() function can be used to force
3232
interning of any string.
3333
Together, these sped the interpreter by up to 20%. */
3434

Lib/test/test_builtin.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -842,30 +842,6 @@ def __int__(self):
842842
self.assertEqual(int(Foo4()), 42L)
843843
self.assertRaises(TypeError, int, Foo5())
844844

845-
def test_intern(self):
846-
self.assertRaises(TypeError, intern)
847-
s = "never interned before"
848-
self.assert_(intern(s) is s)
849-
s2 = s.swapcase().swapcase()
850-
self.assert_(intern(s2) is s)
851-
852-
# Subclasses of string can't be interned, because they
853-
# provide too much opportunity for insane things to happen.
854-
# We don't want them in the interned dict and if they aren't
855-
# actually interned, we don't want to create the appearance
856-
# that they are by allowing intern() to succeeed.
857-
class S(str):
858-
def __hash__(self):
859-
return 123
860-
861-
self.assertRaises(TypeError, intern, S("abc"))
862-
863-
# It's still safe to pass these strings to routines that
864-
# call intern internally, e.g. PyObject_SetAttr().
865-
s = S("abc")
866-
setattr(s, s, s)
867-
self.assertEqual(getattr(s, s), s)
868-
869845
def test_iter(self):
870846
self.assertRaises(TypeError, iter)
871847
self.assertRaises(TypeError, iter, 42, 42)

Lib/test/test_sys.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,31 @@ def test_43581(self):
350350
# the test runs under regrtest.
351351
self.assert_(sys.__stdout__.encoding == sys.__stderr__.encoding)
352352

353+
def test_intern(self):
354+
self.assertRaises(TypeError, sys.intern)
355+
s = "never interned before"
356+
self.assert_(sys.intern(s) is s)
357+
s2 = s.swapcase().swapcase()
358+
self.assert_(sys.intern(s2) is s)
359+
360+
# Subclasses of string can't be interned, because they
361+
# provide too much opportunity for insane things to happen.
362+
# We don't want them in the interned dict and if they aren't
363+
# actually interned, we don't want to create the appearance
364+
# that they are by allowing intern() to succeeed.
365+
class S(str):
366+
def __hash__(self):
367+
return 123
368+
369+
self.assertRaises(TypeError, sys.intern, S("abc"))
370+
371+
# It's still safe to pass these strings to routines that
372+
# call intern internally, e.g. PyObject_SetAttr().
373+
s = S("abc")
374+
setattr(s, s, s)
375+
self.assertEqual(getattr(s, s), s)
376+
377+
353378
def test_main():
354379
test.test_support.run_unittest(SysModuleTest)
355380

Misc/NEWS

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ What's New in Python 3000?
1212
TO DO
1313
-----
1414

15-
- See PEP 3000.
15+
- See PEP 3000, 3100.
1616

1717
- Test merging certain changes from the 2.5 HEAD code.
1818

@@ -36,7 +36,11 @@ TO DO
3636
Core and Builtins
3737
-----------------
3838

39-
- Renamed nb_nonzero to nb_bool and __nonzero__ to __bool__
39+
- Moved intern() to sys.intern().
40+
41+
- exec is now a function.
42+
43+
- Renamed nb_nonzero to nb_bool and __nonzero__ to __bool__.
4044

4145
- Classic classes are a thing of the past. All classes are new style.
4246

@@ -90,7 +94,7 @@ Core and Builtins
9094
- zip returns an iterator
9195

9296
- Additions:
93-
set literals
97+
set literals, ellipsis literal
9498

9599

96100
Extension Modules

Misc/python-mode.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ support for features needed by `python-mode'.")
382382
"delattr" "dict" "dir" "divmod"
383383
"enumerate" "eval" "execfile" "exit" "file"
384384
"filter" "float" "getattr" "globals" "hasattr"
385-
"hash" "hex" "id" "int" "intern"
385+
"hash" "hex" "id" "int"
386386
"isinstance" "issubclass" "iter" "len" "license"
387387
"list" "locals" "long" "map" "max" "min" "object"
388388
"oct" "open" "ord" "pow" "property" "range"

Python/bltinmodule.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,31 +1118,6 @@ PyDoc_STRVAR(hex_doc,
11181118
Return the hexadecimal representation of an integer or long integer.");
11191119

11201120

1121-
static PyObject *
1122-
builtin_intern(PyObject *self, PyObject *args)
1123-
{
1124-
PyObject *s;
1125-
if (!PyArg_ParseTuple(args, "S:intern", &s))
1126-
return NULL;
1127-
if (!PyString_CheckExact(s)) {
1128-
PyErr_SetString(PyExc_TypeError,
1129-
"can't intern subclass of string");
1130-
return NULL;
1131-
}
1132-
Py_INCREF(s);
1133-
PyString_InternInPlace(&s);
1134-
return s;
1135-
}
1136-
1137-
PyDoc_STRVAR(intern_doc,
1138-
"intern(string) -> string\n\
1139-
\n\
1140-
``Intern'' the given string. This enters the string in the (global)\n\
1141-
table of interned strings whose purpose is to speed up dictionary lookups.\n\
1142-
Return the string itself or the previously interned string object with the\n\
1143-
same value.");
1144-
1145-
11461121
static PyObject *
11471122
builtin_iter(PyObject *self, PyObject *args)
11481123
{
@@ -2069,7 +2044,6 @@ static PyMethodDef builtin_methods[] = {
20692044
{"hash", builtin_hash, METH_O, hash_doc},
20702045
{"hex", builtin_hex, METH_O, hex_doc},
20712046
{"id", builtin_id, METH_O, id_doc},
2072-
{"intern", builtin_intern, METH_VARARGS, intern_doc},
20732047
{"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
20742048
{"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
20752049
{"iter", builtin_iter, METH_VARARGS, iter_doc},

Python/sysmodule.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,32 @@ operating system filenames."
264264

265265
#endif
266266

267+
268+
static PyObject *
269+
sys_intern(PyObject *self, PyObject *args)
270+
{
271+
PyObject *s;
272+
if (!PyArg_ParseTuple(args, "S:intern", &s))
273+
return NULL;
274+
if (!PyString_CheckExact(s)) {
275+
PyErr_SetString(PyExc_TypeError,
276+
"can't intern subclass of string");
277+
return NULL;
278+
}
279+
Py_INCREF(s);
280+
PyString_InternInPlace(&s);
281+
return s;
282+
}
283+
284+
PyDoc_STRVAR(intern_doc,
285+
"intern(string) -> string\n\
286+
\n\
287+
``Intern'' the given string. This enters the string in the (global)\n\
288+
table of interned strings whose purpose is to speed up dictionary lookups.\n\
289+
Return the string itself or the previously interned string object with the\n\
290+
same value.");
291+
292+
267293
/*
268294
* Cached interned string objects used for calling the profile and
269295
* trace functions. Initialized by trace_init().
@@ -772,6 +798,7 @@ static PyMethodDef sys_methods[] = {
772798
{"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
773799
getwindowsversion_doc},
774800
#endif /* MS_WINDOWS */
801+
{"intern", sys_intern, METH_VARARGS, intern_doc},
775802
#ifdef USE_MALLOPT
776803
{"mdebug", sys_mdebug, METH_VARARGS},
777804
#endif

0 commit comments

Comments
 (0)