Skip to content

Commit

Permalink
Fixed #176 and made tests run in Python 2.7 (which is what the #176 b…
Browse files Browse the repository at this point in the history
…ug was about).
  • Loading branch information
jpivarski committed Nov 24, 2020
1 parent 7ad4eee commit b021bb4
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 23 deletions.
12 changes: 6 additions & 6 deletions tests/test_0001-source-class.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ def test_http_port():
@pytest.mark.network
def test_http_size():
with uproot4.source.http.HTTPSource(
"https://scikit-hep.org/uproot/examples/Zmumu.root",
"https://scikit-hep.org/uproot3/examples/Zmumu.root",
timeout=10,
num_fallback_workers=1,
) as source:
size1 = source.num_bytes

with uproot4.source.http.MultithreadedHTTPSource(
"https://scikit-hep.org/uproot/examples/Zmumu.root", num_workers=1, timeout=10
"https://scikit-hep.org/uproot3/examples/Zmumu.root", num_workers=1, timeout=10
) as source:
size2 = source.num_bytes

Expand All @@ -186,14 +186,14 @@ def test_http_size():
@pytest.mark.network
def test_http_size_port():
with uproot4.source.http.HTTPSource(
"https://scikit-hep.org:443/uproot/examples/Zmumu.root",
"https://scikit-hep.org:443/uproot3/examples/Zmumu.root",
timeout=10,
num_fallback_workers=1,
) as source:
size1 = source.num_bytes

with uproot4.source.http.MultithreadedHTTPSource(
"https://scikit-hep.org:443/uproot/examples/Zmumu.root",
"https://scikit-hep.org:443/uproot3/examples/Zmumu.root",
num_workers=1,
timeout=10,
) as source:
Expand All @@ -217,7 +217,7 @@ def test_http_fail():
def test_no_multipart():
for num_workers in [1, 2]:
with uproot4.source.http.MultithreadedHTTPSource(
"https://scikit-hep.org/uproot/examples/Zmumu.root",
"https://scikit-hep.org/uproot3/examples/Zmumu.root",
num_workers=num_workers,
timeout=10,
) as source:
Expand Down Expand Up @@ -246,7 +246,7 @@ def test_no_multipart_fail():
def test_fallback():
for num_workers in [1, 2]:
with uproot4.source.http.HTTPSource(
"https://scikit-hep.org/uproot/examples/Zmumu.root",
"https://scikit-hep.org/uproot3/examples/Zmumu.root",
timeout=10,
num_fallback_workers=num_workers,
) as source:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_0006-notify-when-downloaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_http_workers():
def test_http_fallback():
notifications = queue.Queue()
with uproot4.source.http.HTTPSource(
"https://scikit-hep.org/uproot/examples/Zmumu.root",
"https://scikit-hep.org/uproot3/examples/Zmumu.root",
timeout=10,
num_fallback_workers=1,
) as source:
Expand All @@ -143,7 +143,7 @@ def test_http_fallback():
def test_http_fallback_workers():
notifications = queue.Queue()
with uproot4.source.http.HTTPSource(
"https://scikit-hep.org/uproot/examples/Zmumu.root",
"https://scikit-hep.org/uproot3/examples/Zmumu.root",
timeout=10,
num_fallback_workers=5,
) as source:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_0045-lazy-arrays-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def test_awkward_pluralization():


def test_lazy_called_on_nonexistent_file():
awkward1 = pytest.importorskip("awkward1")
filename = "nonexistent_file.root"
with pytest.raises(FileNotFoundError) as excinfo:
with pytest.raises(uproot4._util._FileNotFoundError) as excinfo:
uproot4.lazy(filename)
assert filename in str(excinfo.value)
6 changes: 3 additions & 3 deletions tests/test_0066-fix-http-fallback-freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@pytest.mark.network
def test():
with uproot4.open(
{"http://scikit-hep.org/uproot/examples/HZZ.root": "events"}
{"http://scikit-hep.org/uproot3/examples/HZZ.root": "events"}
) as t:
t["MET_px"].array()
t["MET_py"].array()
t["MET_px"].array(library="np")
t["MET_py"].array(library="np")
10 changes: 10 additions & 0 deletions tests/test_0088-read-with-http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def test_issue176():
assert len(data) == 100000


@pytest.mark.network
def test_issue176_again():
with uproot4.open(
"https://starterkit.web.cern.ch/starterkit/data/advanced-python-2019/dalitzdata.root"
) as f:
data = f["tree"].arrays(["Y1", "Y2"], library="np")
assert len(data["Y1"]) == 100000
assert len(data["Y2"]) == 100000


@pytest.mark.network
def test_issue121():
with uproot4.open(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_0172-allow-allocators-in-vector-typenames.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def test():
assert (
t["rec_part_px_VecOps"].typename == "std::vector<float>"
) # without the allocator
t["rec_part_px_VecOps"].array()
t["rec_part_px_VecOps"].array(library="np")
2 changes: 1 addition & 1 deletion tests/test_0173-empty-and-multiprocessing-bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def readone(filename):
f.decompression_executor = uproot4.ThreadPoolExecutor()
t = f["events"]
b = t["px1"]
b.array()
b.array(library="np")


def test_multiprocessing():
Expand Down
9 changes: 6 additions & 3 deletions tests/test_0182-complain-about-missing-files.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ def test():
bad = one.replace(".root", "-DOES-NOT-EXIST.root")
okay = one.replace(".root", "-DOES-NOT-EXIST-*.root")

assert len(list(uproot4.iterate([one, two], step_size="1 TB"))) == 2
assert len(list(uproot4.iterate([one, two], step_size="1 TB", library="np"))) == 2

with pytest.raises(uproot4._util._FileNotFoundError):
list(uproot4.iterate([one, two, bad]))
list(uproot4.iterate([one, two, bad], library="np"))

assert len(list(uproot4.iterate([one, two, okay], step_size="1 TB"))) == 2
assert (
len(list(uproot4.iterate([one, two, okay], step_size="1 TB", library="np")))
== 2
)
50 changes: 44 additions & 6 deletions uproot4/source/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,13 @@ def handle_multipart(self, source, futures, results, response):
Helper function for :py:meth:`~uproot4.source.http.HTTPResource.multifuture`
to handle the multipart GET response.
"""
if hasattr(response, "readline"):
response_buffer = response
else:
response_buffer = _ResponseBuffer(response)

for i in uproot4._util.range(len(futures)):
range_string, size = self.next_header(response)
range_string, size = self.next_header(response_buffer)
if range_string is None:
raise OSError(
"""found {0} of {1} expected headers in HTTP multipart
Expand All @@ -397,7 +402,7 @@ def handle_multipart(self, source, futures, results, response):
)

length = stop - start
results[start, stop] = response.read(length)
results[start, stop] = response_buffer.read(length)

if len(results[start, stop]) != length:
raise OSError(
Expand All @@ -413,12 +418,12 @@ def handle_multipart(self, source, futures, results, response):

future._run(self)

def next_header(self, response):
def next_header(self, response_buffer):
"""
Helper function for :py:meth:`~uproot4.source.http.HTTPResource.multifuture`
to return the next header from the ``response``.
to return the next header from the ``response_buffer``.
"""
line = response.fp.readline()
line = response_buffer.readline()
range_string, size = None, None
while range_string is None:
m = self._content_range_size.match(line)
Expand All @@ -430,7 +435,7 @@ def next_header(self, response):
if m is not None:
range_string = m.group(1)
size = None
line = response.fp.readline()
line = response_buffer.readline()
if len(line.strip()) == 0:
break
return range_string, size
Expand All @@ -452,6 +457,39 @@ def task(resource):
return uproot4.source.futures.ResourceFuture(task)


class _ResponseBuffer(object):
CHUNK = 1024

def __init__(self, stream):
self.already_read = b""
self.stream = stream

def read(self, length):
if length < len(self.already_read):
out = self.already_read[:length]
self.already_read = self.already_read[length:]
return out

elif len(self.already_read) > 0:
out = self.already_read
self.already_read = b""
return out + self.stream.read(length - len(out))

else:
return self.stream.read(length)

def readline(self):
while True:
try:
index = self.already_read.index(b"\n")
except ValueError:
self.already_read = self.already_read + self.stream.read(self.CHUNK)
else:
out = self.already_read[: index + 1]
self.already_read = self.already_read[index + 1 :]
return out


class HTTPSource(uproot4.source.chunk.Source):
"""
Args:
Expand Down

0 comments on commit b021bb4

Please sign in to comment.