[Python-3000-checkins] r55781 - in python/branches/py3k-struni: Lib/test/test_sys.py Python/sysmodule.c
walter.doerwald
python-3000-checkins at python.org
Tue Jun 5 22:22:07 CEST 2007
Author: walter.doerwald
Date: Tue Jun 5 22:22:04 2007
New Revision: 55781
Modified:
python/branches/py3k-struni/Lib/test/test_sys.py
python/branches/py3k-struni/Python/sysmodule.c
Log:
Change sys.intern() so that unicode strings can be
interned too. Add a test for this.
Modified: python/branches/py3k-struni/Lib/test/test_sys.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_sys.py (original)
+++ python/branches/py3k-struni/Lib/test/test_sys.py Tue Jun 5 22:22:04 2007
@@ -356,7 +356,7 @@
# We don't want them in the interned dict and if they aren't
# actually interned, we don't want to create the appearance
# that they are by allowing intern() to succeeed.
- class S(str):
+ class S(str8):
def __hash__(self):
return 123
@@ -368,6 +368,17 @@
setattr(s, s, s)
self.assertEqual(getattr(s, s), s)
+ s = "never interned as unicode before"
+ self.assert_(sys.intern(s) is s)
+ s2 = s.swapcase().swapcase()
+ self.assert_(sys.intern(s2) is s)
+
+ class U(str):
+ def __hash__(self):
+ return 123
+
+ self.assertRaises(TypeError, sys.intern, U("abc"))
+
def test_main():
test.test_support.run_unittest(SysModuleTest)
Modified: python/branches/py3k-struni/Python/sysmodule.c
==============================================================================
--- python/branches/py3k-struni/Python/sysmodule.c (original)
+++ python/branches/py3k-struni/Python/sysmodule.c Tue Jun 5 22:22:04 2007
@@ -266,14 +266,21 @@
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");
+ if (PyString_CheckExact(s)) {
+ Py_INCREF(s);
+ PyString_InternInPlace(&s);
+ return s;
+ }
+ else if (PyUnicode_CheckExact(s)) {
+ Py_INCREF(s);
+ PyUnicode_InternInPlace(&s);
+ return s;
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "can't intern %.400s", s->ob_type->tp_name);
return NULL;
}
- Py_INCREF(s);
- PyString_InternInPlace(&s);
- return s;
}
PyDoc_STRVAR(intern_doc,
More information about the Python-3000-checkins
mailing list