Repository navigation
[CPyCppyy] Correct check for temporaries in Python 3.14 - #19017
Conversation
Test Results 19 files 19 suites 3d 9h 46m 37s ⏱️ For more details on these failures, see this check. Results for commit 3194ba8. ♻️ This comment has been updated with latest results. |
|
Here the detailed failure without this PR: =================================== FAILURES ===================================
________________________ TestCPP11FEATURES.test07_move _________________________
self = <test.test_cpp11features.TestCPP11FEATURES object at 0x7f2bb89fe140>
def test07_move(self):
"""Move construction, assignment, and methods"""
import cppyy, gc
def moveit(T):
assert T.s_instance_counter == 0
from cppyy.gbl import std
# move constructor
i1 = T()
assert T.s_move_counter == 0
i2 = T(i1) # cctor
assert T.s_move_counter == 0
if ispypy or 0x3000000 <= sys.hexversion:
i3 = T(std.move(T())) # can't check ref-count
else:
i3 = T(T()) # should call move, not memoized cctor
assert T.s_move_counter == 1
i3 = T(std.move(T())) # both move and ref-count
assert T.s_move_counter == 2
i4 = T(std.move(i1))
assert T.s_move_counter == 3
# move assignment
i4.__assign__(i2)
assert T.s_move_counter == 3
if ispypy or 0x3000000 <= sys.hexversion:
i4.__assign__(std.move(T())) # can't check ref-count
else:
i4.__assign__(T())
assert T.s_move_counter == 4
i4.__assign__(std.move(i2))
assert T.s_move_counter == 5
del i1, i2, i3, i4
gc.collect()
assert T.s_instance_counter == 0
# order of moving and normal functions are reversed in 1, 2, for
# overload resolution testing
> moveit(cppyy.gbl.TestMoving1)
../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py:255:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
T = <class cppyy.gbl.TestMoving1 at 0x2f42f3d0>
def moveit(T):
assert T.s_instance_counter == 0
from cppyy.gbl import std
# move constructor
i1 = T()
assert T.s_move_counter == 0
i2 = T(i1) # cctor
assert T.s_move_counter == 0
if ispypy or 0x3000000 <= sys.hexversion:
i3 = T(std.move(T())) # can't check ref-count
else:
i3 = T(T()) # should call move, not memoized cctor
assert T.s_move_counter == 1
i3 = T(std.move(T())) # both move and ref-count
assert T.s_move_counter == 2
i4 = T(std.move(i1))
assert T.s_move_counter == 3
# move assignment
i4.__assign__(i2)
> assert T.s_move_counter == 3
E assert 4 == 3
E + where 4 = <class cppyy.gbl.TestMoving1 at 0x2f42f3d0>.s_move_counter
../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py:238: AssertionError
____________ TestCPP11FEATURES.test15_unique_ptr_template_deduction ____________
self = <test.test_cpp11features.TestCPP11FEATURES object at 0x7f2bb8870a10>
def test15_unique_ptr_template_deduction(self):
"""Argument type deduction with std::unique_ptr"""
import cppyy
cppyy.cppdef("""namespace UniqueTempl {
template <typename T>
std::unique_ptr<T> returnptr(std::unique_ptr<T>&& a) {
return std::move(a);
} }""")
uptr_in = cppyy.gbl.std.make_unique[int]()
uptr_out = cppyy.gbl.UniqueTempl.returnptr["int"](cppyy.gbl.std.move(uptr_in))
assert not not uptr_out
uptr_in = cppyy.gbl.std.make_unique['int']()
> with raises(ValueError): # not an RValue
E Failed: DID NOT RAISE <class 'ValueError'>
../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py:447: Failed
__________________ TestCPP11FEATURES.test16_unique_ptr_moves ___________________
self = <test.test_cpp11features.TestCPP11FEATURES object at 0x7f2bb8870b90>
def test16_unique_ptr_moves(self):
"""std::unique_ptr requires moves"""
import cppyy
cppyy.cppdef("""namespace unique_ptr_moves {
template <typename T>
std::unique_ptr<T> returnptr_value(std::unique_ptr<T> a) {
return std::move(a);
}
template <typename T>
std::unique_ptr<T> returnptr_move(std::unique_ptr<T>&& a) {
return std::move(a);
} }""")
up = cppyy.gbl.std.make_unique[int](42)
ns = cppyy.gbl.unique_ptr_moves
up = ns.returnptr_value(up) ; assert up and up.get()[0] == 42
up = ns.returnptr_value(cppyy.gbl.std.move(up)); assert up and up.get()[0] == 42
up = ns.returnptr_move(cppyy.gbl.std.move(up)) ; assert up and up.get()[0] == 42
> with raises(TypeError):
E Failed: DID NOT RAISE <class 'TypeError'>
../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py:471: Failed
=============================== warnings summary ===============================
test/test_cpp11features.py::TestCPP11FEATURES::test01_smart_ptr
/home/rembserj/code/root/root_build/lib/cppyy_backend/loader.py:147: UserWarning: No precompiled header available (cannot import name 'get_cppversion' from 'cppyy_backend._get_cppflags' (/home/rembserj/code/root/root_build/lib/cppyy_backend/_get_cppflags.py)); this may impact performance.
warnings.warn('No precompiled header available (%s); this may impact performance.' % msg)
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================== short test summary info ============================
XFAIL ../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py::TestCPP11FEATURES::test09_lambda_calls
XFAIL ../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py::TestCPP11FEATURES::test11_chrono - reason: [NOTRUN] Crashes
XFAIL ../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py::TestCPP11FEATURES::test12_stdfunction
XFAIL ../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py::TestCPP11FEATURES::test14_shared_ptr_passing
XFAIL ../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py::TestCPP11FEATURES::test19_smartptr_from_callback
FAILED ../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py::TestCPP11FEATURES::test07_move
FAILED ../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py::TestCPP11FEATURES::test15_unique_ptr_template_deduction
FAILED ../../../../../../root_src/bindings/pyroot/cppyy/cppyy/test/test_cpp11features.py::TestCPP11FEATURES::test16_unique_ptr_moves
============== 3 failed, 11 passed, 5 xfailed, 1 warning in 2.71s ==============
CMake Error at /home/rembserj/code/root/root_src/cmake/modules/RootTestDriver.cmake:232 (message):
error code: 1 |
According to the Python release notes, the correct way to check for temporaries is to use the new [PyUnstable_Object_IsUniqueReferencedTemporary()](https://docs.python.org/3.14/c-api/object.html#c.PyUnstable_Object_IsUniqueReferencedTemporary). See in particular the "Porting to Python 3.14" section in the release notes: https://docs.python.org/3.14/whatsnew/3.14.html#id12 This fixes several failures in the `cppyy/test/test_cpp11features.py` test with Python 3.14. Tested locally with a Python 3.14 build.
|
Yes, the ref-count check was always a mess as it varies by Python version how many ref-counts are added for the frame. That said, that API looks like it's created for subinterpreters and the purpose there would be quite different. |
|
For sure the final word on what's the right thing to do here is not out, but naively I don't see why this would only apply to subinterpreters. In the release notes:
That's why I thought it would be better to use this method. But I suppose once Python 3.14 is out, it will be easier to try things on different platforms and the release notes will be clearer |
changes taken from root-project/root#19017
changes taken from root-project/root#19017
changes taken from root-project/root#19017
changes taken from root-project/root#19017
changes taken from root-project/root#19017
vector_iter() took two references to the container, one when storing it and one after deciding whether the container is a temporary, while the iterator's dealloc releases only one. Every iteration over a std::vector proxy therefore leaked a reference to it: the proxy, and the C++ object if Python owns it, never got freed. ROOT's own cppyy copy got this right in root-project#19017 (commit 700fee6), which moved the temporary check before the increment so that PyUnstable_Object_IsUniqueReferencedTemporary() sees the untouched refcount on Python 3.14, and dropped the original increment. The port of that change to the compiler-research fork, compiler-research/CPyCppyy#151 (commit 8eba11b58c, reapplied as 7e086216a3), added the new increment after the check but kept the original one. cppjit inherited both in its initial import (compiler-research/cppjit@38b22b99be), and ROOT with the vendoring in 552d291. Upstream cppjit still has both. Beyond the memory, the leak made pyroot-pyz-ttree-branch flaky: the proxies leaked by the earlier read test stayed registered in the memory regulator under addresses the closed file had freed, so when a later `new std::vector<double>` landed on one of them, reading the pointer data member returned the stale proxy instead of a reference proxy, and TTree::Branch() got bound to the proxy's own pointer slot. 🤖 Done with the help of AI
vector_iter() took two references to the container, one when storing it and one after deciding whether the container is a temporary, while the iterator's dealloc releases only one. Every iteration over a `std::vector` proxy therefore leaked a reference to it: the proxy, and the C++ object if Python owns it, never got freed. ROOT's own cppyy copy got this right in root-project/root#19017 (commit 700fee6e7b6c), which moved the temporary check before the increment so that PyUnstable_Object_IsUniqueReferencedTemporary() sees the untouched refcount on Python 3.14, and dropped the original increment. The port of that change to the compiler-research fork, compiler-research/CPyCppyy#151 (commit compiler-research/CPyCppyy@8eba11b58c, reapplied as compiler-research/CPyCppyy@7e086216a3), added the new increment after the check but kept the original one. cppjit inherited both in its initial import (38b22b99be), and ROOT with the vendoring in 552d291c328. Upstream cppjit still has both. Beyond the memory, the leak made ROOT's pyroot-pyz-ttree-branch flaky: the proxies leaked by the earlier read test stayed registered in the memory regulator under addresses the closed file had freed, so when a later `new std::vector<double>` landed on one of them, reading the pointer data member returned the stale proxy instead of a reference proxy, and TTree::Branch() got bound to the proxy's own pointer slot.
According to the Python release notes, the correct way to check for temporaries is to use the new
PyUnstable_Object_IsUniqueReferencedTemporary().
See in particular the "Porting to Python 3.14" section in the release notes:
https://docs.python.org/3.14/whatsnew/3.14.html#id12
This fixes several failures in the
cppyy/test/test_cpp11features.pytest with Python 3.14.
Tested locally with a Python 3.14 build.