blob: 32842717bc6fad93a6e187e9fe34cdb93be8e07b [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`pyclbr` --- Python class browser support
2==============================================
3
4.. module:: pyclbr
5 :synopsis: Supports information extraction for a Python class browser.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
Raymond Hettingera1993682011-01-27 01:20:32 +00009**Source code:** :source:`Lib/pyclbr.py`
10
11--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
Christian Heimes81ee3ef2008-05-04 22:42:01 +000013The :mod:`pyclbr` module can be used to determine some limited information
14about the classes, methods and top-level functions defined in a module. The
15information provided is sufficient to implement a traditional three-pane
16class browser. The information is extracted from the source code rather
17than by importing the module, so this module is safe to use with untrusted
18code. This restriction makes it impossible to use this module with modules
19not implemented in Python, including all standard and optional extension
20modules.
Georg Brandl116aa622007-08-15 14:28:22 +000021
22
Georg Brandl18244152009-09-02 20:34:52 +000023.. function:: readmodule(module, path=None)
Georg Brandl116aa622007-08-15 14:28:22 +000024
Christian Heimes81ee3ef2008-05-04 22:42:01 +000025 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.
Georg Brandl116aa622007-08-15 14:28:22 +000030
31
Georg Brandl18244152009-09-02 20:34:52 +000032.. function:: readmodule_ex(module, path=None)
Georg Brandl116aa622007-08-15 14:28:22 +000033
Christian Heimes81ee3ef2008-05-04 22:42:01 +000034 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.
Georg Brandl116aa622007-08-15 14:28:22 +000040
41
42.. _pyclbr-class-objects:
43
Christian Heimes81ee3ef2008-05-04 22:42:01 +000044Class Objects
45-------------
Georg Brandl116aa622007-08-15 14:28:22 +000046
Christian Heimes81ee3ef2008-05-04 22:42:01 +000047The :class:`Class` objects used as values in the dictionary returned by
48:func:`readmodule` and :func:`readmodule_ex` provide the following data
Senthil Kumarana6bac952011-07-04 11:28:30 -070049attributes:
Georg Brandl116aa622007-08-15 14:28:22 +000050
51
Christian Heimes81ee3ef2008-05-04 22:42:01 +000052.. attribute:: Class.module
Georg Brandl116aa622007-08-15 14:28:22 +000053
54 The name of the module defining the class described by the class descriptor.
55
56
Christian Heimes81ee3ef2008-05-04 22:42:01 +000057.. attribute:: Class.name
Georg Brandl116aa622007-08-15 14:28:22 +000058
59 The name of the class.
60
61
Christian Heimes81ee3ef2008-05-04 22:42:01 +000062.. attribute:: Class.super
Georg Brandl116aa622007-08-15 14:28:22 +000063
Christian Heimes81ee3ef2008-05-04 22:42:01 +000064 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.
Georg Brandl116aa622007-08-15 14:28:22 +000069
70
Christian Heimes81ee3ef2008-05-04 22:42:01 +000071.. attribute:: Class.methods
Georg Brandl116aa622007-08-15 14:28:22 +000072
73 A dictionary mapping method names to line numbers.
74
75
Christian Heimes81ee3ef2008-05-04 22:42:01 +000076.. attribute:: Class.file
Georg Brandl116aa622007-08-15 14:28:22 +000077
78 Name of the file containing the ``class`` statement defining the class.
79
80
Christian Heimes81ee3ef2008-05-04 22:42:01 +000081.. attribute:: Class.lineno
Georg Brandl116aa622007-08-15 14:28:22 +000082
83 The line number of the ``class`` statement within the file named by
Georg Brandl502d9a52009-07-26 15:02:41 +000084 :attr:`~Class.file`.
Georg Brandl116aa622007-08-15 14:28:22 +000085
86
87.. _pyclbr-function-objects:
88
Christian Heimes81ee3ef2008-05-04 22:42:01 +000089Function Objects
90----------------
Georg Brandl116aa622007-08-15 14:28:22 +000091
Christian Heimes81ee3ef2008-05-04 22:42:01 +000092The :class:`Function` objects used as values in the dictionary returned by
Senthil Kumarana6bac952011-07-04 11:28:30 -070093:func:`readmodule_ex` provide the following attributes:
Georg Brandl116aa622007-08-15 14:28:22 +000094
95
Christian Heimes81ee3ef2008-05-04 22:42:01 +000096.. attribute:: Function.module
Georg Brandl116aa622007-08-15 14:28:22 +000097
98 The name of the module defining the function described by the function
99 descriptor.
100
101
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000102.. attribute:: Function.name
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104 The name of the function.
105
106
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000107.. attribute:: Function.file
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109 Name of the file containing the ``def`` statement defining the function.
110
111
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000112.. attribute:: Function.lineno
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000114 The line number of the ``def`` statement within the file named by
Georg Brandl502d9a52009-07-26 15:02:41 +0000115 :attr:`~Function.file`.
Georg Brandl116aa622007-08-15 14:28:22 +0000116