Repository navigation
Open
Description
PyPy3 looks promising, as it's rumored to be fast, and it supports version 3.6.9 of "the Python standard". Our main target platform just so happens to be Python 3.6.x.
Our PyPy3 compatibility is already near 100%. This issue documents what remains to be done.
- Test compatibility with PyPy. Most things should work already.
- Yes, pretty much everything already works. All tests pass except
async_raise.
- Yes, pretty much everything already works. All tests pass except
- Macros? Is the
astclose enough to CPython's?- Yes, macros work.
unpythonic.misc.async_raise? This may or may not work, depending on what is available in PyPy3'sctypesmodule. (It's only needed for remote Ctrl+C in the REPL server, but that's a nice feature to have.)- Doesn't work in PyPy, and without hacking PyPy itself, currently no way to make it work. See details below.
- Get rid of
__del__methods, since they may be delayed or not called at all, and in most cases having a__del__is just not pythonic anyway.- The one in
unpythonic.tco._jumpdoesn't (really) matter, since its only purpose is to print a warning. The one inunpythonic.dynassign._DynLiveViewis more serious. How to get around it? Awithblock is no solution here, since the_DynLiveViewinstance is returned byunpythonic.dyn.asdict, so it has indefinite dynamic extent, depending on the code that callsasdict. Requiring users to call a.close()method explicitly just invites omission by accident or even on purpose (e.g. who ever callssocket.shutdown()?). OTOH, maybe_DynLiveViewcould be a singleton? The dynamic scopes are already context-managed, and that's really all that matters.How about for the lose, instead. Can't work, since the dynamic scope stacks are thread-local. Having observers linger around a bit longer than they absolutely need to, or not having them cleaned up when the process exits, are not really a problem. In practice, the old solution should be fine in PyPy, too. Hence, reverted in 089f7cc._DynLiveViewsingleton FTW. 1a0f0f5.
- The one in
- Anything else? Add more details here. Maybe refer to Python compatibility and Differences between PyPy and CPython.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Activity
Technologicat commentedon Jan 7, 2020
Ok, so PyPy doesn't have
ctypes.pythonapi, but it hascpyext, which at least sounds promising. Will have to look at this a bit more.Hm,
cpyext'spystate.pydoesn't seem to havePyThreadState_SetAsyncExc. Andstubs.pyindeed confirms it's missing.In CPython, the
signalstdlib module docs suggestKeyboardInterruptusually works somewhat differently: the process receives an OS-level SIGINT, which is caught by the default handler - in the main Python thread, since that's the only one that can handle OS-level signals in CPython - which then raisesKeyboardInterrupt. Look ma, no async raise. The magic already happened when the main thread's execution ended up in the SIGINT handler, which then just needs to perform a regular raise.That solution is not applicable to a REPL server - we really need an async raise to trigger the exception in the desired thread (the one that's running the REPL session we want to send the Ctrl+C to). So maybe a small addition to
pystate.pyin PyPy is what we need - if PyPy's internals allow us to implement the missing function.Hm. Another approach:
signal.pthread_killwith__pypy__.thread.signals_enabled, and a PyPy-onlywithblock around the call to the REPL'sinteract?It's not as general as the current CPython solution, since it can only do OS signals, not arbitrary exceptions, but that's good enough for what we want to do here. To make it general (just for feature parity), we could provide a mechanism that usurps e.g. SIGUSR1 to asynchronously send any exception via a global dictionary acting as a mailbox, but the user code still needs to enable OS signals on PyPy (specifically for the targeted block of code in the target thread) for that to work. OTOH, this is Python, where explicit is better than implicit. OTTH, that takes up one OS signal. OTFH, we can provide a decorator that adds the PyPy-specific
withblock (no-opping in CPython), so it's as easy as decorating the appropriate function - indeed, the thread entrypoint - with it.The only unclear thing is what the PyPy docs mean by that a signal might be handled in either the main thread or one currently inside such a
withblock. Is PyPy'spthread_killspecific enough to hit the target thread? (On CPython, it's documented as not useful to try to target any Python thread but the main one.)If we go the mailbox route, we should probably keep track of thread idents that have been marked as accepting async raises. Global dict, clean up in a
finallyclause inside the decorator; both the OS and Python are allowed to recycle thread idents.This approach gives us a consistent
async_raiseAPI across CPython and PyPy. Requiring to mark threads that allowasync_raisekeeps things explicit - no need to worry aboutKeyboardInterruptin threads other than the main one (as usual!), unless marked.This way we can also know at the time
async_raiseis called whether the target thread advertises itself as accepting anasync_raise, so we can fail-fast if given an inappropriateThreadinstance.Technologicat commentedon Jan 8, 2020
Macros should be fine, MacroPy has been tested on PyPy.
Technologicat commentedon Jan 9, 2020
Quickly tested on PyPy 7.3.0 (
runtests.py), all tests exceptasync_raisepass.REPL server/client seem to have a small problem with missingEDIT: Implementing prompt detection in 2658ede fixed this, too.\r, when printing just\n. Need to investigate.But mostly, it's already working, including all macros!
Technologicat commentedon Jan 9, 2020
Meh. Tested, and it turns out
pthread_killisn't specific enough to always hit the targeted thread. The might in the docs really means that; it's basically random which signal-enabled thread gets to run the signal handler (in the sense of OS signals, not in the unrelated CLsignalsense).So it may be we really need to implement
PyThreadState_SetAsyncExcin PyPy to getasync_raiseto work in PyPy3... not really looking forward to that experience.Technologicat commentedon Jan 12, 2020
Regarding
PyThreadState_SetAsyncExc:py3.6, not the default branch. (The default branch in the PyPy repo is for continued support of 2.7, due to the RPython toolchain itself being based on a restricted dialect of 2.7.)executioncontexthas a promising-soundingset_exception.stubs.py.executioncontextof the correct thread. We get the thread ident as input, and the interpreter must store the thread contexts somewhere...Meh,
cpyextfunctions aren't even exposed to the Python level, unlike CPython'sctypes.pythonapi. Maybe PyPy3 doesn't currently have a natural place for this. May need to ask the devs about it.Technologicat commentedon Feb 6, 2020
Alternative way to signal a particular thread: signalfd.
See some context.
Just need to test if it works in PyPy...
[-]Make unpythonic compatible with PyPy3[/-][+]Push PyPy3 compatibility to 100%[/+]Technologicat commentedon Apr 24, 2026
Status update (2026-04-24, post-2.1.0):
Current state is effectively "resolved with one documented gap":
pyproject.toml). Everything works including macros, dialects, tests — exceptasync_raise.async_raiseis already gated:unpythonic/excutil.py:304raisesNotImplementedErroron PyPy. The REPL server falls back gracefully; PyPy users don't get remote Ctrl+C, but everything else functions.net.server's remote Ctrl+C is affected.Checked upstream status (2026-04-24):
PyThreadState_SetAsyncExcis still a stub in PyPy'spypy/module/cpyext/stubs.py(justraise NotImplementedError), and__pypy__exposes no equivalent primitive. So the 2020 conclusion stands: makingasync_raisework on PyPy requires upstream PyPy work, not something unpythonic can do from the outside.Keeping this open at ∞ as the long-running tracker for PyPy-specific quirks. New concrete compatibility issues, if any, can be filed as subissues.
— Claude (Opus 4.7)