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

Add function to get caller's name: sys._getframemodulename() #86682

Closed
zooba opened this issue Nov 30, 2020 · 4 comments
Closed

Add function to get caller's name: sys._getframemodulename() #86682

zooba opened this issue Nov 30, 2020 · 4 comments
Assignees
Labels
3.12 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@zooba
Copy link
Member

zooba commented Nov 30, 2020

BPO 42516
Nosy @vstinner, @zooba, @corona10, @FFY00

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2020-11-30.21:54:46.811>
labels = ['interpreter-core', 'type-feature', '3.10']
title = "Add function to get caller's name"
updated_at = <Date 2021-06-11.16:17:34.552>
user = 'https://github.com/zooba'

bugs.python.org fields:

activity = <Date 2021-06-11.16:17:34.552>
actor = 'corona10'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2020-11-30.21:54:46.811>
creator = 'steve.dower'
dependencies = []
files = []
hgrepos = []
issue_num = 42516
keywords = []
message_count = 1.0
messages = ['382199']
nosy_count = 4.0
nosy_names = ['vstinner', 'steve.dower', 'corona10', 'FFY00']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'test needed'
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue42516'
versions = ['Python 3.10']

Linked PRs

@zooba
Copy link
Member Author

zooba commented Nov 30, 2020

We have a lot of stdlib code that looks like:

try:
    nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
    pass

While technically it handles sys._getframe being missing, it would be nice to handle it better for this scenario.

I'm already using a sys._get_calling_module_name() as an internal patch to avoid exposing the _getframe() calls to Python code. (As I recall, it cleans up basically all of the uses apart from the traceback module.) This lets us treat sys._getframe() calls as suspicious, because now most code never uses it.

/*[clinic input]
sys._get_calling_module_name
Return the name of the calling module.
[clinic start generated code]*/
static PyObject *
sys__get_calling_module_name_impl(PyObject *module)
/*[clinic end generated code]*/
{
    PyFrameObject *f = _PyThreadState_GET()->frame;
    PyObject *r;
    if (f == NULL) {
        Py_RETURN_NONE;
    }
    f = f->f_back;
    if (f == NULL) {
        Py_RETURN_NONE;
    }
    r = _PyDict_GetItemIdWithError(f->f_globals, &PyId___name__);
    if (!r) {
        PyErr_Clear();
        r = Py_None;
    }
    Py_INCREF(r);
    return r;
}

For something that will live beyond a separate patch, it might make sense to add as much functionality as needed for the warning module, which currently skips some importlib frames and a caller-specified count. I wouldn't want to make it too much more complex though.

Separating this out would make it easier for other implementations to support enum, typing, collections, and any other modules that want the caller's name, even if they can't easily/efficiently support a full _getframe().

Thoughts?

@zooba zooba added 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Nov 30, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@zooba zooba added 3.12 bugs and security fixes and removed 3.10 only security fixes labels Nov 15, 2022
@zooba zooba self-assigned this Nov 15, 2022
zooba added a commit to zooba/cpython that referenced this issue Nov 15, 2022
…ame calls in collections, doctest, enum, and typing modules
@JelleZijlstra
Copy link
Member

Related: faster-cpython/ideas#238, #90701, #30950.

@zooba
Copy link
Member Author

zooba commented Nov 15, 2022

Hah, I didn't even know about those. Was mostly just trying to cut down on my log spam from everyone calling _getframe all the time :)

zooba added a commit to zooba/cpython that referenced this issue Nov 16, 2022
zooba added a commit to zooba/cpython that referenced this issue Nov 17, 2022
zooba added a commit to zooba/cpython that referenced this issue Nov 21, 2022
zooba added a commit to zooba/cpython that referenced this issue Jan 10, 2023
zooba added a commit that referenced this issue Jan 13, 2023
…tframe (GH-99520)

Also updates calls in collections, doctest, enum, and typing modules to use _getframemodulename first when available.
@hauntsaninja
Copy link
Contributor

Looks like this was completed

@vstinner vstinner changed the title Add function to get caller's name Add function to get caller's name: sys._getframemodulename() Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants