|
4 | 4 |
|
5 | 5 | import os |
6 | 6 | import struct |
7 | | -from tempfile import NamedTemporaryFile |
| 7 | +from tempfile import TemporaryDirectory |
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 |
|
|
14 | 14 | @pytest.mark.parametrize("abi", [5, 6]) |
15 | 15 | @pytest.mark.asyncio |
16 | 16 | async def test_compile_file(abi: int): |
17 | | - with NamedTemporaryFile("w", delete=False, encoding="utf-8") as f: |
18 | | - try: |
| 17 | + with TemporaryDirectory() as temp_dir: |
| 18 | + with open(os.path.join(temp_dir, "test.py"), "w", encoding="utf-8") as f: |
19 | 19 | f.write("print('test')") |
20 | | - f.close() |
21 | 20 |
|
22 | | - mpy = await compile_file( |
23 | | - os.path.dirname(f.name), os.path.basename(f.name), abi=abi |
24 | | - ) |
| 21 | + mpy = await compile_file( |
| 22 | + os.path.dirname(f.name), os.path.basename(f.name), abi=abi |
| 23 | + ) |
25 | 24 |
|
26 | | - magic, abi_ver, flags, int_bits = struct.unpack_from("<BBBB", mpy) |
| 25 | + magic, abi_ver, flags, int_bits = struct.unpack_from("<BBBB", mpy) |
27 | 26 |
|
28 | | - assert chr(magic) == "M" |
29 | | - assert abi_ver == abi |
30 | | - assert flags == 0 |
31 | | - assert int_bits == 31 |
32 | | - finally: |
33 | | - os.unlink(f.name) |
| 27 | + assert chr(magic) == "M" |
| 28 | + assert abi_ver == abi |
| 29 | + assert flags == 0 |
| 30 | + assert int_bits == 31 |
34 | 31 |
|
35 | 32 |
|
36 | 33 | @pytest.mark.asyncio |
37 | 34 | async def test_compile_file_invalid_abi(): |
38 | | - with NamedTemporaryFile("w", delete=False, encoding="utf-8") as f: |
39 | | - try: |
| 35 | + with TemporaryDirectory() as temp_dir: |
| 36 | + with open(os.path.join(temp_dir, "test.py"), "w", encoding="utf-8") as f: |
40 | 37 | f.write("print('test')") |
41 | | - f.close() |
42 | | - |
43 | | - with pytest.raises(ValueError, match="mpy_version must be 5 or 6"): |
44 | | - await compile_file( |
45 | | - os.path.dirname(f.name), os.path.basename(f.name), abi=4 |
46 | | - ) |
47 | | - finally: |
48 | | - os.unlink(f.name) |
| 38 | + |
| 39 | + with pytest.raises(ValueError, match="mpy_version must be 5 or 6"): |
| 40 | + await compile_file(os.path.dirname(f.name), os.path.basename(f.name), abi=4) |
0 commit comments