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.
Asked
Active
Viewed 247 times
1 Answers
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).

DaoWen
- 32,589
- 6
- 74
- 101
-
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 ? – Aug 07 '14 at 03:53
-
@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, ...)`. – DaoWen Aug 07 '14 at 04:40
-
You sir get my votes :) Thank you. Sorry for throwing more questions but do you by any chance have any idea about this ? http://stackoverflow.com/questions/25129311/how-do-virtual-machines-render-gui/ – Aug 07 '14 at 04:48
-
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 Aug 07 '14 at 21:56
-
@jbm - Several years ago I tried embedding a null byte in the `
` of an HTML document using `` 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... – DaoWen Aug 07 '14 at 23:53 -
@DaoWen, that's interesting. C's influence is pervasive indeed. Re Python, I tried (in CPython 2.7.3) `print "foo\x00bar"` and `print "foo\0bar"` just now and both printed `foobar`. It seems I'll have to dig a little deeper. – jbm Aug 08 '14 at 01:07