Skip to content

Commit 1557763

Browse files
vyasrLecrisUT
andauthored
tests: add more tests of package traversal (#906)
This PR adds tests (including some xfailed ones) demonstrated patterns of package/subpackage access via import and importlib that are expected to work correctly for both normal and editable installs. Related to #807. --------- Signed-off-by: Cristian Le <[email protected]> Co-authored-by: Cristian Le <[email protected]>
1 parent aed38e0 commit 1557763

31 files changed

+473
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.15...3.26)
2+
project(${SKBUILD_PROJECT_NAME} LANGUAGES C)
3+
4+
find_package(
5+
Python
6+
COMPONENTS Interpreter Development.Module
7+
REQUIRED)
8+
9+
python_add_library(emod MODULE emod.c WITH_SOABI)
10+
install(TARGETS emod DESTINATION .)
11+
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/pmod.py" DESTINATION .)
12+
13+
add_subdirectory(pkg)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#include <Python.h>
3+
4+
float square(float x) { return x * x; }
5+
6+
static PyObject *square_wrapper(PyObject *self, PyObject *args) {
7+
float input, result;
8+
if (!PyArg_ParseTuple(args, "f", &input)) {
9+
return NULL;
10+
}
11+
result = square(input);
12+
return PyFloat_FromDouble(result);
13+
}
14+
15+
static PyMethodDef emod_methods[] = {
16+
{"square", square_wrapper, METH_VARARGS, "Square function"},
17+
{NULL, NULL, 0, NULL}};
18+
19+
static struct PyModuleDef emod_module = {PyModuleDef_HEAD_INIT, "emod",
20+
NULL, -1, emod_methods};
21+
22+
/* name here must match extension name, with PyInit_ prefix */
23+
PyMODINIT_FUNC PyInit_emod(void) {
24+
return PyModule_Create(&emod_module);
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def square(x: float) -> float: ...
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
python_add_library(emod_a MODULE emod_a.c WITH_SOABI)
2+
3+
install(TARGETS emod_a DESTINATION pkg/)
4+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testfile" "This is the file")
5+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/testfile" DESTINATION pkg/)
6+
7+
add_subdirectory(sub_a)
8+
add_subdirectory(sub_b)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Don't let ruff sort imports in this file, we want to keep them with the comments as is
2+
# for clarity.
3+
# ruff: noqa: I001, F401
4+
# mypy: ignore-errors
5+
6+
# Level zero import global modules
7+
from pmod import square as psquare
8+
from emod import square as esquare
9+
10+
# Level one pure modules
11+
from .pmod_a import square as psquare_a
12+
13+
# Level one extension modules
14+
from .emod_a import square as esquare_a
15+
16+
# Level one subpackages
17+
from . import sub_a
18+
19+
# Level two pure modules
20+
from .sub_a.pmod_b import square as psquare_b
21+
22+
# Level two extension modules
23+
from .sub_a.emod_b import square as esquare_b
24+
25+
# Level two subpackages
26+
from .sub_b import sub_c
27+
28+
# Level three pure modules
29+
from .sub_b.sub_c.pmod_d import square as psquare_d
30+
31+
# Level three extension modules
32+
from .sub_b.sub_c.emod_d import square as esquare_d
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#include <Python.h>
3+
4+
float square(float x) { return x * x; }
5+
6+
static PyObject *square_wrapper(PyObject *self, PyObject *args) {
7+
float input, result;
8+
if (!PyArg_ParseTuple(args, "f", &input)) {
9+
return NULL;
10+
}
11+
result = square(input);
12+
return PyFloat_FromDouble(result);
13+
}
14+
15+
static PyMethodDef emod_a_methods[] = {
16+
{"square", square_wrapper, METH_VARARGS, "Square function"},
17+
{NULL, NULL, 0, NULL}};
18+
19+
static struct PyModuleDef emod_a_module = {PyModuleDef_HEAD_INIT, "emod_a",
20+
NULL, -1, emod_a_methods};
21+
22+
/* name here must match extension name, with PyInit_ prefix */
23+
PyMODINIT_FUNC PyInit_emod_a(void) {
24+
return PyModule_Create(&emod_a_module);
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def square(x: float) -> float: ...
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ruff: noqa: I001, F401
2+
# mypy: ignore-errors
3+
4+
# Level one import sibling
5+
from .emod_a import square as esquare
6+
7+
8+
def square(x):
9+
return x * x
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
python_add_library(emod_b MODULE emod_b.c WITH_SOABI)
2+
3+
install(TARGETS emod_b DESTINATION pkg/sub_a)
4+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testfile" "This is the file")
5+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/testfile" DESTINATION pkg/sub_a)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ruff: noqa: I001, F401
2+
# mypy: ignore-errors
3+
4+
from .pmod_b import square as psquare
5+
from .emod_b import square as esquare
6+
7+
# Level one import cousin
8+
from .. import sub_b
9+
from ..sub_b.pmod_c import square as psquare_c
10+
from ..sub_b.emod_c import square as esquare_c
11+
12+
# Level one import distant cousin
13+
from ..sub_b import sub_c
14+
from ..sub_b.sub_c.pmod_d import square as psquare_d
15+
from ..sub_b.sub_c.emod_d import square as esquare_d

0 commit comments

Comments
 (0)