Skip to content

Commit

Permalink
[PyROOT] Adapt tests to the new default memory policy "strict"
Browse files Browse the repository at this point in the history
  • Loading branch information
guitargeek committed Sep 4, 2023
1 parent 6ab9b4e commit b75eb69
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 30 deletions.
60 changes: 34 additions & 26 deletions python/basic/PyROOT_basictests.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,25 @@ def test1Strings( self ):
def test2Lists( self ):
"""Test list/TList behavior and compatibility"""

# A TList is non-owning. In order to fill the TList with in-place created
# objects, we write this little helper to create a new TObjectString
# whose lifetime is managed by a separate owning Python list.
objects = []
def make_obj_str(s):
objects.append(TObjString(s))
return objects[-1]

l = TList()
l.Add( TObjString('a') )
l.Add( TObjString('b') )
l.Add( TObjString('c') )
l.Add( TObjString('d') )
l.Add( TObjString('e') )
l.Add( TObjString('f') )
l.Add( TObjString('g') )
l.Add( TObjString('h') )
l.Add( TObjString('i') )
l.Add( TObjString('j') )
l.Add( make_obj_str('a') )
l.Add( make_obj_str('b') )
l.Add( make_obj_str('c') )
l.Add( make_obj_str('d') )
l.Add( make_obj_str('e') )
l.Add( make_obj_str('f') )
l.Add( make_obj_str('g') )
l.Add( make_obj_str('h') )
l.Add( make_obj_str('i') )
l.Add( make_obj_str('j') )

self.assertEqual( len(l), 10 )
self.assertEqual( l[3], 'd' )
Expand All @@ -299,14 +307,14 @@ def test2Lists( self ):

self.assertEqual( list(l), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] )

l[3] = TObjString('z')
l[3] = make_obj_str('z')
self.assertEqual( list(l), ['a', 'b', 'c', 'z', 'e', 'f', 'g', 'h', 'i', 'j'] )

del l[2]
self.assertEqual( list(l), ['a', 'b', 'z', 'e', 'f', 'g', 'h', 'i', 'j'] )

self.assertTrue( TObjString('b') in l )
self.assertTrue( not TObjString('x') in l )
self.assertTrue( make_obj_str('b') in l )
self.assertTrue( not make_obj_str('x') in l )

self.assertEqual( list(l[2:6]), ['z', 'e', 'f', 'g'] )
self.assertEqual( list(l[2:6:2]), ['z', 'f'] )
Expand All @@ -317,27 +325,27 @@ def test2Lists( self ):
del l[2:4]
self.assertEqual( list(l), ['a', 'b', 'f', 'g', 'h', 'i', 'j'] )

l[2:5] = [ TObjString('1'), TObjString('2') ]
l[2:5] = [ make_obj_str('1'), make_obj_str('2') ]
self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j'] )

l[6:6] = [ TObjString('3') ]
l[6:6] = [ make_obj_str('3') ]
self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3'] )

l.append( TObjString('4') )
l.append( make_obj_str('4') )
self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4'] )

l.extend( [ TObjString('5'), TObjString('j') ] )
l.extend( [ make_obj_str('5'), make_obj_str('j') ] )
self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4', '5', 'j'] )
self.assertEqual( l.count( 'b' ), 1 )
self.assertEqual( l.count( 'j' ), 2 )
self.assertEqual( l.count( 'x' ), 0 )

self.assertEqual( l.index( TObjString( 'i' ) ), 4 )
self.assertRaises( ValueError, l.index, TObjString( 'x' ) )
self.assertEqual( l.index( make_obj_str( 'i' ) ), 4 )
self.assertRaises( ValueError, l.index, make_obj_str( 'x' ) )

l.insert( 3, TObjString('6') )
l.insert( 20, TObjString('7') )
l.insert( -1, TObjString('8') )
l.insert( 3, make_obj_str('6') )
l.insert( 20, make_obj_str('7') )
l.insert( -1, make_obj_str('8') )
if not self.legacy_pyroot:
# The pythonisation of TSeqCollection in experimental PyROOT mimics the
# behaviour of the Python list, in this case for insert.
Expand All @@ -346,7 +354,7 @@ def test2Lists( self ):
# element of the list.
self.assertEqual(list(l), ['a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '8', '7'])
# Re-synchronize with current PyROOT's list
l.insert(0, TObjString('8'))
l.insert(0, make_obj_str('8'))
self.assertEqual(list(l), ['8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '8', '7'])
l.pop(-2)
self.assertEqual(list(l), ['8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '7'])
Expand All @@ -356,10 +364,10 @@ def test2Lists( self ):
self.assertEqual( l.pop(3), '1' )
self.assertEqual( list(l), ['8', 'a', 'b', '6', '2', 'i', 'j', '3', '4', '5', 'j'] )

l.remove( TObjString( 'j' ) )
l.remove( TObjString( '3' ) )
l.remove( make_obj_str( 'j' ) )
l.remove( make_obj_str( '3' ) )

self.assertRaises( ValueError, l.remove, TObjString( 'x' ) )
self.assertRaises( ValueError, l.remove, make_obj_str( 'x' ) )
self.assertEqual( list(l), ['8', 'a', 'b', '6', '2', 'i', '4', '5', 'j'] )

l.reverse()
Expand Down
8 changes: 8 additions & 0 deletions python/memory/PyROOT_memorytests.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def test3ObjectCallHeuristics( self ):
kMemoryStrict = ROOT.kMemoryStrict
kMemoryHeuristics = ROOT.kMemoryHeuristics

# This unit test assumes that the global memory policy is set to
# "heuristics" at the beginning. However, as of ROOT 6.30, the default
# policy is "strict", so we need to reset this at the beginning to
# "heuristics" and reset the default policy at the end.
SetMemoryPolicy( kMemoryHeuristics )

# reference calls should not give up ownership
a = MemTester()
self.assertEqual( MemTester.counter, 1 )
Expand Down Expand Up @@ -191,6 +197,8 @@ def test3ObjectCallHeuristics( self ):
del c # c not derived from TObject, no notification
self.assertEqual( MemTester.counter, 0 )

SetMemoryPolicy( kMemoryStrict )

def test4DestructionOfDerivedClass( self ):
"""Derived classes should call base dtor automatically"""

Expand Down
3 changes: 2 additions & 1 deletion python/pythonizations/PyROOT_smartptrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ def test04_reset(self):
# ROOT-10245
import ROOT

ROOT.gROOT.ProcessLine('std::shared_ptr<TObject> optr(new TObject());')
ROOT.gROOT.ProcessLine('auto optr = std::make_shared<TObject>();')
o2 = ROOT.TObject()
ROOT.SetOwnership(o2, False) # This object will be owned by the smart pointer
ROOT.optr.__smartptr__().reset(o2)
assert ROOT.optr == o2

Expand Down
4 changes: 2 additions & 2 deletions python/pythonizations/SmartPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class MyShareable {

int MyShareable::sInstances = 0;

shared_ptr<MyShareable> mine = shared_ptr<MyShareable>(new MyShareable);
shared_ptr<MyShareable> mine = std::make_shared<MyShareable>();

void renew_mine() { mine = shared_ptr<MyShareable>(new MyShareable); }
void renew_mine() { mine = std::make_shared<MyShareable>(); }

shared_ptr<MyShareable> gime_mine();
shared_ptr<MyShareable>* gime_mine_ptr();
Expand Down
3 changes: 2 additions & 1 deletion python/regression/PyROOT_regressiontests.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,8 @@ def test1GetListOfGraphs(self):
# ROOT-9040
if not legacy_pyroot:
mg = ROOT.TMultiGraph()
mg.Add(ROOT.TGraph())
tg = ROOT.TGraph()
mg.Add(tg)

l = mg.GetListOfGraphs()
self.assertEqual(l.TestBit(ROOT.kMustCleanup), False)
Expand Down

0 comments on commit b75eb69

Please sign in to comment.