5.6. sourceinfo.objectinfo

This modules provides for the location of Python execution by means of the package ‘inspect’ extended by additional sources for a simple API.

The stack frame of inspect is in particular reduced to the common parameter spos, which is an abstraction of the ‘stack-position’ representing the level of history within the caller level. The value spos==0 is the function itself, whereas spos==1 is the first level caller. Consequently spos==2 is the caller of the caller, etc.

The categories of provided RTTI comprise:

  • packages - Python packages.

  • modules - Python modules - a.k.a. source files.

  • callers - Python functions and class/object methods.

The following definiton is applied:

Note

A package is represented by an imported top-entity which could either be a self-contained module, or the __init__.py special module as the top-entity from a set of modules within a sub directory structure.

So physically a package is a distribution unit, which provides one or more modules. Thus the displayed result in case of packages is the module when a self-contained, the path when a multi-module package.

Where the following attributes are available:__

  • name(package, module, function)

  • OID - dotted relative path to matching item of sys.path

  • filename

  • filepathname

  • item of sys.path

  • relative path to item of sys.path

  • line number

Dependent on the call context, some of the attribute values may not be available. E.g. when called from within the python/ipython shell, or ‘main’.

The API is designed here as a collection of slim functions only in order to avoid any overhead for generic application.

5.6.1. Modules

PySourceInfo - runtime type and source information on Python.

Based on the stack-frames of inspect, main parameter is spos - stack position.

  • spos=1 is caller, spos=2 is caller-of-caller, etc..

  • ‘r’ is a regular expression for ‘search()’.

Details see @local-manuals or @[https://pythonhosted.org/pysourceinfo/]

5.6.2. Constants

5.6.2.1. Re-Mapped imp Constants

Literally re-mapped constants from imp package for single import. These are obsolete in imp with >=3.3, but continued within PySourceInfo.

  • MT_UNKNOWN = 0

  • MT_SOURCE = 1 # imp.PY_SOURCE

  • MT_COMPILED = 2 # imp.PY_COMPILED

  • MT_EXTENSION = 3 # imp.C_EXTENSION

  • MT_DIRECTORY = 5 # imp.PKG_DIRECTORY

  • MT_BUILTIN = 6 # imp.C_BUILTIN

  • MT_FROZEN = 7 # imp.PY_FROZEN

5.6.3. Functions

5.6.3.1. getcaller_module

sourceinfo.objectinfo.getcaller_module(spos=1)[source]

Caller module.

Parameters

spos – Caller position on the stack.

Returns

Returns the caller module.

Raises

pass-through

5.6.3.2. getcaller_module_name

sourceinfo.objectinfo.getcaller_module_name(spos=1)[source]

Name of caller module, else None. Both approaches for evaluation the actual relative module name seem to have their own challenges, module.__name__ and getmodule_name().

Parameters

spos – Caller position on the stack.

Returns

Returns the name of caller module. The dotted object path is relative to the actual used sys.path item.

Raises

pass-through

5.6.3.3. getcaller_module_name_sub

sourceinfo.objectinfo.getcaller_module_name_sub(spos=1)[source]

Name from package to module.

Parameters

spos – Caller position on the stack.

Returns

Returns the sub-name as the portion from package to module.

Raises

pass-through

5.6.3.4. getcaller_module_oid

sourceinfo.objectinfo.getcaller_module_oid(spos=1)[source]

OID of caller module.

Parameters

spos – Caller position on the stack.

Returns

Returns the OID of the module.

Raises

pass-through

5.6.3.5. getcaller_module_oid_sub

sourceinfo.objectinfo.getcaller_module_oid_sub(spos=1)[source]

OID of caller module.

Parameters

spos – Caller position on the stack.

Returns

Returns the sub-OID of the module.

Raises

pass-through

5.6.3.6. getcaller_name

sourceinfo.objectinfo.getcaller_name(spos=1)[source]

Name of caller.

Parameters

spos – Caller position on the stack.

Returns

Returns the name.

Raises

pass-through

5.6.3.7. getcaller_package_name

sourceinfo.objectinfo.getcaller_package_name(spos=1)[source]

Name of first matching package containing the caller. The package is defined as the the first part of the module name. Relies on ‘inspect’.

Parameters

spos – Caller position on the stack.

Returns

Returns the package name when defined, else None.

Raises

pass-through

5.6.3.8. getmodule_by_id

sourceinfo.objectinfo.getmodule_by_id(i)[source]

Loaded module by ID.

Parameters

n – ID of loaded module.

Returns

Returns the loaded module.

Raises

passed through exceptions

5.6.3.9. getmodule_by_name

sourceinfo.objectinfo.getmodule_by_name(n)[source]

Loaded module by given name.

Parameters

n – Name of the loaded module.

Returns

Returns the loaded module.

Raises

pass-through

5.6.3.10. getmodule_name

sourceinfo.objectinfo.getmodule_name(mod)[source]

Name similar to basename of loaded module, same as getmodule_oid.

Parameters

mod – Module.

Returns

Returns the name of the loaded module.

Raises

pass-through

5.6.3.11. getmodule_name_sub

sourceinfo.objectinfo.getmodule_name_sub(mod)[source]

Name relative to PYTHONPATH/sys.path of loaded module, same as getmodule_oid_sub.

Parameters

mod – Module.

Returns

Returns the sub-name of the loaded module.

Raises

passed through exceptions

5.6.3.12. getmodule_oid

sourceinfo.objectinfo.getmodule_oid(mod)[source]

Name relative to PYTHONPATH/sys.path of loaded module.

Parameters

mod – Module.

Returns

Returns the package name.

Raises

pass-through

5.6.3.13. getmodule_oid_sub

sourceinfo.objectinfo.getmodule_oid_sub(mod)[source]

Name relative to PYTHONPATH/sys.path of loaded module.

Parameters

mod – Module.

Returns

Returns the package name.

Raises

pass-through

5.6.3.14. getmodule_package_name

sourceinfo.objectinfo.getmodule_package_name(mod)[source]

Name of package for loaded module.

Parameters

mod – Reference to a loaded module.

Returns

Returns the package name of the loaded module.

Raises

pass-through

5.6.3.15. getmodule_type

sourceinfo.objectinfo.getmodule_type(mod)[source]

Type for loaded module as defined by module ‘imp.*’

Parameters

mod – Reference to a loaded module.

Returns

Returns the module type as defined by imp, else None.

ret:=(
      C_BUILTIN
    | C_EXTENSION
    | PKG_DIRECTORY
    | PY_COMPILED
    | PY_FROZEN
    | PY_SOURCE
    | None
    )

Raises

passed through exceptions

5.6.4. Exceptions

5.6.5. Resources