Skip to content

inspect "lite" for stack and function/method signatures #10475

Open
@jbrelwof

Description

@jbrelwof

TL;DR - It would be useful to have some functionality provided by the standard Python inspect module in CircuitPython, especially

  • basic stack frame inspection (i.e. walk up the call stack from within a function)
  • simple callable signature inspection (parameter names required positional / optional positional / keyword counts)

There are a number of useful Python patterns which rely on the ability to get information about the "caller" from with a function/method (i.e. walk back up the stack). These typically use the inspect module from the Python Standard Library. Attempting to create a fully functional CircuitPython version of inspect would be a significant challenge at best, and might be effectively impossible (short of back-porting most of CPython into CircuitPython, which would add a huge amount of overhead).

However, there are a number of things CircuitPython already "knows" (at least mostly) about the running code. Specifically, method name / file / line number information for the current stack, and the number of positional and names of keyword parameters for a callable. These could theoretically be made available without adding any overhead (beyond compiled code to expose accessor methods). In my experience, this covers a substantial portion of inspect module use cases, and at least some of the remainder gets into deep patterns (like meta class and annotation/docstring gymnastics).

I'd like to see a module (probably named something CircuitPython specific, instead of attempting to mimic a subset of inspect in a python source-compatible manner ) implementing something similar to

  • inspect.currentFrame() / inspect.stack(...) / inspect.trace(...)
    • returning an instance (or list of) something similar to inspect.FrameInfo
      • with less information - some things available in "full" python may not even exist internally in CircuitPython, but ideally with
        • function name ( info.function )
        • filename ( info.filename )
        • line number ( info.lineno or info.frame.f_code.co_firstlineno )
        • globals ( info.frame.f_globals )
        • module ( possibly just sys.modules[info.frame.f_globals['__name__'])
        • prior frame ( info.f_back )
      • most/all of this information is already used by the exception handling system and traceback messages
        • might not be easily accessible without actually unwinding the stack ?
    • inspect.signature(...)
      • given a callable or a FrameInfo-ish instance from stack inspection, get
      • the name
      • positional arg count
      • required positional arg count
      • keyword only count
      • parameter names
      • globals
      • some of this information must already be available, as attempting to invoke a callable defined in user python code with too few or too many positional parameters, or unknown kwd parameters generates an exception
      • most appears to be contained in mp_bytecode_prelude_t, but that might not be available unless built with MICROPY_PERSISTENT_CODE_SAVE?

It would not be necessary (and might even cause confusion) to try and have the return value types be "compatible" with standard inspect.FrameInfo, frame, codeobject, and inspect.Signature types.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy