summaryrefslogtreecommitdiffstats
path: root/Lib/pydoc.py
diff options
context:
space:
mode:
authorKa-Ping Yee <ping@zesty.ca>2001-03-02 01:19:14 (GMT)
committerKa-Ping Yee <ping@zesty.ca>2001-03-02 01:19:14 (GMT)
commita2fe103c9b75b20f2cd1362b7ecbd7edff1fc66c (patch)
tree324267b61126bebef2ad8c20d751a5be4efc5959 /Lib/pydoc.py
parent7fc49a44414f1bd696a199f7e8bda9a58995e91a (diff)
downloadcpython-a2fe103c9b75b20f2cd1362b7ecbd7edff1fc66c.zip
cpython-a2fe103c9b75b20f2cd1362b7ecbd7edff1fc66c.tar.gz
cpython-a2fe103c9b75b20f2cd1362b7ecbd7edff1fc66c.tar.bz2
Use imp.get_suffixes to determine a module name in modulename(file).
When possible, display strings containing backslashes using r'' notation.
Diffstat (limited to 'Lib/pydoc.py')
-rwxr-xr-xLib/pydoc.py32
1 files changed, 25 insertions, 7 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index 8d498b7..9829d5c 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -127,10 +127,13 @@ def stripid(text):
def modulename(path):
"""Return the Python module name for a given path, or None."""
filename = os.path.basename(path)
- for ending in ['.py', '.pyc', '.pyd', '.pyo',
- 'module.so', 'module.so.1', '.so']:
- if len(filename) > len(ending) and filename[-len(ending):] == ending:
- return filename[:-len(ending)]
+ suffixes = map(lambda (suffix, mode, kind): (len(suffix), suffix),
+ imp.get_suffixes())
+ suffixes.sort()
+ suffixes.reverse() # try longest suffixes first, in case they overlap
+ for length, suffix in suffixes:
+ if len(filename) > length and filename[-length:] == suffix:
+ return filename[:-length]
class DocImportError(Exception):
"""Class for errors while trying to import something to document it."""
@@ -205,9 +208,15 @@ class HTMLRepr(Repr):
return self.escape(cram(stripid(repr(x)), self.maxother))
def repr_string(self, x, level):
- text = self.escape(cram(x, self.maxstring))
- return re.sub(r'((\\[\\abfnrtv]|\\x..|\\u....)+)',
- r'<font color="#c040c0">\1</font>', repr(text))
+ test = cram(x, self.maxstring)
+ testrepr = repr(test)
+ if '\\' in test and '\\' not in replace(testrepr, (r'\\', '')):
+ # Backslashes are only literal in the string and are never
+ # needed to make any special characters, so show a raw string.
+ return 'r' + testrepr[0] + self.escape(test) + testrepr[0]
+ return re.sub(r'((\\[\\abfnrtv\'"]|\\x..|\\u....)+)',
+ r'<font color="#c040c0">\1</font>',
+ self.escape(testrepr))
def repr_instance(self, x, level):
try:
@@ -596,6 +605,15 @@ class TextRepr(Repr):
else:
return cram(stripid(repr(x)), self.maxother)
+ def repr_string(self, x, level):
+ test = cram(x, self.maxstring)
+ testrepr = repr(test)
+ if '\\' in test and '\\' not in replace(testrepr, (r'\\', '')):
+ # Backslashes are only literal in the string and are never
+ # needed to make any special characters, so show a raw string.
+ return 'r' + testrepr[0] + test + testrepr[0]
+ return testrepr
+
def repr_instance(self, x, level):
try:
return cram(stripid(repr(x)), self.maxstring)