Skip to content

Fix pygame.Vector{2,3} initialization from numpy arrays #3408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Matiiss
Copy link
Member

@Matiiss Matiiss commented Apr 20, 2025

Originally reported by @damusss on the Pygame Community discord server

numpy arrays (evidently) implement the numeric protocol, thus they go through the first check that checks if the object is a number and subsequently fail on conversion to a double because the array is >1 dimensional

MRE:

import numpy as np
import pygame

arr = np.array([1, 0])
pygame.Vector2(arr)

Traceback:

pygame-ce 2.5.3 (SDL 2.30.12, Python 3.12.8)
TypeError: only length-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/matiiss/projects/Something/for_so.py", line 5, in <module>
    pygame.Vector2(arr)
SystemError: <class 'pygame.math.Vector2'> returned a result with an exception set

…omething that is a sequence, implements a numeric protocol and it fails on conversion to double (also improved error checking for PyFloat_AsDouble)
@Matiiss Matiiss requested a review from a team as a code owner April 20, 2025 16:38
@Matiiss Matiiss added this to the 2.5.4 milestone Apr 20, 2025
@Matiiss Matiiss added math pygame.math bugfix PR that fixes bug labels Apr 20, 2025
@damusss
Copy link
Member

damusss commented Apr 20, 2025

checking from phone but I think you copy pasted tests to the vector 3 without changing them to vector 3?

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the PR 👍

Copy link
Member

@damusss damusss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for fixing this! :) looks good to me

@damusss damusss merged commit dec6098 into pygame-community:main Apr 21, 2025
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix PR that fixes bug math pygame.math
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants