0

Any Python geeks over here know how Python's interpreter exactly prints the output. I need the exact source file. So far, I found that if Python's interpreter is printing anything it is calling "PyEval_CallObject" or maybe I am wrong. Any pointers regarding this ? I want to see exactly how Python interprets a print statement i.e., write to stdout. If you could point to how Python's interpreter writes to files also would be great. Thank you for the help.

1 Answer 1

2

I'm not really a Python geek, but it's not that hard to grep the source base and find where these things are implemented.

It looks like there are several Python opcodes associated with the print statement, and they're all clustered together in ceval.c.

They all seem to delegate to the PyFile_* family of functions for writing to stdout. If you look at PyFile_WriteString you'll see that it just calls fputs.

You might also find the code for the built-in print function helpful (i.e. __builtin__.print, back-ported from Python 3).

Sign up to request clarification or add additional context in comments.

6 Comments

Fantastic. File write is easily fputs. So I am still digging to find the method for stdout print. Could it be just a regular printf statement ?
@ShankarMJ - It's also fputs. They just pass the stdout file handle to fputs. Kind of like how calling printf(...) is basically the same as fprintf(stdout, ...).
You sir get my votes :) Thank you. Sorry for throwing more questions but do you by any chance have any idea about this ? stackoverflow.com/questions/25129311/…
Interesting. I'd wondered what Python (or similar) did to handle printing strings with null bytes, but it looks like the answer is "nothing".
@jbm - Several years ago I tried embedding a null byte in the <title> of an HTML document using &#0; or something like that. At that time the current version of Firefox truncated the title at the null character! You might be surprised how many places C-string behaviors leak through...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.