-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathtest_zintegration.py
More file actions
204 lines (183 loc) · 7.59 KB
/
Copy pathtest_zintegration.py
File metadata and controls
204 lines (183 loc) · 7.59 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import os, sys, shutil
import subprocess
import sysconfig
import textwrap
from pathlib import Path
from testing.udir import udir
import pytest
if sys.platform == 'win32':
pytestmark = pytest.mark.skip('snippets do not run on win32')
if sys.version_info < (2, 7):
pytestmark = pytest.mark.skip(
'fails e.g. on a Debian/Ubuntu which patches virtualenv'
' in a non-2.6-friendly way')
def create_venv(name):
tmpdir = udir / name
try:
# FUTURE: we should probably update this to use venv for at least more modern Pythons, and
# install setuptools/pip/etc explicitly for the tests that require them (as venv has stopped including
# setuptools by default for newer versions).
subprocess.check_call(['virtualenv',
#'--never-download', <= could be added, but causes failures
# in random cases on random machines
'-p', os.path.abspath(sys.executable),
str(tmpdir)])
# Newer venv/virtualenv no longer include setuptools by default, which
# breaks a number of these tests; ensure they're always present
subprocess.check_call([
str(tmpdir / 'bin/python'),
'-m',
'pip',
'install',
'setuptools',
'--upgrade'
])
except OSError as e:
pytest.skip("Cannot execute virtualenv: %s" % (e,))
site_packages = None
for dirpath, dirnames, filenames in os.walk(tmpdir):
if Path(dirpath).name == 'site-packages':
site_packages = dirpath
break
paths = ""
if site_packages:
try:
from cffi import _pycparser
modules = ('cffi', '_cffi_backend')
except ImportError:
modules = ('cffi', '_cffi_backend', 'pycparser')
try:
import ply
except ImportError:
pass
else:
modules += ('ply',) # needed for older versions of pycparser
paths = []
for module in modules:
target = __import__(module, None, None, [])
if not hasattr(target, '__file__'): # for _cffi_backend on pypy
continue
src = os.path.abspath(target.__file__)
for end in ['__init__.pyc', '__init__.pyo', '__init__.py']:
if src.lower().endswith(end):
src = src[:-len(end)-1]
break
paths.append(os.path.dirname(src))
paths = os.pathsep.join(paths)
return tmpdir, paths
SNIPPET_DIR = Path(__file__).absolute().parent / 'snippets'
def really_run_setup_and_program(dirname, venv_dir_and_paths, python_snippet):
venv_dir, paths = venv_dir_and_paths
def remove(dir):
dir = str(SNIPPET_DIR / dirname / dir)
shutil.rmtree(dir, ignore_errors=True)
remove('build')
remove('__pycache__')
for basedir in (SNIPPET_DIR / dirname).iterdir():
remove(basedir / '__pycache__')
olddir = os.getcwd()
python_f = udir / 'x.py'
python_f.write_text(textwrap.dedent(python_snippet))
try:
os.chdir(str(SNIPPET_DIR / dirname))
if os.name == 'nt':
bindir = 'Scripts'
else:
bindir = 'bin'
vp = str(venv_dir / bindir / 'python')
env = os.environ.copy()
env['PYTHONPATH'] = paths
subprocess.check_call((vp, 'setup.py', 'clean'), env=env)
# there's a setuptools/easy_install bug that causes this to fail when the build/install occur together and
# we're in the same directory with the build (it tries to look up dependencies for itself on PyPI);
# subsequent runs will succeed because this test doesn't properly clean up the build- use pip for now.
subprocess.check_call((vp, '-m', 'pip', 'install', '.', '--no-build-isolation'), env=env)
subprocess.check_call((vp, str(python_f)), env=env)
finally:
os.chdir(olddir)
def run_setup_and_program(dirname, python_snippet):
venv_dir = create_venv(dirname + '-cpy')
really_run_setup_and_program(dirname, venv_dir, python_snippet)
#
sys._force_generic_engine_ = True
try:
venv_dir = create_venv(dirname + '-gen')
really_run_setup_and_program(dirname, venv_dir, python_snippet)
finally:
del sys._force_generic_engine_
# the two files lextab.py and yacctab.py are created by not-correctly-
# installed versions of pycparser.
assert not (SNIPPET_DIR / dirname / 'lextab.py').exists()
assert not (SNIPPET_DIR / dirname / 'yacctab.py').exists()
@pytest.mark.thread_unsafe(reason="very slow in parallel")
class TestZIntegration:
def teardown_class(self):
if udir.is_dir():
shutil.rmtree(udir)
udir.mkdir()
def test_infrastructure(self):
run_setup_and_program('infrastructure', '''
import snip_infrastructure
assert snip_infrastructure.func() == 42
''')
def test_distutils_module(self):
run_setup_and_program("distutils_module", '''
import snip_basic_verify
p = snip_basic_verify.C.getpwuid(0)
assert snip_basic_verify.ffi.string(p.pw_name) == b"root"
''')
def test_distutils_package_1(self):
run_setup_and_program("distutils_package_1", '''
import snip_basic_verify1
p = snip_basic_verify1.C.getpwuid(0)
assert snip_basic_verify1.ffi.string(p.pw_name) == b"root"
''')
def test_distutils_package_2(self):
run_setup_and_program("distutils_package_2", '''
import snip_basic_verify2
p = snip_basic_verify2.C.getpwuid(0)
assert snip_basic_verify2.ffi.string(p.pw_name) == b"root"
''')
def test_setuptools_module(self):
run_setup_and_program("setuptools_module", '''
import snip_setuptools_verify
p = snip_setuptools_verify.C.getpwuid(0)
assert snip_setuptools_verify.ffi.string(p.pw_name) == b"root"
''')
def test_setuptools_package_1(self):
run_setup_and_program("setuptools_package_1", '''
import snip_setuptools_verify1
p = snip_setuptools_verify1.C.getpwuid(0)
assert snip_setuptools_verify1.ffi.string(p.pw_name) == b"root"
''')
def test_setuptools_package_2(self):
run_setup_and_program("setuptools_package_2", '''
import snip_setuptools_verify2
p = snip_setuptools_verify2.C.getpwuid(0)
assert snip_setuptools_verify2.ffi.string(p.pw_name) == b"root"
''')
def test_set_py_limited_api(self):
from cffi.setuptools_ext import _set_py_limited_api
try:
import setuptools
except ImportError as e:
pytest.skip(str(e))
orig_version = setuptools.__version__
# free-threaded Python does not yet support limited API
expecting_limited_api = (
not hasattr(sys, 'gettotalrefcount') and not
(sysconfig.get_config_var("Py_GIL_DISABLED") and sys.version_info < (3, 15))
)
try:
setuptools.__version__ = '26.0.0'
from setuptools import Extension
kwds = _set_py_limited_api(Extension, {})
assert kwds.get('py_limited_api', False) == expecting_limited_api
setuptools.__version__ = '25.0'
kwds = _set_py_limited_api(Extension, {})
assert kwds.get('py_limited_api', False) == False
setuptools.__version__ = 'development'
kwds = _set_py_limited_api(Extension, {})
assert kwds.get('py_limited_api', False) == expecting_limited_api
finally:
setuptools.__version__ = orig_version