8000 pkgdata: add importlib.resources fallback and unit test by Bitshifter-9 · Pull Request #4658 · pygame/pygame · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@Bitshifter-9
Copy link

Summary

Add a fallback implementation that uses the standard-library importlib.resources when pkg_resources is not available, and include a focused unit test to verify getResource can open package data files.

Changes

  • src_py/pkgdata.py — add importlib.resources.is_resource / open_binary fallback for resource_exists / resource_stream, preserving the original conservative stubs when neither backend is present.
  • test/test_pkgdata.py — new unit test that creates a temporary fake package and verifies pkgdata.getResource('example.dat', pkgname=...) returns the expected contents. The test can be run directly with Python and is compatible with pytest.

Why

Prevents runtime NotImplementedError in minimal environments that lack pkg_resources (e.g., some packaged apps or minimal containers). Using the standard-library importlib.resources on Python 3.7+ improves compatibility for pyinstaller/zipapp and environments without setuptools.

Testing

Run the focused test:

  • python3 test/test_pkgdata.py
  • or: pytest -q test/test_pkgdata.py::test_get_resource_reads_file

I ran the test locally and it passed.

Compatibility / Notes

  • importlib.resources.is_resource and open_binary are available on CPython 3.7+; if importlib.resources is unavailable the original stubs remain in place.
  • The fallback accepts either a package name (string) or a module object, mirroring prior behavior.
  • If importlib.resources.open_binary raises unexpected errors, the code raises NotImplementedError so callers can fall back to the filesystem lookup as before.

How to review

  • Confirm src_py/pkgdata.py correctly uses importlib.resources (behavior and error handling).
  • Run the provided test locally: python3 test/test_pkgdata.py.
  • Optionally run CI to verify no regressions.

Suggested labels

  • patch, tests, compatibility

Suggested reviewers

  • Maintainers familiar with resource/packaging code (e.g., package area maintainers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0