Skip to content

Commit f85a31c

Browse files
committed
[Python] Make the tests work for any CPyCppyy memory policy
This makes the tests work in general, no matter which memory policy is used (in particular, make the case of the "strict" memory policy work).
1 parent 6ef1063 commit f85a31c

File tree

6 files changed

+51
-30
lines changed

6 files changed

+51
-30
lines changed

python/basic/PyROOT_basictests.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,25 @@ def test1Strings( self ):
279279
def test2Lists( self ):
280280
"""Test list/TList behavior and compatibility"""
281281

282+
# A TList is non-owning. In order to fill the TList with in-place created
283+
# objects, we write this little helper to create a new TObjectString
284+
# whose lifetime is managed by a separate owning Python list.
285+
objects = []
286+
def make_obj_str(s):
287+
objects.append(TObjString(s))
288+
return objects[-1]
289+
282290
l = TList()
283-
l.Add( TObjString('a') )
284-
l.Add( TObjString('b') )
285-
l.Add( TObjString('c') )
286-
l.Add( TObjString('d') )
287-
l.Add( TObjString('e') )
288-
l.Add( TObjString('f') )
289-
l.Add( TObjString('g') )
290-
l.Add( TObjString('h') )
291-
l.Add( TObjString('i') )
292-
l.Add( TObjString('j') )
291+
l.Add( make_obj_str('a') )
292+
l.Add( make_obj_str('b') )
293+
l.Add( make_obj_str('c') )
294+
l.Add( make_obj_str('d') )
295+
l.Add( make_obj_str('e') )
296+
l.Add( make_obj_str('f') )
297+
l.Add( make_obj_str('g') )
298+
l.Add( make_obj_str('h') )
299+
l.Add( make_obj_str('i') )
300+
l.Add( make_obj_str('j') )
293301

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

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

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

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

308-
self.assertTrue( TObjString('b') in l )
309-
self.assertTrue( not TObjString('x') in l )
316+
self.assertTrue( make_obj_str('b') in l )
317+
self.assertTrue( not make_obj_str('x') in l )
310318

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

320-
l[2:5] = [ TObjString('1'), TObjString('2') ]
328+
l[2:5] = [ make_obj_str('1'), make_obj_str('2') ]
321329
self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j'] )
322330

323-
l[6:6] = [ TObjString('3') ]
331+
l[6:6] = [ make_obj_str('3') ]
324332
self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3'] )
325333

326-
l.append( TObjString('4') )
334+
l.append( make_obj_str('4') )
327335
self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4'] )
328336

329-
l.extend( [ TObjString('5'), TObjString('j') ] )
337+
l.extend( [ make_obj_str('5'), make_obj_str('j') ] )
330338
self.assertEqual( list(l), ['a', 'b', '1', '2', 'i', 'j', '3', '4', '5', 'j'] )
331339
self.assertEqual( l.count( 'b' ), 1 )
332340
self.assertEqual( l.count( 'j' ), 2 )
333341
self.assertEqual( l.count( 'x' ), 0 )
334342

335-
self.assertEqual( l.index( TObjString( 'i' ) ), 4 )
336-
self.assertRaises( ValueError, l.index, TObjString( 'x' ) )
343+
self.assertEqual( l.index( make_obj_str( 'i' ) ), 4 )
344+
self.assertRaises( ValueError, l.index, make_obj_str( 'x' ) )
337345

338-
l.insert( 3, TObjString('6') )
339-
l.insert( 20, TObjString('7') )
340-
l.insert( -1, TObjString('8') )
346+
l.insert( 3, make_obj_str('6') )
347+
l.insert( 20, make_obj_str('7') )
348+
l.insert( -1, make_obj_str('8') )
341349
if not self.legacy_pyroot:
342350
# The pythonisation of TSeqCollection in experimental PyROOT mimics the
343351
# behaviour of the Python list, in this case for insert.
@@ -346,7 +354,7 @@ def test2Lists( self ):
346354
# element of the list.
347355
self.assertEqual(list(l), ['a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '8', '7'])
348356
# Re-synchronize with current PyROOT's list
349-
l.insert(0, TObjString('8'))
357+
l.insert(0, make_obj_str('8'))
350358
self.assertEqual(list(l), ['8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '8', '7'])
351359
l.pop(-2)
352360
self.assertEqual(list(l), ['8', 'a', 'b', '1', '6', '2', 'i', 'j', '3', '4', '5', 'j', '7'])
@@ -356,10 +364,10 @@ def test2Lists( self ):
356364
self.assertEqual( l.pop(3), '1' )
357365
self.assertEqual( list(l), ['8', 'a', 'b', '6', '2', 'i', 'j', '3', '4', '5', 'j'] )
358366

359-
l.remove( TObjString( 'j' ) )
360-
l.remove( TObjString( '3' ) )
367+
l.remove( make_obj_str( 'j' ) )
368+
l.remove( make_obj_str( '3' ) )
361369

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

365373
l.reverse()

python/distrdf/backends/check_friend_trees_alignment.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def create_chain():
2020
for i in range(3, 6):
2121
friend.Add(f"{FILENAMES[i]}?#{TREENAMES[i]}")
2222

23+
# The friend will be owned by the main TChain
24+
ROOT.SetOwnership(friend, False)
25+
2326
main.AddFriend(friend, "friend")
2427

2528
return main

python/memory/PyROOT_memorytests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ def test3ObjectCallHeuristics( self ):
9797
kMemoryStrict = ROOT.kMemoryStrict
9898
kMemoryHeuristics = ROOT.kMemoryHeuristics
9999

100+
# This unit test assumes that the global memory policy is set to
101+
# "heuristics" at the beginning, so let's make sure of that.
102+
old_memory_policy = SetMemoryPolicy( kMemoryHeuristics )
103+
100104
# reference calls should not give up ownership
101105
a = MemTester()
102106
self.assertEqual( MemTester.counter, 1 )
@@ -191,6 +195,8 @@ def test3ObjectCallHeuristics( self ):
191195
del c # c not derived from TObject, no notification
192196
self.assertEqual( MemTester.counter, 0 )
193197

198+
SetMemoryPolicy( old_memory_policy )
199+
194200
def test4DestructionOfDerivedClass( self ):
195201
"""Derived classes should call base dtor automatically"""
196202

python/pythonizations/PyROOT_smartptrtest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ def test04_reset(self):
110110
# ROOT-10245
111111
import ROOT
112112

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

python/pythonizations/SmartPtr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class MyShareable {
2222

2323
int MyShareable::sInstances = 0;
2424

25-
shared_ptr<MyShareable> mine = shared_ptr<MyShareable>(new MyShareable);
25+
shared_ptr<MyShareable> mine = std::make_shared<MyShareable>();
2626

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

2929
shared_ptr<MyShareable> gime_mine();
3030
shared_ptr<MyShareable>* gime_mine_ptr();

python/regression/PyROOT_regressiontests.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,10 @@ def test1GetListOfGraphs(self):
589589
# ROOT-9040
590590
if not legacy_pyroot:
591591
mg = ROOT.TMultiGraph()
592-
mg.Add(ROOT.TGraph())
592+
tg = ROOT.TGraph()
593+
# The TMultiGraph will take the ownership of the added TGraphs
594+
ROOT.SetOwnership(tg, False)
595+
mg.Add(tg)
593596

594597
l = mg.GetListOfGraphs()
595598
self.assertEqual(l.TestBit(ROOT.kMustCleanup), False)

0 commit comments

Comments
 (0)