Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`inspect` --- Inspect live objects |
| 3 | ======================================= |
| 4 | |
| 5 | .. module:: inspect |
| 6 | :synopsis: Extract information and source code from live objects. |
| 7 | .. moduleauthor:: Ka-Ping Yee <ping@lfw.org> |
| 8 | .. sectionauthor:: Ka-Ping Yee <ping@lfw.org> |
| 9 | |
| 10 | |
| 11 | .. versionadded:: 2.1 |
| 12 | |
| 13 | The :mod:`inspect` module provides several useful functions to help get |
| 14 | information about live objects such as modules, classes, methods, functions, |
| 15 | tracebacks, frame objects, and code objects. For example, it can help you |
| 16 | examine the contents of a class, retrieve the source code of a method, extract |
| 17 | and format the argument list for a function, or get all the information you need |
| 18 | to display a detailed traceback. |
| 19 | |
| 20 | There are four main kinds of services provided by this module: type checking, |
| 21 | getting source code, inspecting classes and functions, and examining the |
| 22 | interpreter stack. |
| 23 | |
| 24 | |
| 25 | .. _inspect-types: |
| 26 | |
| 27 | Types and members |
| 28 | ----------------- |
| 29 | |
| 30 | The :func:`getmembers` function retrieves the members of an object such as a |
Georg Brandl | 3e9d66f | 2008-03-03 20:37:55 +0000 | [diff] [blame] | 31 | class or module. The sixteen functions whose names begin with "is" are mainly |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 32 | provided as convenient choices for the second argument to :func:`getmembers`. |
| 33 | They also help you determine when you can expect to find the following special |
| 34 | attributes: |
| 35 | |
| 36 | +-----------+-----------------+---------------------------+-------+ |
| 37 | | Type | Attribute | Description | Notes | |
| 38 | +===========+=================+===========================+=======+ |
| 39 | | module | __doc__ | documentation string | | |
| 40 | +-----------+-----------------+---------------------------+-------+ |
| 41 | | | __file__ | filename (missing for | | |
| 42 | | | | built-in modules) | | |
| 43 | +-----------+-----------------+---------------------------+-------+ |
| 44 | | class | __doc__ | documentation string | | |
| 45 | +-----------+-----------------+---------------------------+-------+ |
| 46 | | | __module__ | name of module in which | | |
| 47 | | | | this class was defined | | |
| 48 | +-----------+-----------------+---------------------------+-------+ |
| 49 | | method | __doc__ | documentation string | | |
| 50 | +-----------+-----------------+---------------------------+-------+ |
| 51 | | | __name__ | name with which this | | |
| 52 | | | | method was defined | | |
| 53 | +-----------+-----------------+---------------------------+-------+ |
| 54 | | | im_class | class object that asked | \(1) | |
| 55 | | | | for this method | | |
| 56 | +-----------+-----------------+---------------------------+-------+ |
Georg Brandl | 3fbe20c | 2008-03-21 19:20:21 +0000 | [diff] [blame] | 57 | | | im_func or | function object | | |
| 58 | | | __func__ | containing implementation | | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 59 | | | | of method | | |
| 60 | +-----------+-----------------+---------------------------+-------+ |
Georg Brandl | 3fbe20c | 2008-03-21 19:20:21 +0000 | [diff] [blame] | 61 | | | im_self or | instance to which this | | |
| 62 | | | __self__ | method is bound, or | | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 63 | | | | ``None`` | | |
| 64 | +-----------+-----------------+---------------------------+-------+ |
| 65 | | function | __doc__ | documentation string | | |
| 66 | +-----------+-----------------+---------------------------+-------+ |
| 67 | | | __name__ | name with which this | | |
| 68 | | | | function was defined | | |
| 69 | +-----------+-----------------+---------------------------+-------+ |
| 70 | | | func_code | code object containing | | |
| 71 | | | | compiled function | | |
Georg Brandl | 63fa168 | 2007-10-21 10:24:20 +0000 | [diff] [blame] | 72 | | | | :term:`bytecode` | | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 73 | +-----------+-----------------+---------------------------+-------+ |
| 74 | | | func_defaults | tuple of any default | | |
| 75 | | | | values for arguments | | |
| 76 | +-----------+-----------------+---------------------------+-------+ |
| 77 | | | func_doc | (same as __doc__) | | |
| 78 | +-----------+-----------------+---------------------------+-------+ |
| 79 | | | func_globals | global namespace in which | | |
| 80 | | | | this function was defined | | |
| 81 | +-----------+-----------------+---------------------------+-------+ |
| 82 | | | func_name | (same as __name__) | | |
| 83 | +-----------+-----------------+---------------------------+-------+ |
Facundo Batista | 759bfc6 | 2008-02-18 03:43:43 +0000 | [diff] [blame] | 84 | | generator | __iter__ | defined to support | | |
| 85 | | | | iteration over container | | |
| 86 | +-----------+-----------------+---------------------------+-------+ |
| 87 | | | close | raises new GeneratorExit | | |
| 88 | | | | exception inside the | | |
| 89 | | | | generator to terminate | | |
| 90 | | | | the iteration | | |
| 91 | +-----------+-----------------+---------------------------+-------+ |
| 92 | | | gi_code | code object | | |
| 93 | +-----------+-----------------+---------------------------+-------+ |
| 94 | | | gi_frame | frame object or possibly | | |
| 95 | | | | None once the generator | | |
| 96 | | | | has been exhausted | | |
| 97 | +-----------+-----------------+---------------------------+-------+ |
| 98 | | | gi_running | set to 1 when generator | | |
| 99 | | | | is executing, 0 otherwise | | |
| 100 | +-----------+-----------------+---------------------------+-------+ |
| 101 | | | next | return the next item from | | |
| 102 | | | | the container | | |
| 103 | +-----------+-----------------+---------------------------+-------+ |
| 104 | | | send | resumes the generator and | | |
| 105 | | | | "sends" a value that | | |
| 106 | | | | becomes the result of the | | |
| 107 | | | | current yield-expression | | |
| 108 | +-----------+-----------------+---------------------------+-------+ |
| 109 | | | throw | used to raise an | | |
| 110 | | | | exception inside the | | |
| 111 | | | | generator | | |
| 112 | +-----------+-----------------+---------------------------+-------+ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 113 | | traceback | tb_frame | frame object at this | | |
| 114 | | | | level | | |
| 115 | +-----------+-----------------+---------------------------+-------+ |
| 116 | | | tb_lasti | index of last attempted | | |
| 117 | | | | instruction in bytecode | | |
| 118 | +-----------+-----------------+---------------------------+-------+ |
| 119 | | | tb_lineno | current line number in | | |
| 120 | | | | Python source code | | |
| 121 | +-----------+-----------------+---------------------------+-------+ |
| 122 | | | tb_next | next inner traceback | | |
| 123 | | | | object (called by this | | |
| 124 | | | | level) | | |
| 125 | +-----------+-----------------+---------------------------+-------+ |
| 126 | | frame | f_back | next outer frame object | | |
| 127 | | | | (this frame's caller) | | |
| 128 | +-----------+-----------------+---------------------------+-------+ |
Georg Brandl | 6f82cd3 | 2010-02-06 18:44:44 +0000 | [diff] [blame] | 129 | | | f_builtins | builtins namespace seen | | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 130 | | | | by this frame | | |
| 131 | +-----------+-----------------+---------------------------+-------+ |
| 132 | | | f_code | code object being | | |
| 133 | | | | executed in this frame | | |
| 134 | +-----------+-----------------+---------------------------+-------+ |
| 135 | | | f_exc_traceback | traceback if raised in | | |
| 136 | | | | this frame, or ``None`` | | |
| 137 | +-----------+-----------------+---------------------------+-------+ |
| 138 | | | f_exc_type | exception type if raised | | |
| 139 | | | | in this frame, or | | |
| 140 | | | | ``None`` | | |
| 141 | +-----------+-----------------+---------------------------+-------+ |
| 142 | | | f_exc_value | exception value if raised | | |
| 143 | | | | in this frame, or | | |
| 144 | | | | ``None`` | | |
| 145 | +-----------+-----------------+---------------------------+-------+ |
| 146 | | | f_globals | global namespace seen by | | |
| 147 | | | | this frame | | |
| 148 | +-----------+-----------------+---------------------------+-------+ |
| 149 | | | f_lasti | index of last attempted | | |
| 150 | | | | instruction in bytecode | | |
| 151 | +-----------+-----------------+---------------------------+-------+ |
| 152 | | | f_lineno | current line number in | | |
| 153 | | | | Python source code | | |
| 154 | +-----------+-----------------+---------------------------+-------+ |
| 155 | | | f_locals | local namespace seen by | | |
| 156 | | | | this frame | | |
| 157 | +-----------+-----------------+---------------------------+-------+ |
| 158 | | | f_restricted | 0 or 1 if frame is in | | |
| 159 | | | | restricted execution mode | | |
| 160 | +-----------+-----------------+---------------------------+-------+ |
| 161 | | | f_trace | tracing function for this | | |
| 162 | | | | frame, or ``None`` | | |
| 163 | +-----------+-----------------+---------------------------+-------+ |
| 164 | | code | co_argcount | number of arguments (not | | |
| 165 | | | | including \* or \*\* | | |
| 166 | | | | args) | | |
| 167 | +-----------+-----------------+---------------------------+-------+ |
| 168 | | | co_code | string of raw compiled | | |
| 169 | | | | bytecode | | |
| 170 | +-----------+-----------------+---------------------------+-------+ |
| 171 | | | co_consts | tuple of constants used | | |
| 172 | | | | in the bytecode | | |
| 173 | +-----------+-----------------+---------------------------+-------+ |
| 174 | | | co_filename | name of file in which | | |
| 175 | | | | this code object was | | |
| 176 | | | | created | | |
| 177 | +-----------+-----------------+---------------------------+-------+ |
| 178 | | | co_firstlineno | number of first line in | | |
| 179 | | | | Python source code | | |
| 180 | +-----------+-----------------+---------------------------+-------+ |
| 181 | | | co_flags | bitmap: 1=optimized ``|`` | | |
| 182 | | | | 2=newlocals ``|`` 4=\*arg | | |
| 183 | | | | ``|`` 8=\*\*arg | | |
| 184 | +-----------+-----------------+---------------------------+-------+ |
| 185 | | | co_lnotab | encoded mapping of line | | |
| 186 | | | | numbers to bytecode | | |
| 187 | | | | indices | | |
| 188 | +-----------+-----------------+---------------------------+-------+ |
| 189 | | | co_name | name with which this code | | |
| 190 | | | | object was defined | | |
| 191 | +-----------+-----------------+---------------------------+-------+ |
| 192 | | | co_names | tuple of names of local | | |
| 193 | | | | variables | | |
| 194 | +-----------+-----------------+---------------------------+-------+ |
| 195 | | | co_nlocals | number of local variables | | |
| 196 | +-----------+-----------------+---------------------------+-------+ |
| 197 | | | co_stacksize | virtual machine stack | | |
| 198 | | | | space required | | |
| 199 | +-----------+-----------------+---------------------------+-------+ |
| 200 | | | co_varnames | tuple of names of | | |
| 201 | | | | arguments and local | | |
| 202 | | | | variables | | |
| 203 | +-----------+-----------------+---------------------------+-------+ |
| 204 | | builtin | __doc__ | documentation string | | |
| 205 | +-----------+-----------------+---------------------------+-------+ |
| 206 | | | __name__ | original name of this | | |
| 207 | | | | function or method | | |
| 208 | +-----------+-----------------+---------------------------+-------+ |
| 209 | | | __self__ | instance to which a | | |
| 210 | | | | method is bound, or | | |
| 211 | | | | ``None`` | | |
| 212 | +-----------+-----------------+---------------------------+-------+ |
| 213 | |
| 214 | Note: |
| 215 | |
| 216 | (1) |
| 217 | .. versionchanged:: 2.2 |
| 218 | :attr:`im_class` used to refer to the class that defined the method. |
| 219 | |
| 220 | |
| 221 | .. function:: getmembers(object[, predicate]) |
| 222 | |
| 223 | Return all the members of an object in a list of (name, value) pairs sorted by |
| 224 | name. If the optional *predicate* argument is supplied, only members for which |
| 225 | the predicate returns a true value are included. |
| 226 | |
Georg Brandl | 91a4808 | 2008-01-06 15:48:20 +0000 | [diff] [blame] | 227 | .. note:: |
| 228 | |
| 229 | :func:`getmembers` does not return metaclass attributes when the argument |
| 230 | is a class (this behavior is inherited from the :func:`dir` function). |
| 231 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 232 | |
| 233 | .. function:: getmoduleinfo(path) |
| 234 | |
| 235 | Return a tuple of values that describe how Python will interpret the file |
| 236 | identified by *path* if it is a module, or ``None`` if it would not be |
Georg Brandl | cbb2e49 | 2011-01-09 08:04:37 +0000 | [diff] [blame] | 237 | identified as a module. The return tuple is ``(name, suffix, mode, |
| 238 | module_type)``, where *name* is the name of the module without the name of |
| 239 | any enclosing package, *suffix* is the trailing part of the file name (which |
| 240 | may not be a dot-delimited extension), *mode* is the :func:`open` mode that |
| 241 | would be used (``'r'`` or ``'rb'``), and *module_type* is an integer giving |
| 242 | the type of the module. *module_type* will have a value which can be |
| 243 | compared to the constants defined in the :mod:`imp` module; see the |
| 244 | documentation for that module for more information on module types. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 245 | |
Georg Brandl | e3c3db5 | 2008-01-11 09:55:53 +0000 | [diff] [blame] | 246 | .. versionchanged:: 2.6 |
| 247 | Returns a :term:`named tuple` ``ModuleInfo(name, suffix, mode, |
| 248 | module_type)``. |
| 249 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 250 | |
| 251 | .. function:: getmodulename(path) |
| 252 | |
| 253 | Return the name of the module named by the file *path*, without including the |
| 254 | names of enclosing packages. This uses the same algorithm as the interpreter |
| 255 | uses when searching for modules. If the name cannot be matched according to the |
| 256 | interpreter's rules, ``None`` is returned. |
| 257 | |
| 258 | |
| 259 | .. function:: ismodule(object) |
| 260 | |
| 261 | Return true if the object is a module. |
| 262 | |
| 263 | |
| 264 | .. function:: isclass(object) |
| 265 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 266 | Return true if the object is a class, whether built-in or created in Python |
| 267 | code. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | .. function:: ismethod(object) |
| 271 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 272 | Return true if the object is a bound method written in Python. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 273 | |
| 274 | |
| 275 | .. function:: isfunction(object) |
| 276 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 277 | Return true if the object is a Python function, which includes functions |
| 278 | created by a :term:`lambda` expression. |
| 279 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 280 | |
Facundo Batista | 759bfc6 | 2008-02-18 03:43:43 +0000 | [diff] [blame] | 281 | .. function:: isgeneratorfunction(object) |
| 282 | |
| 283 | Return true if the object is a Python generator function. |
| 284 | |
Andrew M. Kuchling | 3fe1843 | 2008-03-04 01:49:37 +0000 | [diff] [blame] | 285 | .. versionadded:: 2.6 |
| 286 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 287 | |
Facundo Batista | 759bfc6 | 2008-02-18 03:43:43 +0000 | [diff] [blame] | 288 | .. function:: isgenerator(object) |
| 289 | |
| 290 | Return true if the object is a generator. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 291 | |
Andrew M. Kuchling | 3fe1843 | 2008-03-04 01:49:37 +0000 | [diff] [blame] | 292 | .. versionadded:: 2.6 |
| 293 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 294 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 295 | .. function:: istraceback(object) |
| 296 | |
| 297 | Return true if the object is a traceback. |
| 298 | |
| 299 | |
| 300 | .. function:: isframe(object) |
| 301 | |
| 302 | Return true if the object is a frame. |
| 303 | |
| 304 | |
| 305 | .. function:: iscode(object) |
| 306 | |
| 307 | Return true if the object is a code. |
| 308 | |
| 309 | |
| 310 | .. function:: isbuiltin(object) |
| 311 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 312 | Return true if the object is a built-in function or a bound built-in method. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 313 | |
| 314 | |
| 315 | .. function:: isroutine(object) |
| 316 | |
| 317 | Return true if the object is a user-defined or built-in function or method. |
| 318 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 319 | |
Christian Heimes | 608c1d8 | 2008-03-03 18:28:04 +0000 | [diff] [blame] | 320 | .. function:: isabstract(object) |
| 321 | |
| 322 | Return true if the object is an abstract base class. |
| 323 | |
| 324 | .. versionadded:: 2.6 |
| 325 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 326 | |
| 327 | .. function:: ismethoddescriptor(object) |
| 328 | |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 329 | Return true if the object is a method descriptor, but not if |
| 330 | :func:`ismethod`, :func:`isclass`, :func:`isfunction` or :func:`isbuiltin` |
| 331 | are true. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 332 | |
Georg Brandl | 5c174bb | 2007-10-21 10:32:54 +0000 | [diff] [blame] | 333 | This is new as of Python 2.2, and, for example, is true of |
| 334 | ``int.__add__``. An object passing this test has a :attr:`__get__` attribute |
| 335 | but not a :attr:`__set__` attribute, but beyond that the set of attributes |
| 336 | varies. :attr:`__name__` is usually sensible, and :attr:`__doc__` often is. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 337 | |
Georg Brandl | 5c174bb | 2007-10-21 10:32:54 +0000 | [diff] [blame] | 338 | Methods implemented via descriptors that also pass one of the other tests |
| 339 | return false from the :func:`ismethoddescriptor` test, simply because the |
| 340 | other tests promise more -- you can, e.g., count on having the |
| 341 | :attr:`im_func` attribute (etc) when an object passes :func:`ismethod`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 342 | |
| 343 | |
| 344 | .. function:: isdatadescriptor(object) |
| 345 | |
| 346 | Return true if the object is a data descriptor. |
| 347 | |
Georg Brandl | 5c174bb | 2007-10-21 10:32:54 +0000 | [diff] [blame] | 348 | Data descriptors have both a :attr:`__get__` and a :attr:`__set__` attribute. |
| 349 | Examples are properties (defined in Python), getsets, and members. The |
| 350 | latter two are defined in C and there are more specific tests available for |
| 351 | those types, which is robust across Python implementations. Typically, data |
| 352 | descriptors will also have :attr:`__name__` and :attr:`__doc__` attributes |
| 353 | (properties, getsets, and members have both of these attributes), but this is |
| 354 | not guaranteed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 355 | |
| 356 | .. versionadded:: 2.3 |
| 357 | |
| 358 | |
| 359 | .. function:: isgetsetdescriptor(object) |
| 360 | |
| 361 | Return true if the object is a getset descriptor. |
| 362 | |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 363 | .. impl-detail:: |
| 364 | |
| 365 | getsets are attributes defined in extension modules via |
| 366 | :ctype:`PyGetSetDef` structures. For Python implementations without such |
| 367 | types, this method will always return ``False``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 368 | |
| 369 | .. versionadded:: 2.5 |
| 370 | |
| 371 | |
| 372 | .. function:: ismemberdescriptor(object) |
| 373 | |
| 374 | Return true if the object is a member descriptor. |
| 375 | |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 376 | .. impl-detail:: |
| 377 | |
| 378 | Member descriptors are attributes defined in extension modules via |
| 379 | :ctype:`PyMemberDef` structures. For Python implementations without such |
| 380 | types, this method will always return ``False``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 381 | |
| 382 | .. versionadded:: 2.5 |
| 383 | |
| 384 | |
| 385 | .. _inspect-source: |
| 386 | |
| 387 | Retrieving source code |
| 388 | ---------------------- |
| 389 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 390 | .. function:: getdoc(object) |
| 391 | |
Georg Brandl | 7be19aa | 2008-06-07 15:59:10 +0000 | [diff] [blame] | 392 | Get the documentation string for an object, cleaned up with :func:`cleandoc`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 393 | |
| 394 | |
| 395 | .. function:: getcomments(object) |
| 396 | |
| 397 | Return in a single string any lines of comments immediately preceding the |
| 398 | object's source code (for a class, function, or method), or at the top of the |
| 399 | Python source file (if the object is a module). |
| 400 | |
| 401 | |
| 402 | .. function:: getfile(object) |
| 403 | |
| 404 | Return the name of the (text or binary) file in which an object was defined. |
| 405 | This will fail with a :exc:`TypeError` if the object is a built-in module, |
| 406 | class, or function. |
| 407 | |
| 408 | |
| 409 | .. function:: getmodule(object) |
| 410 | |
| 411 | Try to guess which module an object was defined in. |
| 412 | |
| 413 | |
| 414 | .. function:: getsourcefile(object) |
| 415 | |
| 416 | Return the name of the Python source file in which an object was defined. This |
| 417 | will fail with a :exc:`TypeError` if the object is a built-in module, class, or |
| 418 | function. |
| 419 | |
| 420 | |
| 421 | .. function:: getsourcelines(object) |
| 422 | |
| 423 | Return a list of source lines and starting line number for an object. The |
| 424 | argument may be a module, class, method, function, traceback, frame, or code |
| 425 | object. The source code is returned as a list of the lines corresponding to the |
| 426 | object and the line number indicates where in the original source file the first |
| 427 | line of code was found. An :exc:`IOError` is raised if the source code cannot |
| 428 | be retrieved. |
| 429 | |
| 430 | |
| 431 | .. function:: getsource(object) |
| 432 | |
| 433 | Return the text of the source code for an object. The argument may be a module, |
| 434 | class, method, function, traceback, frame, or code object. The source code is |
| 435 | returned as a single string. An :exc:`IOError` is raised if the source code |
| 436 | cannot be retrieved. |
| 437 | |
| 438 | |
Georg Brandl | 7be19aa | 2008-06-07 15:59:10 +0000 | [diff] [blame] | 439 | .. function:: cleandoc(doc) |
| 440 | |
| 441 | Clean up indentation from docstrings that are indented to line up with blocks |
| 442 | of code. Any whitespace that can be uniformly removed from the second line |
| 443 | onwards is removed. Also, all tabs are expanded to spaces. |
| 444 | |
| 445 | .. versionadded:: 2.6 |
| 446 | |
| 447 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 448 | .. _inspect-classes-functions: |
| 449 | |
| 450 | Classes and functions |
| 451 | --------------------- |
| 452 | |
| 453 | |
| 454 | .. function:: getclasstree(classes[, unique]) |
| 455 | |
| 456 | Arrange the given list of classes into a hierarchy of nested lists. Where a |
| 457 | nested list appears, it contains classes derived from the class whose entry |
| 458 | immediately precedes the list. Each entry is a 2-tuple containing a class and a |
| 459 | tuple of its base classes. If the *unique* argument is true, exactly one entry |
| 460 | appears in the returned structure for each class in the given list. Otherwise, |
| 461 | classes using multiple inheritance and their descendants will appear multiple |
| 462 | times. |
| 463 | |
| 464 | |
| 465 | .. function:: getargspec(func) |
| 466 | |
Georg Brandl | cbb2e49 | 2011-01-09 08:04:37 +0000 | [diff] [blame] | 467 | Get the names and default values of a Python function's arguments. A tuple of |
| 468 | four things is returned: ``(args, varargs, keywords, defaults)``. *args* is a |
| 469 | list of the argument names (it may contain nested lists). *varargs* and |
| 470 | *keywords* are the names of the ``*`` and ``**`` arguments or |
| 471 | ``None``. *defaults* is a tuple of default argument values or None if there |
| 472 | are no default arguments; if this tuple has *n* elements, they correspond to |
| 473 | the last *n* elements listed in *args*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 474 | |
Georg Brandl | e3c3db5 | 2008-01-11 09:55:53 +0000 | [diff] [blame] | 475 | .. versionchanged:: 2.6 |
| 476 | Returns a :term:`named tuple` ``ArgSpec(args, varargs, keywords, |
| 477 | defaults)``. |
| 478 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 479 | |
| 480 | .. function:: getargvalues(frame) |
| 481 | |
Georg Brandl | cbb2e49 | 2011-01-09 08:04:37 +0000 | [diff] [blame] | 482 | Get information about arguments passed into a particular frame. A tuple of |
| 483 | four things is returned: ``(args, varargs, keywords, locals)``. *args* is a |
| 484 | list of the argument names (it may contain nested lists). *varargs* and |
| 485 | *keywords* are the names of the ``*`` and ``**`` arguments or ``None``. |
| 486 | *locals* is the locals dictionary of the given frame. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 487 | |
Georg Brandl | e3c3db5 | 2008-01-11 09:55:53 +0000 | [diff] [blame] | 488 | .. versionchanged:: 2.6 |
| 489 | Returns a :term:`named tuple` ``ArgInfo(args, varargs, keywords, |
| 490 | locals)``. |
| 491 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 492 | |
| 493 | .. function:: formatargspec(args[, varargs, varkw, defaults, formatarg, formatvarargs, formatvarkw, formatvalue, join]) |
| 494 | |
| 495 | Format a pretty argument spec from the four values returned by |
| 496 | :func:`getargspec`. The format\* arguments are the corresponding optional |
| 497 | formatting functions that are called to turn names and values into strings. |
| 498 | |
| 499 | |
| 500 | .. function:: formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue, join]) |
| 501 | |
| 502 | Format a pretty argument spec from the four values returned by |
| 503 | :func:`getargvalues`. The format\* arguments are the corresponding optional |
| 504 | formatting functions that are called to turn names and values into strings. |
| 505 | |
| 506 | |
| 507 | .. function:: getmro(cls) |
| 508 | |
| 509 | Return a tuple of class cls's base classes, including cls, in method resolution |
| 510 | order. No class appears more than once in this tuple. Note that the method |
| 511 | resolution order depends on cls's type. Unless a very peculiar user-defined |
| 512 | metatype is in use, cls will be the first element of the tuple. |
| 513 | |
| 514 | |
Benjamin Peterson | 7e21325 | 2010-03-30 17:58:13 +0000 | [diff] [blame] | 515 | .. function:: getcallargs(func[, *args][, **kwds]) |
| 516 | |
| 517 | Bind the *args* and *kwds* to the argument names of the Python function or |
| 518 | method *func*, as if it was called with them. For bound methods, bind also the |
| 519 | first argument (typically named ``self``) to the associated instance. A dict |
| 520 | is returned, mapping the argument names (including the names of the ``*`` and |
| 521 | ``**`` arguments, if any) to their values from *args* and *kwds*. In case of |
| 522 | invoking *func* incorrectly, i.e. whenever ``func(*args, **kwds)`` would raise |
| 523 | an exception because of incompatible signature, an exception of the same type |
| 524 | and the same or similar message is raised. For example:: |
| 525 | |
| 526 | >>> from inspect import getcallargs |
| 527 | >>> def f(a, b=1, *pos, **named): |
| 528 | ... pass |
| 529 | >>> getcallargs(f, 1, 2, 3) |
| 530 | {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)} |
| 531 | >>> getcallargs(f, a=2, x=4) |
| 532 | {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()} |
| 533 | >>> getcallargs(f) |
| 534 | Traceback (most recent call last): |
| 535 | ... |
| 536 | TypeError: f() takes at least 1 argument (0 given) |
| 537 | |
| 538 | .. versionadded:: 2.7 |
| 539 | |
| 540 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 541 | .. _inspect-stack: |
| 542 | |
| 543 | The interpreter stack |
| 544 | --------------------- |
| 545 | |
| 546 | When the following functions return "frame records," each record is a tuple of |
| 547 | six items: the frame object, the filename, the line number of the current line, |
| 548 | the function name, a list of lines of context from the source code, and the |
| 549 | index of the current line within that list. |
| 550 | |
Georg Brandl | 16a57f6 | 2009-04-27 15:29:09 +0000 | [diff] [blame] | 551 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 552 | |
| 553 | Keeping references to frame objects, as found in the first element of the frame |
| 554 | records these functions return, can cause your program to create reference |
| 555 | cycles. Once a reference cycle has been created, the lifespan of all objects |
| 556 | which can be accessed from the objects which form the cycle can become much |
| 557 | longer even if Python's optional cycle detector is enabled. If such cycles must |
| 558 | be created, it is important to ensure they are explicitly broken to avoid the |
| 559 | delayed destruction of objects and increased memory consumption which occurs. |
| 560 | |
| 561 | Though the cycle detector will catch these, destruction of the frames (and local |
| 562 | variables) can be made deterministic by removing the cycle in a |
| 563 | :keyword:`finally` clause. This is also important if the cycle detector was |
| 564 | disabled when Python was compiled or using :func:`gc.disable`. For example:: |
| 565 | |
| 566 | def handle_stackframe_without_leak(): |
| 567 | frame = inspect.currentframe() |
| 568 | try: |
| 569 | # do something with the frame |
| 570 | finally: |
| 571 | del frame |
| 572 | |
| 573 | The optional *context* argument supported by most of these functions specifies |
| 574 | the number of lines of context to return, which are centered around the current |
| 575 | line. |
| 576 | |
| 577 | |
| 578 | .. function:: getframeinfo(frame[, context]) |
| 579 | |
| 580 | Get information about a frame or traceback object. A 5-tuple is returned, the |
| 581 | last five elements of the frame's frame record. |
| 582 | |
Georg Brandl | e3c3db5 | 2008-01-11 09:55:53 +0000 | [diff] [blame] | 583 | .. versionchanged:: 2.6 |
| 584 | Returns a :term:`named tuple` ``Traceback(filename, lineno, function, |
| 585 | code_context, index)``. |
| 586 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 587 | |
| 588 | .. function:: getouterframes(frame[, context]) |
| 589 | |
| 590 | Get a list of frame records for a frame and all outer frames. These frames |
| 591 | represent the calls that lead to the creation of *frame*. The first entry in the |
| 592 | returned list represents *frame*; the last entry represents the outermost call |
| 593 | on *frame*'s stack. |
| 594 | |
| 595 | |
| 596 | .. function:: getinnerframes(traceback[, context]) |
| 597 | |
| 598 | Get a list of frame records for a traceback's frame and all inner frames. These |
| 599 | frames represent calls made as a consequence of *frame*. The first entry in the |
| 600 | list represents *traceback*; the last entry represents where the exception was |
| 601 | raised. |
| 602 | |
| 603 | |
| 604 | .. function:: currentframe() |
| 605 | |
| 606 | Return the frame object for the caller's stack frame. |
| 607 | |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 608 | .. impl-detail:: |
| 609 | |
| 610 | This function relies on Python stack frame support in the interpreter, |
| 611 | which isn't guaranteed to exist in all implementations of Python. If |
| 612 | running in an implementation without Python stack frame support this |
| 613 | function returns ``None``. |
Michael Foord | 668be58 | 2009-09-13 16:46:19 +0000 | [diff] [blame] | 614 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 615 | |
| 616 | .. function:: stack([context]) |
| 617 | |
| 618 | Return a list of frame records for the caller's stack. The first entry in the |
| 619 | returned list represents the caller; the last entry represents the outermost |
| 620 | call on the stack. |
| 621 | |
| 622 | |
| 623 | .. function:: trace([context]) |
| 624 | |
| 625 | Return a list of frame records for the stack between the current frame and the |
| 626 | frame in which an exception currently being handled was raised in. The first |
| 627 | entry in the list represents the caller; the last entry represents where the |
| 628 | exception was raised. |
| 629 | |