Skip to content

Commit c027609

Browse files
committed
Drop Python 2/3 compatibility shim from util.py
Replace the version_info-gated import of subprocess.check_output / getoutput (and the py3 / spop / branched fpath usage) with a direct import of subprocess.getoutput. Also defines fpath unconditionally at module level so the manylinux1 fallback no longer references an undefined name on macOS/Windows. Gate numpy .astype path with isinstance check in _FFIArray The duck-typed try/except (TypeError, AttributeError) around seq.astype(np.float64) caught two distinct conditions: 1. numpy arrays whose buffer wasn't writable (TypeError from from_buffer) -- handled by retrying with from_buffer_copy. 2. array.array / list / tuple inputs that lack .astype entirely (AttributeError) -- handled by falling through to array_type.from_buffer(array("d", seq)). Make the numpy vs non-numpy branch explicit with isinstance(seq, np.ndarray) so the .astype call lives on a known-numpy path. Resolves the ty 'possibly-missing-attribute' diagnostic on .astype without changing runtime behaviour for any input type. The two .astype calls carry # ty: ignore[no-matching-overload] because ty's current numpy stubs can't unify any dtype argument form (np.float64, "float64", np.dtype(...)) against the overloaded signature -- a stubs limitation rather than a real type error. Signed-off-by: Stephan Hügel <shugel@tcd.ie>
1 parent ad9d26c commit c027609

1 file changed

Lines changed: 19 additions & 39 deletions

File tree

src/convertbng/util.py

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,24 @@
1919
cdll,
2020
string_at,
2121
)
22-
from sys import platform, version_info
22+
from subprocess import getoutput
23+
from sys import platform
2324

2425
import numpy as np
2526

2627
__author__ = "Stephan Hügel"
2728

2829
file_path = os.path.dirname(__file__)
2930

30-
if platform == "darwin":
31-
prefix = "lib"
32-
ext = "dylib"
33-
elif "linux" in platform:
34-
prefix = "lib"
35-
ext = "so"
36-
fpath = os.path.join(file_path, ".libs")
37-
38-
elif "win32" in platform:
39-
prefix = ""
40-
ext = "dll"
41-
4231
prefix = {"win32": ""}.get(platform, "lib")
4332
extension = {"darwin": ".dylib", "win32": ".dll"}.get(platform, ".so")
44-
45-
# Python 3 check
46-
if version_info > (3, 0):
47-
from subprocess import getoutput as spop
48-
49-
py3 = True
50-
else:
51-
from subprocess import check_output as spop
52-
53-
py3 = False
33+
fpath = {"darwin": "", "win32": ""}.get(platform, os.path.join(file_path, ".libs"))
5434

5535
try:
5636
lib = cdll.LoadLibrary(os.path.join(file_path, prefix + "lonlat_bng" + extension))
5737
except OSError:
5838
# the Rust lib's been grafted by manylinux1
59-
if not py3:
60-
fname = spop(["ls", fpath]).split()[0]
61-
else:
62-
fname = spop(["ls %s" % fpath]).split()[0]
39+
fname = getoutput("ls %s" % fpath).split()[0]
6340
lib = cdll.LoadLibrary(os.path.join(file_path, ".libs", fname))
6441

6542

@@ -75,12 +52,12 @@ def from_param(cls, seq):
7552

7653
def __init__(self, seq, data_type=c_double):
7754
"""
78-
Convert sequence of values into array, then ctypes Structure
55+
Convert sequence of values into array, then ctypes Structure.
7956
80-
Rather than checking types (bad), we just try to blam seq
81-
into a ctypes object using from_buffer. If that doesn't work,
82-
we try successively more conservative approaches:
83-
numpy array -> array.array -> read-only buffer -> CPython iterable
57+
numpy arrays go via .astype(float64) and from_buffer, falling
58+
back to from_buffer_copy for read-only / non-contiguous buffers.
59+
Anything else (array.array, list, tuple, iterable) is funnelled
60+
through array.array("d", seq).
8461
"""
8562
if isinstance(seq, float):
8663
seq = array("d", [seq])
@@ -90,14 +67,17 @@ def __init__(self, seq, data_type=c_double):
9067
# we've got an iterator or a generator, so consume it
9168
seq = array("d", seq)
9269
array_type = data_type * len(seq)
93-
try:
94-
raw_seq = array_type.from_buffer(seq.astype(np.float64))
95-
except (TypeError, AttributeError):
70+
if isinstance(seq, np.ndarray):
9671
try:
97-
raw_seq = array_type.from_buffer_copy(seq.astype(np.float64))
98-
except (TypeError, AttributeError):
99-
# it's a list or a tuple
100-
raw_seq = array_type.from_buffer(array("d", seq))
72+
raw_seq = array_type.from_buffer(
73+
seq.astype(np.float64) # ty: ignore[no-matching-overload]
74+
)
75+
except TypeError:
76+
raw_seq = array_type.from_buffer_copy(
77+
seq.astype(np.float64) # ty: ignore[no-matching-overload]
78+
)
79+
else:
80+
raw_seq = array_type.from_buffer(array("d", seq))
10181
self.data = cast(raw_seq, c_void_p)
10282
self.len = len(seq)
10383

0 commit comments

Comments
 (0)