Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-86682: Adds sys._getframemodulename as a lightweight alternative to _getframe #99520

Merged
merged 15 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,22 @@ always available.
.. versionadded:: 3.2


.. function:: _getcallingmodule([depth])
zooba marked this conversation as resolved.
Show resolved Hide resolved

Return the name of a module from the call stack. If optional integer *depth*
is given, return the module that many calls below the top of the stack. If
that is deeper than the call stack, or if the module is unidentifiable,
``None`` is returned. The default for *depth* is zero, returning the
module at the top of the call stack.

.. audit-event:: sys._getcallingmodule depth sys._getcallingmodule

.. impl-detail::

This function should be used for internal and specialized purposes only.
It is not guaranteed to exist in all implementations of Python.


.. function:: _getframe([depth])

Return a frame object from the call stack. If optional integer *depth* is
Expand Down
9 changes: 6 additions & 3 deletions Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,12 @@ def __getnewargs__(self):
# specified a particular module.
if module is None:
try:
module = _sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
pass
module = _sys._getcallingmodule(1) or '__main__'
except AttributeError:
try:
module = _sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
pass
if module is not None:
result.__module__ = module

Expand Down
8 changes: 7 additions & 1 deletion Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ def _normalize_module(module, depth=2):
elif isinstance(module, str):
return __import__(module, globals(), locals(), ["*"])
elif module is None:
return sys.modules[sys._getframe(depth).f_globals['__name__']]
try:
try:
return sys.modules[sys._getcallingmodule(depth)]
except AttributeError:
return sys.modules[sys._getframe(depth).f_globals['__name__']]
except KeyError:
pass
else:
raise TypeError("Expected a module, string, or None")

Expand Down
12 changes: 7 additions & 5 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,13 +860,15 @@ def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, s
member_name, member_value = item
classdict[member_name] = member_value

# TODO: replace the frame hack if a blessed way to know the calling
# module is ever developed
if module is None:
try:
module = sys._getframe(2).f_globals['__name__']
except (AttributeError, ValueError, KeyError):
pass
module = sys._getcallingmodule(2)
except AttributeError:
# Fall back on _getframe if _getcallingmodule is missing
try:
module = sys._getframe(2).f_globals['__name__']
except (AttributeError, ValueError, KeyError):
pass
if module is None:
_make_class_unpicklable(classdict)
else:
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ def hook(event, args):
sys._getframe()


def test_sys_getcallingmodule():
import sys

def hook(event, args):
if event.startswith("sys."):
print(event, *args)

sys.addaudithook(hook)
sys._getcallingmodule()


def test_threading():
import _thread

Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ def test_sys_getframe(self):

self.assertEqual(actual, expected)

def test_sys_getcallingmodule(self):
returncode, events, stderr = self.run_python("test_sys_getcallingmodule")
if returncode:
self.fail(stderr)

if support.verbose:
print(*events, sep='\n')
actual = [(ev[0], ev[2]) for ev in events]
expected = [("sys._getcallingmodule", "0")]

self.assertEqual(actual, expected)


def test_threading(self):
returncode, events, stderr = self.run_python("test_threading")
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ def test_getframe(self):
is sys._getframe().f_code
)

def test_getcallingmodule(self):
# Default depth gets ourselves
self.assertEqual(__name__, sys._getcallingmodule())
self.assertEqual("unittest.case", sys._getcallingmodule(1))

# sys._current_frames() is a CPython-only gimmick.
@threading_helper.reap_threads
@threading_helper.requires_working_threading()
Expand Down
8 changes: 6 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1942,11 +1942,15 @@ def _no_init_or_replace_init(self, *args, **kwargs):


def _caller(depth=1, default='__main__'):
try:
return sys._getcallingmodule(depth + 1) or default
except AttributeError: # For platforms without _getcallingmodule()
pass
try:
return sys._getframe(depth + 1).f_globals.get('__name__', default)
except (AttributeError, ValueError): # For platforms without _getframe()
return None

pass
return None

def _allow_reckless_class_checks(depth=3):
"""Allow instance and class checks for special stdlib modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure runtime-created collections have the correct module name using
the newly added (internal) :func:`sys._getcallingmodulename`.
69 changes: 68 additions & 1 deletion Python/clinic/sysmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2180,6 +2180,41 @@ sys_is_stack_trampoline_active_impl(PyObject *module)
}


/*[clinic input]
sys._getcallingmodule

depth: int = 0

Return the name of the calling module, or None if not available.

The default depth returns the module containing the call to this API.
A more typical use in a library will pass a depth of 1 to get the user's
module rather than the library module.
[clinic start generated code]*/

static PyObject *
sys__getcallingmodule_impl(PyObject *module, int depth)
/*[clinic end generated code: output=2035dac7eecb38ae input=8138669512c41eec]*/
{
if (PySys_Audit("sys._getcallingmodule", "i", depth) < 0) {
return NULL;
}
_PyInterpreterFrame *f = _PyThreadState_GET()->cframe->current_frame;
while (f && (f->owner == FRAME_OWNED_BY_CSTACK || depth-- > 0)) {
f = f->previous;
}
zooba marked this conversation as resolved.
Show resolved Hide resolved
if (f == NULL) {
Py_RETURN_NONE;
}
PyObject *r = PyDict_GetItemWithError(f->f_globals, &_Py_ID(__name__));
zooba marked this conversation as resolved.
Show resolved Hide resolved
if (!r) {
PyErr_Clear();
r = Py_None;
}
return Py_NewRef(r);
}


static PyMethodDef sys_methods[] = {
/* Might as well keep this in alphabetic order */
SYS_ADDAUDITHOOK_METHODDEF
Expand Down Expand Up @@ -2207,6 +2242,7 @@ static PyMethodDef sys_methods[] = {
SYS_GETRECURSIONLIMIT_METHODDEF
{"getsizeof", _PyCFunction_CAST(sys_getsizeof),
METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
SYS__GETCALLINGMODULE_METHODDEF
SYS__GETFRAME_METHODDEF
SYS_GETWINDOWSVERSION_METHODDEF
SYS__ENABLELEGACYWINDOWSFSENCODING_METHODDEF
Expand Down