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

Skip to content

Commit dee7bee

Browse files
bpo-34392: Add sys. _is_interned() (GH-8755)
1 parent 0e732d0 commit dee7bee

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
lines changed

Doc/library/sys.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,18 @@ always available.
12051205

12061206
.. versionadded:: 3.12
12071207

1208+
.. function:: _is_interned(string)
1209+
1210+
Return :const:`True` if the given string is "interned", :const:`False`
1211+
otherwise.
1212+
1213+
.. versionadded:: 3.13
1214+
1215+
.. impl-detail::
1216+
1217+
It is not guaranteed to exist in all implementations of Python.
1218+
1219+
12081220
.. data:: last_type
12091221
last_value
12101222
last_traceback

Doc/whatsnew/3.13.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@ sqlite3
297297
object is not :meth:`closed <sqlite3.Connection.close>` explicitly.
298298
(Contributed by Erlend E. Aasland in :gh:`105539`.)
299299

300+
sys
301+
---
302+
303+
* Add the :func:`sys._is_interned` function to test if the string was interned.
304+
This function is not guaranteed to exist in all implementations of Python.
305+
(Contributed by Serhiy Storchaka in :gh:`78573`.)
306+
300307
tkinter
301308
-------
302309

Lib/test/test_sys.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,23 @@ def test_43581(self):
691691
self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding)
692692

693693
def test_intern(self):
694+
has_is_interned = (test.support.check_impl_detail(cpython=True)
695+
or hasattr(sys, '_is_interned'))
694696
self.assertRaises(TypeError, sys.intern)
697+
self.assertRaises(TypeError, sys.intern, b'abc')
698+
if has_is_interned:
699+
self.assertRaises(TypeError, sys._is_interned)
700+
self.assertRaises(TypeError, sys._is_interned, b'abc')
695701
s = "never interned before" + str(random.randrange(0, 10**9))
696702
self.assertTrue(sys.intern(s) is s)
703+
if has_is_interned:
704+
self.assertIs(sys._is_interned(s), True)
697705
s2 = s.swapcase().swapcase()
706+
if has_is_interned:
707+
self.assertIs(sys._is_interned(s2), False)
698708
self.assertTrue(sys.intern(s2) is s)
709+
if has_is_interned:
710+
self.assertIs(sys._is_interned(s2), False)
699711

700712
# Subclasses of string can't be interned, because they
701713
# provide too much opportunity for insane things to happen.
@@ -707,6 +719,8 @@ def __hash__(self):
707719
return 123
708720

709721
self.assertRaises(TypeError, sys.intern, S("abc"))
722+
if has_is_interned:
723+
self.assertIs(sys._is_interned(S("abc")), False)
710724

711725
@requires_subinterpreters
712726
def test_subinterp_intern_dynamically_allocated(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added :func:`sys._is_interned`.

Python/clinic/sysmodule.c.h

Lines changed: 35 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/sysmodule.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,23 @@ sys_intern_impl(PyObject *module, PyObject *s)
989989
}
990990

991991

992+
/*[clinic input]
993+
sys._is_interned -> bool
994+
995+
string: unicode
996+
/
997+
998+
Return True if the given string is "interned".
999+
[clinic start generated code]*/
1000+
1001+
static int
1002+
sys__is_interned_impl(PyObject *module, PyObject *string)
1003+
/*[clinic end generated code: output=c3678267b4e9d7ed input=039843e17883b606]*/
1004+
{
1005+
return PyUnicode_CHECK_INTERNED(string);
1006+
}
1007+
1008+
9921009
/*
9931010
* Cached interned string objects used for calling the profile and
9941011
* trace functions.
@@ -2462,6 +2479,7 @@ static PyMethodDef sys_methods[] = {
24622479
SYS_GETWINDOWSVERSION_METHODDEF
24632480
SYS__ENABLELEGACYWINDOWSFSENCODING_METHODDEF
24642481
SYS_INTERN_METHODDEF
2482+
SYS__IS_INTERNED_METHODDEF
24652483
SYS_IS_FINALIZING_METHODDEF
24662484
SYS_MDEBUG_METHODDEF
24672485
SYS_SETSWITCHINTERVAL_METHODDEF

0 commit comments

Comments
 (0)