python / cpython

The Python programming language
https://www.python.org/
Other
60.05k stars 29.08k forks source link

sys._getframe() for getting the current stack frame #33393

Closed warsaw closed 23 years ago

warsaw commented 23 years ago
BPO 402106
Nosy @gvanrossum, @freddrake, @warsaw
Files
  • None: None
  • 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: ```python assignee = 'https://github.com/warsaw' closed_at = created_at = labels = ['interpreter-core'] title = 'sys._getframe() for getting the current stack frame' updated_at = user = 'https://github.com/warsaw' ``` bugs.python.org fields: ```python activity = actor = 'barry' assignee = 'barry' closed = True closed_date = None closer = None components = ['Interpreter Core'] creation = creator = 'barry' dependencies = [] files = ['2901'] hgrepos = [] issue_num = 402106 keywords = ['patch'] message_count = 9.0 messages = ['34707', '34708', '34709', '34710', '34711', '34712', '34713', '34714', '34715'] nosy_count = 4.0 nosy_names = ['gvanrossum', 'fdrake', 'ping', 'barry'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue402106' versions = [] ```

    warsaw commented 23 years ago
    freddrake commented 23 years ago

    My only comment on the docs (both docstring and LaTeX) is that it should say "ValueError is raised" rather than "a ValueError is raised" (drop the "a"). Otherwise looks good; I've not run it through the formatting.

    gvanrossum commented 23 years ago

    The docstring and docs should say that the default depth is zero. They should also mention that this is for internal use only. Can you implement this in Jython too?

    gvanrossum commented 23 years ago

    I disagree with Ping. The function needs to be clearly marked as non-public-API -- it's an interface that should be used only to implement a few blessed higher-level library modules.

    It differs from getrefcount(), which is harmless, and exc_info(), which is intended to be used in preference to exc_type etc. (in order to be thread-safe).

    BTW, I'm accepting the patch -- I don't know why it hasn't been checked in yet. (The doc changes suggested should still be made.)

    warsaw commented 23 years ago

    This patch adds a function to the sys module which will return a frame object. By default, it returns the frame object of the current stack frame, but an optional integer argument can be supplied to retrieve any frame higher up the stack. If the integer is greater than the depth of the stack, a ValueError is raised.

    warsaw commented 23 years ago

    Good point. Patch updated. Thanks.

    warsaw commented 23 years ago

    Oops, fixed one small typo.

    warsaw commented 23 years ago

    Checking in now. sysmodule.c 2.79 libsys.tex 1.43

    9647ba2a-5717-4481-b336-914e78a93294 commented 23 years ago

    It's a small point, but i'd much prefer the function to be called sys.getframe() rather than sys._getframe(). There aren't any other functions in sys that begin with an underscore, and it's understood that routines in sys manipulate the Python interpreter system anyway (such as getrefcount or exc_info). This routine isn't private within sys; it's exported by sys, so it should be called sys.getframe().