summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetr Viktorin <encukou@gmail.com>2020-12-08 16:36:53 (GMT)
committerGitHub <noreply@github.com>2020-12-08 16:36:53 (GMT)
commitc168b5078f88874b9acd993ac886f82269c780dd (patch)
treee4c2b04e56d9be570f7d7f33c4f8d1d0a180e6f9
parent4aa67853cc7d6ed4f9ebb726ceaa2c89f9feabda (diff)
downloadcpython-c168b5078f88874b9acd993ac886f82269c780dd.zip
cpython-c168b5078f88874b9acd993ac886f82269c780dd.tar.gz
cpython-c168b5078f88874b9acd993ac886f82269c780dd.tar.bz2
bpo-42111: Make the xxlimited module an example of best extension module practices (GH-23226)
- Copy existing xxlimited to xxlimited53 (named for the limited API version it uses) - Build both modules, both in debug and release - Test both modules
-rw-r--r--Lib/test/test_xxlimited.py79
-rw-r--r--Misc/NEWS.d/next/C API/2020-10-21-18-43-06.bpo-42111.9pvtrc.rst2
-rw-r--r--Modules/xxlimited.c379
-rw-r--r--Modules/xxlimited_35.c301
-rw-r--r--PC/layout/main.py2
-rw-r--r--PCbuild/pcbuild.proj1
-rw-r--r--PCbuild/readme.txt3
-rw-r--r--PCbuild/xxlimited.vcxproj4
-rw-r--r--PCbuild/xxlimited_35.vcxproj114
-rw-r--r--PCbuild/xxlimited_35.vcxproj.filters13
-rw-r--r--setup.py8
11 files changed, 748 insertions, 158 deletions
diff --git a/Lib/test/test_xxlimited.py b/Lib/test/test_xxlimited.py
new file mode 100644
index 0000000..e3f521d
--- /dev/null
+++ b/Lib/test/test_xxlimited.py
@@ -0,0 +1,79 @@
+import unittest
+from test.support import import_helper
+import types
+
+xxlimited = import_helper.import_module('xxlimited')
+xxlimited_35 = import_helper.import_module('xxlimited_35')
+
+
+class CommonTests:
+ module: types.ModuleType
+
+ def test_xxo_new(self):
+ xxo = self.module.Xxo()
+
+ def test_xxo_attributes(self):
+ xxo = self.module.Xxo()
+ with self.assertRaises(AttributeError):
+ xxo.foo
+ with self.assertRaises(AttributeError):
+ del xxo.foo
+
+ xxo.foo = 1234
+ self.assertEqual(xxo.foo, 1234)
+
+ del xxo.foo
+ with self.assertRaises(AttributeError):
+ xxo.foo
+
+ def test_foo(self):
+ # the foo function adds 2 numbers
+ self.assertEqual(self.module.foo(1, 2), 3)
+
+ def test_str(self):
+ self.assertTrue(issubclass(self.module.Str, str))
+ self.assertIsNot(self.module.Str, str)
+
+ custom_string = self.module.Str("abcd")
+ self.assertEqual(custom_string, "abcd")
+ self.assertEqual(custom_string.upper(), "ABCD")
+
+ def test_new(self):
+ xxo = self.module.new()
+ self.assertEqual(xxo.demo("abc"), "abc")
+
+
+class TestXXLimited(CommonTests, unittest.TestCase):
+ module = xxlimited
+
+ def test_xxo_demo(self):
+ xxo = self.module.Xxo()
+ other = self.module.Xxo()
+ self.assertEqual(xxo.demo("abc"), "abc")
+ self.assertEqual(xxo.demo(xxo), xxo)
+ self.assertEqual(xxo.demo(other), other)
+ self.assertEqual(xxo.demo(0), None)
+
+ def test_error(self):
+ with self.assertRaises(self.module.Error):
+ raise self.module.Error
+
+
+class TestXXLimited35(CommonTests, unittest.TestCase):
+ module = xxlimited_35
+
+ def test_xxo_demo(self):
+ xxo = self.module.Xxo()
+ other = self.module.Xxo()
+ self.assertEqual(xxo.demo("abc"), "abc")
+ self.assertEqual(xxo.demo(0), None)
+
+ def test_roj(self):
+ # the roj function always fails
+ with self.assertRaises(SystemError):
+ self.module.roj(0)
+
+ def test_null(self):
+ null1 = self.module.Null()
+ null2 = self.module.Null()
+ self.assertNotEqual(null1, null2)
diff --git a/Misc/NEWS.d/next/C API/2020-10-21-18-43-06.bpo-42111.9pvtrc.rst b/Misc/NEWS.d/next/C API/2020-10-21-18-43-06.bpo-42111.9pvtrc.rst
new file mode 100644
index 0000000..3fb718c
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2020-10-21-18-43-06.bpo-42111.9pvtrc.rst
@@ -0,0 +1,2 @@
+Update the ``xxlimited`` module to be a better example of how to use the
+limited C API.
diff --git a/Modules/xxlimited.c b/Modules/xxlimited.c
index 5b05a94..883c8a9 100644
--- a/Modules/xxlimited.c
+++ b/Modules/xxlimited.c
@@ -3,47 +3,103 @@
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
- module. If your module is named foo your sourcefile should be named
- foomodule.c.
+ module. If your module is named foo your source file should be named
+ foo.c or foomodule.c.
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
- floatobject.h for an example. */
+ floatobject.h for an example.
-/* Xxo objects */
+ 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 = {}
+
+ 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]
+
+ 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
+ */
#include "Python.h"
-static PyObject *ErrorObject;
+// Module state
+typedef struct {
+ PyObject *Xxo_Type; // Xxo class
+ PyObject *Error_Type; // Error class
+} xx_state;
+
+
+/* Xxo objects */
+// Instance state
typedef struct {
PyObject_HEAD
PyObject *x_attr; /* Attributes dictionary */
} XxoObject;
-static PyObject *Xxo_Type;
-
-#define XxoObject_Check(v) Py_IS_TYPE(v, Xxo_Type)
+// XXX: no good way to do this yet
+// #define XxoObject_Check(v) Py_IS_TYPE(v, Xxo_Type)
static XxoObject *
-newXxoObject(PyObject *arg)
+newXxoObject(PyObject *module)
{
+ xx_state *state = PyModule_GetState(module);
+ if (state == NULL) {
+ return NULL;
+ }
XxoObject *self;
- self = PyObject_GC_New(XxoObject, (PyTypeObject*)Xxo_Type);
- if (self == NULL)
+ self = PyObject_GC_New(XxoObject, (PyTypeObject*)state->Xxo_Type);
+ if (self == NULL) {
return NULL;
+ }
self->x_attr = NULL;
return self;
}
-/* Xxo methods */
+/* Xxo finalization */
static int
Xxo_traverse(XxoObject *self, visitproc visit, void *arg)
{
+ // Visit the type
Py_VISIT(Py_TYPE(self));
+
+ // Visit the attribute dict
Py_VISIT(self->x_attr);
return 0;
}
@@ -54,26 +110,18 @@ Xxo_finalize(XxoObject *self)
Py_CLEAR(self->x_attr);
}
-static PyObject *
-Xxo_demo(XxoObject *self, PyObject *args)
+static void
+Xxo_dealloc(XxoObject *self)
{
- PyObject *o = NULL;
- if (!PyArg_ParseTuple(args, "|O:demo", &o))
- return NULL;
- /* Test availability of fast type checks */
- if (o != NULL && PyUnicode_Check(o)) {
- Py_INCREF(o);
- return o;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ Xxo_finalize(self);
+ PyTypeObject *tp = Py_TYPE(self);
+ freefunc free = PyType_GetSlot(tp, Py_tp_free);
+ free(self);
+ Py_DECREF(tp);
}
-static PyMethodDef Xxo_methods[] = {
- {"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
- PyDoc_STR("demo() -> None")},
- {NULL, NULL} /* sentinel */
-};
+
+/* Xxo attribute handling */
static PyObject *
Xxo_getattro(XxoObject *self, PyObject *name)
@@ -92,45 +140,109 @@ Xxo_getattro(XxoObject *self, PyObject *name)
}
static int
-Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
+Xxo_setattro(XxoObject *self, PyObject *name, PyObject *v)
{
if (self->x_attr == NULL) {
+ // prepare the attribute dict
self->x_attr = PyDict_New();
- if (self->x_attr == NULL)
+ if (self->x_attr == NULL) {
return -1;
+ }
}
if (v == NULL) {
- int rv = PyDict_DelItemString(self->x_attr, name);
- if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
+ // delete an attribute
+ int rv = PyDict_DelItem(self->x_attr, name);
+ if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_SetString(PyExc_AttributeError,
"delete non-existing Xxo attribute");
+ return -1;
+ }
return rv;
}
- else
- return PyDict_SetItemString(self->x_attr, name, v);
+ 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)) {
+ Py_INCREF(o);
+ return o;
+ }
+
+ /* test if the argument is of the Xxo class */
+ if (PyObject_TypeCheck(o, defining_class)) {
+ Py_INCREF(o);
+ return o;
+ }
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
+static PyMethodDef Xxo_methods[] = {
+ {"demo", (PyCFunction)(void(*)(void))Xxo_demo,
+ METH_METHOD | METH_FASTCALL | METH_KEYWORDS, PyDoc_STR("demo(o) -> o")},
+ {NULL, NULL} /* sentinel */
+};
+
+/* Xxo type definition */
+
+PyDoc_STRVAR(Xxo_doc,
+ "A class that explicitly stores attributes in an internal dict");
+
static PyType_Slot Xxo_Type_slots[] = {
- {Py_tp_doc, "The Xxo type"},
+ {Py_tp_doc, (char *)Xxo_doc},
{Py_tp_traverse, Xxo_traverse},
{Py_tp_finalize, Xxo_finalize},
+ {Py_tp_dealloc, Xxo_dealloc},
{Py_tp_getattro, Xxo_getattro},
- {Py_tp_setattr, Xxo_setattr},
+ {Py_tp_setattro, Xxo_setattro},
{Py_tp_methods, Xxo_methods},
- {0, 0},
+ {0, 0}, /* sentinel */
};
static PyType_Spec Xxo_Type_spec = {
- "xxlimited.Xxo",
- sizeof(XxoObject),
- 0,
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
- Xxo_Type_slots
+ .name = "xxlimited.Xxo",
+ .basicsize = sizeof(XxoObject),
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
+ .slots = Xxo_Type_slots,
};
-/* --------------------------------------------------------------------- */
-/* Function of two integers returning integer */
+/* 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) */
PyDoc_STRVAR(xx_foo_doc,
"foo(i,j)\n\
@@ -138,7 +250,7 @@ PyDoc_STRVAR(xx_foo_doc,
Return the sum of i and j.");
static PyObject *
-xx_foo(PyObject *self, PyObject *args)
+xx_foo(PyObject *module, PyObject *args)
{
long i, j;
long res;
@@ -152,153 +264,110 @@ xx_foo(PyObject *self, PyObject *args)
/* Function of no arguments returning new Xxo object */
static PyObject *
-xx_new(PyObject *self, PyObject *args)
+xx_new(PyObject *module, PyObject *Py_UNUSED(unused))
{
XxoObject *rv;
- if (!PyArg_ParseTuple(args, ":new"))
- return NULL;
- rv = newXxoObject(args);
+ rv = newXxoObject(module);
if (rv == NULL)
return NULL;
return (PyObject *)rv;
}
-/* Test bad format character */
-
-static PyObject *
-xx_roj(PyObject *self, PyObject *args)
-{
- PyObject *a;
- long b;
- if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-
-/* ---------- */
-
-static PyType_Slot Str_Type_slots[] = {
- {Py_tp_base, NULL}, /* filled out in module init function */
- {0, 0},
-};
-
-static PyType_Spec Str_Type_spec = {
- "xxlimited.Str",
- 0,
- 0,
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
- Str_Type_slots
-};
-
-/* ---------- */
-
-static PyObject *
-null_richcompare(PyObject *self, PyObject *other, int op)
-{
- Py_RETURN_NOTIMPLEMENTED;
-}
-
-static PyType_Slot Null_Type_slots[] = {
- {Py_tp_base, NULL}, /* filled out in module init */
- {Py_tp_new, NULL},
- {Py_tp_richcompare, null_richcompare},
- {0, 0}
-};
-
-static PyType_Spec Null_Type_spec = {
- "xxlimited.Null",
- 0, /* basicsize */
- 0, /* itemsize */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
- Null_Type_slots
-};
-/* ---------- */
/* List of functions defined in the module */
static PyMethodDef xx_methods[] = {
- {"roj", xx_roj, METH_VARARGS,
- PyDoc_STR("roj(a,b) -> None")},
{"foo", xx_foo, METH_VARARGS,
xx_foo_doc},
- {"new", xx_new, METH_VARARGS,
+ {"new", xx_new, METH_NOARGS,
PyDoc_STR("new() -> new Xx object")},
{NULL, NULL} /* sentinel */
};
+
+/* The module itself */
+
PyDoc_STRVAR(module_doc,
"This is a template module just for instruction.");
static int
xx_modexec(PyObject *m)
{
- PyObject *o;
-
- /* Due to cross platform compiler issues the slots must be filled
- * here. It's required for portability to Windows without requiring
- * C++. */
- Null_Type_slots[0].pfunc = &PyBaseObject_Type;
- Null_Type_slots[1].pfunc = PyType_GenericNew;
- Str_Type_slots[0].pfunc = &PyUnicode_Type;
-
- Xxo_Type = PyType_FromSpec(&Xxo_Type_spec);
- if (Xxo_Type == NULL)
- goto fail;
-
- /* Add some symbolic constants to the module */
- if (ErrorObject == NULL) {
- ErrorObject = PyErr_NewException("xxlimited.error", NULL, NULL);
- if (ErrorObject == NULL)
- goto fail;
+ 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;
}
- Py_INCREF(ErrorObject);
- PyModule_AddObject(m, "error", ErrorObject);
-
- /* Add Xxo */
- o = PyType_FromSpec(&Xxo_Type_spec);
- if (o == NULL)
- goto fail;
- PyModule_AddObject(m, "Xxo", o);
-
- /* Add Str */
- o = PyType_FromSpec(&Str_Type_spec);
- if (o == NULL)
- goto fail;
- PyModule_AddObject(m, "Str", o);
-
- /* Add Null */
- o = PyType_FromSpec(&Null_Type_spec);
- if (o == NULL)
- goto fail;
- PyModule_AddObject(m, "Null", o);
+ Py_DECREF(Str_Type);
+
return 0;
- fail:
- Py_XDECREF(m);
- return -1;
}
-
static PyModuleDef_Slot xx_slots[] = {
{Py_mod_exec, xx_modexec},
{0, NULL}
};
+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;
+}
+
static struct PyModuleDef xxmodule = {
PyModuleDef_HEAD_INIT,
- "xxlimited",
- module_doc,
- 0,
- xx_methods,
- xx_slots,
- NULL,
- NULL,
- NULL
+ .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.
+ */
};
+
/* Export function for the module (*must* be called PyInit_xx) */
PyMODINIT_FUNC
diff --git a/Modules/xxlimited_35.c b/Modules/xxlimited_35.c
new file mode 100644
index 0000000..ce96e8c
--- /dev/null
+++ b/Modules/xxlimited_35.c
@@ -0,0 +1,301 @@
+
+/* This module is compiled using limited API from Python 3.5,
+ * making sure that it works as expected.
+ *
+ * See the xxlimited module for an extension module template.
+ */
+
+/* Xxo objects */
+
+#include "Python.h"
+
+static PyObject *ErrorObject;
+
+typedef struct {
+ PyObject_HEAD
+ PyObject *x_attr; /* Attributes dictionary */
+} XxoObject;
+
+static PyObject *Xxo_Type;
+
+#define XxoObject_Check(v) Py_IS_TYPE(v, Xxo_Type)
+
+static XxoObject *
+newXxoObject(PyObject *arg)
+{
+ XxoObject *self;
+ self = PyObject_GC_New(XxoObject, (PyTypeObject*)Xxo_Type);
+ if (self == NULL)
+ return NULL;
+ self->x_attr = NULL;
+ return self;
+}
+
+/* Xxo methods */
+
+static int
+Xxo_traverse(XxoObject *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->x_attr);
+ return 0;
+}
+
+static void
+Xxo_finalize(XxoObject *self)
+{
+ Py_CLEAR(self->x_attr);
+}
+
+static PyObject *
+Xxo_demo(XxoObject *self, PyObject *args)
+{
+ PyObject *o = NULL;
+ if (!PyArg_ParseTuple(args, "|O:demo", &o))
+ return NULL;
+ /* Test availability of fast type checks */
+ if (o != NULL && PyUnicode_Check(o)) {
+ Py_INCREF(o);
+ return o;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static PyMethodDef Xxo_methods[] = {
+ {"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
+ PyDoc_STR("demo() -> None")},
+ {NULL, NULL} /* sentinel */
+};
+
+static PyObject *
+Xxo_getattro(XxoObject *self, PyObject *name)
+{
+ if (self->x_attr != NULL) {
+ PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
+ if (v != NULL) {
+ Py_INCREF(v);
+ return v;
+ }
+ else if (PyErr_Occurred()) {
+ return NULL;
+ }
+ }
+ return PyObject_GenericGetAttr((PyObject *)self, name);
+}
+
+static int
+Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
+{
+ if (self->x_attr == NULL) {
+ self->x_attr = PyDict_New();
+ if (self->x_attr == NULL)
+ return -1;
+ }
+ if (v == NULL) {
+ int rv = PyDict_DelItemString(self->x_attr, name);
+ if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
+ PyErr_SetString(PyExc_AttributeError,
+ "delete non-existing Xxo attribute");
+ return rv;
+ }
+ else
+ return PyDict_SetItemString(self->x_attr, name, v);
+}
+
+static PyType_Slot Xxo_Type_slots[] = {
+ {Py_tp_doc, "The Xxo type"},
+ {Py_tp_traverse, Xxo_traverse},
+ {Py_tp_finalize, Xxo_finalize},
+ {Py_tp_getattro, Xxo_getattro},
+ {Py_tp_setattr, Xxo_setattr},
+ {Py_tp_methods, Xxo_methods},
+ {0, 0},
+};
+
+static PyType_Spec Xxo_Type_spec = {
+ "xxlimited.Xxo",
+ sizeof(XxoObject),
+ 0,
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
+ Xxo_Type_slots
+};
+
+/* --------------------------------------------------------------------- */
+
+/* Function of two integers returning integer */
+
+PyDoc_STRVAR(xx_foo_doc,
+"foo(i,j)\n\
+\n\
+Return the sum of i and j.");
+
+static PyObject *
+xx_foo(PyObject *self, PyObject *args)
+{
+ 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 *
+xx_new(PyObject *self, PyObject *args)
+{
+ XxoObject *rv;
+
+ if (!PyArg_ParseTuple(args, ":new"))
+ return NULL;
+ rv = newXxoObject(args);
+ if (rv == NULL)
+ return NULL;
+ return (PyObject *)rv;
+}
+
+/* Test bad format character */
+
+static PyObject *
+xx_roj(PyObject *self, PyObject *args)
+{
+ PyObject *a;
+ long b;
+ if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+
+/* ---------- */
+
+static PyType_Slot Str_Type_slots[] = {
+ {Py_tp_base, NULL}, /* filled out in module init function */
+ {0, 0},
+};
+
+static PyType_Spec Str_Type_spec = {
+ "xxlimited.Str",
+ 0,
+ 0,
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ Str_Type_slots
+};
+
+/* ---------- */
+
+static PyObject *
+null_richcompare(PyObject *self, PyObject *other, int op)
+{
+ Py_RETURN_NOTIMPLEMENTED;
+}
+
+static PyType_Slot Null_Type_slots[] = {
+ {Py_tp_base, NULL}, /* filled out in module init */
+ {Py_tp_new, NULL},
+ {Py_tp_richcompare, null_richcompare},
+ {0, 0}
+};
+
+static PyType_Spec Null_Type_spec = {
+ "xxlimited.Null",
+ 0, /* basicsize */
+ 0, /* itemsize */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ Null_Type_slots
+};
+
+/* ---------- */
+
+/* List of functions defined in the module */
+
+static PyMethodDef xx_methods[] = {
+ {"roj", xx_roj, METH_VARARGS,
+ PyDoc_STR("roj(a,b) -> None")},
+ {"foo", xx_foo, METH_VARARGS,
+ xx_foo_doc},
+ {"new", xx_new, METH_VARARGS,
+ PyDoc_STR("new() -> new Xx object")},
+ {NULL, NULL} /* sentinel */
+};
+
+PyDoc_STRVAR(module_doc,
+"This is a module for testing limited API from Python 3.5.");
+
+static int
+xx_modexec(PyObject *m)
+{
+ PyObject *o;
+
+ /* Due to cross platform compiler issues the slots must be filled
+ * here. It's required for portability to Windows without requiring
+ * C++. */
+ Null_Type_slots[0].pfunc = &PyBaseObject_Type;
+ Null_Type_slots[1].pfunc = PyType_GenericNew;
+ Str_Type_slots[0].pfunc = &PyUnicode_Type;
+
+ Xxo_Type = PyType_FromSpec(&Xxo_Type_spec);
+ if (Xxo_Type == NULL)
+ goto fail;
+
+ /* Add some symbolic constants to the module */
+ if (ErrorObject == NULL) {
+ ErrorObject = PyErr_NewException("xxlimited.error", NULL, NULL);
+ if (ErrorObject == NULL)
+ goto fail;
+ }
+ Py_INCREF(ErrorObject);
+ PyModule_AddObject(m, "error", ErrorObject);
+
+ /* Add Xxo */
+ o = PyType_FromSpec(&Xxo_Type_spec);
+ if (o == NULL)
+ goto fail;
+ PyModule_AddObject(m, "Xxo", o);
+
+ /* Add Str */
+ o = PyType_FromSpec(&Str_Type_spec);
+ if (o == NULL)
+ goto fail;
+ PyModule_AddObject(m, "Str", o);
+
+ /* Add Null */
+ o = PyType_FromSpec(&Null_Type_spec);
+ if (o == NULL)
+ goto fail;
+ PyModule_AddObject(m, "Null", o);
+ return 0;
+ fail:
+ Py_XDECREF(m);
+ return -1;
+}
+
+
+static PyModuleDef_Slot xx_slots[] = {
+ {Py_mod_exec, xx_modexec},
+ {0, NULL}
+};
+
+static struct PyModuleDef xxmodule = {
+ PyModuleDef_HEAD_INIT,
+ "xxlimited_35",
+ module_doc,
+ 0,
+ xx_methods,
+ xx_slots,
+ NULL,
+ NULL,
+ NULL
+};
+
+/* Export function for the module (*must* be called PyInit_xx) */
+
+PyMODINIT_FUNC
+PyInit_xxlimited_35(void)
+{
+ return PyModuleDef_Init(&xxmodule);
+}
diff --git a/PC/layout/main.py b/PC/layout/main.py
index 3eef755..8c69c915 100644
--- a/PC/layout/main.py
+++ b/PC/layout/main.py
@@ -36,7 +36,7 @@ from .support.nuspec import *
BDIST_WININST_FILES_ONLY = FileNameSet("wininst-*", "bdist_wininst.py")
BDIST_WININST_STUB = "PC/layout/support/distutils.command.bdist_wininst.py"
-TEST_PYDS_ONLY = FileStemSet("xxlimited", "_ctypes_test", "_test*")
+TEST_PYDS_ONLY = FileStemSet("xxlimited", "xxlimited_35", "_ctypes_test", "_test*")
TEST_DIRS_ONLY = FileNameSet("test", "tests")
IDLE_DIRS_ONLY = FileNameSet("idlelib")
diff --git a/PCbuild/pcbuild.proj b/PCbuild/pcbuild.proj
index 4d416c5..8e7088d 100644
--- a/PCbuild/pcbuild.proj
+++ b/PCbuild/pcbuild.proj
@@ -66,6 +66,7 @@
<!-- Test modules -->
<TestModules Include="_ctypes_test;_testbuffer;_testcapi;_testinternalcapi;_testembed;_testimportmultiple;_testmultiphase;_testconsole" />
<TestModules Include="xxlimited" Condition="'$(Configuration)' == 'Release'" />
+ <TestModules Include="xxlimited_35" Condition="'$(Configuration)' == 'Release'" />
<Projects Include="@(TestModules->'%(Identity).vcxproj')" Condition="$(IncludeTests)">
<!-- Disable parallel build for test modules -->
<BuildInParallel>false</BuildInParallel>
diff --git a/PCbuild/readme.txt b/PCbuild/readme.txt
index 73833d5..4335c9f 100644
--- a/PCbuild/readme.txt
+++ b/PCbuild/readme.txt
@@ -125,6 +125,9 @@ python3dll
xxlimited
builds an example module that makes use of the PEP 384 Stable ABI,
see Modules\xxlimited.c
+xxlimited_35
+ ditto for testing the Python 3.5 stable ABI, see
+ Modules\xxlimited_35.c
The following sub-projects are for individual modules of the standard
library which are implemented in C; each one builds a DLL (renamed to
diff --git a/PCbuild/xxlimited.vcxproj b/PCbuild/xxlimited.vcxproj
index 776335a1..ece1691 100644
--- a/PCbuild/xxlimited.vcxproj
+++ b/PCbuild/xxlimited.vcxproj
@@ -94,7 +94,7 @@
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
- <PreprocessorDefinitions>%(PreprocessorDefinitions);Py_LIMITED_API=0x03060000</PreprocessorDefinitions>
+ <PreprocessorDefinitions>%(PreprocessorDefinitions);Py_LIMITED_API=0x03100000</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalDependencies>wsock32.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -111,4 +111,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
-</Project> \ No newline at end of file
+</Project>
diff --git a/PCbuild/xxlimited_35.vcxproj b/PCbuild/xxlimited_35.vcxproj
new file mode 100644
index 0000000..7e49ead
--- /dev/null
+++ b/PCbuild/xxlimited_35.vcxproj
@@ -0,0 +1,114 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|ARM">
+ <Configuration>Debug</Configuration>
+ <Platform>ARM</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Debug|ARM64">
+ <Configuration>Debug</Configuration>
+ <Platform>ARM64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Debug|x64">
+ <Configuration>Debug</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="PGInstrument|ARM">
+ <Configuration>PGInstrument</Configuration>
+ <Platform>ARM</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="PGInstrument|ARM64">
+ <Configuration>PGInstrument</Configuration>
+ <Platform>ARM64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="PGInstrument|Win32">
+ <Configuration>PGInstrument</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="PGInstrument|x64">
+ <Configuration>PGInstrument</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="PGUpdate|ARM">
+ <Configuration>PGUpdate</Configuration>
+ <Platform>ARM</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="PGUpdate|ARM64">
+ <Configuration>PGUpdate</Configuration>
+ <Platform>ARM64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="PGUpdate|Win32">
+ <Configuration>PGUpdate</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="PGUpdate|x64">
+ <Configuration>PGUpdate</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|ARM">
+ <Configuration>Release</Configuration>
+ <Platform>ARM</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|ARM64">
+ <Configuration>Release</Configuration>
+ <Platform>ARM64</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|x64">
+ <Configuration>Release</Configuration>
+ <Platform>x64</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <ProjectGuid>{fb868ea7-f93a-4d9b-be78-ca4e9ba14fff}</ProjectGuid>
+ <RootNamespace>xxlimited_35</RootNamespace>
+ <Keyword>Win32Proj</Keyword>
+ </PropertyGroup>
+ <Import Project="python.props" />
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Label="Configuration">
+ <ConfigurationType>DynamicLibrary</ConfigurationType>
+ <CharacterSet>NotSet</CharacterSet>
+ <SupportPGO>false</SupportPGO>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <PropertyGroup>
+ <TargetExt>.pyd</TargetExt>
+ </PropertyGroup>
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ <Import Project="pyproject.props" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup>
+ <_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
+ </PropertyGroup>
+ <ItemDefinitionGroup>
+ <ClCompile>
+ <PreprocessorDefinitions>%(PreprocessorDefinitions);Py_LIMITED_API=0x03060000</PreprocessorDefinitions>
+ </ClCompile>
+ <Link>
+ <AdditionalDependencies>wsock32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemGroup>
+ <ClCompile Include="..\Modules\xxlimited_35.c" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="python3dll.vcxproj">
+ <Project>{885d4898-d08d-4091-9c40-c700cfe3fc5a}</Project>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project>
diff --git a/PCbuild/xxlimited_35.vcxproj.filters b/PCbuild/xxlimited_35.vcxproj.filters
new file mode 100644
index 0000000..35bfb05
--- /dev/null
+++ b/PCbuild/xxlimited_35.vcxproj.filters
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup>
+ <Filter Include="Source Files">
+ <UniqueIdentifier>{5be27194-6530-452d-8d86-3767b991fa83}</UniqueIdentifier>
+ </Filter>
+ </ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="..\Modules\xxlimited_35.c">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ </ItemGroup>
+</Project>
diff --git a/setup.py b/setup.py
index 90588e8..ca5a04d 100644
--- a/setup.py
+++ b/setup.py
@@ -1803,8 +1803,16 @@ class PyBuildExt(build_ext):
## self.add(Extension('xx', ['xxmodule.c']))
if 'd' not in sysconfig.get_config_var('ABIFLAGS'):
+ # Non-debug mode: Build xxlimited with limited API
self.add(Extension('xxlimited', ['xxlimited.c'],
+ define_macros=[('Py_LIMITED_API', '0x03100000')]))
+ self.add(Extension('xxlimited_35', ['xxlimited_35.c'],
define_macros=[('Py_LIMITED_API', '0x03050000')]))
+ else:
+ # Debug mode: Build xxlimited with the full API
+ # (which is compatible with the limited one)
+ self.add(Extension('xxlimited', ['xxlimited.c']))
+ self.add(Extension('xxlimited_35', ['xxlimited_35.c']))
def detect_tkinter_explicitly(self):
# Build _tkinter using explicit locations for Tcl/Tk.