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 9 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 .)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/pmod.py" DESTINATION .)

This should not be needed, specify the wheel.packages in the pyproject.toml instead


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);
}
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)
48 changes: 48 additions & 0 deletions tests/packages/importlib_editable/pkg/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Don't let ruff sort imports in this file, we want to keep them with the comments as is
# for clarity.
# ruff: noqa: I001

# Level one pure modules
from . import pmod_a

# Level one extension modules
from . import emod_a

# Level one subpackages
from . import sub_a, sub_b

# Level two pure modules
from .sub_a import pmod_b
from .sub_b import pmod_c

# Level two extension modules
from .sub_a import emod_b
from .sub_b import emod_c

# Level two subpackages
from .sub_b import sub_c, sub_d
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from .sub_b import sub_c, sub_d
from .sub_b import sub_c

Let's focus the import on a single branch, and use the rest for navigation only.


# Level three pure modules
from .sub_b.sub_c import pmod_d
from .sub_b.sub_d import pmod_e

# Level three extension modules
from .sub_b.sub_c import emod_d
from .sub_b.sub_d import emod_e

__all__ = [
"emod_a",
"emod_b",
"emod_c",
"emod_d",
"emod_e",
"pmod_a",
"pmod_b",
"pmod_c",
"pmod_d",
"pmod_e",
"sub_a",
"sub_b",
"sub_c",
"sub_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);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these be populated with an actual implementation. Probably:

def square(x: float): -> float
    ...

Empty file.
2 changes: 2 additions & 0 deletions tests/packages/importlib_editable/pkg/pmod_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
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)
3 changes: 3 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,3 @@
from . import emod_b, pmod_b

__all__ = ["emod_b", "pmod_b"]
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);
}
Empty file.
2 changes: 2 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,2 @@
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)
31 changes: 31 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,31 @@
# Don't let ruff sort imports in this file, we want to keep them with the comments as is
# for clarity.
# ruff: noqa: I001

# Level one pure modules
from . import pmod_c

# Level one extension modules
from . import emod_c

# Level one subpackages
from . import sub_c, sub_d

# Level two pure modules
from .sub_c import pmod_d
from .sub_d import pmod_e

# Level two extension modules
from .sub_c import emod_d
from .sub_d import emod_e

__all__ = [
"emod_c",
"emod_d",
"emod_e",
"pmod_c",
"pmod_d",
"pmod_e",
"sub_c",
"sub_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);
}
Empty file.
2 changes: 2 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,2 @@
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/)
3 changes: 3 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,3 @@
from . import emod_d, pmod_d

__all__ = ["emod_d", "pmod_d"]
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);
}
Empty file.
2 changes: 2 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,2 @@
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/)
3 changes: 3 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,3 @@
from . import emod_e, pmod_e

__all__ = ["pmod_e", "emod_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);
}
Empty file.
2 changes: 2 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,2 @@
def square(x):
return x * x
2 changes: 2 additions & 0 deletions tests/packages/importlib_editable/pmod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
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