From e119bf06019ef67e7091571d305980735daca112 Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Wed, 4 Jul 2018 00:22:48 +0200 Subject: [PATCH] fixed py3 bugs --- .travis.yml | 48 ++++++++++++-------------- examples/getting_started_listing_04.py | 2 +- examples/guide_histogram_pickle.py | 4 +-- examples/guide_python_histogram.py | 10 +++--- src/python/axis.cpp | 14 +++++--- 5 files changed, 39 insertions(+), 39 deletions(-) diff --git a/.travis.yml b/.travis.yml index 345a418d..f719da6d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,38 +28,34 @@ branches: matrix: include: - # - os: linux # minimum gcc - # python: 2.7 - # env: - # CC=gcc CXX=g++ - # BUILD_PYTHON=OFF - # BUILD_NUMPY=OFF - # BUILD_SERIALIZATION=OFF - # - os: linux # maximum gcc - # python: 2.7 - # env: - # CC=gcc CXX=g++ - # BUILD_PYTHON=ON - # BUILD_NUMPY=ON - # BUILD_SERIALIZATION=ON + - os: linux # minimum gcc + env: + CC=gcc CXX=g++ PYTHON_VERSION=2.7 + BUILD_PYTHON=OFF + BUILD_NUMPY=OFF + BUILD_SERIALIZATION=OFF + - os: linux # maximum gcc + env: + CC=gcc CXX=g++ PYTHON_VERSION=2.7 + BUILD_PYTHON=ON + BUILD_NUMPY=ON + BUILD_SERIALIZATION=ON - os: linux # maximum gcc env: CC=gcc CXX=g++ PYTHON_VERSION=3.6 BUILD_PYTHON=ON BUILD_NUMPY=OFF BUILD_SERIALIZATION=ON - # - os: linux # maximum clang - # python: 2.7 - # env: - # CC=clang CXX=clang++ - # BUILD_PYTHON=ON - # BUILD_NUMPY=ON - # BUILD_SERIALIZATION=ON - # - os: linux # coverage gcc - # python: 2.7 - # env: - # CC=gcc CXX=g++ - # CMAKE_BUILD_TYPE=coverage + - os: linux # maximum clang + env: + CC=clang CXX=clang++ + BUILD_PYTHON=ON PYTHON_VERSION=2.7 + BUILD_NUMPY=ON + BUILD_SERIALIZATION=ON + - os: linux # coverage gcc + env: + CC=gcc CXX=g++ PYTHON_VERSION=2.7 + CMAKE_BUILD_TYPE=coverage git: depth: 1 diff --git a/examples/getting_started_listing_04.py b/examples/getting_started_listing_04.py index 3471b661..5132ab9a 100644 --- a/examples/getting_started_listing_04.py +++ b/examples/getting_started_listing_04.py @@ -13,6 +13,6 @@ for x in (2e0, 2e1, 2e2, 2e3, 2e4): # iterate over bins and access bin counter for idx, (lower, upper) in enumerate(h.axis(0)): print("bin {0} x in [{1}, {2}): {3} +/- {4}".format( - idx, lower, upper, h.bin(idx).value, h.bin(idx).variance ** 0.5)) + idx, lower, upper, h.bin(idx).value, h.bin(idx).variance ** 0.5)) #] diff --git a/examples/guide_histogram_pickle.py b/examples/guide_histogram_pickle.py index 686f9089..7dd3aa10 100644 --- a/examples/guide_histogram_pickle.py +++ b/examples/guide_histogram_pickle.py @@ -17,9 +17,9 @@ print(h4.bin(0).value, h4.bin(1).value) # prints: 2.0 2.0 # now save the histogram -with open("h4_saved.pkl", "w") as f: +with open("h4_saved.pkl", "wb") as f: pickle.dump(h4, f) -with open("h4_saved.pkl", "r") as f: +with open("h4_saved.pkl", "rb") as f: h5 = pickle.load(f) print(h4 == h5) diff --git a/examples/guide_python_histogram.py b/examples/guide_python_histogram.py index 234dcf82..eb4f2c34 100644 --- a/examples/guide_python_histogram.py +++ b/examples/guide_python_histogram.py @@ -1,5 +1,5 @@ #[ guide_python_histogram - +from __future__ import print_function import histogram as hg # make 1-d histogram with 5 logarithmic bins from 1e0 to 1e5 @@ -11,14 +11,14 @@ for x in (2e0, 2e1, 2e2, 2e3, 2e4): # iterate over bins and access bin counter for idx, (lower, upper) in enumerate(h.axis(0)): - print "bin {0} x in [{1}, {2}): {3} +/- {4}".format( - idx, lower, upper, h.bin(idx).value, h.bin(idx).variance ** 0.5) + print("bin {0} x in [{1}, {2}): {3} +/- {4}".format( + idx, lower, upper, h.bin(idx).value, h.bin(idx).variance ** 0.5)) # under- and overflow bins are accessed like in C++ lo, up = h.axis(0)[-1] -print "underflow [{0}, {1}): {2} +/- {3}".format(lo, up, h.bin(-1).value, h.bin(-1).variance) +print("underflow [{0}, {1}): {2} +/- {3}".format(lo, up, h.bin(-1).value, h.bin(-1).variance)) lo, up = h.axis(0)[5] -print "overflow [{0}, {1}): {2} +/- {3}".format(lo, up, h.bin(5).value, h.bin(5).variance) +print("overflow [{0}, {1}): {2} +/- {3}".format(lo, up, h.bin(5).value, h.bin(5).variance)) # prints: # bin 0 x in [1.0, 10.0): 4.0 +/- 4.0 diff --git a/src/python/axis.cpp b/src/python/axis.cpp index 9b7e42e1..9d95306e 100644 --- a/src/python/axis.cpp +++ b/src/python/axis.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include @@ -89,7 +89,8 @@ bp::object variable_init(bp::tuple args, bp::dict kwargs) { auto uo = bha::uoflow::on; while (bp::len(kwargs) > 0) { bp::tuple kv = kwargs.popitem(); - boost::string_view k(bp::extract(kv[0]), bp::len(kv[0])); + const char* key_cstr = bp::extract(kv[0]); + boost::string_view k(key_cstr, bp::len(kv[0])); bp::object v = kv[1]; if (k == "label") label = boost::string_view(bp::extract(v), bp::len(v)); @@ -120,7 +121,8 @@ bp::object category_init(bp::tuple args, bp::dict kwargs) { boost::string_view label; while (bp::len(kwargs) > 0) { bp::tuple kv = kwargs.popitem(); - boost::string_view k(bp::extract(kv[0]), bp::len(kv[0])); + const char* key_cstr = bp::extract(kv[0]); + boost::string_view k(key_cstr, bp::len(kv[0])); bp::object v = kv[1]; if (k == "label") label = boost::string_view(bp::extract(v), bp::len(v)); @@ -270,7 +272,9 @@ void register_axis_types() { class_("generic_iterator", init()) .def("__iter__", &generic_iterator::self) - .def("next", &generic_iterator::next); + .def("__next__", &generic_iterator::next) // Python3 + .def("next", &generic_iterator::next) // Python2 + ; class_>( "regular", @@ -319,7 +323,7 @@ void register_axis_types() { no_init) .def(init( (arg("self"), arg("bin"), arg("phase") = 0.0, - arg("perimeter") = boost::math::double_constants::two_pi, + arg("perimeter") = bh::detail::two_pi, arg("label") = ""))) .def(axis_suite>());