2

Let's take the following python function:

def add(x,y):
    return x+y

Disassembling it gives me:

dis.dis(add)
2           0 LOAD_FAST                0 (x)
            2 LOAD_FAST                1 (y)
            4 BINARY_ADD
            6 RETURN_VALUE

This seems pretty similar to the (non-optimized) assembly instructions that would be run on an addition, something along the lines of:

mov %rdi, %rax   # LOAD_FAST x
mov %rsi, %rbx   # LOAD_FAST y
add %rbx, %rax   # BINARY_ADD
mov %rax, %rax   # RETURN_VALUE, just including here to be explicit

My question is what does pythondo once it has the byte code. Does it run it on the machine or are there various C function that it calls to do each of the instructions? For example, the above would be translated into:

{
    ...
    int(?) x = 0;
    int(?) y = 1;
    int(?) tmp = y+x;
    return tmp;
}

What would be a good resource to learn this (for example, the C file(s) where the actual evaluation/execution is done)?


There is an interesting answer here about how it is done in CPython: https://stackoverflow.com/a/19917906/651174. Do all implementations do this somewhat similarly, or does this vary? How much does the python standard require (or not at all)?

David542
  • 104,438
  • 178
  • 489
  • 842
  • 3
    The details will depend in the implementation of the interpreter — it's not defined by the language specification. So, to answer it you would need to look at the source code of the implementation you're interested in. – martineau Apr 06 '21 at 23:57
  • @martineau that's interesting. Does the specification say anything about the implementation, or the implementation is free to do whatever it wants? Do most of them take similar routes? – David542 Apr 07 '21 at 00:00
  • 1
    I don't recall ever seeing details like that in the language-spec. Sometimes in the documentation there will be mention of the details of how the CPython interpreter does things (an implicit warning not to count on it being that way everywhere). Frankly I'm not familiar with many other implementations. There's even one implements in Python itself called [pypy](https://www.pypy.org/) (although doing something like that actually isn't that uncommon). – martineau Apr 07 '21 at 00:11
  • 3
    CPython runs the bytecode on a bytecode interpreter. None of this is part of the Python language itself, the fact that CPython uses bytecode is *entirely an implementation detail*. – juanpa.arrivillaga Apr 07 '21 at 00:13
  • Related [What exactly is the python interpreter implemented with?](https://stackoverflow.com/questions/21717660/what-exactly-is-the-python-interpreter-implemented-with) – martineau Apr 07 '21 at 00:13
  • @martineau it looks like pypy executes against the cpu? https://github.com/mozillazg/pypy/blob/11af013b3723c15c9ce51f68fd5c2db51f74ba3d/rpython/jit/backend/x86/rx86.py – David542 Apr 07 '21 at 00:26
  • Yes, I guess it does (because it's actually Python implemented in terms of [RPython](https://rpython.readthedocs.io/en/latest/faq.html#what-is-this-rpython-language) which is a restricted subset of the Python language which itself looks like it gets converted into different flavors of machine code). As I said earlier, I'm not that well-acquainted with the details of most implementations other than CPython). – martineau Apr 07 '21 at 00:38

0 Answers0