‘pysourceinfo.PySourceInfo’ - Module

PySourceInfo - runtime type information on Python source location: package, module, and caller.

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:

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.

Functions

getCallerFileName

pysourceinfo.PySourceInfo.getCallerFileName(spos=1)[source]

Returns the filename of caller source file.

Args:
spos: Caller position on the stack.
Returns:
Returns the filename.
Raises:
passed through exceptions:

getCallerFilePathName

pysourceinfo.PySourceInfo.getCallerFilePathName(spos=1)[source]

Returns the pathname of caller source file.

Args:
spos: Caller position on the stack.
Returns:
Returns the filepathname.
Raises:
passed through exceptions:

getCallerFuncName

pysourceinfo.PySourceInfo.getCallerFuncName(spos=1)[source]

Returns the name of caller function.

Args:
spos: Caller position on the stack.
Returns:
Returns the filepathname.
Raises:
passed through exceptions:

getCallerLinenumber

pysourceinfo.PySourceInfo.getCallerLinenumber(spos=1)[source]

Returns the line number of caller.

Args:
spos: Caller position on the stack.
Returns:
Returns the filepathname.
Raises:
passed through exceptions:

getCallerModule

pysourceinfo.PySourceInfo.getCallerModule(spos=1)[source]

Returns the caller module.

Args:
spos: Caller position on the stack.
Returns:
Returns the caller module.
Raises:
passed through exceptions:

getCallerModuleFilePathName

pysourceinfo.PySourceInfo.getCallerModuleFilePathName(spos=1)[source]

Returns the filepathname of the module.

Args:
spos: Caller position on the stack.
Returns:
Returns the filepathname of the caller module.
Raises:
passed through exceptions:

getCallerModuleName

pysourceinfo.PySourceInfo.getCallerModuleName(spos=1)[source]

Returns the name of the caller module.

Both approaches for evaluation the actual relative module name seem to have their own challenges, module.__name__ and inspect.getmodulename.

Args:
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:
passed through exceptions:

getCallerModulePathName

pysourceinfo.PySourceInfo.getCallerModulePathName(spos=1)[source]

Returns the pathname of the module.

Args:
spos: Caller position on the stack.
Returns:
Returns the filepathname of module.
Raises:
passed through exceptions:

getCallerModulePythonPath

pysourceinfo.PySourceInfo.getCallerModulePythonPath(spos=1)[source]

Returns the prefix item from sys.path used for the caller module based on ‘inspect’.

Args:
spos: Caller position on the stack.
Returns:
Returns the name of caller module.
Raises:
passed through exceptions:

getCallerName

pysourceinfo.PySourceInfo.getCallerName(spos=1)[source]

Returns the name of the caller.

Args:
spos: Caller position on the stack.
Returns:
Returns the filepathname.
Raises:
passed through exceptions:

getCallerNameOID

pysourceinfo.PySourceInfo.getCallerNameOID(spos=1)[source]

Returns the name of the caller in dotted notation.

Args:
spos: Caller position on the stack.
Returns:
Returns the package name.
Raises:
passed through exceptions:

getCallerNamespaceGlobal

pysourceinfo.PySourceInfo.getCallerNamespaceGlobal(spos=1)[source]

Returns the global namespace of the caller.

Args:
spos: Caller position on the stack.
Returns:
Returns a reference to the callers global namespace.
Raises:
passed through exceptions:

getCallerNamespaceLocal

pysourceinfo.PySourceInfo.getCallerNamespaceLocal(spos=1)[source]

Returns the local namespace of the caller.

Args:
spos: Caller position on the stack.
Returns:
Returns a reference to the callers local namespace.
Raises:
passed through exceptions:

getCallerPackageFilePathName

pysourceinfo.PySourceInfo.getCallerPackageFilePathName(spos=1)[source]

Returns the filepathname of the package containing the caller.

Args:
spos: Caller position on the stack.
Returns:
Returns the package name.
Raises:
passed through exceptions:

getCallerPackageName

pysourceinfo.PySourceInfo.getCallerPackageName(spos=1)[source]

Returns the name of the first matching package containing the caller.

Relies on ‘inspect’.

Args:
spos: Caller position on the stack.
Returns:
Returns the package name when defined, else None.
Raises:
passed through exceptions:

getCallerPackagePathName

pysourceinfo.PySourceInfo.getCallerPackagePathName(spos=1)[source]

Returns the pathname to the first matching package directory of the caller.

Relies on ‘inspect’.

Args:
spos: Caller position on the stack.
Returns:
Returns the path name to the package.
Raises:
passed through exceptions:

getCallerPackagePythonPath

pysourceinfo.PySourceInfo.getCallerPackagePythonPath(spos=1)[source]

Returns the python path for first matching package of the caller.

Relies on ‘inspect’.

Intentionally the same as ‘getCallerPackagePathName’.

Args:
spos: Caller position on the stack.
Returns:
Returns the path name to the package.
Raises:
passed through exceptions:

getCallerSysPathPackageName

pysourceinfo.PySourceInfo.getCallerSysPathPackageName(spos=1)[source]

Returns the name of the first matching package on sys.path.

Evaluates ‘sys.path’ first, else switches to ‘inspect’.

Args:
spos: Caller position on the stack.
Returns:
Returns the package name when defined, else None.
Raises:
passed through exceptions:

getCallerSysPathPackageSysPathName

pysourceinfo.PySourceInfo.getCallerSysPathPackageSysPathName(spos=1)[source]

Returns the pathname to the first matching package directory of the caller.

Evaluates ‘sys.path’ first, else switches to ‘inspect’.

Args:
spos: Caller position on the stack.
Returns:
Returns the path name to the package.
Raises:
passed through exceptions:

getCallerSysPathPackageSysPathNameRel

getCallerSysPathPackagePythonPath

pysourceinfo.PySourceInfo.getCallerSysPathPackagePythonPath(spos=1)[source]

Returns the python path for first matching package of the caller.

Evaluates ‘sys.path’ first, else switches to ‘inspect’.

Intentionally the same as ‘getCallerPackagePathName’.

Args:
spos: Caller position on the stack.
Returns:
Returns the path name to the package.
Raises:
passed through exceptions:

getCallerPathName

pysourceinfo.PySourceInfo.getCallerPathName(spos=1)[source]

Returns the pathname of caller source file.

Args:
spos: Caller position on the stack.
Returns:
Returns the filename.
Raises:
passed through exceptions:

getModuleFilePathName

pysourceinfo.PySourceInfo.getModuleFilePathName(mod)[source]

Returns the filepathname of the loaded module.

Args:
mod: Reference to a loaded module.
Returns:
Returns the filepathname of the loaded module.
Raises:
passed through exceptions:

getModulePathName

pysourceinfo.PySourceInfo.getModulePathName(mod)[source]

Returns the pathname of the loaded module.

Args:
mod: Reference to a loaded module.
Returns:
Returns the pathname of the loaded module.
Raises:
passed through exceptions:

getModuleSourceFilePathName

pysourceinfo.PySourceInfo.getModuleSourceFilePathName(mod)[source]

Returns the filepathname of the source code for loaded module.

Args:
mod: Reference to a loaded module.
Returns:
Returns the filepathname of the source for the loaded module, else None.
Raises:
passed through exceptions:

getPythonPathFromSysPath

pysourceinfo.PySourceInfo.getPythonPathFromSysPath(pname, plist=None)[source]

Gets the first matching prefix from sys.path.

Foreseen to be used for canonical base reference in unit tests. This enables in particular for generic tests of filesystem positions where originally absolute pathnames were required.

Args:

pname: Pathname.

plist: List of possible python paths.
default := sys.path
Returns:
Returns the first matching path prefix from sys.path.
Raises:
passed through exceptions:

getPythonPathRel

pysourceinfo.PySourceInfo.getPythonPathRel(fpname, plist=None)[source]

Returns the relative path name for the first match on plist.

REMARK: Refer also to ‘getCallerNameSpceGlobal’.

Args:

fpname: The filepathname.

plist: List of possible python paths.
default := sys.path
Returns:
Returns the path postfix for fpname when found, else ‘None’.
Raises:
passed through exceptions:

getStackFuncList

pysourceinfo.PySourceInfo.getStackFuncList(fromtop=False)[source]

Returns a list of current mem-addresses on the stack.

Args:

fromtop: If True, return the reversed list.

default:=False
Returns:

Returns the list of function names on stack.

fromtop==False: stack[0] == result[0],...

fromtop==True: stack[0] == result[-1],... - reversed

Raises:
passed through exceptions:

getStackFuncMap

pysourceinfo.PySourceInfo.getStackFuncMap(funp)[source]

Returns a list of mem-addresses for each call of funcp on the stack.

Args:
funp: Function pointer.
Returns:
Returns the list of calls for the function on stack.
Raises:
passed through exceptions:

getStackFuncNameList

pysourceinfo.PySourceInfo.getStackFuncNameList(fromtop=False)[source]

Returns a list of current functions names on stack.

Args:

fromtop: If True, return the reversed list.

default:=False
Returns:

Returns the list of function names on stack.

fromtop==False: stack[0] == result[0],...

fromtop==True: stack[0] == result[-1],... - reversed

Raises:
passed through exceptions:

getStackFuncNameMap

pysourceinfo.PySourceInfo.getStackFuncNameMap(fun)[source]

Returns a list of funcnames for each call of func on the stack.

Args:
fun: Function name as regexpr.
Returns:
Returns the list of function names on stack.
Raises:
passed through exceptions:

getStackLen

pysourceinfo.PySourceInfo.getStackLen()[source]

Returns the length of current stack.

Args:

Returns:
Returns the len of current stack.
Raises:
passed through exceptions:

getStackSposForFunc

pysourceinfo.PySourceInfo.getStackSposForFunc(funp)[source]

Returns the id:=mem-address of the caller function.

The ID is independent over of the call context during it’s lifetime.

Args:
funp: Function on current stack.
Returns:
Returns the spos.
Raises:
passed through exceptions:

getStackSposForFuncName

pysourceinfo.PySourceInfo.getStackSposForFuncName(fun, fromtop=False)[source]

Returns the current stack position(spos) of the function name.

The position and it’s content are specific to the call context. When fromtop==False, the position is ‘almost’ static - for the lifetime of the referenced object.

Args:
fun=(literal|regexpr): Caller function name on the stack.
literal: a literal name regexpr: a regular expression for ‘re’
fromtop: If True, return the value as a topdown index for

the current stack, thus negative/<0.

default:=False

Returns:

Returns the spos, for loops and validation use:

fromtop==False: result >=0

fromtop==True: result <0

Raises:
passed through exceptions: