Skip to content

Push PyPy3 compatibility to 100% #58

Open

Description

@Technologicat

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.
  • Macros? Is the ast close enough to CPython's?
    • Yes, macros work.
  • unpythonic.misc.async_raise? This may or may not work, depending on what is available in PyPy3's ctypes module. (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._jump doesn't (really) matter, since its only purpose is to print a warning.
    • The one in unpythonic.dynassign._DynLiveView is more serious. How to get around it? A with block is no solution here, since the _DynLiveView instance is returned by unpythonic.dyn.asdict, so it has indefinite dynamic extent, depending on the code that calls asdict. Requiring users to call a .close() method explicitly just invites omission by accident or even on purpose (e.g. who ever calls socket.shutdown()?). OTOH, maybe _DynLiveView could be a singleton? The dynamic scopes are already context-managed, and that's really all that matters. _DynLiveView singleton FTW. 1a0f0f5. 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.
  • Anything else? Add more details here. Maybe refer to Python compatibility and Differences between PyPy and CPython.

Activity

  1. added
    enhancement
    on Jan 7, 2020
  2. added this to the ∞ milestone on Jan 7, 2020
  3. self-assigned this
    on Jan 7, 2020
  4. Technologicat commented on Jan 7, 2020

    @Technologicat
    OwnerAuthor

    Ok, so PyPy doesn't havectypes.pythonapi, but it has cpyext, which at least sounds promising. Will have to look at this a bit more.


    Hm, cpyext's pystate.py doesn't seem to have PyThreadState_SetAsyncExc. And stubs.py indeed confirms it's missing.

    In CPython, the signal stdlib module docs suggest KeyboardInterrupt usually 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 raises KeyboardInterrupt. 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.py in PyPy is what we need - if PyPy's internals allow us to implement the missing function.


    Hm. Another approach: signal.pthread_kill with __pypy__.thread.signals_enabled, and a PyPy-only with block around the call to the REPL's interact?

    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 with block (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 with block. Is PyPy's pthread_kill specific 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 finally clause inside the decorator; both the OS and Python are allowed to recycle thread idents.

    This approach gives us a consistent async_raise API across CPython and PyPy. Requiring to mark threads that allow async_raise keeps things explicit - no need to worry about KeyboardInterrupt in threads other than the main one (as usual!), unless marked.

    This way we can also know at the time async_raise is called whether the target thread advertises itself as accepting an async_raise, so we can fail-fast if given an inappropriate Thread instance.

  5. Technologicat commented on Jan 8, 2020

    @Technologicat
    OwnerAuthor

    Macros should be fine, MacroPy has been tested on PyPy.

  6. Technologicat commented on Jan 9, 2020

    @Technologicat
    OwnerAuthor

    Quickly tested on PyPy 7.3.0 (runtests.py), all tests except async_raise pass.

    REPL server/client seem to have a small problem with missing \r, when printing just \n. Need to investigate. EDIT: Implementing prompt detection in 2658ede fixed this, too.

    But mostly, it's already working, including all macros!

  7. Technologicat commented on Jan 9, 2020

    @Technologicat
    OwnerAuthor

    Meh. Tested, and it turns out pthread_kill isn'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 CL signal sense).

    So it may be we really need to implement PyThreadState_SetAsyncExc in PyPy to get async_raise to work in PyPy3... not really looking forward to that experience.

  8. Technologicat commented on Jan 12, 2020

    @Technologicat
    OwnerAuthor

    Regarding PyThreadState_SetAsyncExc:

    • Note the branch is 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.)
    • PyPy's executioncontext has a promising-sounding set_exception.
    • We already have the right function signature in stubs.py.
    • So the question is, how to grab the executioncontext of the correct thread. We get the thread ident as input, and the interpreter must store the thread contexts somewhere...

    Meh, cpyext functions aren't even exposed to the Python level, unlike CPython's ctypes.pythonapi. Maybe PyPy3 doesn't currently have a natural place for this. May need to ask the devs about it.

  9. Technologicat commented on Feb 6, 2020

    @Technologicat
    OwnerAuthor

    Alternative way to signal a particular thread: signalfd.

    See some context.

    Just need to test if it works in PyPy...

  10. changed the title [-]Make unpythonic compatible with PyPy3[/-] [+]Push PyPy3 compatibility to 100%[/+] on Feb 11, 2020
  11. Technologicat commented on Apr 24, 2026

    @Technologicat
    OwnerAuthor

    Status update (2026-04-24, post-2.1.0):

    Current state is effectively "resolved with one documented gap":

    • PyPy 3.11 is on the supported matrix (pyproject.toml). Everything works including macros, dialects, tests — except async_raise.
    • async_raise is already gated: unpythonic/excutil.py:304 raises NotImplementedError on PyPy. The REPL server falls back gracefully; PyPy users don't get remote Ctrl+C, but everything else functions.
    • User-visible consequence is narrow — only net.server's remote Ctrl+C is affected.

    Checked upstream status (2026-04-24): PyThreadState_SetAsyncExc is still a stub in PyPy's pypy/module/cpyext/stubs.py (just raise NotImplementedError), and __pypy__ exposes no equivalent primitive. So the 2020 conclusion stands: making async_raise work 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions