Skip to content

Commit d148411

Browse files
committed
Inline shim enum.auto; document shims for os.fwalk and os.O_DIRECTORY
1 parent a2b0d8c commit d148411

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

ipfshttpclient/filescanner.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
re_pattern_t = re_pattern_type = type(re.compile(""))
2222

2323

24-
enum_auto = enum.auto
25-
26-
27-
O_DIRECTORY = getattr(os, "O_DIRECTORY", 0) # type: int
24+
# Windows does not have os.O_DIRECTORY
25+
O_DIRECTORY: int = getattr(os, "O_DIRECTORY", 0)
2826

2927

28+
# Mac does not have os.fwalk even thru Python 3.9
3029
HAVE_FWALK: bool = hasattr(os, "fwalk")
3130
HAVE_FWALK_BYTES = HAVE_FWALK and sys.version_info >= (3, 7)
3231

@@ -410,8 +409,8 @@ def matcher_from_spec(spec: match_spec_t[ty.AnyStr], *, # type: ignore[misc] #
410409
from .filescanner_ty import FSNodeType, FSNodeEntry
411410
else:
412411
class FSNodeType(enum.Enum):
413-
FILE = enum_auto()
414-
DIRECTORY = enum_auto()
412+
FILE = enum.auto()
413+
DIRECTORY = enum.auto()
415414

416415
FSNodeEntry = ty.NamedTuple("FSNodeEntry", [
417416
("type", FSNodeType),

test/functional/test_files.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import shutil
3-
import sys
43
import tempfile
54

65
import pytest
@@ -12,7 +11,8 @@
1211
import conftest
1312

1413

15-
O_DIRECTORY = getattr(os, "O_DIRECTORY", 0) # type: int
14+
# Windows does not have os.O_DIRECTORY
15+
O_DIRECTORY: int = getattr(os, "O_DIRECTORY", 0)
1616

1717

1818
### test_add_multiple_from_list
@@ -263,15 +263,15 @@ def test_add_filepattern_from_dirname(client, cleanup_pins):
263263
reason="No point in disabling os.fwalk if it isn't actually supported")
264264
def test_add_filepattern_from_dirname_nofwalk(client, cleanup_pins, monkeypatch):
265265
monkeypatch.setattr(ipfshttpclient.filescanner, "HAVE_FWALK", False)
266-
266+
267267
res = client.add(FAKE_DIR_PATH, pattern=FAKE_DIR_FNPATTERN1)
268268
assert conftest.sort_by_key(res) == conftest.sort_by_key(FAKE_DIR_FNPATTERN1_HASH)
269269

270270

271271
@pytest.mark.skipif(not ipfshttpclient.filescanner.HAVE_FWALK,
272272
reason="Passing directory as file descriptor requires os.fwalk")
273273
def test_add_filepattern_from_dirfd(client, cleanup_pins):
274-
fd = os.open(str(FAKE_DIR_PATH), os.O_RDONLY | O_DIRECTORY) # type: int
274+
fd: int = os.open(str(FAKE_DIR_PATH), os.O_RDONLY | O_DIRECTORY)
275275
try:
276276
res = client.add(fd, pattern=FAKE_DIR_FNPATTERN1)
277277
finally:
@@ -288,18 +288,18 @@ def test_add_filepattern_from_dirname_recursive(client, cleanup_pins):
288288
reason="No point in disabling os.fwalk if it isn't actually supported")
289289
def test_add_filepattern_from_dirname_recursive_nofwalk(client, cleanup_pins, monkeypatch):
290290
monkeypatch.setattr(ipfshttpclient.filescanner, "HAVE_FWALK", False)
291-
291+
292292
res = client.add(FAKE_DIR_PATH, pattern=FAKE_DIR_FNPATTERN1, recursive=True)
293293
assert conftest.sort_by_key(res) == conftest.sort_by_key(FAKE_DIR_FNPATTERN1_RECURSIVE_HASH)
294294

295295

296-
@pytest.mark.skipif(sys.platform.startswith("win"),
296+
@pytest.mark.skipif(not O_DIRECTORY,
297297
reason="Opening directory FDs does not work on Windows")
298298
def test_add_filepattern_from_dirfd_recursive_nofwalk(client, cleanup_pins, monkeypatch):
299299
monkeypatch.setattr(ipfshttpclient.filescanner, "HAVE_FWALK", False)
300-
300+
301301
with pytest.raises(NotImplementedError):
302-
fd = os.open(str(FAKE_DIR_PATH), os.O_RDONLY | O_DIRECTORY) # type: int
302+
fd: int = os.open(str(FAKE_DIR_PATH), os.O_RDONLY | O_DIRECTORY)
303303
try:
304304
client.add(fd, pattern=FAKE_DIR_FNPATTERN1, recursive=True)
305305
finally:
@@ -309,7 +309,7 @@ def test_add_filepattern_from_dirfd_recursive_nofwalk(client, cleanup_pins, monk
309309
@pytest.mark.skipif(not ipfshttpclient.filescanner.HAVE_FWALK,
310310
reason="Passing directory as file descriptor requires os.fwalk")
311311
def test_add_filepattern_from_dirfd_recursive(client, cleanup_pins):
312-
fd = os.open(str(FAKE_DIR_PATH), os.O_RDONLY | O_DIRECTORY) # type: int
312+
fd: int = os.open(str(FAKE_DIR_PATH), os.O_RDONLY | O_DIRECTORY)
313313
try:
314314
res = client.add(fd, pattern=FAKE_DIR_FNPATTERN1, recursive=True)
315315
finally:
@@ -327,7 +327,7 @@ def test_add_filepattern_from_dirname_recursive_binary(client, cleanup_pins):
327327
reason="No point in disabling os.fwalk if it isn't actually supported")
328328
def test_add_filepattern_from_dirname_recursive_nofwalk_binary(client, cleanup_pins, monkeypatch):
329329
monkeypatch.setattr(ipfshttpclient.filescanner, "HAVE_FWALK", False)
330-
330+
331331
res = client.add(os.fsencode(str(FAKE_DIR_PATH)),
332332
pattern=os.fsencode(FAKE_DIR_FNPATTERN1), recursive=True)
333333
assert conftest.sort_by_key(res) == conftest.sort_by_key(FAKE_DIR_FNPATTERN1_RECURSIVE_HASH)

0 commit comments

Comments
 (0)