File: test_subinterpreter.py

package info (click to toggle)
python-ijson 3.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 668 kB
  • sloc: python: 2,687; ansic: 1,776; sh: 4; makefile: 3
file content (56 lines) | stat: -rw-r--r-- 1,368 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import importlib.util
import os
import pickle
import queue
import sys
import threading
import time

import pytest

try:
    from test.support import interpreters
    from test.support.interpreters import queues
except ImportError:
    pytest.skip("No sub-interpreter support", allow_module_level=True)


@pytest.fixture(name="interpreter")
def interpreter_fixture():
    interpreter = interpreters.create()
    yield interpreter
    interpreter.close()


def test_ijson_can_be_loaded(interpreter):
    interpreter.exec('import ijson')


def test_ijson_yajl2_backend_can_be_loaded(interpreter):
    spec = importlib.util.find_spec("ijson.backends._yajl2")
    if spec is None:
        pytest.skip("yajl2_c is not built")
    interpreter.exec('import ijson')
    interpreter.exec('ijson.get_backend("yajl2_c")')


def test_ijson_can_run(interpreter):

    queue = queues.create()
    VALUE = 43
    interpreter.prepare_main(queue_id=queue.id, value_in=VALUE)

    def ijson_in_subinterpreter():
        from test.support.interpreters.queues import Queue

        import ijson

        value_out = next(ijson.items(str(value_in).encode('ascii'), prefix=''))
        queue = Queue(queue_id)
        queue.put(value_out)

    thread = interpreter.call_in_thread(ijson_in_subinterpreter)
    value_out = queue.get(timeout=5)
    thread.join()

    assert VALUE == value_out