[New-bugs-announce] [issue14483] inspect.getsource fails to read a file of only comments

Sean Grider report at bugs.python.org
Tue Apr 3 15:51:54 CEST 2012


New submission from Sean Grider <sean.grider at gmail.com>:

I have a custom parser that generates html files to describe what python scripts do depending on their source comments. I use inspect.getsourcelines to parse out different comments styles (I use #@, #@@ and #$ to signal custom comments)

I recently found that getsource and getsourcelines return nothing if the file contains nothing other than a def main and it's comments.

It seems that getsource stops reading after the last non-comment line. For example:

def main():
  """ description """
  #comment1
  #comment2
  #comment3

getsource on the above file will return def main and the doc_string but nothing else.

if however I change it to:
def main():
  """ description """
  #comment1
  #comment2
  #comment3
  return

I now get the entire file, but just doing the following:
def main():
  """ description """
  #comment1
  my_var = 123
  #comment2
  #comment3

will now give me def main, the doc_string and comment1, but nothing else.

Is this expected behavior? I would think that the parser should not care if the source is comments or code, I just want to read whatever is in the file.

This behavior is present on 2.7.2 on both Windows 7x64 and RedHat 2.6.39.4-2.fc12.x86_64

----------
messages: 157417
nosy: Sean.Grider
priority: normal
severity: normal
status: open
title: inspect.getsource fails to read a file of only comments
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14483>
_______________________________________


More information about the New-bugs-announce mailing list