Search code examples
pythoncinterpreterpyc

How Python's interpreter prints text ? - The source code


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.


Solution

  • 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).