Is it possible to access the source code of a python script passed to python on standard in?

StackOverflow https://stackoverflow.com/questions/3147823

  •  01-10-2019
  •  | 
  •  

Pergunta

This is a bit of a random question that is more out of curiosity than any specific need.

Is it possible to write some python code that will print some stuff out, including the source code itself, without having the python code stored in a file? For example, doing something like this at the Bash prompt:

$ echo '
> print "The Code:"
> PrintScript() # What would this function look like?
> for i in range(5):
>     print i,
> print "!"
> ' | python

and get an output like this:

The Code:
print "The Code:"
PrintScript() # What would this function look like?
for i in range(5):
    print i,
print "!"
0 1 2 3 4 5 !

I suspect that this probably can't be done, but given python's introspection capabilities, I was curious to know whether it extended to this level.

Foi útil?

Solução

That's the closest I'm getting:

echo 'import  __main__,inspect;print inspect.getsource(__main__)' | python

which fails... In any case, the original code is eaten up (read from stdin) by the interpreter at startup. At most you may be able to get to the compiled code, again through the __main__ module.

Update:

The dis module is supposed to give you a disassembly of all functions in a module, but even that one isn't seeing any code:

$ echo -e 'import  __main__,dis;print dis.dis(__main__)' | python
None

And even when I throw in a function:

$ echo -e "import  __main__,dis;print dis.dis(__main__)\ndef x():\n pass" | python
None

Outras dicas

Yes, it is indeed possible to write a program which outputs it's own source. You don't need even introspection for this tasks, you just need to be able to print computed strings (works with every language).

The technique is called Quine and here is a rather short example in Python:

quine = 'quine = %r\r\nprint quine %% quine'
print quine % quine

But quines aren't limited to such simple programs. They can do much more, for example printing their own source backwards and so on... :)

print open(__file__).read(),

This will work on UNIX systems I think, but I'm not sure about Windows. The trailing comma makes sure that the source code is printed exactly, without an extra trailing newline.

Just realized (based on the comments below) that this does not work if your source code comes from sys.stdin, which is exactly what you were asking for. In that case, you might take advantage of some of the ideas outlined on this page about quines (programs printing their own source codes) in Python, but none of the solutions would be a single function that just works. A language-independent discussion is here.

So, in short, no, I don't think this is possible with a single function if your source code comes from the standard input. There might be a possibility to access the interpreted form of your program as a Python code object and translate that back into source form, but the translated form will almost surely not match the original file contents exactly. (For instance, the comments and the shebang line would definitely be stripped away).

closest you can get is using readline to interrogate the command history if available from what i can see e.g. but i suspect this may not contain stuff piped into the session and would only work for interactive sessions anyway

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top