Hakan Çelik | aea045a | 2020-02-24 05:00:40 +0300 | [diff] [blame] | 1 | :mod:`pyclbr` --- Python module browser support |
| 2 | =============================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: pyclbr |
Hakan Çelik | aea045a | 2020-02-24 05:00:40 +0300 | [diff] [blame] | 5 | :synopsis: Supports information extraction for a Python module browser. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 8 | |
Raymond Hettinger | a199368 | 2011-01-27 01:20:32 +0000 | [diff] [blame] | 9 | **Source code:** :source:`Lib/pyclbr.py` |
| 10 | |
| 11 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 13 | The :mod:`pyclbr` module provides limited information about the |
Andrés Delfino | 271818f | 2018-09-14 14:13:09 -0300 | [diff] [blame] | 14 | functions, classes, and methods defined in a Python-coded module. The |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 15 | information is sufficient to implement a module browser. The |
Andrés Delfino | 271818f | 2018-09-14 14:13:09 -0300 | [diff] [blame] | 16 | information is extracted from the Python source code rather than by |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 17 | importing the module, so this module is safe to use with untrusted code. |
| 18 | This restriction makes it impossible to use this module with modules not |
| 19 | implemented in Python, including all standard and optional extension |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 20 | modules. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | |
| 22 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 23 | .. function:: readmodule(module, path=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 25 | Return a dictionary mapping module-level class names to class |
| 26 | descriptors. If possible, descriptors for imported base classes are |
| 27 | included. Parameter *module* is a string with the name of the module |
| 28 | to read; it may be the name of a module within a package. If given, |
| 29 | *path* is a sequence of directory paths prepended to ``sys.path``, |
| 30 | which is used to locate the module source code. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
Hakan Çelik | aea045a | 2020-02-24 05:00:40 +0300 | [diff] [blame] | 32 | This function is the original interface and is only kept for back |
| 33 | compatibility. It returns a filtered version of the following. |
| 34 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 36 | .. function:: readmodule_ex(module, path=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 38 | Return a dictionary-based tree containing a function or class |
| 39 | descriptors for each function and class defined in the module with a |
| 40 | ``def`` or ``class`` statement. The returned dictionary maps |
| 41 | module-level function and class names to their descriptors. Nested |
| 42 | objects are entered into the children dictionary of their parent. As |
| 43 | with readmodule, *module* names the module to be read and *path* is |
| 44 | prepended to sys.path. If the module being read is a package, the |
| 45 | returned dictionary has a key ``'__path__'`` whose value is a list |
| 46 | containing the package search path. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 48 | .. versionadded:: 3.7 |
| 49 | Descriptors for nested definitions. They are accessed through the |
penguindustin | 9646630 | 2019-05-06 14:57:17 -0400 | [diff] [blame] | 50 | new children attribute. Each has a new parent attribute. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 52 | The descriptors returned by these functions are instances of |
| 53 | Function and Class classes. Users are not expected to create instances |
| 54 | of these classes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | .. _pyclbr-function-objects: |
| 58 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 59 | Function Objects |
| 60 | ---------------- |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 61 | Class :class:`Function` instances describe functions defined by def |
| 62 | statements. They have the following attributes: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 64 | |
| 65 | .. attribute:: Function.file |
| 66 | |
| 67 | Name of the file in which the function is defined. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
| 69 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 70 | .. attribute:: Function.module |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 72 | The name of the module defining the function described. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
| 74 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 75 | .. attribute:: Function.name |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | The name of the function. |
| 78 | |
| 79 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 80 | .. attribute:: Function.lineno |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 82 | The line number in the file where the definition starts. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 84 | |
| 85 | .. attribute:: Function.parent |
| 86 | |
| 87 | For top-level functions, None. For nested functions, the parent. |
| 88 | |
| 89 | .. versionadded:: 3.7 |
| 90 | |
| 91 | |
| 92 | .. attribute:: Function.children |
| 93 | |
| 94 | A dictionary mapping names to descriptors for nested functions and |
| 95 | classes. |
| 96 | |
| 97 | .. versionadded:: 3.7 |
| 98 | |
| 99 | |
Batuhan Taskaya | fa476fe | 2020-11-11 10:14:12 +0300 | [diff] [blame] | 100 | .. attribute:: Function.is_async |
| 101 | |
| 102 | ``True`` for functions that are defined with the ``async`` prefix, ``False`` otherwise. |
| 103 | |
| 104 | .. versionadded:: 3.10 |
| 105 | |
| 106 | |
csabella | 246ff3b | 2017-07-03 21:31:25 -0400 | [diff] [blame] | 107 | .. _pyclbr-class-objects: |
| 108 | |
| 109 | Class Objects |
| 110 | ------------- |
| 111 | Class :class:`Class` instances describe classes defined by class |
| 112 | statements. They have the same attributes as Functions and two more. |
| 113 | |
| 114 | |
| 115 | .. attribute:: Class.file |
| 116 | |
| 117 | Name of the file in which the class is defined. |
| 118 | |
| 119 | |
| 120 | .. attribute:: Class.module |
| 121 | |
| 122 | The name of the module defining the class described. |
| 123 | |
| 124 | |
| 125 | .. attribute:: Class.name |
| 126 | |
| 127 | The name of the class. |
| 128 | |
| 129 | |
| 130 | .. attribute:: Class.lineno |
| 131 | |
| 132 | The line number in the file where the definition starts. |
| 133 | |
| 134 | |
| 135 | .. attribute:: Class.parent |
| 136 | |
| 137 | For top-level classes, None. For nested classes, the parent. |
| 138 | |
| 139 | .. versionadded:: 3.7 |
| 140 | |
| 141 | |
| 142 | .. attribute:: Class.children |
| 143 | |
| 144 | A dictionary mapping names to descriptors for nested functions and |
| 145 | classes. |
| 146 | |
| 147 | .. versionadded:: 3.7 |
| 148 | |
| 149 | |
| 150 | .. attribute:: Class.super |
| 151 | |
| 152 | A list of :class:`Class` objects which describe the immediate base |
| 153 | classes of the class being described. Classes which are named as |
| 154 | superclasses but which are not discoverable by :func:`readmodule_ex` |
| 155 | are listed as a string with the class name instead of as |
| 156 | :class:`Class` objects. |
| 157 | |
| 158 | |
| 159 | .. attribute:: Class.methods |
| 160 | |
| 161 | A dictionary mapping method names to line numbers. This can be |
| 162 | derived from the newer children dictionary, but remains for |
| 163 | back-compatibility. |