|
| 1 | ++++++++++++++++ |
| 2 | +Python runtimes |
| 3 | ++++++++++++++++ |
| 4 | + |
| 5 | +To be able to implement :ref:`backward compatibility <back-compat>`, the plan |
| 6 | +is to provide multiple Python runtimes, at least two. An "old" runtime with |
| 7 | +maximum backward compatibility, and one "new" runtime which includes |
| 8 | +experimental new changes but requires using the newer C API. |
| 9 | + |
| 10 | +Runtimes |
| 11 | +======== |
| 12 | + |
| 13 | +Regular Python: /usr/bin/python3 |
| 14 | +-------------------------------- |
| 15 | + |
| 16 | +* Python compiled in release mode |
| 17 | +* This runtime still provides ``Py_INCREF()`` macro: |
| 18 | + modify ``PyObject.ob_refcnt`` at the ABI level. |
| 19 | +* Compatible with Python 3.7 C **API**. |
| 20 | +* Should be compatible with Python 3.7 **ABI**. |
| 21 | + |
| 22 | +Compared to Python 3.7 regular runtime, this runtime no longer check its |
| 23 | +arguments. The debug runtime should now be preferred to develop C extensions |
| 24 | +and to run tests. |
| 25 | + |
| 26 | +Example of Python 3.7 code:: |
| 27 | + |
| 28 | + int |
| 29 | + PyList_Append(PyObject *op, PyObject *newitem) |
| 30 | + { |
| 31 | + if (PyList_Check(op) && (newitem != NULL)) |
| 32 | + return app1((PyListObject *)op, newitem); |
| 33 | + PyErr_BadInternalCall(); |
| 34 | + return -1; |
| 35 | + } |
| 36 | + |
| 37 | +The ``if (PyList_Check(op) && (newitem != NULL))`` belongs to the debug runtime |
| 38 | +and should be removed from the regular Python. |
| 39 | + |
| 40 | +Debug runtime: /usr/bin/python3-dbg |
| 41 | +----------------------------------- |
| 42 | + |
| 43 | +* Compatible with Python 3.7 C **API**. |
| 44 | +* Compatible with regular runtime 3.8 **ABI**, but **not compatible** |
| 45 | + with regular runtime 3.7 ABI. |
| 46 | +* CPython compiled with ``./configure --with-pydebug`` |
| 47 | +* Provide ``sys.gettotalrefcount()`` which allows to check for reference leaks. |
| 48 | +* C function calls check most arguments: check type, value range, etc. |
| 49 | +* Runtime compiled with C assertion: crash (kill itself with SIGABRT signal) |
| 50 | + if a C assertion fails (``assert(...);``). |
| 51 | +* Use the debug hooks on memory allocators by default, as ``PYTHONDEBUG=debug`` |
| 52 | + environment variable: detect memory under- and overflow and misusage of |
| 53 | + memory allocators. |
| 54 | +* Compiled without compiler optimizations (``-Og`` or even ``-O0``) to be |
| 55 | + usable with a debugger like ``gdb``: python-gdb.py should work perfectly. |
| 56 | + However, the regular runtime is unusable with gdb since most variables and |
| 57 | + function arguments are stored in registers, and so gdb fails with the |
| 58 | + "<optimized out>" message. |
| 59 | + |
| 60 | +Experimental runtime: experimental-python3 |
| 61 | +------------------------------------------ |
| 62 | + |
| 63 | +* Loading a C extension compiled with Python 3.7 must fail. |
| 64 | +* Loading a C extension compiled with the Python C API 3.8 in the backward |
| 65 | + compatible mode must fail. |
| 66 | +* Only C extensions compiled with the **new** Python C API 3.8 can be loaded. |
| 67 | + You have to **opt-in** for this runtime. |
| 68 | +* Not compatible with Python 3.7 API: PyDict_GetItem() is gone, |
| 69 | + PyDict_GetItemRef() must be used instead. |
| 70 | +* Not compatible with Python 3.8 ABI: using Py_INCREF() macro uses |
| 71 | + ``PyObject.ob_refcnt`` at the ABI level, whereas this field must **not** be |
| 72 | + access at the ABI level. |
| 73 | +* ``Py_GC`` header and ``PyObject`` structure can be very different from the |
| 74 | + one used by the regular and debug runtimes. |
| 75 | + |
| 76 | +Technically, this experimental runtime is likely to be a opt-in compilation |
| 77 | +mode of the upstream CPython code base. |
| 78 | + |
| 79 | +Your runtime |
| 80 | +------------ |
| 81 | + |
| 82 | +Since a :ref:`stable ABI <stable-abi>` have been designed, if all your C |
| 83 | +extensions have opt-in for the new C API: you are now allowed to fork CPython |
| 84 | +and experiment your own flavor CPython. Do whatever you want: C extensions only |
| 85 | +access your runtime through runtime. |
| 86 | + |
| 87 | +PyPy, RustPython and others |
| 88 | +--------------------------- |
| 89 | + |
| 90 | +Since the ABI (and the C API) became smaller, you can imagine that Python |
| 91 | +implementations other than CPython will be able to more easily have a **full |
| 92 | +and up-to-date support** of the latest full C API. |
| 93 | + |
| 94 | +**Welcome into the bright world of multiple new cool and compatible Python |
| 95 | +runtimes!** |
0 commit comments