Closed
Description
When wrapping a class like so
# vec3.pxd
cdef extern from "math3d_c.h":
cdef cppclass Vector3[T]:
T x, y, z
Vector3() except +
Vector3(T x, T y, T z) except +
Vector3[T] operator*(T &b)
Vector3[T] operator/(T &b)
# math3d.pyx
from . cimport vec3
cdef class Vector3:
cdef vec3.Vector3[double] c_vec
def __init__(self, double x=0, double y=0, double z=0):
self.c_vec.x = x
self.c_vec.y = y
self.c_vec.z = z
def __mul__(Vector3 a, double scalar):
return new_vector3_from(a.c_vec * scalar)
def __truediv__(Vector3 a, double scalar):
if scalar == 0:
raise ZeroDivisionError("Vector3 division by zero")
return new_vector3_from(a.c_vec / scalar)
# new_vector3_from just constructs a Python Vector3 object from a C++ Vector3 object, unrelated
The code generated by Cython for __mul__
is correct, however for __truediv__
, Cython unnecessarily casts scalar
to a Vector3.
// mul
__pyx_t_1 = ((PyObject *)__pyx_f_6extlib_6math3d_new_vector3_from((__pyx_v_a->c_vec * __pyx_v_scalar))); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 30, __pyx_L1_error)
// truediv
__pyx_t_2 = ((PyObject *)__pyx_f_6extlib_6math3d_new_vector3_from((__pyx_v_a->c_vec / ((Vector3<double> )__pyx_v_scalar)))); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 43, __pyx_L1_error)
Which, of course, causes an error if the wrapped class doesn't provide an overload for that method.