From 6315ab75b32aa2b083d8ffe0ea80ff447c572230 Mon Sep 17 00:00:00 2001 From: jpivarski Date: Mon, 19 Sep 2022 17:33:53 -0500 Subject: [PATCH 1/3] fix: regularize ROOT type aliases to C fundamental type names. --- src/uproot/model.py | 55 +++++++++++++++++-- ...est_0278-specializations-for-TParameter.py | 6 ++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/uproot/model.py b/src/uproot/model.py index 8e060eed4..ba2919f07 100644 --- a/src/uproot/model.py +++ b/src/uproot/model.py @@ -105,7 +105,11 @@ def reset_classes(): reload(uproot.models.TMatrixT) -_classname_regularize = re.compile(r"\s*(<|>|::)\s*") +_classname_regularize = re.compile(r"\s*(<|>|,|::)\s*") +_classname_regularize_type = re.compile( + r"[<,](Bool_t|Char_t|UChar_t|Short_t|UShort_t|Int_t|UInt_t|Long_t|ULong_t|Long64_t|ULong64_t|Size_t|Float_t|Double_t|LongDouble_t)[>,]" +) + _classname_encode_pattern = re.compile(rb"[^a-zA-Z0-9]+") _classname_decode_antiversion = re.compile(rb".*_([0-9a-f][0-9a-f])+_v([0-9]+)$") _classname_decode_version = re.compile(rb".*_v([0-9]+)$") @@ -130,10 +134,51 @@ def classname_regularize(classname): If ``classname`` is None, this function returns None. Otherwise, it must be a string and it returns a string. """ - if classname is None: - return classname - else: - return re.sub(_classname_regularize, r"\1", classname) + if classname is not None: + classname = re.sub(_classname_regularize, r"\1", classname) + + m = _classname_regularize_type.search(classname) + + while m is not None: + start, stop = m.span(1) + token = classname[start:stop] + if token == "Bool_t": + replacement = "bool" + elif token == "Char_t": + replacement = "char" + elif token == "UChar_t": + replacement = "unsigned char" + elif token == "Short_t": + replacement = "short" + elif token == "UShort_t": + replacement = "unsigned short" + elif token == "Int_t": + replacement = "int" + elif token == "UInt_t": + replacement = "unsigned int" + elif token == "Long_t": + replacement = "long" + elif token == "ULong_t": + replacement = "unsigned long" + elif token == "Long64_t": + replacement = "long long" + elif token == "ULong64_t": + replacement = "unsigned long long" + elif token == "Size_t": + replacement = "size_t" + elif token == "Float_t": + replacement = "float" + elif token == "Double_t": + replacement = "double" + elif token == "LongDouble_t": + replacement = "long double" + else: + raise AssertionError + classname = classname[:start] + replacement + classname[stop:] + + m = _classname_regularize_type.search(classname) + + return classname def classname_decode(encoded_classname): diff --git a/tests/test_0278-specializations-for-TParameter.py b/tests/test_0278-specializations-for-TParameter.py index c0292b864..d67b67322 100644 --- a/tests/test_0278-specializations-for-TParameter.py +++ b/tests/test_0278-specializations-for-TParameter.py @@ -14,3 +14,9 @@ def test(): assert p assert int(p) == 500 assert float(p) == 500.0 + + +def test_issue_707(): + with uproot.open(skhep_testdata.data_path("uproot-issue-707.root")) as file: + p = file["NumberOfPrimariesEdep"] + assert p.value == 100000004 From 94d4312cf269ce0ae166aca0aca3e6c7061e761b Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 20 Sep 2022 18:28:52 +0100 Subject: [PATCH 2/3] wip: use translation table instead of if-chain This improves safety and readability --- src/uproot/model.py | 55 ++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/src/uproot/model.py b/src/uproot/model.py index ba2919f07..368d1f258 100644 --- a/src/uproot/model.py +++ b/src/uproot/model.py @@ -105,9 +105,29 @@ def reset_classes(): reload(uproot.models.TMatrixT) +_root_alias_to_c_primitive = { + "Bool_t": "bool", + "Char_t": "char", + "UChar_t": "unsigned char", + "Short_t": "short", + "UShort_t": "unsigned short", + "Int_t": "int", + "UInt_t": "unsigned int", + "Long_t": "long", + "ULong_t": "unsigned long", + "Long64_t": "long long", + "ULong64_t": "unsigned long long", + "Size_t": "size_t", + "Float_t": "float", + "Double_t": "double", + "LongDouble_t": "long double", +} + _classname_regularize = re.compile(r"\s*(<|>|,|::)\s*") _classname_regularize_type = re.compile( - r"[<,](Bool_t|Char_t|UChar_t|Short_t|UShort_t|Int_t|UInt_t|Long_t|ULong_t|Long64_t|ULong64_t|Size_t|Float_t|Double_t|LongDouble_t)[>,]" + r"[<,](" + + "|".join([re.escape(p) for p in _root_alias_to_c_primitive]) + + r")[>,]" ) _classname_encode_pattern = re.compile(rb"[^a-zA-Z0-9]+") @@ -142,38 +162,7 @@ def classname_regularize(classname): while m is not None: start, stop = m.span(1) token = classname[start:stop] - if token == "Bool_t": - replacement = "bool" - elif token == "Char_t": - replacement = "char" - elif token == "UChar_t": - replacement = "unsigned char" - elif token == "Short_t": - replacement = "short" - elif token == "UShort_t": - replacement = "unsigned short" - elif token == "Int_t": - replacement = "int" - elif token == "UInt_t": - replacement = "unsigned int" - elif token == "Long_t": - replacement = "long" - elif token == "ULong_t": - replacement = "unsigned long" - elif token == "Long64_t": - replacement = "long long" - elif token == "ULong64_t": - replacement = "unsigned long long" - elif token == "Size_t": - replacement = "size_t" - elif token == "Float_t": - replacement = "float" - elif token == "Double_t": - replacement = "double" - elif token == "LongDouble_t": - replacement = "long double" - else: - raise AssertionError + replacement = _root_alias_to_c_primitive[token] classname = classname[:start] + replacement + classname[stop:] m = _classname_regularize_type.search(classname) From 7d9813c556a9860a46e62fed0635b8f870f3b545 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 Sep 2022 17:33:01 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/uproot/model.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/uproot/model.py b/src/uproot/model.py index 368d1f258..e53d5a03a 100644 --- a/src/uproot/model.py +++ b/src/uproot/model.py @@ -125,9 +125,7 @@ def reset_classes(): _classname_regularize = re.compile(r"\s*(<|>|,|::)\s*") _classname_regularize_type = re.compile( - r"[<,](" - + "|".join([re.escape(p) for p in _root_alias_to_c_primitive]) - + r")[>,]" + r"[<,](" + "|".join([re.escape(p) for p in _root_alias_to_c_primitive]) + r")[>,]" ) _classname_encode_pattern = re.compile(rb"[^a-zA-Z0-9]+")