bpo-26985: Add missing info of code object in inspect documentation (GH-1090) (GH-1099)

This commit is contained in:
Xiang Zhang 2017-04-13 11:14:17 +08:00 committed by GitHub
parent f0c416799b
commit 14944c6230
3 changed files with 223 additions and 191 deletions

View file

@ -34,185 +34,198 @@ provided as convenient choices for the second argument to :func:`getmembers`.
They also help you determine when you can expect to find the following special They also help you determine when you can expect to find the following special
attributes: attributes:
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| Type | Attribute | Description | | Type | Attribute | Description |
+===========+=================+===========================+ +===========+===================+===========================+
| module | __doc__ | documentation string | | module | __doc__ | documentation string |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __file__ | filename (missing for | | | __file__ | filename (missing for |
| | | built-in modules) | | | | built-in modules) |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| class | __doc__ | documentation string | | class | __doc__ | documentation string |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __name__ | name with which this | | | __name__ | name with which this |
| | | class was defined | | | | class was defined |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __qualname__ | qualified name | | | __qualname__ | qualified name |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __module__ | name of module in which | | | __module__ | name of module in which |
| | | this class was defined | | | | this class was defined |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| method | __doc__ | documentation string | | method | __doc__ | documentation string |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __name__ | name with which this | | | __name__ | name with which this |
| | | method was defined | | | | method was defined |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __qualname__ | qualified name | | | __qualname__ | qualified name |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __func__ | function object | | | __func__ | function object |
| | | containing implementation | | | | containing implementation |
| | | of method | | | | of method |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __self__ | instance to which this | | | __self__ | instance to which this |
| | | method is bound, or | | | | method is bound, or |
| | | ``None`` | | | | ``None`` |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| function | __doc__ | documentation string | | function | __doc__ | documentation string |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __name__ | name with which this | | | __name__ | name with which this |
| | | function was defined | | | | function was defined |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __qualname__ | qualified name | | | __qualname__ | qualified name |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __code__ | code object containing | | | __code__ | code object containing |
| | | compiled function | | | | compiled function |
| | | :term:`bytecode` | | | | :term:`bytecode` |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __defaults__ | tuple of any default | | | __defaults__ | tuple of any default |
| | | values for positional or | | | | values for positional or |
| | | keyword parameters | | | | keyword parameters |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __kwdefaults__ | mapping of any default | | | __kwdefaults__ | mapping of any default |
| | | values for keyword-only | | | | values for keyword-only |
| | | parameters | | | | parameters |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __globals__ | global namespace in which | | | __globals__ | global namespace in which |
| | | this function was defined | | | | this function was defined |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __annotations__ | mapping of parameters | | | __annotations__ | mapping of parameters |
| | | names to annotations; | | | | names to annotations; |
| | | ``"return"`` key is | | | | ``"return"`` key is |
| | | reserved for return | | | | reserved for return |
| | | annotations. | | | | annotations. |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| traceback | tb_frame | frame object at this | | traceback | tb_frame | frame object at this |
| | | level | | | | level |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | tb_lasti | index of last attempted | | | tb_lasti | index of last attempted |
| | | instruction in bytecode | | | | instruction in bytecode |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | tb_lineno | current line number in | | | tb_lineno | current line number in |
| | | Python source code | | | | Python source code |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | tb_next | next inner traceback | | | tb_next | next inner traceback |
| | | object (called by this | | | | object (called by this |
| | | level) | | | | level) |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| frame | f_back | next outer frame object | | frame | f_back | next outer frame object |
| | | (this frame's caller) | | | | (this frame's caller) |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | f_builtins | builtins namespace seen | | | f_builtins | builtins namespace seen |
| | | by this frame | | | | by this frame |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | f_code | code object being | | | f_code | code object being |
| | | executed in this frame | | | | executed in this frame |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | f_globals | global namespace seen by | | | f_globals | global namespace seen by |
| | | this frame | | | | this frame |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | f_lasti | index of last attempted | | | f_lasti | index of last attempted |
| | | instruction in bytecode | | | | instruction in bytecode |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | f_lineno | current line number in | | | f_lineno | current line number in |
| | | Python source code | | | | Python source code |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | f_locals | local namespace seen by | | | f_locals | local namespace seen by |
| | | this frame | | | | this frame |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | f_restricted | 0 or 1 if frame is in | | | f_restricted | 0 or 1 if frame is in |
| | | restricted execution mode | | | | restricted execution mode |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | f_trace | tracing function for this | | | f_trace | tracing function for this |
| | | frame, or ``None`` | | | | frame, or ``None`` |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| code | co_argcount | number of arguments (not | | code | co_argcount | number of arguments (not |
| | | including \* or \*\* | | | | including keyword only |
| | | args) | | | | arguments, \* or \*\* |
+-----------+-----------------+---------------------------+ | | | args) |
| | co_code | string of raw compiled | +-----------+-------------------+---------------------------+
| | | bytecode | | | co_code | string of raw compiled |
+-----------+-----------------+---------------------------+ | | | bytecode |
| | co_consts | tuple of constants used | +-----------+-------------------+---------------------------+
| | | in the bytecode | | | co_cellvars | tuple of names of cell |
+-----------+-----------------+---------------------------+ | | | variables (referenced by |
| | co_filename | name of file in which | | | | containing scopes) |
| | | this code object was | +-----------+-------------------+---------------------------+
| | | created | | | co_consts | tuple of constants used |
+-----------+-----------------+---------------------------+ | | | in the bytecode |
| | co_firstlineno | number of first line in | +-----------+-------------------+---------------------------+
| | | Python source code | | | co_filename | name of file in which |
+-----------+-----------------+---------------------------+ | | | this code object was |
| | co_flags | bitmap of ``CO_*`` flags, | | | | created |
| | | read more :ref:`here | +-----------+-------------------+---------------------------+
| | | <inspect-module-co-flags>`| | | co_firstlineno | number of first line in |
+-----------+-----------------+---------------------------+ | | | Python source code |
| | co_lnotab | encoded mapping of line | +-----------+-------------------+---------------------------+
| | | numbers to bytecode | | | co_flags | bitmap of ``CO_*`` flags, |
| | | indices | | | | read more :ref:`here |
+-----------+-----------------+---------------------------+ | | | <inspect-module-co-flags>`|
| | co_name | name with which this code | +-----------+-------------------+---------------------------+
| | | object was defined | | | co_lnotab | encoded mapping of line |
+-----------+-----------------+---------------------------+ | | | numbers to bytecode |
| | co_names | tuple of names of local | | | | indices |
| | | variables | +-----------+-------------------+---------------------------+
+-----------+-----------------+---------------------------+ | | co_freevars | tuple of names of free |
| | co_nlocals | number of local variables | | | | variables (referenced via |
+-----------+-----------------+---------------------------+ | | | a function's closure) |
| | co_stacksize | virtual machine stack | +-----------+-------------------+---------------------------+
| | | space required | | | co_kwonlyargcount | number of keyword only |
+-----------+-----------------+---------------------------+ | | | arguments (not including |
| | co_varnames | tuple of names of | | | | \*\* arg) |
| | | arguments and local | +-----------+-------------------+---------------------------+
| | | variables | | | co_name | name with which this code |
+-----------+-----------------+---------------------------+ | | | object was defined |
| generator | __name__ | name | +-----------+-------------------+---------------------------+
+-----------+-----------------+---------------------------+ | | co_names | tuple of names of local |
| | __qualname__ | qualified name | | | | variables |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | gi_frame | frame | | | co_nlocals | number of local variables |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | gi_running | is the generator running? | | | co_stacksize | virtual machine stack |
+-----------+-----------------+---------------------------+ | | | space required |
| | gi_code | code | +-----------+-------------------+---------------------------+
+-----------+-----------------+---------------------------+ | | co_varnames | tuple of names of |
| | gi_yieldfrom | object being iterated by | | | | arguments and local |
| | | ``yield from``, or | | | | variables |
| | | ``None`` | +-----------+-------------------+---------------------------+
+-----------+-----------------+---------------------------+ | generator | __name__ | name |
| coroutine | __name__ | name | +-----------+-------------------+---------------------------+
+-----------+-----------------+---------------------------+ | | __qualname__ | qualified name |
| | __qualname__ | qualified name | +-----------+-------------------+---------------------------+
+-----------+-----------------+---------------------------+ | | gi_frame | frame |
| | cr_await | object being awaited on, | +-----------+-------------------+---------------------------+
| | | or ``None`` | | | gi_running | is the generator running? |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | cr_frame | frame | | | gi_code | code |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | cr_running | is the coroutine running? | | | gi_yieldfrom | object being iterated by |
+-----------+-----------------+---------------------------+ | | | ``yield from``, or |
| | cr_code | code | | | | ``None`` |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| builtin | __doc__ | documentation string | | coroutine | __name__ | name |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __name__ | original name of this | | | __qualname__ | qualified name |
| | | function or method | +-----------+-------------------+---------------------------+
+-----------+-----------------+---------------------------+ | | cr_await | object being awaited on, |
| | __qualname__ | qualified name | | | | or ``None`` |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | __self__ | instance to which a | | | cr_frame | frame |
| | | method is bound, or | +-----------+-------------------+---------------------------+
| | | ``None`` | | | cr_running | is the coroutine running? |
+-----------+-----------------+---------------------------+ +-----------+-------------------+---------------------------+
| | cr_code | code |
+-----------+-------------------+---------------------------+
| builtin | __doc__ | documentation string |
+-----------+-------------------+---------------------------+
| | __name__ | original name of this |
| | | function or method |
+-----------+-------------------+---------------------------+
| | __qualname__ | qualified name |
+-----------+-------------------+---------------------------+
| | __self__ | instance to which a |
| | | method is bound, or |
| | | ``None`` |
+-----------+-------------------+---------------------------+
.. versionchanged:: 3.5 .. versionchanged:: 3.5
@ -1268,6 +1281,10 @@ Code Objects Bit Flags
Python code objects have a ``co_flags`` attribute, which is a bitmap of Python code objects have a ``co_flags`` attribute, which is a bitmap of
the following flags: the following flags:
.. data:: CO_OPTIMIZED
The code object is optimized, using fast locals.
.. data:: CO_NEWLOCALS .. data:: CO_NEWLOCALS
If set, a new dict will be created for the frame's ``f_locals`` when If set, a new dict will be created for the frame's ``f_locals`` when
@ -1281,6 +1298,10 @@ the following flags:
The code object has a variable keyword parameter (``**kwargs``-like). The code object has a variable keyword parameter (``**kwargs``-like).
.. data:: CO_NESTED
The flag is set when the code object is a nested function.
.. data:: CO_GENERATOR .. data:: CO_GENERATOR
The flag is set when the code object is a generator function, i.e. The flag is set when the code object is a generator function, i.e.

View file

@ -253,18 +253,24 @@ def iscode(object):
"""Return true if the object is a code object. """Return true if the object is a code object.
Code objects provide these attributes: Code objects provide these attributes:
co_argcount number of arguments (not including * or ** args) co_argcount number of arguments (not including *, ** args
co_code string of raw compiled bytecode or keyword only arguments)
co_consts tuple of constants used in the bytecode co_code string of raw compiled bytecode
co_filename name of file in which this code object was created co_cellvars tuple of names of cell variables
co_firstlineno number of first line in Python source code co_consts tuple of constants used in the bytecode
co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg co_filename name of file in which this code object was created
co_lnotab encoded mapping of line numbers to bytecode indices co_firstlineno number of first line in Python source code
co_name name with which this code object was defined co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
co_names tuple of names of local variables | 16=nested | 32=generator | 64=nofree | 128=coroutine
co_nlocals number of local variables | 256=iterable_coroutine | 512=async_generator
co_stacksize virtual machine stack space required co_freevars tuple of names of free variables
co_varnames tuple of names of arguments and local variables""" co_kwonlyargcount number of keyword only arguments (not including ** arg)
co_lnotab encoded mapping of line numbers to bytecode indices
co_name name with which this code object was defined
co_names tuple of names of local variables
co_nlocals number of local variables
co_stacksize virtual machine stack space required
co_varnames tuple of names of arguments and local variables"""
return isinstance(object, types.CodeType) return isinstance(object, types.CodeType)
def isbuiltin(object): def isbuiltin(object):

View file

@ -86,6 +86,11 @@ Build
- bpo-29643: Fix ``--enable-optimization`` didn't work. - bpo-29643: Fix ``--enable-optimization`` didn't work.
Documentation
-------------
- bpo-26985: Add missing info of code object in inspect documentation.
What's New in Python 3.6.1? What's New in Python 3.6.1?
=========================== ===========================