Skip to content

Commit c956761

Browse files
authored
feat: correct fsspec source serialization (#1033)
* test different handlers * correct serialization of fsspec source * fsspec is not yet required
1 parent 0b5d8d6 commit c956761

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/uproot/source/fsspec.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,27 @@ def __init__(self, file_path: str, **options):
3333
if k not in uproot.reading.open.defaults.keys()
3434
}
3535

36-
self._executor = FSSpecLoopExecutor()
37-
3836
self._fs, self._file_path = fsspec.core.url_to_fs(file_path, **storage_options)
3937

4038
# What should we do when there is a chain of filesystems?
4139
self._async_impl = self._fs.async_impl
4240

43-
self._file = self._fs.open(self._file_path)
41+
self._executor = None
42+
self._file = None
4443
self._fh = None
44+
4545
self._num_requests = 0
4646
self._num_requested_chunks = 0
4747
self._num_requested_bytes = 0
48+
49+
self._open()
50+
4851
self.__enter__()
4952

53+
def _open(self):
54+
self._executor = FSSpecLoopExecutor()
55+
self._file = self._fs.open(self._file_path)
56+
5057
def __repr__(self):
5158
path = repr(self._file_path)
5259
if len(self._file_path) > 10:
@@ -56,6 +63,8 @@ def __repr__(self):
5663
def __getstate__(self):
5764
state = dict(self.__dict__)
5865
state.pop("_executor")
66+
state.pop("_file")
67+
state.pop("_fh")
5968
return state
6069

6170
def __setstate__(self, state):

tests/test_0302_pickle.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE
22

3-
import os
43
import pickle
5-
import sys
64

75
import pytest
86
import skhep_testdata
97

108
import uproot
119

12-
pytest.importorskip("awkward")
1310

14-
15-
def test_pickle_roundtrip_mmap():
16-
with uproot.open(skhep_testdata.data_path("uproot-small-dy-withoffsets.root")) as f:
11+
@pytest.mark.parametrize(
12+
"handler",
13+
[
14+
uproot.source.file.MemmapSource,
15+
# uproot.source.fsspec.FSSpecSource,
16+
],
17+
)
18+
def test_pickle_roundtrip_local(handler):
19+
with uproot.open(
20+
skhep_testdata.data_path("uproot-small-dy-withoffsets.root"), handler=handler
21+
) as f:
1722
pkl = pickle.dumps(f["tree"])
1823

1924
branch = pickle.loads(pkl)["Muon_pt"]
@@ -32,9 +37,20 @@ def test_pickle_roundtrip_mmap():
3237
]
3338

3439

40+
@pytest.mark.parametrize(
41+
"handler",
42+
[
43+
uproot.source.http.HTTPSource,
44+
# uproot.source.fsspec.FSSpecSource,
45+
],
46+
)
3547
@pytest.mark.network
36-
def test_pickle_roundtrip_http():
37-
with uproot.open("https://scikit-hep.org/uproot3/examples/Zmumu.root") as f:
48+
def test_pickle_roundtrip_http(handler):
49+
pytest.importorskip("aiohttp")
50+
51+
with uproot.open(
52+
"https://scikit-hep.org/uproot3/examples/Zmumu.root", handler=handler
53+
) as f:
3854
pkl = pickle.dumps(f["events"])
3955

4056
tree = pickle.loads(pkl)
@@ -53,12 +69,20 @@ def test_pickle_roundtrip_http():
5369
]
5470

5571

72+
@pytest.mark.parametrize(
73+
"handler",
74+
[
75+
uproot.source.xrootd.XRootDSource,
76+
# uproot.source.fsspec.FSSpecSource,
77+
],
78+
)
5679
@pytest.mark.network
5780
@pytest.mark.xrootd
58-
def test_pickle_roundtrip_xrootd():
81+
def test_pickle_roundtrip_xrootd(handler):
5982
pytest.importorskip("XRootD")
6083
with uproot.open(
61-
"root://eospublic.cern.ch//eos/root-eos/cms_opendata_2012_nanoaod/Run2012B_DoubleMuParked.root"
84+
"root://eospublic.cern.ch//eos/root-eos/cms_opendata_2012_nanoaod/Run2012B_DoubleMuParked.root",
85+
handler=handler,
6286
) as f:
6387
pkl = pickle.dumps(f["Events"])
6488

0 commit comments

Comments
 (0)