From c3e1e90221d65f6c6db1fb0b65bd48205674d7f3 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 7 Jun 2014 16:44:00 -0700 Subject: don't remove self from example code in the HTML output (closes #13223) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patch by Víctor Terrón. --- Lib/pydoc.py | 9 +++++++-- Lib/test/pydoc_mod.py | 10 ++++++++++ Lib/test/test_pydoc.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ Misc/NEWS | 3 +++ 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 160ad4a..254fe40 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -581,10 +581,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 9c53324..aa93a33 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 5c62f68..7ff8d6d 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -38,6 +38,7 @@ FILE CLASSES __builtin__.object B + C A \x20\x20\x20\x20 class A @@ -59,6 +60,26 @@ CLASSES | Data and other attributes defined here: |\x20\x20 | NO_MEANING = 'eggs' +\x20\x20\x20\x20 + class C(__builtin__.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() @@ -108,6 +129,7 @@ expected_html_pattern = \
B +
C
A @@ -142,6 +164,28 @@ expected_html_pattern = \ Data and other attributes defined here:
NO_MEANING = 'eggs'
+

+ + + +\x20\x20\x20\x20 + +
 
+class C(__builtin__.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 d7510b5..155811e 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -28,6 +28,9 @@ Library - Issue #21304: Backport the key derivation function hashlib.pbkdf2_hmac from Python 3 per PEP 466. +- Issue #13223: Fix pydoc.writedoc so that the HTML documentation for methods + that use 'self' in the example code is generated correctly. + - Issue #21552: Fixed possible integer overflow of too long string lengths in the tkinter module on 64-bit platforms. -- cgit v0.12