Skip to content

Commit

Permalink
Add tests for sub-interpreter support
Browse files Browse the repository at this point in the history
These are flaky as they use internal CPython packages, but that will do
until the "interpreters" module is made eventually official.

Signed-off-by: Rodrigo Tobar <[email protected]>
  • Loading branch information
rtobar committed Sep 23, 2024
1 parent ba86d34 commit f691713
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions test/test_subinterpreter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import contextlib
from typing import Iterable, Tuple, no_type_check

import pytest

import crc32c

try:
from test.support.interpreters import Interpreter # type: ignore
from test.support.interpreters import channels, create
except ImportError:
pytest.skip("No sub-interpreter support", allow_module_level=True)


Channels = Tuple[channels.RecvChannel, channels.SendChannel]


@pytest.fixture(name="interpreter")
def interpreter_fixture() -> Iterable[Interpreter]:
interpreter = create()
with contextlib.closing(interpreter):
yield interpreter


@pytest.fixture(name="channels")
def channels_fixture() -> Iterable[Channels]:
recv, send = channels.create()
yield recv, send
recv.close()
if not send.is_closed:
send.close()


def test_crc32c_can_be_loaded(interpreter: Interpreter) -> None:
interpreter.exec("import crc32c")
# all good, no exception raised


@pytest.mark.calculates_crc32c
def test_crc32c_can_run(interpreter: Interpreter, channels: Channels) -> None:

recv_channel, send_channel = channels
VALUE = b"test"
interpreter.prepare_main(channel_id=send_channel.id, value_in=VALUE)

@no_type_check
def crc32c_in_subinterpreter() -> None:
from test.support.interpreters.channels import SendChannel # type: ignore

import crc32c

value_out = crc32c.crc32c(value_in)
SendChannel(channel_id).send(value_out)

thread = interpreter.call_in_thread(crc32c_in_subinterpreter)
result = recv_channel.recv()
thread.join()
assert result == crc32c.crc32c(VALUE)

0 comments on commit f691713

Please sign in to comment.