MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_argument_conversions

Function test_argument_conversions

tests/test_numpy_array.py:591–629  ·  view source on GitHub ↗
(forcecast, contiguity, noconvert)

Source from the content-addressed store, hash-verified

589 )
590)
591def test_argument_conversions(forcecast, contiguity, noconvert):
592 function_name = "accept_double"
593 if contiguity == "C":
594 function_name += "_c_style"
595 elif contiguity == "F":
596 function_name += "_f_style"
597 if forcecast:
598 function_name += "_forcecast"
599 if noconvert:
600 function_name += "_noconvert"
601 function = getattr(m, function_name)
602
603 for dtype in [np.dtype("float32"), np.dtype("float64"), np.dtype("complex128")]:
604 for order in ["C", "F"]:
605 for shape in [(2, 2), (1, 3, 1, 1), (1, 1, 1), (0,)]:
606 if not noconvert:
607 # If noconvert is not passed, only complex128 needs to be truncated and
608 # "cannot be safely obtained". So without `forcecast`, the argument shouldn't
609 # be accepted.
610 should_raise = dtype.name == "complex128" and not forcecast
611 else:
612 # If noconvert is passed, only float64 and the matching order is accepted.
613 # If at most one dimension has a size greater than 1, the array is also
614 # trivially contiguous.
615 trivially_contiguous = sum(1 for d in shape if d > 1) <= 1
616 should_raise = dtype.name != "float64" or (
617 contiguity is not None
618 and contiguity != order
619 and not trivially_contiguous
620 )
621
622 array = np.zeros(shape, dtype=dtype, order=order)
623 if not should_raise:
624 function(array)
625 else:
626 with pytest.raises(
627 TypeError, match="incompatible function arguments"
628 ):
629 function(array)
630
631
632@pytest.mark.xfail("env.PYPY")

Callers

nothing calls this directly

Calls 3

getattrFunction · 0.85
functionClass · 0.85
dtypeMethod · 0.45

Tested by

no test coverage detected