-1

Assuming you have a python file like so

#python
#comment
x = raw_input()
exec(x)

How could you get the source of the entire file, including the comments with exec?

  • You want the source of the currently module/script? Why? And is the `exec` relevant to the question at all? If so, how? (And why are you doing it in the first place?) – abarnert May 07 '13 at 20:36
  • sounds like somebody found a hole in a web script... – MattDMo May 07 '13 at 20:59

3 Answers3

2

This is exactly what the inspect module is there for. See the Retrieving source code section in particular.

If you're trying to get the source of the currently-running module:

thismodule = sys.modules[__name__]
inspect.getsource(thismodule)
abarnert
  • 354,177
  • 51
  • 601
  • 671
1

If you're not totally bound to using exec, this is simple:

print open(__file__).read()
Andenthal
  • 849
  • 5
  • 13
  • While it might be considered cheating, I consider this a great example of a quine. http://en.wikipedia.org/wiki/Quine_(computing) And answers the question, too! – Tim S. May 07 '13 at 20:33
  • Except that this can't possibly read zipped/frozen/etc. source files… and may read a binary file as if it were text. – abarnert May 07 '13 at 20:35
0

Not sure what you are planning to use this for, but I have been using this to reduce work required to maintain my command line scripts. I always used open(_file_,'r')

'''
Head comments ...
'''
.
.
. 
def getheadcomments():
    """
    This function will make a string from the text between the first and 
    second ''' encountered. Its purpose is to make maintenance of the comments
    easier by only requiring one change for the main comments. 
    """
    desc_list = []
    start_and_break = "'''"
    read_line_bool = False
    #Get self name and read self line by line. 
    for line in open(__file__,'r'):
        if read_line_bool:
            if not start_and_break in line:
                desc_list.append(line)
            else:
                break    
        if (start_and_break in line) and read_line_bool == False:
            read_line_bool = True
    return ''.join(desc_list)
.
.
.
parser = argparse.ArgumentParser(description=getheadcomments())  

This way your comments at the top of the program will be output when your run the program from command line with the --help option.

tweirick
  • 171
  • 5
  • Why? `inspect.getdoc`, will give you the docstring of a module (or any other object). It will handle double-quoted docstrings, and probably various other things that your code won't, and it runs it all through `cleandoc`. – abarnert May 07 '13 at 20:38
  • Also… isn't this just a really complicated way to write `split`? – abarnert May 07 '13 at 20:41
  • Well this is the first time I have heard about the inspect module. I agree that is a better solution. – tweirick May 07 '13 at 20:45
  • Haha, I feel dumb now. I have have problems with text processing functions failing silently on large data sets in the past. So I am paranoid about using them for large amounts of text. None of my programs are anywhere near large though. Thanks for the criticism. – tweirick May 07 '13 at 20:51
  • I've never heard of text processing functions in Python failing silently on large data sets. Not even back in the 1.x days. (I have, of course, heard of memory errors, other exceptions, even segfaults…) But complicated custom code that calls a dozen functions instead of one? I've heard of that failing all the time… like every time I check anything in and pass it to users or QA. :) – abarnert May 07 '13 at 20:55