Skip to content

Commit f9fac7e

Browse files
stefanormattip
authored andcommitted
Migrate away from the "py" module
Replace py.local in udir with pathlib.Path. Change some code to be more idiomatic for pathlib on modern Python. Fixes: #216
1 parent 7020b2d commit f9fac7e

File tree

13 files changed

+150
-152
lines changed

13 files changed

+150
-152
lines changed

testing/cffi0/test_function.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,18 +337,18 @@ def test_function_typedef(self):
337337
def test_fputs_custom_FILE(self):
338338
if self.Backend is CTypesBackend:
339339
pytest.skip("FILE not supported with the ctypes backend")
340-
filename = str(udir.join('fputs_custom_FILE'))
340+
temp_file = udir / 'fputs_custom_FILE'
341341
ffi = FFI(backend=self.Backend())
342342
ffi.cdef("int fputs(const char *, FILE *);")
343343
needs_dlopen_none()
344344
C = ffi.dlopen(None)
345-
with open(filename, 'wb') as f:
345+
with temp_file.open('wb') as f:
346346
f.write(b'[')
347347
C.fputs(b"hello from custom file", f)
348348
f.write(b'][')
349349
C.fputs(b"some more output", f)
350350
f.write(b']')
351-
with open(filename, 'rb') as f:
351+
with temp_file.open('rb') as f:
352352
res = f.read()
353353
assert res == b'[hello from custom file][some more output]'
354354

testing/cffi0/test_ownlib.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ class TestOwnLib(object):
130130
def setup_class(cls):
131131
cls.module = None
132132
from testing.udir import udir
133-
udir.join('testownlib.c').write(SOURCE)
133+
(udir / 'testownlib.c').write_text(SOURCE)
134134
if sys.platform == 'win32':
135135
# did we already build it?
136136
if cls.Backend is CTypesBackend:
137-
dll_path = str(udir) + '\\testownlib1.dll' # only ascii for the ctypes backend
137+
dll_path = f"{udir}\\testownlib1.dll" # only ascii for the ctypes backend
138138
else:
139-
dll_path = str(udir) + '\\' + (u+'testownlib\u03be.dll') # non-ascii char
139+
dll_path = f"{udir}\\testownlib\u03be.dll" # non-ascii char
140140
if os.path.exists(dll_path):
141141
cls.module = dll_path
142142
return
@@ -159,7 +159,7 @@ def setup_class(cls):
159159
cmd = '"%s" %s' % (vcvarsall, arch) + ' & cl.exe testownlib.c ' \
160160
' /LD /Fetestownlib.dll'
161161
subprocess.check_call(cmd, cwd = str(udir), shell=True)
162-
os.rename(str(udir) + '\\testownlib.dll', dll_path)
162+
os.rename(f"{udir}\\testownlib.dll", dll_path)
163163
cls.module = dll_path
164164
else:
165165
encoded = None
@@ -177,7 +177,7 @@ def setup_class(cls):
177177
subprocess.check_call(
178178
"cc testownlib.c -shared -fPIC -o '%s.so'" % (encoded,),
179179
cwd=str(udir), shell=True)
180-
cls.module = os.path.join(str(udir), unicode_name + (u+'.so'))
180+
cls.module = f"{udir / unicode_name}.so"
181181
print(repr(cls.module))
182182

183183
def test_getting_errno(self):

testing/cffi0/test_zdistutils.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def setup_class(self):
2121
self.lib_m = 'msvcrt'
2222

2323
def teardown_class(self):
24-
if udir.isdir():
25-
udir.remove(ignore_errors=True)
26-
udir.ensure(dir=1)
24+
if udir.is_dir():
25+
shutil.rmtree(udir)
26+
udir.mkdir()
2727

2828
def test_locate_engine_class(self):
2929
cls = _locate_engine_class(FFI(), self.generic)
@@ -55,12 +55,11 @@ def test_write_source_explicit_filename(self):
5555
csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
5656
v = Verifier(ffi, csrc, force_generic_engine=self.generic,
5757
libraries=[self.lib_m])
58-
v.sourcefilename = filename = str(udir.join('write_source.c'))
58+
source_file = udir / 'write_source.c'
59+
v.sourcefilename = str(source_file)
5960
v.write_source()
60-
assert filename == v.sourcefilename
61-
with open(filename, 'r') as f:
62-
data = f.read()
63-
assert csrc in data
61+
assert str(source_file) == v.sourcefilename
62+
assert csrc in source_file.read_text()
6463

6564
def test_write_source_to_file_obj(self):
6665
ffi = FFI()
@@ -95,9 +94,10 @@ def test_compile_module_explicit_filename(self):
9594
v = Verifier(ffi, csrc, force_generic_engine=self.generic,
9695
libraries=[self.lib_m])
9796
basename = self.__class__.__name__[:10] + '_test_compile_module'
98-
v.modulefilename = filename = str(udir.join(basename + '.so'))
97+
module_file = udir / (basename + '.so')
98+
v.modulefilename = str(module_file)
9999
v.compile_module()
100-
assert filename == v.modulefilename
100+
assert str(module_file) == v.modulefilename
101101
assert v.get_module_name() == basename
102102
if v.generates_python_module():
103103
mod = load_dynamic(v.get_module_name(), v.modulefilename)
@@ -136,7 +136,7 @@ def test_verifier_args(self):
136136
ffi = FFI()
137137
ffi.cdef("double sin(double x);")
138138
csrc = '/*hi there %s!4*/#include "test_verifier_args.h"\n' % self
139-
udir.join('test_verifier_args.h').write('#include <math.h>\n')
139+
(udir / 'test_verifier_args.h').write_text('#include <math.h>\n')
140140
v = Verifier(ffi, csrc, include_dirs=[str(udir)],
141141
force_generic_engine=self.generic,
142142
libraries=[self.lib_m])
@@ -189,21 +189,21 @@ def test_extension_forces_write_source(self):
189189
def test_extension_object_extra_sources(self):
190190
ffi = FFI()
191191
ffi.cdef("double test1eoes(double x);")
192-
extra_source = str(udir.join('extension_extra_sources.c'))
193-
with open(extra_source, 'w') as f:
194-
f.write('double test1eoes(double x) { return x * 6.0; }\n')
192+
extra_source = udir / 'extension_extra_sources.c'
193+
extra_source.write_text(
194+
'double test1eoes(double x) { return x * 6.0; }\n')
195195
csrc = '/*9%s*/' % self + '''
196196
double test1eoes(double x); /* or #include "extra_sources.h" */
197197
'''
198-
lib = ffi.verify(csrc, sources=[extra_source],
198+
lib = ffi.verify(csrc, sources=[str(extra_source)],
199199
force_generic_engine=self.generic)
200200
assert lib.test1eoes(7.0) == 42.0
201201
v = ffi.verifier
202202
ext = v.get_extension()
203203
assert 'distutils.extension.Extension' in str(ext.__class__) or \
204204
'setuptools.extension.Extension' in str(ext.__class__)
205205
assert ext.sources == [maybe_relative_path(v.sourcefilename),
206-
extra_source]
206+
str(extra_source)]
207207
assert ext.name == v.get_module_name()
208208

209209
def test_install_and_reload_module(self, targetpackage='', ext_package=''):
@@ -212,7 +212,8 @@ def test_install_and_reload_module(self, targetpackage='', ext_package=''):
212212
pytest.skip("test requires os.fork()")
213213

214214
if targetpackage:
215-
udir.ensure(targetpackage, dir=1).ensure('__init__.py')
215+
(udir / targetpackage).mkdir(exist_ok=True)
216+
(udir / targetpackage / "__init__.py").touch(exist_ok=True)
216217
sys.path.insert(0, str(udir))
217218

218219
def make_ffi(**verifier_args):
@@ -232,7 +233,7 @@ def make_ffi(**verifier_args):
232233
assert lib.test1iarm(1.5) == 63.0
233234
# "install" the module by moving it into udir (/targetpackage)
234235
if targetpackage:
235-
target = udir.join(targetpackage)
236+
target = udir / targetpackage
236237
else:
237238
target = udir
238239
shutil.move(ffi.verifier.modulefilename, str(target))

testing/cffi0/test_zintegration.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import py, os, sys, shutil
1+
import os, sys, shutil
22
import subprocess
33
import sysconfig
44
import textwrap
5+
from pathlib import Path
56
from testing.udir import udir
67
import pytest
78

@@ -13,7 +14,7 @@
1314
' in a non-2.6-friendly way')
1415

1516
def create_venv(name):
16-
tmpdir = udir.join(name)
17+
tmpdir = udir / name
1718
try:
1819
# FUTURE: we should probably update this to use venv for at least more modern Pythons, and
1920
# install setuptools/pip/etc explicitly for the tests that require them (as venv has stopped including
@@ -27,7 +28,7 @@ def create_venv(name):
2728
# Newer venv/virtualenv no longer include setuptools by default, which
2829
# breaks a number of these tests; ensure they're always present
2930
subprocess.check_call([
30-
os.path.join(tmpdir, 'bin/python'),
31+
str(tmpdir / 'bin/python'),
3132
'-m',
3233
'pip',
3334
'install',
@@ -39,8 +40,8 @@ def create_venv(name):
3940
pytest.skip("Cannot execute virtualenv: %s" % (e,))
4041

4142
site_packages = None
42-
for dirpath, dirnames, filenames in os.walk(str(tmpdir)):
43-
if os.path.basename(dirpath) == 'site-packages':
43+
for dirpath, dirnames, filenames in os.walk(tmpdir):
44+
if Path(dirpath).name == 'site-packages':
4445
site_packages = dirpath
4546
break
4647
paths = ""
@@ -70,27 +71,27 @@ def create_venv(name):
7071
paths = os.pathsep.join(paths)
7172
return tmpdir, paths
7273

73-
SNIPPET_DIR = py.path.local(__file__).join('..', 'snippets')
74+
SNIPPET_DIR = Path(__file__).absolute().parent / 'snippets'
7475

7576
def really_run_setup_and_program(dirname, venv_dir_and_paths, python_snippet):
7677
venv_dir, paths = venv_dir_and_paths
7778
def remove(dir):
78-
dir = str(SNIPPET_DIR.join(dirname, dir))
79+
dir = str(SNIPPET_DIR / dirname / dir)
7980
shutil.rmtree(dir, ignore_errors=True)
8081
remove('build')
8182
remove('__pycache__')
82-
for basedir in os.listdir(str(SNIPPET_DIR.join(dirname))):
83-
remove(os.path.join(basedir, '__pycache__'))
83+
for basedir in (SNIPPET_DIR / dirname).iterdir():
84+
remove(basedir / '__pycache__')
8485
olddir = os.getcwd()
85-
python_f = udir.join('x.py')
86-
python_f.write(textwrap.dedent(python_snippet))
86+
python_f = udir / 'x.py'
87+
python_f.write_text(textwrap.dedent(python_snippet))
8788
try:
88-
os.chdir(str(SNIPPET_DIR.join(dirname)))
89+
os.chdir(str(SNIPPET_DIR / dirname))
8990
if os.name == 'nt':
9091
bindir = 'Scripts'
9192
else:
9293
bindir = 'bin'
93-
vp = str(venv_dir.join(bindir).join('python'))
94+
vp = str(venv_dir / bindir / 'python')
9495
env = os.environ.copy()
9596
env['PYTHONPATH'] = paths
9697
subprocess.check_call((vp, 'setup.py', 'clean'), env=env)
@@ -114,15 +115,15 @@ def run_setup_and_program(dirname, python_snippet):
114115
del sys._force_generic_engine_
115116
# the two files lextab.py and yacctab.py are created by not-correctly-
116117
# installed versions of pycparser.
117-
assert not os.path.exists(str(SNIPPET_DIR.join(dirname, 'lextab.py')))
118-
assert not os.path.exists(str(SNIPPET_DIR.join(dirname, 'yacctab.py')))
118+
assert not (SNIPPET_DIR / dirname / 'lextab.py').exists()
119+
assert not (SNIPPET_DIR / dirname / 'yacctab.py').exists()
119120

120121
@pytest.mark.thread_unsafe(reason="very slow in parallel")
121122
class TestZIntegration(object):
122123
def teardown_class(self):
123-
if udir.isdir():
124-
udir.remove(ignore_errors=True)
125-
udir.ensure(dir=1)
124+
if udir.is_dir():
125+
shutil.rmtree(udir)
126+
udir.mkdir()
126127

127128
def test_infrastructure(self):
128129
run_setup_and_program('infrastructure', '''

0 commit comments

Comments
 (0)