|
10 | 10 |
|
11 | 11 | --------------
|
12 | 12 |
|
13 |
| -The :mod:`pyclbr` module can be used to determine some limited information |
14 |
| -about the classes, methods and top-level functions defined in a module. The |
15 |
| -information provided is sufficient to implement a traditional three-pane |
16 |
| -class browser. The information is extracted from the source code rather |
17 |
| -than by importing the module, so this module is safe to use with untrusted |
18 |
| -code. This restriction makes it impossible to use this module with modules |
19 |
| -not implemented in Python, including all standard and optional extension |
| 13 | +The :mod:`pyclbr` module provides limited information about the |
| 14 | +functions, classes, and methods defined in a python-coded module. The |
| 15 | +information is sufficient to implement a module browser. The |
| 16 | +information is extracted from the python source code rather than by |
| 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 |
20 | 20 | modules.
|
21 | 21 |
|
22 | 22 |
|
23 | 23 | .. function:: readmodule(module, path=None)
|
24 | 24 |
|
25 |
| - Read a module and return a dictionary mapping class names to class |
26 |
| - descriptor objects. The parameter *module* should be the name of a |
27 |
| - module as a string; it may be the name of a module within a package. The |
28 |
| - *path* parameter should be a sequence, and is used to augment the value |
29 |
| - of ``sys.path``, which is used to locate module source code. |
| 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. |
30 | 31 |
|
31 | 32 |
|
32 | 33 | .. function:: readmodule_ex(module, path=None)
|
33 | 34 |
|
34 |
| - Like :func:`readmodule`, but the returned dictionary, in addition to |
35 |
| - mapping class names to class descriptor objects, also maps top-level |
36 |
| - function names to function descriptor objects. Moreover, if the module |
37 |
| - being read is a package, the key ``'__path__'`` in the returned |
38 |
| - dictionary has as its value a list which contains the package search |
39 |
| - path. |
| 35 | + Return a dictionary-based tree containing a function or class |
| 36 | + descriptors for each function and class defined in the module with a |
| 37 | + ``def`` or ``class`` statement. The returned dictionary maps |
| 38 | + module-level function and class names to their descriptors. Nested |
| 39 | + objects are entered into the children dictionary of their parent. As |
| 40 | + with readmodule, *module* names the module to be read and *path* is |
| 41 | + prepended to sys.path. If the module being read is a package, the |
| 42 | + returned dictionary has a key ``'__path__'`` whose value is a list |
| 43 | + containing the package search path. |
40 | 44 |
|
| 45 | +.. versionadded:: 3.7 |
| 46 | + Descriptors for nested definitions. They are accessed through the |
| 47 | + new children attibute. Each has a new parent attribute. |
41 | 48 |
|
42 |
| -.. _pyclbr-class-objects: |
| 49 | +The descriptors returned by these functions are instances of |
| 50 | +Function and Class classes. Users are not expected to create instances |
| 51 | +of these classes. |
43 | 52 |
|
44 |
| -Class Objects |
45 |
| -------------- |
46 | 53 |
|
47 |
| -The :class:`Class` objects used as values in the dictionary returned by |
48 |
| -:func:`readmodule` and :func:`readmodule_ex` provide the following data |
49 |
| -attributes: |
| 54 | +.. _pyclbr-function-objects: |
50 | 55 |
|
| 56 | +Function Objects |
| 57 | +---------------- |
| 58 | +Class :class:`Function` instances describe functions defined by def |
| 59 | +statements. They have the following attributes: |
51 | 60 |
|
52 |
| -.. attribute:: Class.module |
53 | 61 |
|
54 |
| - The name of the module defining the class described by the class descriptor. |
| 62 | +.. attribute:: Function.file |
55 | 63 |
|
| 64 | + Name of the file in which the function is defined. |
56 | 65 |
|
57 |
| -.. attribute:: Class.name |
58 | 66 |
|
59 |
| - The name of the class. |
| 67 | +.. attribute:: Function.module |
60 | 68 |
|
| 69 | + The name of the module defining the function described. |
61 | 70 |
|
62 |
| -.. attribute:: Class.super |
63 | 71 |
|
64 |
| - A list of :class:`Class` objects which describe the immediate base |
65 |
| - classes of the class being described. Classes which are named as |
66 |
| - superclasses but which are not discoverable by :func:`readmodule` are |
67 |
| - listed as a string with the class name instead of as :class:`Class` |
68 |
| - objects. |
| 72 | +.. attribute:: Function.name |
| 73 | + |
| 74 | + The name of the function. |
69 | 75 |
|
70 | 76 |
|
71 |
| -.. attribute:: Class.methods |
| 77 | +.. attribute:: Function.lineno |
| 78 | + |
| 79 | + The line number in the file where the definition starts. |
| 80 | + |
| 81 | + |
| 82 | +.. attribute:: Function.parent |
| 83 | + |
| 84 | + For top-level functions, None. For nested functions, the parent. |
| 85 | + |
| 86 | + .. versionadded:: 3.7 |
| 87 | + |
| 88 | + |
| 89 | +.. attribute:: Function.children |
| 90 | + |
| 91 | + A dictionary mapping names to descriptors for nested functions and |
| 92 | + classes. |
72 | 93 |
|
73 |
| - A dictionary mapping method names to line numbers. |
| 94 | + .. versionadded:: 3.7 |
| 95 | + |
| 96 | + |
| 97 | +.. _pyclbr-class-objects: |
| 98 | + |
| 99 | +Class Objects |
| 100 | +------------- |
| 101 | +Class :class:`Class` instances describe classes defined by class |
| 102 | +statements. They have the same attributes as Functions and two more. |
74 | 103 |
|
75 | 104 |
|
76 | 105 | .. attribute:: Class.file
|
77 | 106 |
|
78 |
| - Name of the file containing the ``class`` statement defining the class. |
| 107 | + Name of the file in which the class is defined. |
79 | 108 |
|
80 | 109 |
|
81 |
| -.. attribute:: Class.lineno |
| 110 | +.. attribute:: Class.module |
82 | 111 |
|
83 |
| - The line number of the ``class`` statement within the file named by |
84 |
| - :attr:`~Class.file`. |
| 112 | + The name of the module defining the class described. |
85 | 113 |
|
86 | 114 |
|
87 |
| -.. _pyclbr-function-objects: |
| 115 | +.. attribute:: Class.name |
88 | 116 |
|
89 |
| -Function Objects |
90 |
| ----------------- |
| 117 | + The name of the class. |
91 | 118 |
|
92 |
| -The :class:`Function` objects used as values in the dictionary returned by |
93 |
| -:func:`readmodule_ex` provide the following attributes: |
94 | 119 |
|
| 120 | +.. attribute:: Class.lineno |
95 | 121 |
|
96 |
| -.. attribute:: Function.module |
| 122 | + The line number in the file where the definition starts. |
97 | 123 |
|
98 |
| - The name of the module defining the function described by the function |
99 |
| - descriptor. |
100 | 124 |
|
| 125 | +.. attribute:: Class.parent |
101 | 126 |
|
102 |
| -.. attribute:: Function.name |
| 127 | + For top-level classes, None. For nested classes, the parent. |
103 | 128 |
|
104 |
| - The name of the function. |
| 129 | + .. versionadded:: 3.7 |
105 | 130 |
|
106 | 131 |
|
107 |
| -.. attribute:: Function.file |
| 132 | +.. attribute:: Class.children |
108 | 133 |
|
109 |
| - Name of the file containing the ``def`` statement defining the function. |
| 134 | + A dictionary mapping names to descriptors for nested functions and |
| 135 | + classes. |
110 | 136 |
|
| 137 | + .. versionadded:: 3.7 |
111 | 138 |
|
112 |
| -.. attribute:: Function.lineno |
113 | 139 |
|
114 |
| - The line number of the ``def`` statement within the file named by |
115 |
| - :attr:`~Function.file`. |
| 140 | +.. attribute:: Class.super |
| 141 | + |
| 142 | + A list of :class:`Class` objects which describe the immediate base |
| 143 | + classes of the class being described. Classes which are named as |
| 144 | + superclasses but which are not discoverable by :func:`readmodule_ex` |
| 145 | + are listed as a string with the class name instead of as |
| 146 | + :class:`Class` objects. |
| 147 | + |
| 148 | + |
| 149 | +.. attribute:: Class.methods |
116 | 150 |
|
| 151 | + A dictionary mapping method names to line numbers. This can be |
| 152 | + derived from the newer children dictionary, but remains for |
| 153 | + back-compatibility. |
0 commit comments