pkgdata: add importlib.resources fallback and unit test #4658
+109
−16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Add a fallback implementation that uses the standard-library
importlib.resourceswhenpkg_resourcesis not available, and include a focused unit test to verifygetResourcecan open package data files.Changes
src_py/pkgdata.py— addimportlib.resources.is_resource/open_binaryfallback forresource_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 verifiespkgdata.getResource('example.dat', pkgname=...)returns the expected contents. The test can be run directly with Python and is compatible withpytest.Why
Prevents runtime
NotImplementedErrorin minimal environments that lackpkg_resources(e.g., some packaged apps or minimal containers). Using the standard-libraryimportlib.resourceson Python 3.7+ improves compatibility for pyinstaller/zipapp and environments withoutsetuptools.Testing
Run the focused test:
python3 test/test_pkgdata.pypytest -q test/test_pkgdata.py::test_get_resource_reads_fileI ran the test locally and it passed.
Compatibility / Notes
importlib.resources.is_resourceandopen_binaryare available on CPython 3.7+; ifimportlib.resourcesis unavailable the original stubs remain in place.importlib.resources.open_binaryraises unexpected errors, the code raisesNotImplementedErrorso callers can fall back to the filesystem lookup as before.How to review
src_py/pkgdata.pycorrectly usesimportlib.resources(behavior and error handling).python3 test/test_pkgdata.py.Suggested labels
patch,tests,compatibilitySuggested reviewers