classification
Title: inspect.getsource fails to read a file of only comments
Type: behavior Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Pam.McANulty, Sean.Grider, benjamin.peterson, r.david.murray, yselivanov
Priority: normal Keywords:

Created on 2012-04-03 13:51 by Sean.Grider, last changed 2016-12-23 19:38 by Pam.McANulty. This issue is now closed.

Messages (6)
msg157417 - (view) Author: Sean Grider (Sean.Grider) Date: 2012-04-03 13:51
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
msg222245 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-04 00:15
@Sean we're sorry for the delay in getting back to you.
msg283881 - (view) Author: Sean Grider (Sean.Grider) Date: 2016-12-23 14:21
I had forgotten all about this bug until I saw an email from Pam today.

The appears to still be some delay.
msg283894 - (view) Author: Pam McA'Nulty (Pam.McANulty) * Date: 2016-12-23 18:37
I just clicked "Random Issue" and it seemed to be an "easy" (ish) one - which is just what I'd like to tackle over xmas break :)
msg283897 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-12-23 19:22
Well, there's nobody being paid for keeping track of bugs and responding, so things do slip through the cracks.  Pinging an issue after there's been a lack of response for a while is appropriate, if you notice it yourself :)  (Mark, while he was trying to be helpful, does not speak for the Python community.)

As far as the question of is this intentional, the answer is yes.  Presumably you are calling it on the 'main' function.  If you call it on the module, you will get all source lines from that module.  (You can't call it "on the file", the argument must be an object.)

You'll see why it is intentional for the 'main' case if you consider that while in Python whitespace is syntactically significant, this is *not* true for Python comments indentation, because comments are treated as if they *are* whitespace.  getsourcelines stops looking at sourcelines on the last line recorded in the lnotab for that function, so any subsequent comments are not considered part of the function.

So, I'm closing this as "not a bug" since it works as expected for me in both python 2.7 and 3.6.

Thanks for waking the issue up, Pam.  You have contributed to python by getting an open issue closed, but you'll have to find something else to work on during the break I'm afraid.
msg283898 - (view) Author: Pam McA'Nulty (Pam.McANulty) * Date: 2016-12-23 19:38
Yeah, I looked at the code and saw what you described, David.  I think I'll see if there's a good place to mention this constraint in the docs and then I'll find another one (besides the macOS build issue I ran into when trying to build the latest master)
History
Date User Action Args
2016-12-23 19:38:41Pam.McANultysetmessages: + msg283898
2016-12-23 19:22:23r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg283897

resolution: not a bug
stage: resolved
2016-12-23 18:37:34Pam.McANultysetmessages: + msg283894
2016-12-23 15:28:13BreamoreBoysetnosy: - BreamoreBoy
2016-12-23 14:21:39Sean.Gridersetmessages: + msg283881
2016-12-23 13:18:19Pam.McANultysetnosy: + Pam.McANulty
2014-07-04 00:15:15BreamoreBoysetnosy: + BreamoreBoy, yselivanov
messages: + msg222245
2012-04-03 14:07:02pitrousetnosy: + benjamin.peterson
2012-04-03 13:51:53Sean.Gridercreate