Skip to content
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

Add more tests of package traversal #906

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/packages/importlib_editable/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.15...3.26)
project(${SKBUILD_PROJECT_NAME} LANGUAGES C)

find_package(
Python
COMPONENTS Interpreter Development.Module
REQUIRED)

python_add_library(emod MODULE emod.c WITH_SOABI)
install(TARGETS emod DESTINATION .)
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/pmod.py" DESTINATION .)

add_subdirectory(pkg)
25 changes: 25 additions & 0 deletions tests/packages/importlib_editable/emod.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

float square(float x) { return x * x; }

static PyObject *square_wrapper(PyObject *self, PyObject *args) {
float input, result;
if (!PyArg_ParseTuple(args, "f", &input)) {
return NULL;
}
result = square(input);
return PyFloat_FromDouble(result);
}

static PyMethodDef emod_methods[] = {
{"square", square_wrapper, METH_VARARGS, "Square function"},
{NULL, NULL, 0, NULL}};

static struct PyModuleDef emod_module = {PyModuleDef_HEAD_INIT, "emod",
NULL, -1, emod_methods};

/* name here must match extension name, with PyInit_ prefix */
PyMODINIT_FUNC PyInit_emod(void) {
return PyModule_Create(&emod_module);
}
1 change: 1 addition & 0 deletions tests/packages/importlib_editable/emod.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def square(x: float) -> float: ...
8 changes: 8 additions & 0 deletions tests/packages/importlib_editable/pkg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
python_add_library(emod_a MODULE emod_a.c WITH_SOABI)

install(TARGETS emod_a DESTINATION pkg/)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testfile" "This is the file")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/testfile" DESTINATION pkg/)

add_subdirectory(sub_a)
add_subdirectory(sub_b)
32 changes: 32 additions & 0 deletions tests/packages/importlib_editable/pkg/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Don't let ruff sort imports in this file, we want to keep them with the comments as is
# for clarity.
# ruff: noqa: I001, F401
# mypy: ignore-errors

# Level zero import global modules
from pmod import square as psquare
from emod import square as esquare

# Level one pure modules
from .pmod_a import square as psquare_a

# Level one extension modules
from .emod_a import square as esquare_a

# Level one subpackages
from . import sub_a

# Level two pure modules
from .sub_a.pmod_b import square as psquare_b

# Level two extension modules
from .sub_a.emod_b import square as esquare_b

# Level two subpackages
from .sub_b import sub_c

# Level three pure modules
from .sub_b.sub_c.pmod_d import square as psquare_d

# Level three extension modules
from .sub_b.sub_c.emod_d import square as esquare_d
25 changes: 25 additions & 0 deletions tests/packages/importlib_editable/pkg/emod_a.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

float square(float x) { return x * x; }

static PyObject *square_wrapper(PyObject *self, PyObject *args) {
float input, result;
if (!PyArg_ParseTuple(args, "f", &input)) {
return NULL;
}
result = square(input);
return PyFloat_FromDouble(result);
}

static PyMethodDef emod_a_methods[] = {
{"square", square_wrapper, METH_VARARGS, "Square function"},
{NULL, NULL, 0, NULL}};

static struct PyModuleDef emod_a_module = {PyModuleDef_HEAD_INIT, "emod_a",
NULL, -1, emod_a_methods};

/* name here must match extension name, with PyInit_ prefix */
PyMODINIT_FUNC PyInit_emod_a(void) {
return PyModule_Create(&emod_a_module);
}
1 change: 1 addition & 0 deletions tests/packages/importlib_editable/pkg/emod_a.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def square(x: float) -> float: ...
9 changes: 9 additions & 0 deletions tests/packages/importlib_editable/pkg/pmod_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ruff: noqa: I001, F401
# mypy: ignore-errors

# Level one import sibling
from .emod_a import square as esquare


def square(x):
return x * x
5 changes: 5 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_a/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
python_add_library(emod_b MODULE emod_b.c WITH_SOABI)

install(TARGETS emod_b DESTINATION pkg/sub_a)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testfile" "This is the file")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/testfile" DESTINATION pkg/sub_a)
15 changes: 15 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_a/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ruff: noqa: I001, F401
# mypy: ignore-errors

from .pmod_b import square as psquare
from .emod_b import square as esquare

# Level one import cousin
from .. import sub_b
from ..sub_b.pmod_c import square as psquare_c
from ..sub_b.emod_c import square as esquare_c

# Level one import distant cousin
from ..sub_b import sub_c
from ..sub_b.sub_c.pmod_d import square as psquare_d
from ..sub_b.sub_c.emod_d import square as esquare_d
25 changes: 25 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_a/emod_b.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

float square(float x) { return x * x; }

static PyObject *square_wrapper(PyObject *self, PyObject *args) {
float input, result;
if (!PyArg_ParseTuple(args, "f", &input)) {
return NULL;
}
result = square(input);
return PyFloat_FromDouble(result);
}

static PyMethodDef emod_b_methods[] = {
{"square", square_wrapper, METH_VARARGS, "Square function"},
{NULL, NULL, 0, NULL}};

static struct PyModuleDef emod_b_module = {PyModuleDef_HEAD_INIT, "emod_b",
NULL, -1, emod_b_methods};

/* name here must match extension name, with PyInit_ prefix */
PyMODINIT_FUNC PyInit_emod_b(void) {
return PyModule_Create(&emod_b_module);
}
1 change: 1 addition & 0 deletions tests/packages/importlib_editable/pkg/sub_a/emod_b.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def square(x: float) -> float: ...
9 changes: 9 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_a/pmod_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ruff: noqa: I001, F401
# mypy: ignore-errors

# Level two import sibling
from .emod_b import square as esquare


def square(x):
return x * x
8 changes: 8 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_b/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
python_add_library(emod_c MODULE emod_c.c WITH_SOABI)

install(TARGETS emod_c DESTINATION pkg/sub_b)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testfile" "This is the file")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/testfile" DESTINATION pkg/sub_b)

add_subdirectory(sub_c)
add_subdirectory(sub_d)
19 changes: 19 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_b/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Don't let ruff sort imports in this file, we want to keep them with the comments as is
# for clarity.
# ruff: noqa: I001, F401
# mypy: ignore-errors

# Level one pure modules
from .pmod_c import square as psquare_c

# Level one extension modules
from .emod_c import square as esquare_c

# Level one subpackages
from . import sub_c

# Level two pure modules
from .sub_c.pmod_d import square as psquare_d

# Level two extension modules
from .sub_c.emod_d import square as esquare_d
25 changes: 25 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_b/emod_c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

float square(float x) { return x * x; }

static PyObject *square_wrapper(PyObject *self, PyObject *args) {
float input, result;
if (!PyArg_ParseTuple(args, "f", &input)) {
return NULL;
}
result = square(input);
return PyFloat_FromDouble(result);
}

static PyMethodDef emod_c_methods[] = {
{"square", square_wrapper, METH_VARARGS, "Square function"},
{NULL, NULL, 0, NULL}};

static struct PyModuleDef emod_c_module = {PyModuleDef_HEAD_INIT, "emod_c",
NULL, -1, emod_c_methods};

/* name here must match extension name, with PyInit_ prefix */
PyMODINIT_FUNC PyInit_emod_c(void) {
return PyModule_Create(&emod_c_module);
}
1 change: 1 addition & 0 deletions tests/packages/importlib_editable/pkg/sub_b/emod_c.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def square(x: float) -> float: ...
9 changes: 9 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_b/pmod_c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ruff: noqa: I001, F401
# mypy: ignore-errors

# Level two import sibling
from .emod_c import square as esquare


def square(x):
return x * x
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
python_add_library(emod_d MODULE emod_d.c WITH_SOABI)

install(TARGETS emod_d DESTINATION pkg/sub_b/sub_c)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testfile" "This is the file")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/testfile"
DESTINATION pkg/sub_b/sub_c/)
10 changes: 10 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_b/sub_c/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ruff: noqa: I001, F401
# mypy: ignore-errors

from .pmod_d import square as psquare_d
from .emod_d import square as esquare_d

# Level one import cousin
from .. import sub_d
from ..sub_d.pmod_e import square as psquare_e
from ..sub_d.emod_e import square as esquare_e
25 changes: 25 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_b/sub_c/emod_d.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

float square(float x) { return x * x; }

static PyObject *square_wrapper(PyObject *self, PyObject *args) {
float input, result;
if (!PyArg_ParseTuple(args, "f", &input)) {
return NULL;
}
result = square(input);
return PyFloat_FromDouble(result);
}

static PyMethodDef emod_d_methods[] = {
{"square", square_wrapper, METH_VARARGS, "Square function"},
{NULL, NULL, 0, NULL}};

static struct PyModuleDef emod_d_module = {PyModuleDef_HEAD_INIT, "emod_d",
NULL, -1, emod_d_methods};

/* name here must match extension name, with PyInit_ prefix */
PyMODINIT_FUNC PyInit_emod_d(void) {
return PyModule_Create(&emod_d_module);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def square(x: float) -> float: ...
9 changes: 9 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_b/sub_c/pmod_d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ruff: noqa: I001, F401
# mypy: ignore-errors

# Level three import sibling
from .emod_d import square as esquare


def square(x):
return x * x
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
python_add_library(emod_e MODULE emod_e.c WITH_SOABI)

install(TARGETS emod_e DESTINATION pkg/sub_b/sub_d/)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/testfile" "This is the file")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/testfile"
DESTINATION pkg/sub_b/sub_d/)
5 changes: 5 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_b/sub_d/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ruff: noqa: I001, F401
# mypy: ignore-errors

from .pmod_e import square as psquare_e
from .emod_e import square as esquare_e
25 changes: 25 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_b/sub_d/emod_e.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

float square(float x) { return x * x; }

static PyObject *square_wrapper(PyObject *self, PyObject *args) {
float input, result;
if (!PyArg_ParseTuple(args, "f", &input)) {
return NULL;
}
result = square(input);
return PyFloat_FromDouble(result);
}

static PyMethodDef emod_e_methods[] = {
{"square", square_wrapper, METH_VARARGS, "Square function"},
{NULL, NULL, 0, NULL}};

static struct PyModuleDef emod_e_module = {PyModuleDef_HEAD_INIT, "emod_e",
NULL, -2, emod_e_methods};

/* name here must match extension name, with PyInit_ prefix */
PyMODINIT_FUNC PyInit_emod_e(void) {
return PyModule_Create(&emod_e_module);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def square(x: float) -> float: ...
9 changes: 9 additions & 0 deletions tests/packages/importlib_editable/pkg/sub_b/sub_d/pmod_e.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ruff: noqa: I001, F401
# mypy: ignore-errors

# Level three import sibling
from .emod_e import square as esquare


def square(x):
return x * x
9 changes: 9 additions & 0 deletions tests/packages/importlib_editable/pmod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ruff: noqa: I001, F401
# mypy: ignore-errors

# Level zero import global sibling
from emod import square as esquare


def square(x):
return x * x
10 changes: 10 additions & 0 deletions tests/packages/importlib_editable/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build-system]
requires = ["scikit-build-core"]
build-backend = "scikit_build_core.build"

[project]
name = "pkg"
version = "0.0.1"

[tool.scikit-build]
build-dir = "build/{wheel_tag}"
Loading
Loading