Skip to content

Commit ff1857d

Browse files
authored
gh-120029: export DEF_TYPE_PARAM compiler flag (#120028)
1 parent e69d068 commit ff1857d

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

Doc/library/symtable.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ Examining Symbol Tables
151151

152152
Return ``True`` if the symbol is a parameter.
153153

154+
.. method:: is_type_parameter()
155+
156+
Return ``True`` if the symbol is a type parameter.
157+
154158
.. method:: is_global()
155159

156160
Return ``True`` if the symbol is global.

Lib/symtable.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"""Interface to the compiler's internal symbol tables"""
22

33
import _symtable
4-
from _symtable import (USE, DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL, DEF_PARAM,
5-
DEF_IMPORT, DEF_BOUND, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE,
6-
LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL)
4+
from _symtable import (
5+
USE,
6+
DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL,
7+
DEF_PARAM, DEF_TYPE_PARAM, DEF_IMPORT, DEF_BOUND, DEF_ANNOT,
8+
SCOPE_OFF, SCOPE_MASK,
9+
FREE, LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL
10+
)
711

812
import weakref
913

@@ -253,13 +257,18 @@ def is_referenced(self):
253257
"""Return *True* if the symbol is used in
254258
its block.
255259
"""
256-
return bool(self.__flags & _symtable.USE)
260+
return bool(self.__flags & USE)
257261

258262
def is_parameter(self):
259263
"""Return *True* if the symbol is a parameter.
260264
"""
261265
return bool(self.__flags & DEF_PARAM)
262266

267+
def is_type_parameter(self):
268+
"""Return *True* if the symbol is a type parameter.
269+
"""
270+
return bool(self.__flags & DEF_TYPE_PARAM)
271+
263272
def is_global(self):
264273
"""Return *True* if the symbol is global.
265274
"""

Lib/test/test_symtable.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ def test_symbol_repr(self):
299299
"<symbol 'x': FREE, USE>")
300300
self.assertEqual(repr(self.other_internal.lookup("some_var")),
301301
"<symbol 'some_var': FREE, USE|DEF_NONLOCAL|DEF_LOCAL>")
302+
self.assertEqual(repr(self.GenericMine.lookup("T")),
303+
"<symbol 'T': LOCAL, DEF_LOCAL|DEF_TYPE_PARAM>")
302304

303305
def test_symtable_entry_repr(self):
304306
expected = f"<symtable entry top({self.top.get_id()}), line {self.top.get_lineno()}>"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Expose :meth:`symtable.Symbol.is_type_parameter` in the :mod:`symtable`
2+
module. Patch by Bénédikt Tran.

Modules/symtablemodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ symtable_init_constants(PyObject *m)
7575
if (PyModule_AddIntMacro(m, DEF_NONLOCAL) < 0) return -1;
7676
if (PyModule_AddIntMacro(m, DEF_LOCAL) < 0) return -1;
7777
if (PyModule_AddIntMacro(m, DEF_PARAM) < 0) return -1;
78+
if (PyModule_AddIntMacro(m, DEF_TYPE_PARAM) < 0) return -1;
7879
if (PyModule_AddIntMacro(m, DEF_FREE) < 0) return -1;
7980
if (PyModule_AddIntMacro(m, DEF_FREE_CLASS) < 0) return -1;
8081
if (PyModule_AddIntMacro(m, DEF_IMPORT) < 0) return -1;
@@ -83,7 +84,8 @@ symtable_init_constants(PyObject *m)
8384

8485
if (PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock) < 0)
8586
return -1;
86-
if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0) return -1;
87+
if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0)
88+
return -1;
8789
if (PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock) < 0)
8890
return -1;
8991
if (PyModule_AddIntConstant(m, "TYPE_ANNOTATION", AnnotationBlock) < 0)

0 commit comments

Comments
 (0)