]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-120029: export `DEF_TYPE_PARAM` compiler flag (#120028)
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Tue, 4 Jun 2024 14:24:22 +0000 (16:24 +0200)
committerGitHub <noreply@github.com>
Tue, 4 Jun 2024 14:24:22 +0000 (07:24 -0700)
Doc/library/symtable.rst
Lib/symtable.py
Lib/test/test_symtable.py
Misc/NEWS.d/next/Library/2024-06-04-14-54-46.gh-issue-120029._1YdTf.rst [new file with mode: 0644]
Modules/symtablemodule.c

index 0480502158433a5a17ee9cb61baaa7ab497ecd15..e17a33f7feb1ab46037f11bf8c051da3e3e1d60d 100644 (file)
@@ -151,6 +151,10 @@ Examining Symbol Tables
 
       Return ``True`` if the symbol is a parameter.
 
+   .. method:: is_type_parameter()
+
+      Return ``True`` if the symbol is a type parameter.
+
    .. method:: is_global()
 
       Return ``True`` if the symbol is global.
index 17f820abd566604ca97d5b0fcfe45a9bfc9ef6f7..ba2f0dafcd0063ad7160a520e005afa710e962d3 100644 (file)
@@ -1,9 +1,13 @@
 """Interface to the compiler's internal symbol tables"""
 
 import _symtable
-from _symtable import (USE, DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL, DEF_PARAM,
-     DEF_IMPORT, DEF_BOUND, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE,
-     LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL)
+from _symtable import (
+    USE,
+    DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL,
+    DEF_PARAM, DEF_TYPE_PARAM, DEF_IMPORT, DEF_BOUND, DEF_ANNOT,
+    SCOPE_OFF, SCOPE_MASK,
+    FREE, LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL
+)
 
 import weakref
 
@@ -253,13 +257,18 @@ class Symbol:
         """Return *True* if the symbol is used in
         its block.
         """
-        return bool(self.__flags & _symtable.USE)
+        return bool(self.__flags & USE)
 
     def is_parameter(self):
         """Return *True* if the symbol is a parameter.
         """
         return bool(self.__flags & DEF_PARAM)
 
+    def is_type_parameter(self):
+        """Return *True* if the symbol is a type parameter.
+        """
+        return bool(self.__flags & DEF_TYPE_PARAM)
+
     def is_global(self):
         """Return *True* if the symbol is global.
         """
index 92b78a8086a83da74520302cdd559212bae92061..ef2a228b15ed4e00f04680be945023a54373a8cc 100644 (file)
@@ -299,6 +299,8 @@ class SymtableTest(unittest.TestCase):
                          "<symbol 'x': FREE, USE>")
         self.assertEqual(repr(self.other_internal.lookup("some_var")),
                          "<symbol 'some_var': FREE, USE|DEF_NONLOCAL|DEF_LOCAL>")
+        self.assertEqual(repr(self.GenericMine.lookup("T")),
+                         "<symbol 'T': LOCAL, DEF_LOCAL|DEF_TYPE_PARAM>")
 
     def test_symtable_entry_repr(self):
         expected = f"<symtable entry top({self.top.get_id()}), line {self.top.get_lineno()}>"
diff --git a/Misc/NEWS.d/next/Library/2024-06-04-14-54-46.gh-issue-120029._1YdTf.rst b/Misc/NEWS.d/next/Library/2024-06-04-14-54-46.gh-issue-120029._1YdTf.rst
new file mode 100644 (file)
index 0000000..e8ea107
--- /dev/null
@@ -0,0 +1,2 @@
+Expose :meth:`symtable.Symbol.is_type_parameter` in the :mod:`symtable`
+module. Patch by Bénédikt Tran.
index b4dbb54c3b47b00cf03c2822ad9f066de1925f46..63c4dd4225298d580cd542ddfd078514a5a5cbbf 100644 (file)
@@ -75,6 +75,7 @@ symtable_init_constants(PyObject *m)
     if (PyModule_AddIntMacro(m, DEF_NONLOCAL) < 0) return -1;
     if (PyModule_AddIntMacro(m, DEF_LOCAL) < 0) return -1;
     if (PyModule_AddIntMacro(m, DEF_PARAM) < 0) return -1;
+    if (PyModule_AddIntMacro(m, DEF_TYPE_PARAM) < 0) return -1;
     if (PyModule_AddIntMacro(m, DEF_FREE) < 0) return -1;
     if (PyModule_AddIntMacro(m, DEF_FREE_CLASS) < 0) return -1;
     if (PyModule_AddIntMacro(m, DEF_IMPORT) < 0) return -1;
@@ -83,7 +84,8 @@ symtable_init_constants(PyObject *m)
 
     if (PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock) < 0)
         return -1;
-    if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0) return -1;
+    if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0)
+        return -1;
     if (PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock) < 0)
         return -1;
     if (PyModule_AddIntConstant(m, "TYPE_ANNOTATION", AnnotationBlock) < 0)