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 2 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
15 changes: 15 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,21 @@ always available.
.. versionadded:: 3.2


.. function:: _getcaller([depth])

Return a function object from the call stack. If optional integer *depth* is
given, return the frame object that many calls below the top of the stack. If
that is deeper than the call stack, :exc:`ValueError` is raised. The default
for *depth* is zero, returning the function at the top of the call stack.

.. audit-event:: sys._getcaller depth sys._getcaller

.. 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 = getattr(_sys._getcaller(1), '__module__', '__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
7 changes: 6 additions & 1 deletion Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ 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:
caller = sys._getcaller(depth)
except AttributeError:
return sys.modules[sys._getframe(depth).f_globals['__name__']]
else:
return sys.modules[caller.__module__]
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 = getattr(sys._getcaller(2), '__module__', None)
except AttributeError:
# Fall back on _getframe if _getcaller 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_getcaller():
import sys

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

sys.addaudithook(hook)
sys._getcaller()


def test_wmi_exec_query():
import _wmi

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_getcaller(self):
returncode, events, stderr = self.run_python("test_sys_getcaller")
if returncode:
self.fail(stderr)

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

self.assertEqual(actual, expected)

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

def test_getcaller(self):
# Default depth gets ourselves
func = type(self).test_getcaller
self.assertIs(func, sys._getcaller())

def get_1(d):
return sys._getcaller(d)

def get_2(d):
return get_1(d)

self.assertIs(get_1, get_1(0))
self.assertIs(func, get_1(1))

self.assertIs(get_1, get_2(0))
self.assertIs(get_2, get_2(1))
self.assertIs(func, get_2(2))

# 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 getattr(sys._getcaller(depth + 1), '__module__', default)
except AttributeError: # For platforms without _getcaller()
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 @@
Changes internal function used to ensure runtime-created collections have
the correct module name from :func:`sys._getframe` to :func:`sys._getcaller`.
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.

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


/*[clinic input]
sys._getcaller

depth: int = 0

Return the calling function object.

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

static PyObject *
sys__getcaller_impl(PyObject *module, int depth)
/*[clinic end generated code: output=250f47adb2372e4a input=1993a1558597f1ed]*/
{
if (PySys_Audit("sys._getcaller", "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;
}
return Py_NewRef(f->f_funcobj);
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 +2243,7 @@ static PyMethodDef sys_methods[] = {
SYS_GETRECURSIONLIMIT_METHODDEF
{"getsizeof", _PyCFunction_CAST(sys_getsizeof),
METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
SYS__GETCALLER_METHODDEF
SYS__GETFRAME_METHODDEF
SYS_GETWINDOWSVERSION_METHODDEF
SYS__ENABLELEGACYWINDOWSFSENCODING_METHODDEF
Expand Down