| 1 |
import subprocess |
| 2 |
import sys |
| 3 |
|
| 4 |
|
| 5 |
def _get_default_context_attr(platform, attr): |
| 6 |
"""Run a subprocess that patches sys.platform before importing billiard. |
| 7 |
|
| 8 |
This exercises the actual import-time conditional without corrupting |
| 9 |
the module state of the current process. |
| 10 |
""" |
| 11 |
result = subprocess.run( |
| 12 |
[sys.executable, '-c', |
| 13 |
'import unittest.mock; ' |
| 14 |
f'unittest.mock.patch("sys.platform", {platform!r}).start(); ' |
| 15 |
'import billiard.context; ' |
| 16 |
f'print(getattr(billiard.context._default_context, {attr!r})())'], |
| 17 |
capture_output=True, text=True, |
| 18 |
) |
| 19 |
assert result.returncode == 0, result.stderr |
| 20 |
return result.stdout.strip() |
| 21 |
|
| 22 |
|
| 23 |
class test_default_context_darwin: |
| 24 |
"""Tests that macOS defaults to spawn, matching CPython (bpo-33725).""" |
| 25 |
|
| 26 |
def test_default_start_method_is_spawn_on_darwin(self): |
| 27 |
method = _get_default_context_attr('darwin', 'get_start_method') |
| 28 |
assert method == 'spawn' |
| 29 |
|
| 30 |
def test_default_start_method_is_fork_on_linux(self): |
| 31 |
method = _get_default_context_attr('linux', 'get_start_method') |
| 32 |
assert method == 'fork' |
| 33 |
|
| 34 |
def test_set_start_method_override_on_darwin(self): |
| 35 |
result = subprocess.run( |
| 36 |
[sys.executable, '-c', |
| 37 |
'import unittest.mock; ' |
| 38 |
'unittest.mock.patch("sys.platform", "darwin").start(); ' |
| 39 |
'import billiard.context; ' |
| 40 |
'ctx = billiard.context._default_context; ' |
| 41 |
'assert ctx.get_start_method() == "spawn"; ' |
| 42 |
'ctx.set_start_method("fork", force=True); ' |
| 43 |
'print(ctx.get_start_method())'], |
| 44 |
capture_output=True, text=True, |
| 45 |
) |
| 46 |
assert result.returncode == 0, result.stderr |
| 47 |
assert result.stdout.strip() == 'fork' |
| 48 |
|
| 49 |
def test_forking_is_enabled_false_on_darwin(self): |
| 50 |
enabled = _get_default_context_attr('darwin', 'forking_is_enabled') |
| 51 |
assert enabled == 'False' |
| 52 |
|
| 53 |
def test_forking_is_enabled_true_on_linux(self): |
| 54 |
enabled = _get_default_context_attr('linux', 'forking_is_enabled') |
| 55 |
assert enabled == 'True' |