Python's REPL reads input line by line. However, function definitions consist from multiple lines.
For example:
>>> def answer():
... return 42
...
>>> answer()
42
How does CPython's parser request additional input after partial def answer(): line?
TLDR: Digging into source code of CPython, I figured out that lazy lexer outputs
>>>and...promts.pymain_replfunction:Which sets name of compiled file to
"<stdin>"."<stdin>", then_PyRun_InteractiveLoopObjectwill be called. It's the REPL loop itself. Also, here>>>and...are loaded to some global state.PyRun_InteractiveOneObjectExreads, parses, compiles and runs single python's objecttok_underflow_interactivefunction, that requests tokens with prompt throughPyOS_Readline(stdin, stdout, tok->prompt)callP.S: The 'Your Guide to the CPython Source Code' article was really helpful. But beware - linked source code is coming from an older branch.