Title: inspect.findsource() should look only for sources
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.1
Status: closed Resolution: duplicate
Dependencies: Superseder: inspect.findsource() returns binary data for shared library modules
View: 4050
Assigned To: Nosy List: ajaksu2, hdima
Priority: normal Keywords: patch

Created on 2009-04-12 18:29 by hdima, last changed 2009-04-25 20:51 by ajaksu2. This issue is now closed.

File name Uploaded Description Edit
inspect.patch hdima, 2009-04-12 18:29 Patch for Lib/
Messages (2)
msg85916 - (view) Author: Dmitry Vasiliev (hdima) Date: 2009-04-12 18:29
Currently help(zlib) gives the following traceback:

Python 3.1a2+ (py3k:71538M, Apr 12 2009, 21:54:44) 
>>> import zlib
>>> help(zlib)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "Lib/", line 429, in __call__
    return*args, **kwds)
  File "Lib/", line 1709, in __call__
  File "Lib/", line 1755, in help
    else: doc(request, 'Help on %s:')
  File "Lib/", line 1505, in doc
    pager(render_doc(thing, title, forceload))
  File "Lib/", line 1500, in render_doc
    return title % desc + '\n\n' + text.document(object, name)
  File "Lib/", line 320, in document
    if inspect.ismodule(object): return self.docmodule(*args)
  File "Lib/", line 1086, in docmodule
    contents.append(self.document(value, key, name))
  File "Lib/", line 321, in document
    if inspect.isclass(object): return self.docclass(*args)
  File "Lib/", line 1131, in docclass
    doc = getdoc(object)
  File "Lib/", line 81, in getdoc
    result = inspect.getdoc(object) or inspect.getcomments(object)
  File "Lib/", line 581, in getcomments
    lines, lnum = findsource(object)
  File "Lib/", line 524, in findsource
    lines = linecache.getlines(file, module.__dict__)
  File "Lib/", line 41, in getlines
    return updatecache(filename, module_globals)
  File "Lib/", line 127, in updatecache
    lines = fp.readlines()
  File "Lib/", line 300, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 41-42:
invalid data

After some investigation I've found that inspect.findsource() try to use
getfile() if getsourcefile() fail to find the source file. I don't see
why findsource() should return lines from a compiled file so I think
it's a bug. The same bug also exists in Python 2.7 although it's not so
critical. I've attached the patch which fixes the problem.
msg86550 - (view) Author: Daniel Diniz (ajaksu2) (Python triager) Date: 2009-04-25 20:51
This is a duplicate of issue 4050.
Date User Action Args
2009-04-25 20:51:20ajaksu2setstatus: open -> closed

nosy: + ajaksu2
messages: + msg86550

superseder: inspect.findsource() returns binary data for shared library modules
resolution: duplicate
2009-04-14 02:51:16ajaksu2setpriority: normal
type: crash -> behavior
stage: test needed
2009-04-12 18:29:11hdimacreate