Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1)

Side by Side Diff: Lib/pydoc.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/pstats.py ('k') | Lib/_pyio.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 """Generate Python documentation in HTML or text for interactive use. 2 """Generate Python documentation in HTML or text for interactive use.
3 3
4 In the Python interpreter, do "from pydoc import help" to provide online 4 In the Python interpreter, do "from pydoc import help" to provide online
5 help. Calling help(thing) on a Python object documents the object. 5 help. Calling help(thing) on a Python object documents the object.
6 6
7 Or, at the shell command line outside of Python: 7 Or, at the shell command line outside of Python:
8 8
9 Run "pydoc <name>" to show documentation on something. <name> may be 9 Run "pydoc <name>" to show documentation on something. <name> may be
10 the name of a function, module, package, or a dotted reference to a 10 the name of a function, module, package, or a dotted reference to a
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 """ 44 """
45 45
46 # Known bugs that can't be fixed here: 46 # Known bugs that can't be fixed here:
47 # - imp.load_module() cannot be prevented from clobbering existing 47 # - imp.load_module() cannot be prevented from clobbering existing
48 # loaded modules, so calling synopsis() on a binary module file 48 # loaded modules, so calling synopsis() on a binary module file
49 # changes the contents of any existing module with the same name. 49 # changes the contents of any existing module with the same name.
50 # - If the __file__ attribute on a module is a relative path and 50 # - If the __file__ attribute on a module is a relative path and
51 # the current directory is changed with os.chdir(), an incorrect 51 # the current directory is changed with os.chdir(), an incorrect
52 # path will be displayed. 52 # path will be displayed.
53 53
54 import os
55 import sys
54 import builtins 56 import builtins
55 import imp 57 import imp
58 import io
56 import inspect 59 import inspect
57 import io
58 import os
59 import pkgutil 60 import pkgutil
60 import platform 61 import platform
61 import re 62 import re
62 import sys
63 import time 63 import time
64 import tokenize
65 import warnings 64 import warnings
66 from collections import deque 65 from collections import deque
67 from reprlib import Repr 66 from reprlib import Repr
68 from traceback import extract_tb, format_exception_only 67 from traceback import extract_tb, format_exception_only
69 68
70 69
71 # --------------------------------------------------------- common routines 70 # --------------------------------------------------------- common routines
72 71
73 def pathdirs(): 72 def pathdirs():
74 """Convert sys.path into a list of absolute, existing, unique paths.""" 73 """Convert sys.path into a list of absolute, existing, unique paths."""
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 else: result = None 214 else: result = None
216 return result 215 return result
217 216
218 def synopsis(filename, cache={}): 217 def synopsis(filename, cache={}):
219 """Get the one-line summary out of a module file.""" 218 """Get the one-line summary out of a module file."""
220 mtime = os.stat(filename).st_mtime 219 mtime = os.stat(filename).st_mtime
221 lastupdate, result = cache.get(filename, (0, None)) 220 lastupdate, result = cache.get(filename, (0, None))
222 if lastupdate < mtime: 221 if lastupdate < mtime:
223 info = inspect.getmoduleinfo(filename) 222 info = inspect.getmoduleinfo(filename)
224 try: 223 try:
225 file = tokenize.open(filename) 224 file = open(filename)
226 except IOError: 225 except IOError:
227 # module can't be opened, so skip it 226 # module can't be opened, so skip it
228 return None 227 return None
229 if info and 'b' in info[2]: # binary modules have to be imported 228 if info and 'b' in info[2]: # binary modules have to be imported
230 try: module = imp.load_module('__temp__', file, filename, info[1:]) 229 try: module = imp.load_module('__temp__', file, filename, info[1:])
231 except: return None 230 except: return None
232 result = (module.__doc__ or '').splitlines()[0] 231 result = (module.__doc__ or '').splitlines()[0]
233 del sys.modules['__temp__'] 232 del sys.modules['__temp__']
234 else: # text modules can be directly examined 233 else: # text modules can be directly examined
235 result = source_synopsis(file) 234 result = source_synopsis(file)
236 file.close() 235 file.close()
237 cache[filename] = (mtime, result) 236 cache[filename] = (mtime, result)
238 return result 237 return result
239 238
240 class ErrorDuringImport(Exception): 239 class ErrorDuringImport(Exception):
241 """Errors that occurred while trying to import something to document it.""" 240 """Errors that occurred while trying to import something to document it."""
242 def __init__(self, filename, exc_info): 241 def __init__(self, filename, exc_info):
243 self.filename = filename 242 self.filename = filename
244 self.exc, self.value, self.tb = exc_info 243 self.exc, self.value, self.tb = exc_info
245 244
246 def __str__(self): 245 def __str__(self):
247 exc = self.exc.__name__ 246 exc = self.exc.__name__
248 return 'problem in %s - %s: %s' % (self.filename, exc, self.value) 247 return 'problem in %s - %s: %s' % (self.filename, exc, self.value)
249 248
250 def importfile(path): 249 def importfile(path):
251 """Import a Python source file or compiled file given its path.""" 250 """Import a Python source file or compiled file given its path."""
252 magic = imp.get_magic() 251 magic = imp.get_magic()
253 with open(path, 'rb') as file: 252 file = open(path, 'r')
254 if file.read(len(magic)) == magic: 253 if file.read(len(magic)) == magic:
255 kind = imp.PY_COMPILED 254 kind = imp.PY_COMPILED
256 else: 255 else:
257 kind = imp.PY_SOURCE 256 kind = imp.PY_SOURCE
258 file.seek(0) 257 file.close()
259 filename = os.path.basename(path) 258 filename = os.path.basename(path)
260 name, ext = os.path.splitext(filename) 259 name, ext = os.path.splitext(filename)
261 try: 260 file = open(path, 'r')
262 module = imp.load_module(name, file, path, (ext, 'r', kind)) 261 try:
263 except: 262 module = imp.load_module(name, file, path, (ext, 'r', kind))
264 raise ErrorDuringImport(path, sys.exc_info()) 263 except:
264 raise ErrorDuringImport(path, sys.exc_info())
265 file.close()
265 return module 266 return module
266 267
267 def safeimport(path, forceload=0, cache={}): 268 def safeimport(path, forceload=0, cache={}):
268 """Import a module; handle errors; return None if the module isn't found. 269 """Import a module; handle errors; return None if the module isn't found.
269 270
270 If the module *is* found but an exception occurs, it's wrapped in an 271 If the module *is* found but an exception occurs, it's wrapped in an
271 ErrorDuringImport exception and reraised. Unlike __import__, if a 272 ErrorDuringImport exception and reraised. Unlike __import__, if a
272 package path is specified, the module at the end of the path is returned, 273 package path is specified, the module at the end of the path is returned,
273 not the package at the beginning. If the optional 'forceload' argument 274 not the package at the beginning. If the optional 'forceload' argument
274 is 1, we reload the module from disk (unless it's a dynamic extension).""" 275 is 1, we reload the module from disk (unless it's a dynamic extension)."""
(...skipping 2029 matching lines...) Expand 10 before | Expand all | Expand 10 after
2304 '#ffffff', '#7799ee') 2305 '#ffffff', '#7799ee')
2305 for name, desc in search_result: 2306 for name, desc in search_result:
2306 results.append(bltinlink(name) + desc) 2307 results.append(bltinlink(name) + desc)
2307 contents = heading + html.bigsection( 2308 contents = heading + html.bigsection(
2308 'key = %s' % key, '#ffffff', '#ee77aa', '<br>'.join(results)) 2309 'key = %s' % key, '#ffffff', '#ee77aa', '<br>'.join(results))
2309 return 'Search Results', contents 2310 return 'Search Results', contents
2310 2311
2311 def html_getfile(path): 2312 def html_getfile(path):
2312 """Get and display a source file listing safely.""" 2313 """Get and display a source file listing safely."""
2313 path = path.replace('%20', ' ') 2314 path = path.replace('%20', ' ')
2314 with tokenize.open(path) as fp: 2315 with open(path, 'r') as fp:
2315 lines = html.escape(fp.read()) 2316 lines = html.escape(fp.read())
2316 body = '<pre>%s</pre>' % lines 2317 body = '<pre>%s</pre>' % lines
2317 heading = html.heading( 2318 heading = html.heading(
2318 '<big><big><strong>File Listing</strong></big></big>', 2319 '<big><big><strong>File Listing</strong></big></big>',
2319 '#ffffff', '#7799ee') 2320 '#ffffff', '#7799ee')
2320 contents = heading + html.bigsection( 2321 contents = heading + html.bigsection(
2321 'File: %s' % path, '#ffffff', '#ee77aa', body) 2322 'File: %s' % path, '#ffffff', '#ee77aa', body)
2322 return 'getfile %s' % path, contents 2323 return 'getfile %s' % path, contents
2323 2324
2324 def html_topics(): 2325 def html_topics():
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
2572 the -b option to explicitly specify the server port. 2573 the -b option to explicitly specify the server port.
2573 2574
2574 {cmd} -w <name> ... 2575 {cmd} -w <name> ...
2575 Write out the HTML documentation for a module to a file in the current 2576 Write out the HTML documentation for a module to a file in the current
2576 directory. If <name> contains a '{sep}', it is treated as a filename; if 2577 directory. If <name> contains a '{sep}', it is treated as a filename; if
2577 it names a directory, documentation is written for all the contents. 2578 it names a directory, documentation is written for all the contents.
2578 """.format(cmd=cmd, sep=os.sep)) 2579 """.format(cmd=cmd, sep=os.sep))
2579 2580
2580 if __name__ == '__main__': 2581 if __name__ == '__main__':
2581 cli() 2582 cli()
OLDNEW
« no previous file with comments | « Lib/pstats.py ('k') | Lib/_pyio.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+