]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-34392: Add sys. _is_interned() (GH-8755)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 4 Dec 2023 09:09:06 +0000 (11:09 +0200)
committerGitHub <noreply@github.com>
Mon, 4 Dec 2023 09:09:06 +0000 (11:09 +0200)
Doc/library/sys.rst
Doc/whatsnew/3.13.rst
Lib/test/test_sys.py
Misc/NEWS.d/next/Core and Builtins/2018-08-13-13-25-15.bpo-34392.9kIlMF.rst [new file with mode: 0644]
Python/clinic/sysmodule.c.h
Python/sysmodule.c

index bf9aaca2a696ded98ce94941cbdae31016463a77..7f359819e6847ee35efcffc7d6ed0c01a39ae097 100644 (file)
@@ -1205,6 +1205,18 @@ always available.
 
    .. versionadded:: 3.12
 
+.. function:: _is_interned(string)
+
+   Return :const:`True` if the given string is "interned", :const:`False`
+   otherwise.
+
+   .. versionadded:: 3.13
+
+   .. impl-detail::
+
+      It is not guaranteed to exist in all implementations of Python.
+
+
 .. data:: last_type
           last_value
           last_traceback
index 372e4a45468e68be720f73cc5034519ece62956b..676305c6e1d06a82ab606a46a07f369ff568287a 100644 (file)
@@ -297,6 +297,13 @@ sqlite3
   object is not :meth:`closed <sqlite3.Connection.close>` explicitly.
   (Contributed by Erlend E. Aasland in :gh:`105539`.)
 
+sys
+---
+
+* Add the :func:`sys._is_interned` function to test if the string was interned.
+  This function is not guaranteed to exist in all implementations of Python.
+  (Contributed by Serhiy Storchaka in :gh:`78573`.)
+
 tkinter
 -------
 
index 0028281596fa4be743110ff18b4ef2f2f97e9ef9..8c2c1a40f74bf245a9a5557ff8b0ea476c3343d5 100644 (file)
@@ -691,11 +691,23 @@ class SysModuleTest(unittest.TestCase):
         self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding)
 
     def test_intern(self):
+        has_is_interned = (test.support.check_impl_detail(cpython=True)
+                           or hasattr(sys, '_is_interned'))
         self.assertRaises(TypeError, sys.intern)
+        self.assertRaises(TypeError, sys.intern, b'abc')
+        if has_is_interned:
+            self.assertRaises(TypeError, sys._is_interned)
+            self.assertRaises(TypeError, sys._is_interned, b'abc')
         s = "never interned before" + str(random.randrange(0, 10**9))
         self.assertTrue(sys.intern(s) is s)
+        if has_is_interned:
+            self.assertIs(sys._is_interned(s), True)
         s2 = s.swapcase().swapcase()
+        if has_is_interned:
+            self.assertIs(sys._is_interned(s2), False)
         self.assertTrue(sys.intern(s2) is s)
+        if has_is_interned:
+            self.assertIs(sys._is_interned(s2), False)
 
         # Subclasses of string can't be interned, because they
         # provide too much opportunity for insane things to happen.
@@ -707,6 +719,8 @@ class SysModuleTest(unittest.TestCase):
                 return 123
 
         self.assertRaises(TypeError, sys.intern, S("abc"))
+        if has_is_interned:
+            self.assertIs(sys._is_interned(S("abc")), False)
 
     @requires_subinterpreters
     def test_subinterp_intern_dynamically_allocated(self):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-08-13-13-25-15.bpo-34392.9kIlMF.rst b/Misc/NEWS.d/next/Core and Builtins/2018-08-13-13-25-15.bpo-34392.9kIlMF.rst
new file mode 100644 (file)
index 0000000..bc4fd1a
--- /dev/null
@@ -0,0 +1 @@
+Added :func:`sys._is_interned`.
index 98717ecc875b8bcede7145d385d7c7c1833c8a44..93b8385a5b4097c2892bf22ebd6c7a084ae2cd33 100644 (file)
@@ -289,6 +289,40 @@ exit:
     return return_value;
 }
 
+PyDoc_STRVAR(sys__is_interned__doc__,
+"_is_interned($module, string, /)\n"
+"--\n"
+"\n"
+"Return True if the given string is \"interned\".");
+
+#define SYS__IS_INTERNED_METHODDEF    \
+    {"_is_interned", (PyCFunction)sys__is_interned, METH_O, sys__is_interned__doc__},
+
+static int
+sys__is_interned_impl(PyObject *module, PyObject *string);
+
+static PyObject *
+sys__is_interned(PyObject *module, PyObject *arg)
+{
+    PyObject *return_value = NULL;
+    PyObject *string;
+    int _return_value;
+
+    if (!PyUnicode_Check(arg)) {
+        _PyArg_BadArgument("_is_interned", "argument", "str", arg);
+        goto exit;
+    }
+    string = arg;
+    _return_value = sys__is_interned_impl(module, string);
+    if ((_return_value == -1) && PyErr_Occurred()) {
+        goto exit;
+    }
+    return_value = PyBool_FromLong((long)_return_value);
+
+exit:
+    return return_value;
+}
+
 PyDoc_STRVAR(sys__settraceallthreads__doc__,
 "_settraceallthreads($module, arg, /)\n"
 "--\n"
@@ -1452,4 +1486,4 @@ exit:
 #ifndef SYS_GETANDROIDAPILEVEL_METHODDEF
     #define SYS_GETANDROIDAPILEVEL_METHODDEF
 #endif /* !defined(SYS_GETANDROIDAPILEVEL_METHODDEF) */
-/*[clinic end generated code: output=f36d45c829250775 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=3dc3b2cb0ce38ebb input=a9049054013a1b77]*/
index c17de44731b7030db47e8959ff15da9cf7a600c3..46878c7c9687f5c449ac5d8f614e838ab4c722f9 100644 (file)
@@ -989,6 +989,23 @@ sys_intern_impl(PyObject *module, PyObject *s)
 }
 
 
+/*[clinic input]
+sys._is_interned -> bool
+
+  string: unicode
+  /
+
+Return True if the given string is "interned".
+[clinic start generated code]*/
+
+static int
+sys__is_interned_impl(PyObject *module, PyObject *string)
+/*[clinic end generated code: output=c3678267b4e9d7ed input=039843e17883b606]*/
+{
+    return PyUnicode_CHECK_INTERNED(string);
+}
+
+
 /*
  * Cached interned string objects used for calling the profile and
  * trace functions.
@@ -2462,6 +2479,7 @@ static PyMethodDef sys_methods[] = {
     SYS_GETWINDOWSVERSION_METHODDEF
     SYS__ENABLELEGACYWINDOWSFSENCODING_METHODDEF
     SYS_INTERN_METHODDEF
+    SYS__IS_INTERNED_METHODDEF
     SYS_IS_FINALIZING_METHODDEF
     SYS_MDEBUG_METHODDEF
     SYS_SETSWITCHINTERVAL_METHODDEF