You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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.pyxfrom . 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:
raiseZeroDivisionError("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.
The text was updated successfully, but these errors were encountered:
10se1ucgo
changed the title
__truediv__ unwanted cast
C++ division operator overloading unwanted cast
Oct 26, 2017
I encountered the same behavior. All other arithmetic operators I used in my code work as expected, only overloading __truediv__() somehow leads to this unwanted cast.
I'm happy to provide more detailed information if necessary to help track the problem down, but the original text pretty much sums up the unwanted behavior.
When wrapping a class like so
The code generated by Cython for
__mul__
is correct, however for__truediv__
, Cython unnecessarily castsscalar
to a Vector3.Which, of course, causes an error if the wrapped class doesn't provide an overload for that method.
The text was updated successfully, but these errors were encountered: