From 592d1708aa14f7a63fb39dae006d13d3d059175e Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Mon, 7 Aug 2023 10:50:38 -0500 Subject: [PATCH 1/3] fix: don't assume Uproot is in global scope in TPython::Eval --- src/uproot/pyroot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/uproot/pyroot.py b/src/uproot/pyroot.py index c6571526e..135e7c15b 100644 --- a/src/uproot/pyroot.py +++ b/src/uproot/pyroot.py @@ -101,15 +101,15 @@ class _Uproot_buffer_sizer : public TObject { char* _uproot_TMessage_reallocate(char* buffer, size_t newsize, size_t oldsize) { _Uproot_buffer_sizer* ptr = reinterpret_cast<_Uproot_buffer_sizer*>( - (void*)TPython::Eval("uproot.pyroot.pyroot_to_buffer.sizer") + (void*)TPython::Eval("__import__('uproot').pyroot.pyroot_to_buffer.sizer") ); ptr->buffer = reinterpret_cast(buffer); ptr->newsize = newsize; ptr->oldsize = oldsize; - TPython::Exec("uproot.pyroot.pyroot_to_buffer.reallocate()"); + TPython::Exec("__import__('uproot').pyroot.pyroot_to_buffer.reallocate()"); - TPyReturn out = TPython::Eval("uproot.pyroot.pyroot_to_buffer.buffer.ctypes.data"); + TPyReturn out = TPython::Eval("__import__('uproot').pyroot.pyroot_to_buffer.buffer.ctypes.data"); return reinterpret_cast((size_t)out); } From 771529a6a61204e953ad416efda7b0a10f04790a Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Mon, 7 Aug 2023 11:01:26 -0500 Subject: [PATCH 2/3] Unfortunately, this test does not trigger the bug (in main). --- ...ssume-uproot-in-global-scope-in-TPython-Eval.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/test_0927-dont-assume-uproot-in-global-scope-in-TPython-Eval.py diff --git a/tests/test_0927-dont-assume-uproot-in-global-scope-in-TPython-Eval.py b/tests/test_0927-dont-assume-uproot-in-global-scope-in-TPython-Eval.py new file mode 100644 index 000000000..a0ac3041e --- /dev/null +++ b/tests/test_0927-dont-assume-uproot-in-global-scope-in-TPython-Eval.py @@ -0,0 +1,14 @@ +# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE + +import random +import string + +import pytest + +ROOT = pytest.importorskip("ROOT") + + +def test(): + name = "".join(random.choices(string.ascii_lowercase, k=10)) + h = ROOT.TH1F(name, "", 100, -5, 5) + assert len(__import__("uproot").from_pyroot(h).values()) == 100 From cf0bf37ba901606f3c22e9ac9a06f7c3b63a5d2a Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Mon, 7 Aug 2023 11:52:15 -0500 Subject: [PATCH 3/3] Use TProfile3D for the test. --- ...0927-dont-assume-uproot-in-global-scope-in-TPython-Eval.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_0927-dont-assume-uproot-in-global-scope-in-TPython-Eval.py b/tests/test_0927-dont-assume-uproot-in-global-scope-in-TPython-Eval.py index a0ac3041e..004537ec7 100644 --- a/tests/test_0927-dont-assume-uproot-in-global-scope-in-TPython-Eval.py +++ b/tests/test_0927-dont-assume-uproot-in-global-scope-in-TPython-Eval.py @@ -10,5 +10,5 @@ def test(): name = "".join(random.choices(string.ascii_lowercase, k=10)) - h = ROOT.TH1F(name, "", 100, -5, 5) - assert len(__import__("uproot").from_pyroot(h).values()) == 100 + h = ROOT.TProfile3D() + assert __import__("uproot").from_pyroot(h).values().shape == (1, 1, 1)