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
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)
+25
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def square(x: float) -> float: ...
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)
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
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def square(x: float) -> float: ...
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
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)
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
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_b_methods[] = {
16+
{"square", square_wrapper, METH_VARARGS, "Square function"},
17+
{NULL, NULL, 0, NULL}};
18+
19+
static struct PyModuleDef emod_b_module = {PyModuleDef_HEAD_INIT, "emod_b",
20+
NULL, -1, emod_b_methods};
21+
22+
/* name here must match extension name, with PyInit_ prefix */
23+
PyMODINIT_FUNC PyInit_emod_b(void) {
24+
return PyModule_Create(&emod_b_module);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def square(x: float) -> float: ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ruff: noqa: I001, F401
2+
# mypy: ignore-errors
3+
4+
# Level two import sibling
5+
from .emod_b import square as esquare
6+
7+
8+
def square(x):
9+
return x * x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
python_add_library(emod_c MODULE emod_c.c WITH_SOABI)
2+
3+
install(TARGETS emod_c DESTINATION pkg/sub_b)
4+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testfile" "This is the file")
5+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/testfile" DESTINATION pkg/sub_b)
6+
7+
add_subdirectory(sub_c)
8+
add_subdirectory(sub_d)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 one pure modules
7+
from .pmod_c import square as psquare_c
8+
9+
# Level one extension modules
10+
from .emod_c import square as esquare_c
11+
12+
# Level one subpackages
13+
from . import sub_c
14+
15+
# Level two pure modules
16+
from .sub_c.pmod_d import square as psquare_d
17+
18+
# Level two extension modules
19+
from .sub_c.emod_d import square as esquare_d
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_c_methods[] = {
16+
{"square", square_wrapper, METH_VARARGS, "Square function"},
17+
{NULL, NULL, 0, NULL}};
18+
19+
static struct PyModuleDef emod_c_module = {PyModuleDef_HEAD_INIT, "emod_c",
20+
NULL, -1, emod_c_methods};
21+
22+
/* name here must match extension name, with PyInit_ prefix */
23+
PyMODINIT_FUNC PyInit_emod_c(void) {
24+
return PyModule_Create(&emod_c_module);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def square(x: float) -> float: ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ruff: noqa: I001, F401
2+
# mypy: ignore-errors
3+
4+
# Level two import sibling
5+
from .emod_c import square as esquare
6+
7+
8+
def square(x):
9+
return x * x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
python_add_library(emod_d MODULE emod_d.c WITH_SOABI)
2+
3+
install(TARGETS emod_d DESTINATION pkg/sub_b/sub_c)
4+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testfile" "This is the file")
5+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/testfile"
6+
DESTINATION pkg/sub_b/sub_c/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ruff: noqa: I001, F401
2+
# mypy: ignore-errors
3+
4+
from .pmod_d import square as psquare_d
5+
from .emod_d import square as esquare_d
6+
7+
# Level one import cousin
8+
from .. import sub_d
9+
from ..sub_d.pmod_e import square as psquare_e
10+
from ..sub_d.emod_e import square as esquare_e
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_d_methods[] = {
16+
{"square", square_wrapper, METH_VARARGS, "Square function"},
17+
{NULL, NULL, 0, NULL}};
18+
19+
static struct PyModuleDef emod_d_module = {PyModuleDef_HEAD_INIT, "emod_d",
20+
NULL, -1, emod_d_methods};
21+
22+
/* name here must match extension name, with PyInit_ prefix */
23+
PyMODINIT_FUNC PyInit_emod_d(void) {
24+
return PyModule_Create(&emod_d_module);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def square(x: float) -> float: ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ruff: noqa: I001, F401
2+
# mypy: ignore-errors
3+
4+
# Level three import sibling
5+
from .emod_d import square as esquare
6+
7+
8+
def square(x):
9+
return x * x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
python_add_library(emod_e MODULE emod_e.c WITH_SOABI)
2+
3+
install(TARGETS emod_e DESTINATION pkg/sub_b/sub_d/)
4+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testfile" "This is the file")
5+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/testfile"
6+
DESTINATION pkg/sub_b/sub_d/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ruff: noqa: I001, F401
2+
# mypy: ignore-errors
3+
4+
from .pmod_e import square as psquare_e
5+
from .emod_e import square as esquare_e
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_e_methods[] = {
16+
{"square", square_wrapper, METH_VARARGS, "Square function"},
17+
{NULL, NULL, 0, NULL}};
18+
19+
static struct PyModuleDef emod_e_module = {PyModuleDef_HEAD_INIT, "emod_e",
20+
NULL, -2, emod_e_methods};
21+
22+
/* name here must match extension name, with PyInit_ prefix */
23+
PyMODINIT_FUNC PyInit_emod_e(void) {
24+
return PyModule_Create(&emod_e_module);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def square(x: float) -> float: ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ruff: noqa: I001, F401
2+
# mypy: ignore-errors
3+
4+
# Level three import sibling
5+
from .emod_e import square as esquare
6+
7+
8+
def square(x):
9+
return x * x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ruff: noqa: I001, F401
2+
# mypy: ignore-errors
3+
4+
# Level zero import global sibling
5+
from emod import square as esquare
6+
7+
8+
def square(x):
9+
return x * x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[build-system]
2+
requires = ["scikit-build-core"]
3+
build-backend = "scikit_build_core.build"
4+
5+
[project]
6+
name = "pkg"
7+
version = "0.0.1"
8+
9+
[tool.scikit-build]
10+
build-dir = "build/{wheel_tag}"

0 commit comments

Comments
 (0)