-
Notifications
You must be signed in to change notification settings - Fork 66
feat: Support importlib.resources
in edittable installs
#399
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
679675c
Add basic editable install navigation tests
LecrisUT 8a1f645
Add importlib.resources test
LecrisUT 37d03cc
Implement `importlib.resources` support
LecrisUT 142227e
Fix test_pep660
LecrisUT 4c6765b
Fix support for testing python < 3.9
LecrisUT f4cddf0
fix(types): Use TypedDict to avoid type ignores
henryiii 8ba0774
chore: minor touchup
henryiii 6260886
tests: use samefile
henryiii bee2bf0
tests: use samefile + EXT_SUFFIX
LecrisUT a28a8e6
ci: check 3.9 on macOS
henryiii 2972c04
tests: parametrize on data package
henryiii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required(VERSION 3.15...3.26) | ||
|
||
project( | ||
${SKBUILD_PROJECT_NAME} | ||
LANGUAGES C | ||
VERSION ${SKBUILD_PROJECT_VERSION}) | ||
|
||
find_package(Python COMPONENTS Interpreter Development.Module) | ||
|
||
python_add_library(c_module MODULE src/shared_pkg/c_module.c WITH_SOABI) | ||
|
||
set(CMakeVar "Some_value_C") | ||
configure_file(src/shared_pkg/data/generated.txt.in | ||
shared_pkg/data/c_generated.txt) | ||
|
||
install( | ||
TARGETS c_module | ||
DESTINATION shared_pkg/ | ||
COMPONENT PythonModule) | ||
install(FILES ${PROJECT_BINARY_DIR}/shared_pkg/data/c_generated.txt | ||
DESTINATION shared_pkg/data/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[build-system] | ||
requires = ["scikit-build-core"] | ||
build-backend = "scikit_build_core.build" | ||
|
||
[project] | ||
name = "navigate_editable" | ||
version = "0.0.1" | ||
dependencies = [ | ||
"importlib-resources; python_version<'3.9'" | ||
] | ||
|
||
[tool.scikit-build] | ||
wheel.packages = ["python/shared_pkg"] |
9 changes: 9 additions & 0 deletions
9
tests/packages/navigate_editable/python/shared_pkg/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .c_module import call_py_method | ||
from .py_module import call_c_method, read_c_generated_txt, read_py_data_txt | ||
|
||
__all__ = [ | ||
"call_py_method", | ||
"call_c_method", | ||
"read_py_data_txt", | ||
"read_c_generated_txt", | ||
] |
2 changes: 2 additions & 0 deletions
2
tests/packages/navigate_editable/python/shared_pkg/c_module.pyi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def c_method() -> str: ... | ||
def call_py_method() -> None: ... |
1 change: 1 addition & 0 deletions
1
tests/packages/navigate_editable/python/shared_pkg/data/py_data.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Some_value_Py |
28 changes: 28 additions & 0 deletions
28
tests/packages/navigate_editable/python/shared_pkg/py_module.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import sys | ||
|
||
if sys.version_info < (3, 9): | ||
from importlib_resources import files | ||
else: | ||
from importlib.resources import files | ||
|
||
from .c_module import c_method | ||
|
||
|
||
def call_c_method(): | ||
print(c_method()) | ||
|
||
|
||
def py_method(): | ||
print("py_method") | ||
|
||
|
||
def read_py_data_txt(): | ||
root = files("shared_pkg.data") | ||
py_data = root / "py_data.txt" | ||
print(py_data.read_text()) | ||
|
||
|
||
def read_c_generated_txt(): | ||
root = files("shared_pkg.data") | ||
c_generated_txt = root / "c_generated.txt" | ||
print(c_generated_txt.read_text()) |
54 changes: 54 additions & 0 deletions
54
tests/packages/navigate_editable/src/shared_pkg/c_module.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#define PY_SSIZE_T_CLEAN | ||
#include <Python.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
const char* c_method() { return "c_method"; } | ||
|
||
static PyObject *c_method_wrapper(PyObject *self, PyObject *args) { | ||
return PyUnicode_FromString(c_method()); | ||
} | ||
|
||
static PyObject *py_method_wrapper(PyObject *self, PyObject *args) { | ||
PyObject *py_module = PyImport_ImportModule("shared_pkg.py_module"); | ||
if (py_module == NULL) { | ||
PyErr_Print(); | ||
fprintf(stderr, "Failed to load shared_pkg.py_module\n"); | ||
exit(1); | ||
} | ||
PyObject *py_method = PyObject_GetAttrString(py_module,(char*)"py_method"); | ||
if (py_method == NULL) { | ||
PyErr_Print(); | ||
fprintf(stderr, "Failed to load shared_pkg.py_module.py_method\n"); | ||
exit(1); | ||
} | ||
|
||
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION > 8 | ||
PyObject *res = PyObject_CallNoArgs(py_method); | ||
#else | ||
PyObject *res = PyObject_CallObject(py_method, NULL); | ||
#endif | ||
|
||
if (res == NULL) { | ||
PyErr_Print(); | ||
fprintf(stderr, "Failed to execute shared_pkg.py_module.py_method\n"); | ||
exit(1); | ||
} | ||
|
||
Py_DECREF(py_module); | ||
Py_DECREF(py_method); | ||
Py_DECREF(res); | ||
Py_RETURN_NONE; | ||
} | ||
|
||
static PyMethodDef c_module_methods[] = { | ||
{"c_method", c_method_wrapper, METH_NOARGS, "C native method"}, | ||
{"call_py_method", py_method_wrapper, METH_NOARGS, "Call python native method"}, | ||
{NULL, NULL, 0, NULL}}; | ||
|
||
static struct PyModuleDef c_module = {PyModuleDef_HEAD_INIT, "c_module", | ||
NULL, -1, c_module_methods}; | ||
|
||
PyMODINIT_FUNC PyInit_c_module(void) { | ||
return PyModule_Create(&c_module); | ||
} |
1 change: 1 addition & 0 deletions
1
tests/packages/navigate_editable/src/shared_pkg/data/generated.txt.in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@CMakeVar@ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.compile() | ||
@pytest.mark.configure() | ||
@pytest.mark.integration() | ||
@pytest.mark.parametrize("isolate", [True, False], ids=["isolated", "notisolated"]) | ||
@pytest.mark.parametrize( | ||
"package", | ||
[ | ||
pytest.param( | ||
True, | ||
id="package", | ||
marks=[pytest.mark.xfail(reason="Only data folders supported currently")], | ||
), | ||
pytest.param(False, id="datafolder"), | ||
], | ||
) | ||
@pytest.mark.usefixtures("navigate_editable") | ||
@pytest.mark.xfail( | ||
sys.version_info[:2] == (3, 9), reason="Python 3.9 not supported yet" | ||
) | ||
def test_navigate_editable(isolated, isolate, package): | ||
isolate_args = ["--no-build-isolation"] if not isolate else [] | ||
isolated.install("pip>=23") | ||
if not isolate: | ||
isolated.install("scikit-build-core[pyproject]") | ||
|
||
if package: | ||
init_py = Path("python/shared_pkg/data/__init__.py") | ||
init_py.touch() | ||
|
||
isolated.install( | ||
"-v", "--config-settings=build-dir=build/{wheel_tag}", *isolate_args, "-e", "." | ||
) | ||
|
||
value = isolated.execute("import shared_pkg; shared_pkg.call_c_method()") | ||
assert value == "c_method" | ||
|
||
value = isolated.execute("import shared_pkg; shared_pkg.call_py_method()") | ||
assert value == "py_method" | ||
|
||
value = isolated.execute("import shared_pkg; shared_pkg.read_py_data_txt()") | ||
assert value == "Some_value_Py" | ||
|
||
value = isolated.execute("import shared_pkg; shared_pkg.read_c_generated_txt()") | ||
assert value == "Some_value_C" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.