From 1a80c4d8fc72e7d36f00a4909d88949e08860487 Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Wed, 14 Aug 2024 13:00:01 +1000 Subject: [PATCH] c type-alias getter thing --- build.py | 11 +++++++++ c/type_alias_value_getter.c | 45 +++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + 3 files changed, 57 insertions(+) create mode 100644 build.py create mode 100644 c/type_alias_value_getter.c diff --git a/build.py b/build.py new file mode 100644 index 0000000..dfc08ec --- /dev/null +++ b/build.py @@ -0,0 +1,11 @@ +from __future__ import annotations + +from setuptools import Extension, setup + +setup( + ext_modules=[ + Extension( + name="basedtyping.type_alias_value_getter", sources=["c/type_alias_value_getter.c"] + ) + ] +) diff --git a/c/type_alias_value_getter.c b/c/type_alias_value_getter.c new file mode 100644 index 0000000..4a4bc02 --- /dev/null +++ b/c/type_alias_value_getter.c @@ -0,0 +1,45 @@ +#define PY_SSIZE_T_CLEAN +#include + +typedef struct { + PyObject_HEAD + PyObject *name; + PyObject *type_params; + PyObject *compute_value; + PyObject *value; + PyObject *module; +} typealiasobject; + +static PyObject * +foo_test(PyObject *self, PyObject *args) +{ + PyObject *alias; + if (!PyArg_ParseTuple(args, "O", &alias)) + return NULL; + typealiasobject *ta = (typealiasobject *)alias; + return Py_NewRef(ta->compute_value); +} + +static PyMethodDef foo_methods[] = { + /* The cast of the function is necessary since PyCFunction values + * only take two PyObject* parameters, and keywdarg_parrot() takes + * three. + */ + {"test", (PyCFunction)(void(*)(void))foo_test, METH_VARARGS, + "debug alias."}, + {NULL, NULL, 0, NULL} /* sentinel */ +}; + +static struct PyModuleDef foo = { + PyModuleDef_HEAD_INIT, + "foo", + NULL, + -1, + foo_methods +}; + +PyMODINIT_FUNC +PyInit_foo(void) +{ + PyModule_Create(&foo); +}; diff --git a/pyproject.toml b/pyproject.toml index 7e6bdba..2ca3090 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,6 +6,7 @@ authors = [ description = "Utilities for basedmypy" name = "basedtyping" version = "0.1.4" +build = "build.py" [tool.poetry.dependencies] python = "^3.8"