1
1
from __future__ import annotations
2
2
3
3
import gc
4
+ import multiprocessing
4
5
import weakref
5
6
6
7
import pytest
12
13
XFAIL_REASON = "Known issues: https://github.com/pybind/pybind11/pull/5654"
13
14
14
15
16
+ try :
17
+ spawn_context = multiprocessing .get_context ("spawn" )
18
+ except ValueError :
19
+ spawn_context = None
20
+
21
+
15
22
@pytest .mark .skipif (
16
23
pybind11_tests .cpp_std_num < 14 ,
17
24
reason = "std::{unique_ptr,make_unique} not available in C++11" ,
@@ -34,16 +41,13 @@ def test_no_init():
34
41
vec .__init__ ()
35
42
36
43
37
- # The test might succeed on some platforms with some compilers, but
38
- # it is not guaranteed to work everywhere. It is marked as xfail.
39
- @pytest .mark .xfail (reason = XFAIL_REASON , raises = SystemError , strict = False )
40
- def test_manual_new ():
44
+ def manual_new_target ():
41
45
# Repeatedly trigger allocation without initialization (raw malloc'ed) to
42
46
# detect uninitialized memory bugs.
43
47
for _ in range (32 ):
44
48
# The holder is a pointer variable while the C++ ctor is not called.
45
49
vec = m .VectorOwns4PythonObjects .__new__ (m .VectorOwns4PythonObjects )
46
- if vec .is_empty ():
50
+ if vec .is_empty (): # <= this line could cause a segfault
47
51
# The C++ compiler initializes container correctly.
48
52
assert vec .size () == 0
49
53
else :
@@ -56,6 +60,23 @@ def test_manual_new():
56
60
vec .append (4 )
57
61
58
62
63
+ # This test might succeed on some platforms with some compilers, but it is not
64
+ # guaranteed to work everywhere. It is marked as non-strict xfail.
65
+ @pytest .mark .xfail (reason = XFAIL_REASON , raises = SystemError , strict = False )
66
+ @pytest .mark .skipif (spawn_context is None , reason = "spawn context not available" )
67
+ def test_manual_new ():
68
+ process = spawn_context .Process (
69
+ target = manual_new_target ,
70
+ name = "manual_new_target" ,
71
+ )
72
+ process .start ()
73
+ process .join ()
74
+ if process .exitcode != 0 :
75
+ raise SystemError (
76
+ "Segmentation Fault: The C++ compiler initializes container incorrectly."
77
+ )
78
+
79
+
59
80
@pytest .mark .skipif ("env.PYPY or env.GRAALPY" , reason = "Cannot reliably trigger GC" )
60
81
@pytest .mark .skipif (
61
82
pybind11_tests .cpp_std_num < 14 ,
0 commit comments