blob: 36b46f483df07b7c2419d625fb013a6fcd60b7a9 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`pyclbr` --- Python class browser support
3==============================================
4
5.. module:: pyclbr
6 :synopsis: Supports information extraction for a Python class browser.
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
9
Skip Montanarof30f6e82008-04-28 02:59:45 +000010The :mod:`pyclbr` module can be used to determine some limited information
11about the classes, methods and top-level functions defined in a module. The
12information provided is sufficient to implement a traditional three-pane
13class browser. The information is extracted from the source code rather
14than by importing the module, so this module is safe to use with untrusted
15code. This restriction makes it impossible to use this module with modules
16not implemented in Python, including all standard and optional extension
17modules.
Georg Brandl8ec7f652007-08-15 14:28:01 +000018
19
Skip Montanarof30f6e82008-04-28 02:59:45 +000020.. function:: readmodule(module[, path=None])
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
Skip Montanarof30f6e82008-04-28 02:59:45 +000022 Read a module and return a dictionary mapping class names to class
23 descriptor objects. The parameter *module* should be the name of a
24 module as a string; it may be the name of a module within a package. The
25 *path* parameter should be a sequence, and is used to augment the value
26 of ``sys.path``, which is used to locate module source code.
Georg Brandl8ec7f652007-08-15 14:28:01 +000027
28
Skip Montanarof30f6e82008-04-28 02:59:45 +000029.. function:: readmodule_ex(module[, path=None])
Georg Brandl8ec7f652007-08-15 14:28:01 +000030
Skip Montanarof30f6e82008-04-28 02:59:45 +000031 Like :func:`readmodule`, but the returned dictionary, in addition to
32 mapping class names to class descriptor objects, also maps top-level
33 function names to function descriptor objects. Moreover, if the module
34 being read is a package, the key ``'__path__'`` in the returned
35 dictionary has as its value a list which contains the package search
36 path.
Georg Brandl8ec7f652007-08-15 14:28:01 +000037
38
39.. _pyclbr-class-objects:
40
Skip Montanarof30f6e82008-04-28 02:59:45 +000041Class Objects
42-------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000043
Skip Montanarof30f6e82008-04-28 02:59:45 +000044The :class:`Class` objects used as values in the dictionary returned by
45:func:`readmodule` and :func:`readmodule_ex` provide the following data
46members:
Georg Brandl8ec7f652007-08-15 14:28:01 +000047
48
Skip Montanarof30f6e82008-04-28 02:59:45 +000049.. attribute:: Class.module
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
51 The name of the module defining the class described by the class descriptor.
52
53
Skip Montanarof30f6e82008-04-28 02:59:45 +000054.. attribute:: Class.name
Georg Brandl8ec7f652007-08-15 14:28:01 +000055
56 The name of the class.
57
58
Skip Montanarof30f6e82008-04-28 02:59:45 +000059.. attribute:: Class.super
Georg Brandl8ec7f652007-08-15 14:28:01 +000060
Skip Montanarof30f6e82008-04-28 02:59:45 +000061 A list of :class:`Class` objects which describe the immediate base
62 classes of the class being described. Classes which are named as
63 superclasses but which are not discoverable by :func:`readmodule` are
64 listed as a string with the class name instead of as :class:`Class`
65 objects.
Georg Brandl8ec7f652007-08-15 14:28:01 +000066
67
Skip Montanarof30f6e82008-04-28 02:59:45 +000068.. attribute:: Class.methods
Georg Brandl8ec7f652007-08-15 14:28:01 +000069
70 A dictionary mapping method names to line numbers.
71
72
Skip Montanarof30f6e82008-04-28 02:59:45 +000073.. attribute:: Class.file
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75 Name of the file containing the ``class`` statement defining the class.
76
77
Skip Montanarof30f6e82008-04-28 02:59:45 +000078.. attribute:: Class.lineno
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
80 The line number of the ``class`` statement within the file named by
Georg Brandl9fa61bb2009-07-26 14:19:57 +000081 :attr:`~Class.file`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
83
84.. _pyclbr-function-objects:
85
Skip Montanarof30f6e82008-04-28 02:59:45 +000086Function Objects
87----------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000088
Skip Montanarof30f6e82008-04-28 02:59:45 +000089The :class:`Function` objects used as values in the dictionary returned by
Georg Brandl8ec7f652007-08-15 14:28:01 +000090:func:`readmodule_ex` provide the following data members:
91
92
Skip Montanarof30f6e82008-04-28 02:59:45 +000093.. attribute:: Function.module
Georg Brandl8ec7f652007-08-15 14:28:01 +000094
95 The name of the module defining the function described by the function
96 descriptor.
97
98
Skip Montanarof30f6e82008-04-28 02:59:45 +000099.. attribute:: Function.name
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100
101 The name of the function.
102
103
Skip Montanarof30f6e82008-04-28 02:59:45 +0000104.. attribute:: Function.file
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105
106 Name of the file containing the ``def`` statement defining the function.
107
108
Skip Montanarof30f6e82008-04-28 02:59:45 +0000109.. attribute:: Function.lineno
Georg Brandl8ec7f652007-08-15 14:28:01 +0000110
Skip Montanarof30f6e82008-04-28 02:59:45 +0000111 The line number of the ``def`` statement within the file named by
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000112 :attr:`~Function.file`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000113