2010-12-03 16:14:31 -04:00
|
|
|
/* Use this file as a template to start implementing a module that
|
|
|
|
also declares object types. All occurrences of 'Xxo' should be changed
|
|
|
|
to something reasonable for your objects. After that, all other
|
|
|
|
occurrences of 'xx' should be changed to something reasonable for your
|
2020-12-08 12:36:53 -04:00
|
|
|
module. If your module is named foo your source file should be named
|
|
|
|
foo.c or foomodule.c.
|
2010-12-03 16:14:31 -04:00
|
|
|
|
|
|
|
You will probably want to delete all references to 'x_attr' and add
|
|
|
|
your own types of attributes instead. Maybe you want to name your
|
|
|
|
local variables other than 'self'. If your object type is needed in
|
|
|
|
other files, you'll have to create a file "foobarobject.h"; see
|
2020-12-08 12:36:53 -04:00
|
|
|
floatobject.h for an example.
|
2010-12-03 16:14:31 -04:00
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
This module roughly corresponds to::
|
|
|
|
|
|
|
|
class Xxo:
|
|
|
|
"""A class that explicitly stores attributes in an internal dict"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
# In the C class, "_x_attr" is not accessible from Python code
|
|
|
|
self._x_attr = {}
|
2022-02-02 11:03:10 -04:00
|
|
|
self._x_exports = 0
|
2020-12-08 12:36:53 -04:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return self._x_attr[name]
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
self._x_attr[name] = value
|
|
|
|
|
|
|
|
def __delattr__(self, name):
|
|
|
|
del self._x_attr[name]
|
|
|
|
|
2022-02-02 11:03:10 -04:00
|
|
|
@property
|
|
|
|
def x_exports(self):
|
|
|
|
"""Return the number of times an internal buffer is exported."""
|
|
|
|
# Each Xxo instance has a 10-byte buffer that can be
|
|
|
|
# accessed via the buffer interface (e.g. `memoryview`).
|
|
|
|
return self._x_exports
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
def demo(o, /):
|
|
|
|
if isinstance(o, str):
|
|
|
|
return o
|
|
|
|
elif isinstance(o, Xxo):
|
|
|
|
return o
|
|
|
|
else:
|
|
|
|
raise Error('argument must be str or Xxo')
|
|
|
|
|
|
|
|
class Error(Exception):
|
|
|
|
"""Exception raised by the xxlimited module"""
|
|
|
|
|
|
|
|
def foo(i: int, j: int, /):
|
|
|
|
"""Return the sum of i and j."""
|
|
|
|
# Unlike this pseudocode, the C function will *only* work with
|
|
|
|
# integers and perform C long int arithmetic
|
|
|
|
return i + j
|
|
|
|
|
|
|
|
def new():
|
|
|
|
return Xxo()
|
|
|
|
|
|
|
|
def Str(str):
|
|
|
|
# A trivial subclass of a built-in type
|
|
|
|
pass
|
|
|
|
*/
|
2010-12-03 16:14:31 -04:00
|
|
|
|
2023-10-30 13:06:09 -03:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
#include "pyconfig.h" // Py_NOGIL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef Py_NOGIL
|
2023-11-01 00:24:33 -03:00
|
|
|
// Need limited C API version 3.12 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
|
|
|
|
#define Py_LIMITED_API 0x030c0000
|
2023-10-30 13:06:09 -03:00
|
|
|
#endif
|
2021-04-02 11:48:11 -03:00
|
|
|
|
2010-12-03 16:14:31 -04:00
|
|
|
#include "Python.h"
|
2022-02-02 11:03:10 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define BUFSIZE 10
|
2010-12-03 16:14:31 -04:00
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
// Module state
|
|
|
|
typedef struct {
|
|
|
|
PyObject *Xxo_Type; // Xxo class
|
|
|
|
PyObject *Error_Type; // Error class
|
|
|
|
} xx_state;
|
|
|
|
|
|
|
|
|
|
|
|
/* Xxo objects */
|
2010-12-03 16:14:31 -04:00
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
// Instance state
|
2010-12-03 16:14:31 -04:00
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
2022-02-02 11:03:10 -04:00
|
|
|
PyObject *x_attr; /* Attributes dictionary */
|
|
|
|
char x_buffer[BUFSIZE]; /* buffer for Py_buffer */
|
|
|
|
Py_ssize_t x_exports; /* how many buffer are exported */
|
2010-12-03 16:14:31 -04:00
|
|
|
} XxoObject;
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
// XXX: no good way to do this yet
|
|
|
|
// #define XxoObject_Check(v) Py_IS_TYPE(v, Xxo_Type)
|
2010-12-03 16:14:31 -04:00
|
|
|
|
|
|
|
static XxoObject *
|
2020-12-08 12:36:53 -04:00
|
|
|
newXxoObject(PyObject *module)
|
2010-12-03 16:14:31 -04:00
|
|
|
{
|
2020-12-08 12:36:53 -04:00
|
|
|
xx_state *state = PyModule_GetState(module);
|
|
|
|
if (state == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-03 16:14:31 -04:00
|
|
|
XxoObject *self;
|
2020-12-08 12:36:53 -04:00
|
|
|
self = PyObject_GC_New(XxoObject, (PyTypeObject*)state->Xxo_Type);
|
|
|
|
if (self == NULL) {
|
2010-12-03 16:14:31 -04:00
|
|
|
return NULL;
|
2020-12-08 12:36:53 -04:00
|
|
|
}
|
2010-12-03 16:14:31 -04:00
|
|
|
self->x_attr = NULL;
|
2022-02-02 11:03:10 -04:00
|
|
|
memset(self->x_buffer, 0, BUFSIZE);
|
|
|
|
self->x_exports = 0;
|
2010-12-03 16:14:31 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
/* Xxo finalization */
|
2010-12-03 16:14:31 -04:00
|
|
|
|
2015-06-04 08:52:57 -03:00
|
|
|
static int
|
gh-89653: PEP 670: Functions don't cast pointers (#91697)
In the limited C API version 3.11 and newer, the following functions
no longer cast their object pointer argument with _PyObject_CAST() or
_PyObject_CAST_CONST():
* Py_REFCNT(), Py_TYPE(), Py_SIZE()
* Py_SET_REFCNT(), Py_SET_TYPE(), Py_SET_SIZE()
* Py_IS_TYPE()
* Py_INCREF(), Py_DECREF()
* Py_XINCREF(), Py_XDECREF()
* Py_NewRef(), Py_XNewRef()
* PyObject_TypeCheck()
* PyType_Check()
* PyType_CheckExact()
Split Py_DECREF() implementation in 3 versions to make the code more
readable.
Update the xxlimited.c extension, which uses the limited C API
version 3.11, to pass PyObject* to these functions.
2022-04-25 19:11:34 -03:00
|
|
|
Xxo_traverse(PyObject *self_obj, visitproc visit, void *arg)
|
2015-06-04 08:52:57 -03:00
|
|
|
{
|
2020-12-08 12:36:53 -04:00
|
|
|
// Visit the type
|
gh-89653: PEP 670: Functions don't cast pointers (#91697)
In the limited C API version 3.11 and newer, the following functions
no longer cast their object pointer argument with _PyObject_CAST() or
_PyObject_CAST_CONST():
* Py_REFCNT(), Py_TYPE(), Py_SIZE()
* Py_SET_REFCNT(), Py_SET_TYPE(), Py_SET_SIZE()
* Py_IS_TYPE()
* Py_INCREF(), Py_DECREF()
* Py_XINCREF(), Py_XDECREF()
* Py_NewRef(), Py_XNewRef()
* PyObject_TypeCheck()
* PyType_Check()
* PyType_CheckExact()
Split Py_DECREF() implementation in 3 versions to make the code more
readable.
Update the xxlimited.c extension, which uses the limited C API
version 3.11, to pass PyObject* to these functions.
2022-04-25 19:11:34 -03:00
|
|
|
Py_VISIT(Py_TYPE(self_obj));
|
2020-12-08 12:36:53 -04:00
|
|
|
|
|
|
|
// Visit the attribute dict
|
gh-89653: PEP 670: Functions don't cast pointers (#91697)
In the limited C API version 3.11 and newer, the following functions
no longer cast their object pointer argument with _PyObject_CAST() or
_PyObject_CAST_CONST():
* Py_REFCNT(), Py_TYPE(), Py_SIZE()
* Py_SET_REFCNT(), Py_SET_TYPE(), Py_SET_SIZE()
* Py_IS_TYPE()
* Py_INCREF(), Py_DECREF()
* Py_XINCREF(), Py_XDECREF()
* Py_NewRef(), Py_XNewRef()
* PyObject_TypeCheck()
* PyType_Check()
* PyType_CheckExact()
Split Py_DECREF() implementation in 3 versions to make the code more
readable.
Update the xxlimited.c extension, which uses the limited C API
version 3.11, to pass PyObject* to these functions.
2022-04-25 19:11:34 -03:00
|
|
|
XxoObject *self = (XxoObject *)self_obj;
|
2015-06-04 08:52:57 -03:00
|
|
|
Py_VISIT(self->x_attr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:51:19 -03:00
|
|
|
static int
|
|
|
|
Xxo_clear(XxoObject *self)
|
|
|
|
{
|
|
|
|
Py_CLEAR(self->x_attr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-26 04:51:58 -03:00
|
|
|
static void
|
gh-89653: PEP 670: Functions don't cast pointers (#91697)
In the limited C API version 3.11 and newer, the following functions
no longer cast their object pointer argument with _PyObject_CAST() or
_PyObject_CAST_CONST():
* Py_REFCNT(), Py_TYPE(), Py_SIZE()
* Py_SET_REFCNT(), Py_SET_TYPE(), Py_SET_SIZE()
* Py_IS_TYPE()
* Py_INCREF(), Py_DECREF()
* Py_XINCREF(), Py_XDECREF()
* Py_NewRef(), Py_XNewRef()
* PyObject_TypeCheck()
* PyType_Check()
* PyType_CheckExact()
Split Py_DECREF() implementation in 3 versions to make the code more
readable.
Update the xxlimited.c extension, which uses the limited C API
version 3.11, to pass PyObject* to these functions.
2022-04-25 19:11:34 -03:00
|
|
|
Xxo_finalize(PyObject *self_obj)
|
2010-12-03 16:14:31 -04:00
|
|
|
{
|
gh-89653: PEP 670: Functions don't cast pointers (#91697)
In the limited C API version 3.11 and newer, the following functions
no longer cast their object pointer argument with _PyObject_CAST() or
_PyObject_CAST_CONST():
* Py_REFCNT(), Py_TYPE(), Py_SIZE()
* Py_SET_REFCNT(), Py_SET_TYPE(), Py_SET_SIZE()
* Py_IS_TYPE()
* Py_INCREF(), Py_DECREF()
* Py_XINCREF(), Py_XDECREF()
* Py_NewRef(), Py_XNewRef()
* PyObject_TypeCheck()
* PyType_Check()
* PyType_CheckExact()
Split Py_DECREF() implementation in 3 versions to make the code more
readable.
Update the xxlimited.c extension, which uses the limited C API
version 3.11, to pass PyObject* to these functions.
2022-04-25 19:11:34 -03:00
|
|
|
XxoObject *self = (XxoObject *)self_obj;
|
2015-06-04 08:52:57 -03:00
|
|
|
Py_CLEAR(self->x_attr);
|
2010-12-03 16:14:31 -04:00
|
|
|
}
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
static void
|
gh-89653: PEP 670: Functions don't cast pointers (#91697)
In the limited C API version 3.11 and newer, the following functions
no longer cast their object pointer argument with _PyObject_CAST() or
_PyObject_CAST_CONST():
* Py_REFCNT(), Py_TYPE(), Py_SIZE()
* Py_SET_REFCNT(), Py_SET_TYPE(), Py_SET_SIZE()
* Py_IS_TYPE()
* Py_INCREF(), Py_DECREF()
* Py_XINCREF(), Py_XDECREF()
* Py_NewRef(), Py_XNewRef()
* PyObject_TypeCheck()
* PyType_Check()
* PyType_CheckExact()
Split Py_DECREF() implementation in 3 versions to make the code more
readable.
Update the xxlimited.c extension, which uses the limited C API
version 3.11, to pass PyObject* to these functions.
2022-04-25 19:11:34 -03:00
|
|
|
Xxo_dealloc(PyObject *self)
|
2010-12-03 16:14:31 -04:00
|
|
|
{
|
2022-07-27 12:03:38 -03:00
|
|
|
PyObject_GC_UnTrack(self);
|
2020-12-08 12:36:53 -04:00
|
|
|
Xxo_finalize(self);
|
|
|
|
PyTypeObject *tp = Py_TYPE(self);
|
|
|
|
freefunc free = PyType_GetSlot(tp, Py_tp_free);
|
|
|
|
free(self);
|
|
|
|
Py_DECREF(tp);
|
2010-12-03 16:14:31 -04:00
|
|
|
}
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
|
|
|
|
/* Xxo attribute handling */
|
2010-12-03 16:14:31 -04:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
Xxo_getattro(XxoObject *self, PyObject *name)
|
|
|
|
{
|
|
|
|
if (self->x_attr != NULL) {
|
2019-02-25 11:59:46 -04:00
|
|
|
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
|
2010-12-03 16:14:31 -04:00
|
|
|
if (v != NULL) {
|
2022-11-14 11:21:40 -04:00
|
|
|
return Py_NewRef(v);
|
2010-12-03 16:14:31 -04:00
|
|
|
}
|
2019-02-25 11:59:46 -04:00
|
|
|
else if (PyErr_Occurred()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-03 16:14:31 -04:00
|
|
|
}
|
|
|
|
return PyObject_GenericGetAttr((PyObject *)self, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-12-08 12:36:53 -04:00
|
|
|
Xxo_setattro(XxoObject *self, PyObject *name, PyObject *v)
|
2010-12-03 16:14:31 -04:00
|
|
|
{
|
|
|
|
if (self->x_attr == NULL) {
|
2020-12-08 12:36:53 -04:00
|
|
|
// prepare the attribute dict
|
2010-12-03 16:14:31 -04:00
|
|
|
self->x_attr = PyDict_New();
|
2020-12-08 12:36:53 -04:00
|
|
|
if (self->x_attr == NULL) {
|
2010-12-03 16:14:31 -04:00
|
|
|
return -1;
|
2020-12-08 12:36:53 -04:00
|
|
|
}
|
2010-12-03 16:14:31 -04:00
|
|
|
}
|
|
|
|
if (v == NULL) {
|
2020-12-08 12:36:53 -04:00
|
|
|
// delete an attribute
|
|
|
|
int rv = PyDict_DelItem(self->x_attr, name);
|
|
|
|
if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
|
2010-12-03 16:14:31 -04:00
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
|
|
"delete non-existing Xxo attribute");
|
2020-12-08 12:36:53 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-12-03 16:14:31 -04:00
|
|
|
return rv;
|
|
|
|
}
|
2020-12-08 12:36:53 -04:00
|
|
|
else {
|
|
|
|
// set an attribute
|
|
|
|
return PyDict_SetItem(self->x_attr, name, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Xxo methods */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
Xxo_demo(XxoObject *self, PyTypeObject *defining_class,
|
|
|
|
PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
|
|
|
|
{
|
|
|
|
if (kwnames != NULL && PyObject_Length(kwnames)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "demo() takes no keyword arguments");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (nargs != 1) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "demo() takes exactly 1 argument");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *o = args[0];
|
|
|
|
|
|
|
|
/* Test if the argument is "str" */
|
|
|
|
if (PyUnicode_Check(o)) {
|
2022-11-14 11:21:40 -04:00
|
|
|
return Py_NewRef(o);
|
2020-12-08 12:36:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test if the argument is of the Xxo class */
|
|
|
|
if (PyObject_TypeCheck(o, defining_class)) {
|
2022-11-14 11:21:40 -04:00
|
|
|
return Py_NewRef(o);
|
2020-12-08 12:36:53 -04:00
|
|
|
}
|
|
|
|
|
2022-11-14 11:21:40 -04:00
|
|
|
return Py_NewRef(Py_None);
|
2010-12-03 16:14:31 -04:00
|
|
|
}
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
static PyMethodDef Xxo_methods[] = {
|
2022-05-03 16:42:14 -03:00
|
|
|
{"demo", _PyCFunction_CAST(Xxo_demo),
|
2020-12-08 12:36:53 -04:00
|
|
|
METH_METHOD | METH_FASTCALL | METH_KEYWORDS, PyDoc_STR("demo(o) -> o")},
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
2022-02-02 11:03:10 -04:00
|
|
|
/* Xxo buffer interface */
|
|
|
|
|
|
|
|
static int
|
|
|
|
Xxo_getbuffer(XxoObject *self, Py_buffer *view, int flags)
|
|
|
|
{
|
|
|
|
int res = PyBuffer_FillInfo(view, (PyObject*)self,
|
|
|
|
(void *)self->x_buffer, BUFSIZE,
|
|
|
|
0, flags);
|
|
|
|
if (res == 0) {
|
|
|
|
self->x_exports++;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Xxo_releasebuffer(XxoObject *self, Py_buffer *view)
|
|
|
|
{
|
|
|
|
self->x_exports--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
Xxo_get_x_exports(XxoObject *self, void *c)
|
|
|
|
{
|
|
|
|
return PyLong_FromSsize_t(self->x_exports);
|
|
|
|
}
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
/* Xxo type definition */
|
|
|
|
|
|
|
|
PyDoc_STRVAR(Xxo_doc,
|
|
|
|
"A class that explicitly stores attributes in an internal dict");
|
|
|
|
|
2022-02-02 11:03:10 -04:00
|
|
|
static PyGetSetDef Xxo_getsetlist[] = {
|
|
|
|
{"x_exports", (getter) Xxo_get_x_exports, NULL, NULL},
|
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-03 16:14:31 -04:00
|
|
|
static PyType_Slot Xxo_Type_slots[] = {
|
2020-12-08 12:36:53 -04:00
|
|
|
{Py_tp_doc, (char *)Xxo_doc},
|
2015-06-04 08:52:57 -03:00
|
|
|
{Py_tp_traverse, Xxo_traverse},
|
2021-05-31 04:51:19 -03:00
|
|
|
{Py_tp_clear, Xxo_clear},
|
2015-06-04 08:52:57 -03:00
|
|
|
{Py_tp_finalize, Xxo_finalize},
|
2020-12-08 12:36:53 -04:00
|
|
|
{Py_tp_dealloc, Xxo_dealloc},
|
2010-12-03 16:14:31 -04:00
|
|
|
{Py_tp_getattro, Xxo_getattro},
|
2020-12-08 12:36:53 -04:00
|
|
|
{Py_tp_setattro, Xxo_setattro},
|
2010-12-03 16:14:31 -04:00
|
|
|
{Py_tp_methods, Xxo_methods},
|
2022-02-02 11:03:10 -04:00
|
|
|
{Py_bf_getbuffer, Xxo_getbuffer},
|
|
|
|
{Py_bf_releasebuffer, Xxo_releasebuffer},
|
|
|
|
{Py_tp_getset, Xxo_getsetlist},
|
2020-12-08 12:36:53 -04:00
|
|
|
{0, 0}, /* sentinel */
|
2010-12-03 16:14:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyType_Spec Xxo_Type_spec = {
|
2020-12-08 12:36:53 -04:00
|
|
|
.name = "xxlimited.Xxo",
|
|
|
|
.basicsize = sizeof(XxoObject),
|
|
|
|
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
|
|
|
.slots = Xxo_Type_slots,
|
2010-12-03 16:14:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
/* Str type definition*/
|
|
|
|
|
|
|
|
static PyType_Slot Str_Type_slots[] = {
|
|
|
|
{0, 0}, /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyType_Spec Str_Type_spec = {
|
|
|
|
.name = "xxlimited.Str",
|
|
|
|
.basicsize = 0,
|
|
|
|
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
|
|
|
.slots = Str_Type_slots,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Function of two integers returning integer (with C "long int" arithmetic) */
|
2010-12-03 16:14:31 -04:00
|
|
|
|
|
|
|
PyDoc_STRVAR(xx_foo_doc,
|
|
|
|
"foo(i,j)\n\
|
|
|
|
\n\
|
|
|
|
Return the sum of i and j.");
|
|
|
|
|
|
|
|
static PyObject *
|
2020-12-08 12:36:53 -04:00
|
|
|
xx_foo(PyObject *module, PyObject *args)
|
2010-12-03 16:14:31 -04:00
|
|
|
{
|
|
|
|
long i, j;
|
|
|
|
long res;
|
|
|
|
if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
|
|
|
|
return NULL;
|
|
|
|
res = i+j; /* XXX Do something here */
|
|
|
|
return PyLong_FromLong(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Function of no arguments returning new Xxo object */
|
|
|
|
|
|
|
|
static PyObject *
|
2020-12-08 12:36:53 -04:00
|
|
|
xx_new(PyObject *module, PyObject *Py_UNUSED(unused))
|
2010-12-03 16:14:31 -04:00
|
|
|
{
|
|
|
|
XxoObject *rv;
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
rv = newXxoObject(module);
|
2010-12-03 16:14:31 -04:00
|
|
|
if (rv == NULL)
|
|
|
|
return NULL;
|
|
|
|
return (PyObject *)rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* List of functions defined in the module */
|
|
|
|
|
|
|
|
static PyMethodDef xx_methods[] = {
|
|
|
|
{"foo", xx_foo, METH_VARARGS,
|
|
|
|
xx_foo_doc},
|
2020-12-08 12:36:53 -04:00
|
|
|
{"new", xx_new, METH_NOARGS,
|
2010-12-03 16:14:31 -04:00
|
|
|
PyDoc_STR("new() -> new Xx object")},
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
|
|
|
|
/* The module itself */
|
|
|
|
|
2010-12-03 16:14:31 -04:00
|
|
|
PyDoc_STRVAR(module_doc,
|
|
|
|
"This is a template module just for instruction.");
|
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
static int
|
|
|
|
xx_modexec(PyObject *m)
|
2010-12-03 16:14:31 -04:00
|
|
|
{
|
2020-12-08 12:36:53 -04:00
|
|
|
xx_state *state = PyModule_GetState(m);
|
|
|
|
|
|
|
|
state->Error_Type = PyErr_NewException("xxlimited.Error", NULL, NULL);
|
|
|
|
if (state->Error_Type == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PyModule_AddType(m, (PyTypeObject*)state->Error_Type) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->Xxo_Type = PyType_FromModuleAndSpec(m, &Xxo_Type_spec, NULL);
|
|
|
|
if (state->Xxo_Type == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PyModule_AddType(m, (PyTypeObject*)state->Xxo_Type) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the Str type. It is not needed from C code, so it is only
|
|
|
|
// added to the module dict.
|
|
|
|
// It does not inherit from "object" (PyObject_Type), but from "str"
|
|
|
|
// (PyUnincode_Type).
|
|
|
|
PyObject *Str_Type = PyType_FromModuleAndSpec(
|
|
|
|
m, &Str_Type_spec, (PyObject *)&PyUnicode_Type);
|
|
|
|
if (Str_Type == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PyModule_AddType(m, (PyTypeObject*)Str_Type) < 0) {
|
|
|
|
return -1;
|
2010-12-03 16:14:31 -04:00
|
|
|
}
|
2020-12-08 12:36:53 -04:00
|
|
|
Py_DECREF(Str_Type);
|
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyModuleDef_Slot xx_slots[] = {
|
|
|
|
{Py_mod_exec, xx_modexec},
|
2023-05-05 18:11:27 -03:00
|
|
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
2015-05-23 09:24:10 -03:00
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
static int
|
|
|
|
xx_traverse(PyObject *module, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
xx_state *state = PyModule_GetState(module);
|
|
|
|
Py_VISIT(state->Xxo_Type);
|
|
|
|
Py_VISIT(state->Error_Type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xx_clear(PyObject *module)
|
|
|
|
{
|
|
|
|
xx_state *state = PyModule_GetState(module);
|
|
|
|
Py_CLEAR(state->Xxo_Type);
|
|
|
|
Py_CLEAR(state->Error_Type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
static struct PyModuleDef xxmodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
2020-12-08 12:36:53 -04:00
|
|
|
.m_name = "xxlimited",
|
|
|
|
.m_doc = module_doc,
|
|
|
|
.m_size = sizeof(xx_state),
|
|
|
|
.m_methods = xx_methods,
|
|
|
|
.m_slots = xx_slots,
|
|
|
|
.m_traverse = xx_traverse,
|
|
|
|
.m_clear = xx_clear,
|
|
|
|
/* m_free is not necessary here: xx_clear clears all references,
|
|
|
|
* and the module state is deallocated along with the module.
|
|
|
|
*/
|
2015-05-23 09:24:10 -03:00
|
|
|
};
|
|
|
|
|
2020-12-08 12:36:53 -04:00
|
|
|
|
2015-05-23 09:24:10 -03:00
|
|
|
/* Export function for the module (*must* be called PyInit_xx) */
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit_xxlimited(void)
|
|
|
|
{
|
|
|
|
return PyModuleDef_Init(&xxmodule);
|
2010-12-03 16:14:31 -04:00
|
|
|
}
|