MCPcopy Index your code
hub / github.com/python/cpython / test_intern

Method test_intern

Lib/test/test_sys.py:764–794  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

762 self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding)
763
764 def test_intern(self):
765 has_is_interned = (test.support.check_impl_detail(cpython=True)
766 or hasattr(sys, '_is_interned'))
767 self.assertRaises(TypeError, sys.intern)
768 self.assertRaises(TypeError, sys.intern, b'abc')
769 if has_is_interned:
770 self.assertRaises(TypeError, sys._is_interned)
771 self.assertRaises(TypeError, sys._is_interned, b'abc')
772 s = "never interned before" + str(random.randrange(0, 10**9))
773 self.assertTrue(sys.intern(s) is s)
774 if has_is_interned:
775 self.assertIs(sys._is_interned(s), True)
776 s2 = s.swapcase().swapcase()
777 if has_is_interned:
778 self.assertIs(sys._is_interned(s2), False)
779 self.assertTrue(sys.intern(s2) is s)
780 if has_is_interned:
781 self.assertIs(sys._is_interned(s2), False)
782
783 # Subclasses of string can't be interned, because they
784 # provide too much opportunity for insane things to happen.
785 # We don't want them in the interned dict and if they aren't
786 # actually interned, we don't want to create the appearance
787 # that they are by allowing intern() to succeed.
788 class S(str):
789 def __hash__(self):
790 return 123
791
792 self.assertRaises(TypeError, sys.intern, S("abc"))
793 if has_is_interned:
794 self.assertIs(sys._is_interned(S("abc")), False)
795
796 @support.cpython_only
797 @requires_subinterpreters

Callers

nothing calls this directly

Calls 8

strFunction · 0.85
randrangeMethod · 0.80
assertTrueMethod · 0.80
internMethod · 0.80
SClass · 0.70
assertRaisesMethod · 0.45
assertIsMethod · 0.45
swapcaseMethod · 0.45

Tested by

no test coverage detected