82
each class in the given list. Otherwise, classes using multiple inheritance and their
descendants will appear multiple times.
inspect.
getargspec
(
func
)
Get the names and default values of a Python function’s arguments. A tuple of four
things is returned:
(args, varargs, keywords, defaults)
. args is a list of the
argument names (it may contain nested lists). varargs and keywords are the names
of the
*
and
**
arguments or
None
. defaults is a tuple of default argument values or
None if there are no default arguments; if this tuple has n elements, they correspond
to the last n elements listed in args.
Changed in version 2.6: Returns a named tuple
ArgSpec(args, varargs, keywords,
defaults)
.
inspect.
getargvalues
(
frame
)
Get information about arguments passed into a particular frame. A tuple of four things
is returned:
(args, varargs, keywords, locals)
. args is a list of the argument
names (it may contain nested lists). varargs and keywords are the names of the
*
and
**
arguments or
None
. locals is the locals dictionary of the given frame.
Changed in version 2.6: Returns a named tuple
ArgInfo(args, varargs, keywords,
locals)
.
inspect.
formatargspec
(
args
[
, varargs, varkw, defaults, formatarg, formatvarargs,
formatvarkw, formatvalue, join
]
)
Format a pretty argument spec from the four values returned by
getargspec()
. The
format* arguments are the corresponding optional formatting functions that are called
to turn names and values into strings.
inspect.
formatargvalues
(
args
[
, varargs, varkw, locals, formatarg, formatvarargs,
formatvarkw, formatvalue, join
]
)
Format a pretty argument spec from the four values returned by
getargvalues()
. The
format* arguments are the corresponding optional formatting functions that are called
to turn names and values into strings.
inspect.
getmro
(
cls
)
Return a tuple of class cls’s base classes, including cls, in method resolution order. No
class appears more than once in this tuple. Note that the method resolution order
depends on cls’s type. Unless a very peculiar user-defined metatype is in use, cls will
be the first element of the tuple.
50
inspect.
getcallargs
(
func[, *args][, **kwds]
)
Bind the args and kwds to the argument names of the Python function or method
func, as if it was called with them. For bound methods, bind also the first argument
(typically named
self
) to the associated instance. A dict is returned, mapping the
argument names (including the names of the
*
and
**
arguments, if any) to their
values from args and kwds. In case of invoking func incorrectly, i.e. whenever
func(*args, **kwds)
would raise an exception because of incompatible signature, an
exception of the same type and the same or similar message is raised. For example:
>>> from inspect import getcallargs
>>> def f(a, b=1, *pos, **named):
... pass
>>> getcallargs(f, 1, 2, 3)
{'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
>>> getcallargs(f, a=2, x=4)
{'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
>>> getcallargs(f)
Traceback (most recent call last):
...
TypeError: f() takes at least 1 argument (0 given)
New in version 2.7.
27.13.4. The interpreter stack
When the following functions return “frame records,” each record is a tuple of six items:
the frame object, the filename, the line number of the current line, the function name, a
list of lines of context from the source code, and the index of the current line within that
list.
Note: Keeping references to frame objects, as found in the first element of the frame
records these functions return, can cause your program to create reference cycles.
Once a reference cycle has been created, the lifespan of all objects which can be
accessed from the objects which form the cycle can become much longer even if
Python’s optional cycle detector is enabled. If such cycles must be created, it is
important to ensure they are explicitly broken to avoid the delayed destruction of
objects and increased memory consumption which occurs.
Though the cycle detector will catch these, destruction of the frames (and local
variables) can be made deterministic by removing the cycle in a
finally
clause. This is
also important if the cycle detector was disabled when Python was compiled or using
gc.disable()
. For example:
70
def handle_stackframe_without_leak():
frame = inspect.currentframe()
try:
# do something with the frame
finally:
del frame
The optional context argument supported by most of these functions specifies the
number of lines of context to return, which are centered around the current line.
inspect.
getframeinfo
(
frame
[
, context
]
)
Get information about a frame or traceback object. A 5-tuple is returned, the last five
elements of the frame’s frame record.
Changed in version 2.6: Returns a named tuple
Traceback(filename, lineno,
function, code_context, index)
.
inspect.
getouterframes
(
frame
[
, context
]
)
Get a list of frame records for a frame and all outer frames. These frames represent
the calls that lead to the creation of frame. The first entry in the returned list
represents frame; the last entry represents the outermost call on frame‘s stack.
inspect.
getinnerframes
(
traceback
[
, context
]
)
Get a list of frame records for a traceback’s frame and all inner frames. These frames
represent calls made as a consequence of frame. The first entry in the list represents
traceback; the last entry represents where the exception was raised.
inspect.
currentframe
()
Return the frame object for the caller’s stack frame.
CPython implementation detail: This function relies on Python stack frame
support in the interpreter, which isn’t guaranteed to exist in all implementations of
Python. If running in an implementation without Python stack frame support this
function returns
None
.
inspect.
stack
(
[
context
]
)
Return a list of frame records for the caller’s stack. The first entry in the returned list
represents the caller; the last entry represents the outermost call on the stack.
inspect.
trace
(
[
context
]
)
16
Return a list of frame records for the stack between the current frame and the frame
in which an exception currently being handled was raised in. The first entry in the list
represents the caller; the last entry represents where the exception was raised.
index
modules |
next |
previous |
Python » Python v2.7.6 documentation » The Python Standard Library » 27. Python
Runtime Services »
© Copyright
1990-2013, Python Software Foundation.
The Python Software Foundation is a non-profit corporation. Please donate.
Last updated on Nov 10, 2013. Found a bug
?
Created using Sphinx
1.0.7.
77
index
modules |
next |
previous |
27.14.
site
— Site-specific configuration hook
Source code: Lib/site.py
This module is automatically imported during initialization. The automatic import
can be suppressed using the interpreter’s -S option.
Importing this module will append site-specific paths to the module search path and add a
few builtins.
It starts by constructing up to four directories from a head and a tail part. For the head
part, it uses
sys.prefix
and
sys.exec_prefix
; empty heads are skipped. For the tail
part, it uses the empty string and then
lib/site-packages
(on Windows) or
lib/pythonX.Y/site-packages
and then
lib/site-python
(on Unix and Macintosh). For
each of the distinct head-tail combinations, it sees if it refers to an existing directory, and
if so, adds it to
sys.path
and also inspects the newly added path for configuration files.
A path configuration file is a file whose name has the form
name.pth
and exists in one of
the four directories mentioned above; its contents are additional items (one per line) to be
added to
sys.path
. Non-existing items are never added to
sys.path
, and no check is
made that the item refers to a directory rather than a file. No item is added to
sys.path
more than once. Blank lines and lines beginning with
#
are skipped. Lines starting with
import
(followed by space or tab) are executed.
Changed in version 2.6: A space or tab is now required after the import keyword.
For example, suppose
sys.prefix
and
sys.exec_prefix
are set to
/usr/local
. The
Python X.Y library is then installed in
/usr/local/lib/pythonX.Y
. Suppose this has a
subdirectory
/usr/local/lib/pythonX.Y/site-packages
with three subsubdirectories,
foo
,
bar
and
spam
, and two path configuration files,
foo.pth
and
bar.pth
. Assume
foo.pth
contains the following:
# foo package configuration
foo
bar
bletch
Python » Python v2.7.6 documentation » The Python Standard Library » 27. Python
Runtime Services »
75
and
bar.pth
contains:
# bar package configuration
bar
Then the following version-specific directories are added to
sys.path
, in this order:
/usr/local/lib/pythonX.Y/site-packages/bar
/usr/local/lib/pythonX.Y/site-packages/foo
Note that
bletch
is omitted because it doesn’t exist; the
bar
directory precedes the
foo
directory because
bar.pth
comes alphabetically before
foo.pth
; and
spam
is omitted
because it is not mentioned in either path configuration file.
After these path manipulations, an attempt is made to import a module named
sitecustomize
, which can perform arbitrary site-specific customizations. It is typically
created by a system administrator in the site-packages directory. If this import fails with
an
ImportError
exception, it is silently ignored.
After this, an attempt is made to import a module named
usercustomize
, which can
perform arbitrary user-specific customizations, if
ENABLE_USER_SITE
is true. This file is
intended to be created in the user site-packages directory (see below), which is part of
sys.path
unless disabled by -s. An
ImportError
will be silently ignored.
Note that for some non-Unix systems,
sys.prefix
and
sys.exec_prefix
are empty, and
the path manipulations are skipped; however the import of
sitecustomize
and
usercustomize
is still attempted.
site.
PREFIXES
A list of prefixes for site-packages directories.
New in version 2.6.
site.
ENABLE_USER_SITE
Flag showing the status of the user site-packages directory.
True
means that it is
enabled and was added to
sys.path
.
False
means that it was disabled by user
request (with -s or
PYTHONNOUSERSITE
).
None
means it was disabled for security
reasons (mismatch between user or group id and effective id) or by an administrator.
New in version 2.6.
site.
USER_SITE
81
Path to the user site-packages for the running Python. Can be
None
if
getusersitepackages()
hasn’t
been
called
yet. Default
value
is
~/.local/lib/pythonX.Y/site-packages
for UNIX and non-framework Mac OS X
builds,
~/Library/Python/X.Y/lib/python/site-packages
for Mac framework builds,
and
%APPDATA%\Python\PythonXY\site-packages
on Windows. This directory is a site
directory, which means that
.pth
files in it will be processed.
New in version 2.6.
site.
USER_BASE
Path to the base directory for the user site-packages. Can be
None
if
getuserbase()
hasn’t been called yet. Default value is
~/.local
for UNIX and Mac OS X non-
framework
builds,
~/Library/Python/X.Y
for Mac framework builds, and
%APPDATA%\Python
for Windows. This value is used by Distutils to compute the
installation directories for scripts, data files, Python modules, etc. for the user
installation scheme. See also
PYTHONUSERBASE
.
New in version 2.6.
site.
addsitedir
(
sitedir, known_paths=None
)
Add a directory to sys.path and process its
.pth
files. Typically used in
sitecustomize
or
usercustomize
(see above).
site.
getsitepackages
()
Return a list containing all global site-packages directories (and possibly site-python).
New in version 2.7.
site.
getuserbase
()
Return the path of the user base directory,
USER_BASE
. If it is not initialized yet, this
function will also set it, respecting
PYTHONUSERBASE
.
New in version 2.7.
site.
getusersitepackages
()
Return the path of the user-specific site-packages directory,
USER_SITE
. If it is not
initialized yet, this function will also set it, respecting
PYTHONNOUSERSITE
and
USER_BASE
.
New in version 2.7.
Documents you may be interested
Documents you may be interested