Skip to content

Commit 93dd46d

Browse files
author
georg.brandl
committed
Patch #4739 by David Laban: add symbols to pydoc help topics,
so that ``help('@')`` works as expected. git-svn-id: http://svn.python.org/projects/python/trunk@67953 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 29bb6ee commit 93dd46d

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

Diff for: Lib/pydoc.py

+55-2
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,42 @@ class Helper:
15741574
'with': ('with', 'CONTEXTMANAGERS EXCEPTIONS yield'),
15751575
'yield': ('yield', ''),
15761576
}
1577+
# Either add symbols to this dictionary or to the symbols dictionary
1578+
# directly: Whichever is easier. They are merged later.
1579+
_symbols_inverse = {
1580+
'STRINGS' : ("'", "'''", "r'", "u'", '"""', '"', 'r"', 'u"'),
1581+
'OPERATORS' : ('+', '-', '*', '**', '/', '//', '%', '<<', '>>', '&',
1582+
'|', '^', '~', '<', '>', '<=', '>=', '==', '!=', '<>'),
1583+
'COMPARISON' : ('<', '>', '<=', '>=', '==', '!=', '<>'),
1584+
'UNARY' : ('-', '~'),
1585+
'AUGMENTEDASSIGNMENT' : ('+=', '-=', '*=', '/=', '%=', '&=', '|=',
1586+
'^=', '<<=', '>>=', '**=', '//='),
1587+
'BITWISE' : ('<<', '>>', '&', '|', '^', '~'),
1588+
'COMPLEX' : ('j', 'J')
1589+
}
1590+
symbols = {
1591+
'%': 'OPERATORS FORMATTING',
1592+
'**': 'POWER',
1593+
',': 'TUPLES LISTS FUNCTIONS',
1594+
'.': 'ATTRIBUTES FLOAT MODULES OBJECTS',
1595+
'...': 'ELLIPSIS',
1596+
':': 'SLICINGS DICTIONARYLITERALS',
1597+
'@': 'def class',
1598+
'\\': 'STRINGS',
1599+
'_': 'PRIVATENAMES',
1600+
'__': 'PRIVATENAMES SPECIALMETHODS',
1601+
'`': 'BACKQUOTES',
1602+
'(': 'TUPLES FUNCTIONS CALLS',
1603+
')': 'TUPLES FUNCTIONS CALLS',
1604+
'[': 'LISTS SUBSCRIPTS SLICINGS',
1605+
']': 'LISTS SUBSCRIPTS SLICINGS'
1606+
}
1607+
for topic, symbols_ in _symbols_inverse.iteritems():
1608+
for symbol in symbols_:
1609+
topics = symbols.get(symbol, topic)
1610+
if topic not in topics:
1611+
topics = topics + ' ' + topic
1612+
symbols[symbol] = topics
15771613

15781614
topics = {
15791615
'TYPES': ('types', 'STRINGS UNICODE NUMBERS SEQUENCES MAPPINGS '
@@ -1717,10 +1753,12 @@ def help(self, request):
17171753
if type(request) is type(''):
17181754
if request == 'help': self.intro()
17191755
elif request == 'keywords': self.listkeywords()
1756+
elif request == 'symbols': self.listsymbols()
17201757
elif request == 'topics': self.listtopics()
17211758
elif request == 'modules': self.listmodules()
17221759
elif request[:8] == 'modules ':
17231760
self.listmodules(split(request)[1])
1761+
elif request in self.symbols: self.showsymbol(request)
17241762
elif request in self.keywords: self.showtopic(request)
17251763
elif request in self.topics: self.showtopic(request)
17261764
elif request: doc(request, 'Help on %s:')
@@ -1766,14 +1804,22 @@ def listkeywords(self):
17661804
''')
17671805
self.list(self.keywords.keys())
17681806

1807+
def listsymbols(self):
1808+
self.output.write('''
1809+
Here is a list of the punctuation symbols which Python assigns special meaning
1810+
to. Enter any symbol to get more help.
1811+
1812+
''')
1813+
self.list(self.symbols.keys())
1814+
17691815
def listtopics(self):
17701816
self.output.write('''
17711817
Here is a list of available topics. Enter any topic name to get more help.
17721818
17731819
''')
17741820
self.list(self.topics.keys())
17751821

1776-
def showtopic(self, topic):
1822+
def showtopic(self, topic, more_xrefs=''):
17771823
try:
17781824
import pydoc_topics
17791825
except ImportError:
@@ -1787,7 +1833,7 @@ def showtopic(self, topic):
17871833
self.output.write('no documentation found for %s\n' % repr(topic))
17881834
return
17891835
if type(target) is type(''):
1790-
return self.showtopic(target)
1836+
return self.showtopic(target, more_xrefs)
17911837

17921838
label, xrefs = target
17931839
try:
@@ -1796,13 +1842,20 @@ def showtopic(self, topic):
17961842
self.output.write('no documentation found for %s\n' % repr(topic))
17971843
return
17981844
pager(strip(doc) + '\n')
1845+
if more_xrefs:
1846+
xrefs = (xrefs or '') + ' ' + more_xrefs
17991847
if xrefs:
18001848
import StringIO, formatter
18011849
buffer = StringIO.StringIO()
18021850
formatter.DumbWriter(buffer).send_flowing_data(
18031851
'Related help topics: ' + join(split(xrefs), ', ') + '\n')
18041852
self.output.write('\n%s\n' % buffer.getvalue())
18051853

1854+
def showsymbol(self, symbol):
1855+
target = self.symbols[symbol]
1856+
topic, _, xrefs = target.partition(' ')
1857+
self.showtopic(topic, xrefs)
1858+
18061859
def listmodules(self, key=''):
18071860
if key:
18081861
self.output.write('''

Diff for: Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ Core and Builtins
8686
Library
8787
-------
8888

89+
- Issue #4739: Add pydoc help topics for symbols, so that e.g. help('@')
90+
works as expected in the interactive environment.
91+
8992
- Issue #4756: zipfile.is_zipfile() now supports file-like objects. Patch by
9093
Gabriel Genellina.
9194

0 commit comments

Comments
 (0)