Skip to content

Commit

Permalink
tests: one more int cast
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed May 25, 2024
1 parent f74d23a commit 0d95846
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions tests/test_kwargs_and_defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
// PyPy needs a garbage collection to get the reference count values to match CPython's behaviour
#ifdef PYPY_VERSION
# define GC_IF_NEEDED ConstructorStats::gc()
# define REFCNT(x) (int) Py_REFCNT(x)
#else
# define GC_IF_NEEDED
# define REFCNT(x) Py_REFCNT(x)
#endif
m.def("arg_refcount_h", [](py::handle h) {
GC_IF_NEEDED;
Expand All @@ -172,7 +174,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
py::tuple t(a.size());
for (size_t i = 0; i < a.size(); i++) {
// Use raw Python API here to avoid an extra, intermediate incref on the tuple item:
t[i] = (int) Py_REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast<py::ssize_t>(i)));
t[i] = REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast<py::ssize_t>(i)));
}
return t;
});
Expand All @@ -182,7 +184,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
t[0] = o.ref_count();
for (size_t i = 0; i < a.size(); i++) {
// Use raw Python API here to avoid an extra, intermediate incref on the tuple item:
t[i + 1] = (int) Py_REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast<py::ssize_t>(i)));
t[i + 1] = REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast<py::ssize_t>(i)));
}
return t;
});
Expand Down
3 changes: 2 additions & 1 deletion tests/test_kwargs_and_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ def test_args_refcount():
# for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
# tuple without having to inc_ref the individual elements, but here we can't, hence the extra
# refs.
assert m.mixed_args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3)
exp3_3 = 2**32 - 1 if exp3 == 2**32 - 1 else exp3 + 3
assert m.mixed_args_refcount(myval, myval, myval) == (exp3_3, exp3_3, exp3_3)

assert m.class_default_argument() == "<class 'decimal.Decimal'>"

0 comments on commit 0d95846

Please sign in to comment.