Changeset 391 for python/trunk/Lib/pydoc.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/pydoc.py
r2 r391 38 38 __date__ = "26 February 2001" 39 39 40 __version__ = "$Revision: 78208$"40 __version__ = "$Revision: 88564 $" 41 41 __credits__ = """Guido van Rossum, for an excellent programming language. 42 42 Tommy Burnette, the original creator of manpy. … … 53 53 # path will be displayed. 54 54 55 import sys, imp, os, re, types, inspect, __builtin__, pkgutil 55 import sys, imp, os, re, types, inspect, __builtin__, pkgutil, warnings 56 56 from repr import Repr 57 57 from string import expandtabs, find, join, lower, split, strip, rfind, rstrip … … 157 157 return yes, no 158 158 159 def visiblename(name, all=None ):159 def visiblename(name, all=None, obj=None): 160 160 """Decide whether to show documentation on a variable.""" 161 161 # Certain special names are redundant. … … 165 165 # Private names are hidden, but special names are displayed. 166 166 if name.startswith('__') and name.endswith('__'): return 1 167 # Namedtuples have public fields and methods with a single leading underscore 168 if name.startswith('_') and hasattr(obj, '_fields'): 169 return 1 167 170 if all is not None: 168 171 # only document that which the programmer exported in __all__ … … 210 213 """Get the one-line summary out of a module file.""" 211 214 mtime = os.stat(filename).st_mtime 212 lastupdate, result = cache.get(filename, ( 0, None))213 if lastupdate < mtime:215 lastupdate, result = cache.get(filename, (None, None)) 216 if lastupdate is None or lastupdate < mtime: 214 217 info = inspect.getmoduleinfo(filename) 215 218 try: … … 357 360 'thread', 'zipimport') or 358 361 (file.startswith(basedir) and 359 not file.startswith(os.path.join(basedir, 'site-packages'))))): 362 not file.startswith(os.path.join(basedir, 'site-packages')))) and 363 object.__name__ not in ('xml.etree', 'test.pydoc_mod')): 360 364 if docloc.startswith("http://"): 361 365 docloc = "%s/%s" % (docloc.rstrip("/"), object.__name__) … … 475 479 """Format a list of items into a multi-column list.""" 476 480 result = '' 477 rows = (len(list)+cols-1)/ cols481 rows = (len(list)+cols-1)//cols 478 482 for col in range(cols): 479 result = result + '<td width="%d%%" valign=top>' % (100/ cols)483 result = result + '<td width="%d%%" valign=top>' % (100//cols) 480 484 for i in range(rows*col, rows*col+rows): 481 485 if i < len(list): … … 627 631 if (all is not None or 628 632 (inspect.getmodule(value) or object) is object): 629 if visiblename(key, all ):633 if visiblename(key, all, object): 630 634 classes.append((key, value)) 631 635 cdict[key] = cdict[value] = '#' + key … … 643 647 if (all is not None or 644 648 inspect.isbuiltin(value) or inspect.getmodule(value) is object): 645 if visiblename(key, all ):649 if visiblename(key, all, object): 646 650 funcs.append((key, value)) 647 651 fdict[key] = '#-' + key … … 649 653 data = [] 650 654 for key, value in inspect.getmembers(object, isdata): 651 if visiblename(key, all ):655 if visiblename(key, all, object): 652 656 data.append((key, value)) 653 657 … … 737 741 push(msg) 738 742 for name, kind, homecls, value in ok: 739 push(self.document(getattr(object, name), name, mod, 740 funcs, classes, mdict, object)) 743 try: 744 value = getattr(object, name) 745 except Exception: 746 # Some descriptors may meet a failure in their __get__. 747 # (bug #1785) 748 push(self._docdescriptor(name, value, mod)) 749 else: 750 push(self.document(value, name, mod, 751 funcs, classes, mdict, object)) 741 752 push('\n') 742 753 return attrs … … 773 784 return attrs 774 785 775 attrs = filter(lambda data: visiblename(data[0] ),786 attrs = filter(lambda data: visiblename(data[0], obj=object), 776 787 classify_class_attrs(object)) 777 788 mdict = {} 778 789 for key, kind, homecls, value in attrs: 779 790 mdict[key] = anchor = '#' + name + '-' + key 780 value = getattr(object, key) 791 try: 792 value = getattr(object, name) 793 except Exception: 794 # Some descriptors may meet a failure in their __get__. 795 # (bug #1785) 796 pass 781 797 try: 782 798 # The value may not be hashable (e.g., a data attr with … … 1042 1058 if (all is not None 1043 1059 or (inspect.getmodule(value) or object) is object): 1044 if visiblename(key, all ):1060 if visiblename(key, all, object): 1045 1061 classes.append((key, value)) 1046 1062 funcs = [] … … 1049 1065 if (all is not None or 1050 1066 inspect.isbuiltin(value) or inspect.getmodule(value) is object): 1051 if visiblename(key, all ):1067 if visiblename(key, all, object): 1052 1068 funcs.append((key, value)) 1053 1069 data = [] 1054 1070 for key, value in inspect.getmembers(object, isdata): 1055 if visiblename(key, all ):1071 if visiblename(key, all, object): 1056 1072 data.append((key, value)) 1057 1073 … … 1113 1129 return result 1114 1130 1115 def docclass(self, object, name=None, mod=None ):1131 def docclass(self, object, name=None, mod=None, *ignored): 1116 1132 """Produce text documentation for a given class object.""" 1117 1133 realname = object.__name__ … … 1158 1174 push(msg) 1159 1175 for name, kind, homecls, value in ok: 1160 push(self.document(getattr(object, name), 1161 name, mod, object)) 1176 try: 1177 value = getattr(object, name) 1178 except Exception: 1179 # Some descriptors may meet a failure in their __get__. 1180 # (bug #1785) 1181 push(self._docdescriptor(name, value, mod)) 1182 else: 1183 push(self.document(value, 1184 name, mod, object)) 1162 1185 return attrs 1163 1186 … … 1186 1209 return attrs 1187 1210 1188 attrs = filter(lambda data: visiblename(data[0] ),1211 attrs = filter(lambda data: visiblename(data[0], obj=object), 1189 1212 classify_class_attrs(object)) 1190 1213 while attrs: … … 1451 1474 if module: 1452 1475 object = module 1453 for part in parts[n:]:1454 try: object = getattr(object, part)1455 except AttributeError: return None1456 return object1457 1476 else: 1458 if hasattr(__builtin__, path): 1459 return getattr(__builtin__, path) 1477 object = __builtin__ 1478 for part in parts[n:]: 1479 try: 1480 object = getattr(object, part) 1481 except AttributeError: 1482 return None 1483 return object 1460 1484 1461 1485 # --------------------------------------- interactive interpreter interface … … 1475 1499 return object, thing 1476 1500 else: 1477 return thing, getattr(thing, '__name__', None) 1501 name = getattr(thing, '__name__', None) 1502 return thing, name if isinstance(name, str) else None 1478 1503 1479 1504 def render_doc(thing, title='Python Library Documentation: %s', forceload=0): … … 1533 1558 # (label, seealso-items). The "label" is the label of the corresponding 1534 1559 # section in the .rst file under Doc/ and an index into the dictionary 1535 # in pydoc_ topics.py.1560 # in pydoc_data/topics.py. 1536 1561 # 1537 1562 # CAUTION: if you change one of these dictionaries, be sure to adapt the 1538 1563 # list of needed labels in Doc/tools/sphinxext/pyspecific.py and 1539 # regenerate the pydoc_ topics.py file by running1564 # regenerate the pydoc_data/topics.py file by running 1540 1565 # make pydoc-topics 1541 1566 # in Doc/ and copying the output file into the Lib/ directory. … … 1705 1730 } 1706 1731 1707 def __init__(self, input, output): 1708 self.input = input 1709 self.output = output 1732 def __init__(self, input=None, output=None): 1733 self._input = input 1734 self._output = output 1735 1736 input = property(lambda self: self._input or sys.stdin) 1737 output = property(lambda self: self._output or sys.stdout) 1710 1738 1711 1739 def __repr__(self): … … 1715 1743 return '<pydoc.Helper instance>' 1716 1744 1717 def __call__(self, request=None): 1718 if request is not None: 1745 _GoInteractive = object() 1746 def __call__(self, request=_GoInteractive): 1747 if request is not self._GoInteractive: 1719 1748 self.help(request) 1720 1749 else: … … 1772 1801 1773 1802 If this is your first time using Python, you should definitely check out 1774 the tutorial on the Internet at http://docs.python.org/ tutorial/.1803 the tutorial on the Internet at http://docs.python.org/%s/tutorial/. 1775 1804 1776 1805 Enter the name of any module, keyword, or topic to get help on writing … … 1782 1811 of what it does; to list the modules whose summaries contain a given word 1783 1812 such as "spam", type "modules spam". 1784 ''' % sys.version[:3])1813 ''' % tuple([sys.version[:3]]*2)) 1785 1814 1786 1815 def list(self, items, columns=4, width=80): … … 1822 1851 def showtopic(self, topic, more_xrefs=''): 1823 1852 try: 1824 import pydoc_ topics1853 import pydoc_data.topics 1825 1854 except ImportError: 1826 1855 self.output.write(''' 1827 1856 Sorry, topic and keyword documentation is not available because the 1828 module "pydoc_ topics" could not be found.1857 module "pydoc_data.topics" could not be found. 1829 1858 ''') 1830 1859 return … … 1838 1867 label, xrefs = target 1839 1868 try: 1840 doc = pydoc_ topics.topics[label]1869 doc = pydoc_data.topics.topics[label] 1841 1870 except KeyError: 1842 1871 self.output.write('no documentation found for %s\n' % repr(topic)) … … 1884 1913 ''') 1885 1914 1886 help = Helper( sys.stdin, sys.stdout)1915 help = Helper() 1887 1916 1888 1917 class Scanner: … … 1960 1989 modname = modname[:-9] + ' (package)' 1961 1990 print modname, desc and '- ' + desc 1962 try: import warnings 1963 except ImportError: pass 1964 else: warnings.filterwarnings('ignore') # ignore problems during import 1965 ModuleScanner().run(callback, key) 1991 def onerror(modname): 1992 pass 1993 with warnings.catch_warnings(): 1994 warnings.filterwarnings('ignore') # ignore problems during import 1995 ModuleScanner().run(callback, key, onerror=onerror) 1966 1996 1967 1997 # --------------------------------------------------- web browser interface … … 2028 2058 class DocServer(BaseHTTPServer.HTTPServer): 2029 2059 def __init__(self, port, callback): 2030 host = (sys.platform == 'mac') and '127.0.0.1' or'localhost'2031 self.address = ( '', port)2060 host = 'localhost' 2061 self.address = (host, port) 2032 2062 self.url = 'http://%s:%d/' % (host, port) 2033 2063 self.callback = callback … … 2145 2175 if sys.platform == 'win32': 2146 2176 os.system('start "%s"' % url) 2147 elif sys.platform == 'mac':2148 try: import ic2149 except ImportError: pass2150 else: ic.launchurl(url)2151 2177 else: 2152 2178 rc = os.system('netscape -remote "openURL(%s)" &' % url)
Note:
See TracChangeset
for help on using the changeset viewer.