5.3. sourceinfo.fileinfo

The fileinfo modules provides information about the source location of callables by utilizing the __file__ variable and the call stack.

The stack frame of inspect is in particular reduced to the common parameter spos, which is the ‘stack-position’ representing the level of the addressed caller. 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 the provided information comprise:

  • packages - Python packages.

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

  • callers - Python functions and class/object methods.

The following definition 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 it is a self-contained module, the path when it is a multi-module package.

The following attributes are provided by fileinfo:

  • name(package, module, function)

  • 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.

5.3.1. Modules

pysourceinfo.fileinfo - information on source files.

Based on the stack-frames of inspect, __file__, and additional attributes.

Details see manuals.

5.3.2. Functions

5.3.2.1. getcaller_filename

sourceinfo.fileinfo.getcaller_filename(spos=1)[source]

Filename of caller’s module.

Parameters

spos – Caller position on the stack.

Returns

Returns the filename.

Raises

pass-through

5.3.2.2. getcaller_filepathname

sourceinfo.fileinfo.getcaller_filepathname(spos=1)[source]

File pathname of caller’s module.

Parameters

spos – Caller position on the stack.

Returns

Returns the file pathname.

Raises

passed through exceptions

5.3.2.3. getcaller_linenumber

sourceinfo.fileinfo.getcaller_linenumber(spos=1)[source]

Source line number of call.

Parameters

spos – Caller position on the stack.

Returns

Returns the line number of the parent call.

Raises

pass-through

5.3.2.4. getcaller_linenumber_def

sourceinfo.fileinfo.getcaller_linenumber_def(spos=1)[source]

First line number of enclosing caller function/method definition.

Parameters

spos – Caller position on the stack.

Returns

Returns the first linenumber of the calling functions/method definition.

Raises

pass-through

5.3.2.5. getcaller_package_filename

sourceinfo.fileinfo.getcaller_package_filename(spos=1)[source]
Parameters

spos – Caller position on the stack.

Returns

Returns the file name of the package.

Raises

pass-through

5.3.2.6. getcaller_package_filepathname

sourceinfo.fileinfo.getcaller_package_filepathname(spos=1, pmatch=1)[source]

Filepathname of the package from sys.path. If not matched by sys.path returns the dirname of the caller.

Parameters

spos – Caller position on the stack.

Returns

Returns the file path name of the package.

Raises

pass-through

5.3.2.7. getcaller_package_pathname

sourceinfo.fileinfo.getcaller_package_pathname(spos=1, pmatch=1)[source]

Pathname of the package from sys.path as used for search of the package. Relies on ‘inspect’.

Parameters

spos – Caller position on the stack.__

Returns

Returns the path name to the package.

Raises

pass-through

5.3.2.8. getcaller_pathname

sourceinfo.fileinfo.getcaller_pathname(spos=1)[source]

pathname of caller source file.

Parameters

spos – Caller position on the stack.

Returns

Returns the filename.

Raises

pass-through

5.3.2.9. getcaller_pathname_rel

sourceinfo.fileinfo.getcaller_pathname_rel(spos=1)[source]

Relative pathname to first matching package directory of caller. Evaluates ‘sys.path’ first, else switches to ‘inspect’.

Parameters

spos – Caller position on the stack.

Returns

Returns the path name to the package.

Raises

pass-through

5.3.2.10. getcaller_pathname_sub

sourceinfo.fileinfo.getcaller_pathname_sub(spos=1)[source]

sub-pathname to first matching module directory of caller. Evaluates ‘sys.path’ first, else switches to ‘inspect’.

Parameters

spos – Caller position on the stack.

Returns

Returns the path name to the package.

Raises

pass-through

5.3.2.11. getcaller_python_pathname

sourceinfo.fileinfo.getcaller_python_pathname(spos=1)[source]

Alias for getcaller_package_pathname

Parameters

spos – Caller position on the stack.

Returns

Returns the name of caller module.

Raises

passed through exceptions

5.3.2.12. getcaller_source_filepathname

sourceinfo.fileinfo.getcaller_source_filepathname(spos=1)[source]

Pathname of caller source.

Parameters

spos – Caller position on the stack.

Returns

Returns the file pathname of the source for the caller, else None.

Raises

pass-through

5.3.2.13. getmodule_filename

sourceinfo.fileinfo.getmodule_filename(mod)[source]

Basename of file for loaded module mod.

Parameters

mod – Reference to a loaded module.

Returns

Returns the basename of the loaded module.

Raises

pass-through

5.3.2.14. getmodule_filepathname

sourceinfo.fileinfo.getmodule_filepathname(mod)[source]

File pathname of loaded module mod.

Parameters

mod – Reference to a loaded module.

Returns

Returns the file pathname of the loaded module.

Raises

pass-through

5.3.2.15. getmodule_package_pathname

sourceinfo.fileinfo.getmodule_package_pathname(mod)[source]

Path name of package for loaded module.

Parameters

mod – Reference to a loaded module.

Returns

Returns the pathname of the package.

Raises

pass-through

5.3.2.16. getmodule_pathname

sourceinfo.fileinfo.getmodule_pathname(mod)[source]

Path name of loaded module.

Parameters

mod – Reference to a loaded module.

Returns

Returns the pathname of the loaded module.

Raises

pass-through

5.3.2.17. getmodule_pathname_rel

sourceinfo.fileinfo.getmodule_pathname_rel(mod, plist=None)[source]

Relative path name to PYTHONPATH for loaded module.

Parameters

mod – Reference to a loaded module.

Returns

Returns the relative pathname of the loaded module.

Raises

pass-through

5.3.2.18. getmodule_pathname_sub

sourceinfo.fileinfo.getmodule_pathname_sub(mod, plist=None)[source]

Path name for loaded module, relative to package path.

Parameters

mod – Reference to a loaded module.

Returns

Returns the sub pathname of the loaded module.

Raises

pass-through

5.3.2.19. getmodule_python_pathname

sourceinfo.fileinfo.getmodule_python_pathname(mod, plist=None)[source]

Path name from PYTHONPATH of loaded module.

Parameters

mod – Reference to a loaded module.

Returns

Returns the pathname of the loaded module.

Raises

pass-through

5.3.3. Exceptions

5.3.4. Resources