From 92019321aa31dbf237ca1f46d74a87eade035877 Mon Sep 17 00:00:00 2001 From: scott snyder <snyder@bnl.gov> Date: Wed, 30 Oct 2019 16:11:19 +0100 Subject: [PATCH] McParticleUtils: Fix py3 compilation. Fix compilation with python 3. --- .../McParticleUtils/CMakeLists.txt | 3 ++- .../McParticleUtils/python/DecayParser.py | 20 ++++++++++--------- .../McParticleUtils/src/DecayParser.cxx | 13 ++++++++++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/PhysicsAnalysis/TruthParticleID/McParticleUtils/CMakeLists.txt b/PhysicsAnalysis/TruthParticleID/McParticleUtils/CMakeLists.txt index 271a639e5f3..2d9f851d8c3 100644 --- a/PhysicsAnalysis/TruthParticleID/McParticleUtils/CMakeLists.txt +++ b/PhysicsAnalysis/TruthParticleID/McParticleUtils/CMakeLists.txt @@ -15,6 +15,7 @@ atlas_depends_on_subdirs( PUBLIC PRIVATE AtlasTest/TestTools Control/StoreGate + Control/RootUtils TestPolicy ) # External dependencies: @@ -32,7 +33,7 @@ atlas_add_library( McParticleUtils INCLUDE_DIRS ${HEPMC_INCLUDE_DIRS} PRIVATE_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} ${HEPPDT_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} ${CLHEP_INCLUDE_DIRS} ${CPPUNIT_INCLUDE_DIRS} PRIVATE_DEFINITIONS ${CLHEP_DEFINITIONS} - LINK_LIBRARIES ${HEPMC_LIBRARIES} AthenaKernel AthContainers EventKernel GaudiKernel AnalysisUtilsLib StoreGateLib SGtests + LINK_LIBRARIES ${HEPMC_LIBRARIES} AthenaKernel AthContainers EventKernel GaudiKernel AnalysisUtilsLib StoreGateLib SGtests RootUtils PRIVATE_LINK_LIBRARIES ${Boost_LIBRARIES} ${HEPPDT_LIBRARIES} ${PYTHON_LIBRARIES} ${CLHEP_LIBRARIES} ${CPPUNIT_LIBRARIES} ) # Install files from the package: diff --git a/PhysicsAnalysis/TruthParticleID/McParticleUtils/python/DecayParser.py b/PhysicsAnalysis/TruthParticleID/McParticleUtils/python/DecayParser.py index e5bc7e2ee8a..2d61c416633 100644 --- a/PhysicsAnalysis/TruthParticleID/McParticleUtils/python/DecayParser.py +++ b/PhysicsAnalysis/TruthParticleID/McParticleUtils/python/DecayParser.py @@ -1,9 +1,11 @@ -# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration +# Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration # @file: McParticleUtils/python/DecayParser.py # @purpose: implement the parsing/tokenization of decay-patterns' strings # @author: Sebastien Binet <binet@cern.ch> +from __future__ import print_function + import re _slot_separator = "+" @@ -16,7 +18,7 @@ def py_parse (cmd): cmd = re.sub(pattern=" ", repl="", string=cmd) -## print "=> [%s]"%cmd +## print ("=> [%s]"%cmd) if cmd == _decay_arrow: # special case of a single arrow without any parent nor child: @@ -25,7 +27,7 @@ def py_parse (cmd): buf = cmd.split (_decay_arrow) assert (len(buf)==2) -## print "==> buf:",buf +## print ("==> buf:",buf) parents = process_block (buf[0]) children= process_block (buf[1]) @@ -39,14 +41,14 @@ def process_block (cmd): if __name__ == "__main__": - print ":"*80 - print "::: tests..." + print (":"*80) + print ("::: tests...") for cmd in ("-1|-2|-3|-4|-5|-6|21 + 1|2|3|4|5|6|21 -> ", "-> 91|92|94" ): - print "::: testing [%s]..." % cmd - print "::: result:",py_parse (cmd) + print ("::: testing [%s]..." % cmd) + print ("::: result:",py_parse (cmd)) - print "::: bye." - print ":"*80 + print ("::: bye.") + print (":"*80) diff --git a/PhysicsAnalysis/TruthParticleID/McParticleUtils/src/DecayParser.cxx b/PhysicsAnalysis/TruthParticleID/McParticleUtils/src/DecayParser.cxx index 4115e4d6e48..7c67f75e510 100755 --- a/PhysicsAnalysis/TruthParticleID/McParticleUtils/src/DecayParser.cxx +++ b/PhysicsAnalysis/TruthParticleID/McParticleUtils/src/DecayParser.cxx @@ -1,5 +1,5 @@ /* - Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration */ /////////////////////////////////////////////////////////////////// @@ -10,6 +10,7 @@ // Python includes #include "Python.h" +#include "RootUtils/PyGetString.h" // STL includes #include <iostream> @@ -160,14 +161,22 @@ void DecayParser::parse( const std::string& inputCmd ) PyObject *sc = PyTuple_GET_ITEM (res, 0); Py_XINCREF (sc); +#if PY_MAJOR_VERSION < 3 if (!sc || !PyInt_Check (sc)) { +#else + if (!sc || !PyLong_Check (sc)) { +#endif Py_XDECREF (sc); Py_DECREF (res); std::string error = "corrupted return code"; throw std::runtime_error (error); } +#if PY_MAJOR_VERSION < 3 Py_ssize_t status = PyInt_AsSsize_t (sc); +#else + Py_ssize_t status = PyLong_AsSsize_t (sc); +#endif if (status != 0) { Py_DECREF (sc); Py_DECREF (res); @@ -409,7 +418,7 @@ py_to_cpp (PyObject* candidates, return false; } - parsed[i][j] = std::string(PyString_AS_STRING(pdgid)); + parsed[i][j] = RootUtils::PyGetString (pdgid).first; } Py_DECREF (cand); -- GitLab