diff --git a/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_tcollection.py b/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_tcollection.py index 7e1f0cf785f3ba175641afc0a98ddb56e28a8630..f1e1c8a8ab291435e3885d81e74d5f45201cfe25 100644 --- a/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_tcollection.py +++ b/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_tcollection.py @@ -87,12 +87,15 @@ def _imul_pyz(self, n): # Python iteration -def _begin_pyz(self): +def _iter_pyz(self): + # Generator function to iterate on TCollections # Parameters: # - self: collection to be iterated - # Returns: - # - TIter iterator on collection - return TIter(self) + it = TIter(self) + o = it.Next() + while o: + yield o + o = it.Next() @pythonization() @@ -117,20 +120,7 @@ def pythonize_tcollection(klass, name): klass.__rmul__ = _mul_pyz klass.__imul__ = _imul_pyz - # Make TCollections iterable. - # In Pythonize.cxx, cppyy injects an `__iter__` method into - # any class that has a `begin` and an `end` methods. - # This is the case of TCollection and its subclasses, where - # cppyy associates `__iter__` with a function that returns the - # result of calling `begin`, i.e. a TIter that already points - # to the first element of the collection. - # That setting breaks the iteration on TCollections, since the - # first time `Next` is called on the iterator it will return - # the second element of the collection (if any), thus skipping - # the first one. - # By pythonising `begin` here, we make sure the iterator returned - # points to nowhere, and it will return the first element of the - # collection on the first invocation of `Next`. - klass.begin = _begin_pyz + # Make TCollections iterable + klass.__iter__ = _iter_pyz return True diff --git a/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_titer.py b/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_titer.py deleted file mode 100644 index 7d41f82f472399cbb7a96809ec4aebccf4a76cb6..0000000000000000000000000000000000000000 --- a/bindings/pyroot_experimental/PyROOT/python/ROOT/pythonization/_titer.py +++ /dev/null @@ -1,39 +0,0 @@ -# Author: Enric Tejedor CERN 01/2019 - -################################################################################ -# Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. # -# All rights reserved. # -# # -# For the licensing terms see $ROOTSYS/LICENSE. # -# For the list of contributors see $ROOTSYS/README/CREDITS. # -################################################################################ - -from ROOT import pythonization -import cppyy - -def _next_pyz(self): - # Parameters: - # - self: iterator on a collection - # Returns: - # - next object in the collection, or raises StopIteration if none - o = self.Next() - if o: - return o - else: - raise StopIteration() - -# The TIter class does not go through the mechanism of lazy pythonisations of -# cppyy, since it is used before such mechanism is put in place. Therefore, we -# define here the pythonisations for TIter as immediate, i.e. executed upon -# import of the ROOT module -@pythonization(lazy = False) -def pythonize_titer(): - klass = cppyy.gbl.TIter - - # Make TIter a Python iterator. - # This makes it possible to iterate over TCollections, since Cppyy - # injects on them an `__iter__` method that returns a TIter. - klass.__next__ = _next_pyz # Py3 - klass.next = _next_pyz # Py2 - - return True diff --git a/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt b/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt index 24e27bad82b689a056591c49046ed22cbc4b6072..d5942b39ba489a3890d29bbe4f82d58cd2fbe8e9 100644 --- a/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt +++ b/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt @@ -17,9 +17,6 @@ ROOT_ADD_PYUNITTEST(pyroot_pyz_ttree_setbranchaddress ttree_setbranchaddress.py ROOT_ADD_PYUNITTEST(pyroot_pyz_ttree_branch ttree_branch.py COPY_TO_BUILDDIR TreeHelper.h) -# TIter pythonisations -ROOT_ADD_PYUNITTEST(pyroot_pyz_titer_iterator titer_iterator.py) - # TCollection and subclasses pythonizations ROOT_ADD_PYUNITTEST(pyroot_pyz_tcollection_len tcollection_len.py) ROOT_ADD_PYUNITTEST(pyroot_pyz_tcollection_listmethods tcollection_listmethods.py) diff --git a/bindings/pyroot_experimental/PyROOT/test/titer_iterator.py b/bindings/pyroot_experimental/PyROOT/test/titer_iterator.py deleted file mode 100644 index 84fa536c4cc1833ec67f6557d628abc6b7e8590d..0000000000000000000000000000000000000000 --- a/bindings/pyroot_experimental/PyROOT/test/titer_iterator.py +++ /dev/null @@ -1,33 +0,0 @@ -import unittest - -import ROOT -from libcppyy import SetOwnership - - -class TIterIterator(unittest.TestCase): - """ - Test for the pythonization that allows instances of TIter to - behave as Python iterators. - """ - - num_elems = 3 - - # Helpers - def create_tcollection(self): - c = ROOT.TList() - for _ in range(self.num_elems): - o = ROOT.TObject() - # Prevent immediate deletion of C++ TObjects - SetOwnership(o, False) - c.Add(o) - - return c - - # Tests - def test_iterator(self): - c = self.create_tcollection() - - itc1 = ROOT.TIter(c) - itc2 = ROOT.TIter(c) - for _ in range(c.GetEntries()): - self.assertEqual(next(itc1), itc2.Next())