From: Benjamin Peterson Date: Sat, 7 Jun 2014 23:44:00 +0000 (-0700) Subject: don't remove self from example code in the HTML output (closes #13223) X-Git-Tag: v3.4.2rc1~422 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ed1160b39c23c4b035b6c203cc6df53274d08939;p=python don't remove self from example code in the HTML output (closes #13223) Patch by Víctor Terrón. --- diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 220a3cb1cc..2dee6eee23 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -595,10 +595,15 @@ class HTMLDoc(Doc): elif pep: url = 'http://www.python.org/dev/peps/pep-%04d/' % int(pep) results.append('%s' % (url, escape(all))) + elif selfdot: + # Create a link for methods like 'self.method(...)' + # and use for attributes like 'self.attr' + if text[end:end+1] == '(': + results.append('self.' + self.namelink(name, methods)) + else: + results.append('self.%s' % name) elif text[end:end+1] == '(': results.append(self.namelink(name, methods, funcs, classes)) - elif selfdot: - results.append('self.%s' % name) else: results.append(self.namelink(name, classes)) here = end diff --git a/Lib/test/pydoc_mod.py b/Lib/test/pydoc_mod.py index f86b5c621e..cda1c9e231 100644 --- a/Lib/test/pydoc_mod.py +++ b/Lib/test/pydoc_mod.py @@ -15,6 +15,16 @@ class B(object): NO_MEANING = "eggs" pass +class C(object): + def say_no(self): + return "no" + def get_answer(self): + """ Return say_no() """ + return self.say_no() + def is_it_true(self): + """ Return self.get_answer() """ + return self.get_answer() + def doc_func(): """ This function solves all of the world's problems: diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index f26fb15f53..188c4c26fe 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -47,6 +47,7 @@ CLASSES builtins.object A B + C \x20\x20\x20\x20 class A(builtins.object) | Hello and goodbye @@ -74,6 +75,26 @@ CLASSES | Data and other attributes defined here: |\x20\x20 | NO_MEANING = 'eggs' +\x20\x20\x20\x20 + class C(builtins.object) + | Methods defined here: + |\x20\x20 + | get_answer(self) + | Return say_no() + |\x20\x20 + | is_it_true(self) + | Return self.get_answer() + |\x20\x20 + | say_no(self) + |\x20\x20 + | ---------------------------------------------------------------------- + | Data descriptors defined here: + |\x20\x20 + | __dict__ + | dictionary for instance variables (if defined) + |\x20\x20 + | __weakref__ + | list of weak references to the object (if defined) FUNCTIONS doc_func() @@ -124,6 +145,7 @@ expected_html_pattern = """
A
B +
C
@@ -165,6 +187,28 @@ Data descriptors defined here:
Data and other attributes defined here:
NO_MEANING = 'eggs'
+

+ + + +\x20\x20\x20\x20 + +
 
+class C(builtins.object)
    Methods defined here:
+
get_answer(self)
Return say_no()
+ +
is_it_true(self)
Return self.get_answer()
+ +
say_no(self)
+ +
+Data descriptors defined here:
+
__dict__
+
dictionary for instance variables (if defined)
+
+
__weakref__
+
list of weak references to the object (if defined)
+

diff --git a/Misc/NEWS b/Misc/NEWS index f581cd4580..57090b6562 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -22,6 +22,9 @@ Core and Builtins Library ------- +- Issue #13223: Fix pydoc.writedoc so that the HTML documentation for methods + that use 'self' in the example code is generated correctly. + - Issue #21463: In urllib.request, fix pruning of the FTP cache. - Issue #21618: The subprocess module could fail to close open fds that were