Skip to content

Commit a21c61e

Browse files
committed
Fix __mul__ and __truediv__ operators for Vector2i
1 parent 530a45a commit a21c61e

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/godot/builtins_pyx/operator.pyx.j2

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,12 @@ def __mul__(self, object other):
247247
{% endif %}
248248
{% endfor%}
249249
cdef void *other_gd_data
250-
cdef {{ operators_multiply[0].return_type.cy_type }} ret
250+
# Some operations return multiple different types (e.g. `Vector2i * float -> Vector2`).
251+
# Hopefully the compiler should be smart enough to detect only a single `ret_XXX`
252+
# is used for any given call.
253+
{% for op in operators_multiply %}
254+
cdef {{ operators_multiply[0].return_type.cy_type }} ret{{ loop.index0 }}
255+
{% endfor %}
251256
{% for op in operators_multiply %}
252257
try:
253258
# Check `other`'s type before creating `ret` (since one created its
@@ -259,9 +264,9 @@ def __mul__(self, object other):
259264
other_gd_data = &(<{{ op.right_type.cy_type }}?>other)._gd_data
260265
{% endif %}
261266
# Call to __new__ bypasses __init__ constructor
262-
ret = {{ op.return_type.cy_type }}.__new__({{ op.return_type.cy_type }})
263-
ret._gd_data = gdapi.gd_{{ builtin.snake_name }}_op_multiply_{{ op.right_type.snake_name }}(&self._gd_data, <{{ op.right_type.c_type }} *>other_gd_data)
264-
return ret
267+
ret{{ loop.index0 }} = {{ op.return_type.cy_type }}.__new__({{ op.return_type.cy_type }})
268+
ret{{ loop.index0 }}._gd_data = gdapi.gd_{{ builtin.snake_name }}_op_multiply_{{ op.right_type.snake_name }}(&self._gd_data, <{{ op.right_type.c_type }} *>other_gd_data)
269+
return ret{{ loop.index0 }}
265270
except TypeError:
266271
pass
267272
{% endfor %}
@@ -277,7 +282,12 @@ def __truediv__(self, object other):
277282
{% endif %}
278283
{% endfor%}
279284
cdef void *other_gd_data
280-
cdef {{ operators_divide[0].return_type.cy_type }} ret
285+
# Some operations return multiple different types (e.g. `Vector2i * float -> Vector2`).
286+
# Hopefully the compiler should be smart enough to detect only a single `ret_XXX`
287+
# is used for any given call.
288+
{% for op in operators_multiply %}
289+
cdef {{ operators_multiply[0].return_type.cy_type }} ret{{ loop.index0 }}
290+
{% endfor %}
281291
{% for op in operators_divide %}
282292
try:
283293
# Check `other`'s type before creating `ret` (since one created its
@@ -289,9 +299,9 @@ def __truediv__(self, object other):
289299
other_gd_data = &(<{{ op.right_type.cy_type }}?>other)._gd_data
290300
{% endif %}
291301
# Call to __new__ bypasses __init__ constructor
292-
ret = {{ op.return_type.cy_type }}.__new__({{ op.return_type.cy_type }})
293-
ret._gd_data = gdapi.gd_{{ builtin.snake_name }}_op_divide_{{ op.right_type.snake_name }}(&self._gd_data, <{{ op.right_type.c_type }} *>other_gd_data)
294-
return ret
302+
ret{{ loop.index0 }} = {{ op.return_type.cy_type }}.__new__({{ op.return_type.cy_type }})
303+
ret{{ loop.index0 }}._gd_data = gdapi.gd_{{ builtin.snake_name }}_op_divide_{{ op.right_type.snake_name }}(&self._gd_data, <{{ op.right_type.c_type }} *>other_gd_data)
304+
return ret{{ loop.index0 }}
295305
except TypeError:
296306
pass
297307
{% endfor %}

0 commit comments

Comments
 (0)