summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@python.org>2023-01-13 11:31:06 (GMT)
committerGitHub <noreply@github.com>2023-01-13 11:31:06 (GMT)
commitb5d4347950399800c6703736d716f08761b29245 (patch)
treebb838154108d6740e062cd5b3d0756fd1a4f2d89 /Lib/test
parent94fc7706b7bc3d57cdd6d15bf8e8c4499ae53a69 (diff)
downloadcpython-b5d4347950399800c6703736d716f08761b29245.zip
cpython-b5d4347950399800c6703736d716f08761b29245.tar.gz
cpython-b5d4347950399800c6703736d716f08761b29245.tar.bz2
gh-86682: Adds sys._getframemodulename as an alternative to using _getframe (GH-99520)
Also updates calls in collections, doctest, enum, and typing modules to use _getframemodulename first when available.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/audit-tests.py11
-rw-r--r--Lib/test/test_audit.py12
-rw-r--r--Lib/test/test_sys.py20
3 files changed, 43 insertions, 0 deletions
diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py
index bf56cea..0edc9d9 100644
--- a/Lib/test/audit-tests.py
+++ b/Lib/test/audit-tests.py
@@ -419,6 +419,17 @@ def test_sys_getframe():
sys._getframe()
+def test_sys_getframemodulename():
+ import sys
+
+ def hook(event, args):
+ if event.startswith("sys."):
+ print(event, *args)
+
+ sys.addaudithook(hook)
+ sys._getframemodulename()
+
+
def test_threading():
import _thread
diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py
index 70f8a77..0b69864 100644
--- a/Lib/test/test_audit.py
+++ b/Lib/test/test_audit.py
@@ -186,6 +186,18 @@ class AuditTest(unittest.TestCase):
self.assertEqual(actual, expected)
+ def test_sys_getframemodulename(self):
+ returncode, events, stderr = self.run_python("test_sys_getframemodulename")
+ if returncode:
+ self.fail(stderr)
+
+ if support.verbose:
+ print(*events, sep='\n')
+ actual = [(ev[0], ev[2]) for ev in events]
+ expected = [("sys._getframemodulename", "0")]
+
+ self.assertEqual(actual, expected)
+
def test_threading(self):
returncode, events, stderr = self.run_python("test_threading")
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 232b799..ab1a065 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -399,6 +399,26 @@ class SysModuleTest(unittest.TestCase):
is sys._getframe().f_code
)
+ def test_getframemodulename(self):
+ # Default depth gets ourselves
+ self.assertEqual(__name__, sys._getframemodulename())
+ self.assertEqual("unittest.case", sys._getframemodulename(1))
+ i = 0
+ f = sys._getframe(i)
+ while f:
+ self.assertEqual(
+ f.f_globals['__name__'],
+ sys._getframemodulename(i) or '__main__'
+ )
+ i += 1
+ f2 = f.f_back
+ try:
+ f = sys._getframe(i)
+ except ValueError:
+ break
+ self.assertIs(f, f2)
+ self.assertIsNone(sys._getframemodulename(i))
+
# sys._current_frames() is a CPython-only gimmick.
@threading_helper.reap_threads
@threading_helper.requires_working_threading()