diff --git a/Control/RootUtils/RootUtils/PyROOTTFilePythonize.h b/Control/RootUtils/RootUtils/PyROOTTFilePythonize.h index f65efbb215d05d2f4df6049906437fa74bd8bd43..2fc9d6bad2f765dead326c950b73d60af5a156a1 100644 --- a/Control/RootUtils/RootUtils/PyROOTTFilePythonize.h +++ b/Control/RootUtils/RootUtils/PyROOTTFilePythonize.h @@ -1,11 +1,7 @@ // This file's extension implies that it's C, but it's really -*- C++ -*-. - /* - Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration */ - -// $Id$ - /** * @file RootUtils/PyROOTTFilePythonize.h * @author Sebastien Binet @@ -54,11 +50,14 @@ public: /** the buffer of bytes */ - char* buf; + std::vector<char> buf; /** + * After we drop py2, we can simplify the code in PyUtils.RootUtils.py + * by just returning a bytes object from here. */ - void* buffer() const { return (void*)buf; } + void* buffer() const { return (void*)buf.data(); } + }; /** @brief read `len` bytes from file `f` diff --git a/Control/RootUtils/RootUtils/RootUtilsPyROOTDict.h b/Control/RootUtils/RootUtils/RootUtilsPyROOTDict.h index 7cb56d351511104384a8d4972d6d55dd9503c63b..de05a3c5e04096e6742000bc5b2442ef176b3731 100644 --- a/Control/RootUtils/RootUtils/RootUtilsPyROOTDict.h +++ b/Control/RootUtils/RootUtils/RootUtilsPyROOTDict.h @@ -1,7 +1,7 @@ // This file's extension implies that it's C, but it's really -*- C++ -*-. /* - Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration */ // $Id: RootUtilsPyROOTDict.h,v 1.5 2008-04-23 19:48:34 ssnyder Exp $ @@ -26,7 +26,7 @@ // Work around a problem sometimes seen with cling in which `struct timespec' // appears to be predeclared without the include guard being defined. // This can cause problems, for example, with headers that include Python.h. -// As a workaroud, force the include guard to be defined when this +// As a workaround, force the include guard to be defined when this // dictionary is loaded. #include "TInterpreter.h" class RootUtilsInit diff --git a/Control/RootUtils/src/pyroot/PyROOTTFilePythonize.cxx b/Control/RootUtils/src/pyroot/PyROOTTFilePythonize.cxx index 0f19e3dbcf380ab97761f03057e6993c0b0023de..c2d80d2132db508ca72c8e88ee2c269598f218ac 100644 --- a/Control/RootUtils/src/pyroot/PyROOTTFilePythonize.cxx +++ b/Control/RootUtils/src/pyroot/PyROOTTFilePythonize.cxx @@ -1,8 +1,6 @@ /* - Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration */ - -// $Id$ /** * @file RootUtils/src/pyroot/PyROOTTFilePythonize.cxx * @author Sebastien Binet @@ -23,26 +21,18 @@ namespace RootUtils { PyBytes::PyBytes(size_t buf_sz) : sz(buf_sz>0?buf_sz:0), - buf(0) + buf(sz) { - if (this->sz>0) { - buf = (char*)malloc(sizeof(char)*this->sz); - } } PyBytes::~PyBytes() { - free(buf); buf = 0; } PyBytes::PyBytes(const PyBytes& rhs) : sz(rhs.sz), - buf(0) + buf(rhs.buf) { - if (this->sz>0) { - buf = (char*)malloc(sizeof(char)*this->sz); - buf = (char*)memcpy(buf, rhs.buf, this->sz); - } } PyBytes& @@ -50,11 +40,7 @@ PyBytes::operator=(const PyBytes& rhs) { if (this != &rhs) { this->sz = rhs.sz; - free(this->buf); this->buf = 0; - if (this->sz>0) { - this->buf = (char*)malloc(sizeof(char)*this->sz); - this->buf = (char*)memcpy(this->buf, rhs.buf, rhs.sz); - } + this->buf = rhs.buf; } return *this; } @@ -85,7 +71,7 @@ _pythonize_read_root_file(TFile* f, Int_t len) len = (len>remain) ? remain : len; PyBytes buf(len); - if (f->ReadBuffer((char*)buf.buf, buf.sz)) { + if (f->ReadBuffer((char*)buf.buf.data(), buf.sz)) { //err return PyBytes(0); } diff --git a/Tools/PyUtils/python/RootUtils.py b/Tools/PyUtils/python/RootUtils.py index 8721cb3042a31a21798f8586d852598cf8f0f9a6..b921060778c5ec902571b6e6f095333e25c79f77 100644 --- a/Tools/PyUtils/python/RootUtils.py +++ b/Tools/PyUtils/python/RootUtils.py @@ -19,18 +19,10 @@ __all__ = [ import os import re import six -from array import array from .Decorators import memoize ### functions ----------------------------------------------------------------- -# Set buffer size, in bytes. -# The argument to reshape is in elements, not bytes. -def _set_byte_size (buf, sz): - eltsz = array(buf.typecode).itemsize - buf.reshape ((sz // eltsz,)) - return - def import_root(batch=True): """a helper method to wrap the 'import ROOT' statement to prevent ROOT from screwing up the display or loading graphics libraries when in batch @@ -158,19 +150,17 @@ def _pythonize_tfile(): """ SZ = 4096 + # FIXME: Once we drop py2, we can simplify this by using a bytes + # object directly instead of PyBytes. if size>=0: #size = _adjust_sz(size) #print ("-->0",self.tell(),size) c_buf = read_root_file(self, size) if c_buf and c_buf.sz: - #print ("-->1",self.tell(),c_buf.sz) - #self.seek(c_buf.sz+self.tell()) - #print ("-->2",self.tell()) - buf = c_buf.buffer() - _set_byte_size (buf, c_buf.sz) + v = c_buf.buf if six.PY3: - return buf.tobytes() - return str(buf[:]) + return bytes([ord(v[i]) for i in range(v.size())]) + return ''.join([v[i] for i in range(v.size())]) return '' else: size = SZ @@ -179,12 +169,18 @@ def _pythonize_tfile(): #size = _adjust_sz(size) c_buf = read_root_file(self, size) if c_buf and c_buf.sz: - buf = c_buf.buffer() - _set_byte_size (buf, c_buf.sz) - out.append(str(buf[:])) + v = c_buf.buf + if six.PY3: + chunk = bytes([ord(v[i]) for i in range(v.size())]) + else: + chunk = ''.join([v[i] for i in range(v.size())]) + out.append(chunk) else: break + if six.PY3: + return b''.join(out) return ''.join(out) + root.TFile.read = read del read