File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -356,7 +356,7 @@ def test_intern(self):
356356 # We don't want them in the interned dict and if they aren't
357357 # actually interned, we don't want to create the appearance
358358 # that they are by allowing intern() to succeeed.
359- class S (str ):
359+ class S (str8 ):
360360 def __hash__ (self ):
361361 return 123
362362
@@ -368,6 +368,17 @@ def __hash__(self):
368368 setattr (s , s , s )
369369 self .assertEqual (getattr (s , s ), s )
370370
371+ s = "never interned as unicode before"
372+ self .assert_ (sys .intern (s ) is s )
373+ s2 = s .swapcase ().swapcase ()
374+ self .assert_ (sys .intern (s2 ) is s )
375+
376+ class U (str ):
377+ def __hash__ (self ):
378+ return 123
379+
380+ self .assertRaises (TypeError , sys .intern , U ("abc" ))
381+
371382
372383def test_main ():
373384 test .test_support .run_unittest (SysModuleTest )
Original file line number Diff line number Diff line change @@ -266,14 +266,21 @@ sys_intern(PyObject *self, PyObject *args)
266266 PyObject * s ;
267267 if (!PyArg_ParseTuple (args , "S:intern" , & s ))
268268 return NULL ;
269- if (!PyString_CheckExact (s )) {
270- PyErr_SetString (PyExc_TypeError ,
271- "can't intern subclass of string" );
269+ if (PyString_CheckExact (s )) {
270+ Py_INCREF (s );
271+ PyString_InternInPlace (& s );
272+ return s ;
273+ }
274+ else if (PyUnicode_CheckExact (s )) {
275+ Py_INCREF (s );
276+ PyUnicode_InternInPlace (& s );
277+ return s ;
278+ }
279+ else {
280+ PyErr_Format (PyExc_TypeError ,
281+ "can't intern %.400s" , s -> ob_type -> tp_name );
272282 return NULL ;
273283 }
274- Py_INCREF (s );
275- PyString_InternInPlace (& s );
276- return s ;
277284}
278285
279286PyDoc_STRVAR (intern_doc ,
You can’t perform that action at this time.
0 commit comments