[Python-checkins] r85972 - in python/branches/pep-0384: Modules/xxlimited.c Objects/object.c setup.py
martin.v.loewis
python-checkins at python.org
Sat Oct 30 11:57:04 CEST 2010
Author: martin.v.loewis
Date: Sat Oct 30 11:57:04 2010
New Revision: 85972
Log:
Add module to exercise limited API.
Added:
python/branches/pep-0384/Modules/xxlimited.c
- copied, changed from r85970, /python/branches/pep-0384/Modules/xxmodule.c
Modified:
python/branches/pep-0384/Objects/object.c
python/branches/pep-0384/setup.py
Copied: python/branches/pep-0384/Modules/xxlimited.c (from r85970, /python/branches/pep-0384/Modules/xxmodule.c)
==============================================================================
--- /python/branches/pep-0384/Modules/xxmodule.c (original)
+++ python/branches/pep-0384/Modules/xxlimited.c Sat Oct 30 11:57:04 2010
@@ -23,15 +23,15 @@
PyObject *x_attr; /* Attributes dictionary */
} XxoObject;
-static PyTypeObject Xxo_Type;
+static PyObject *Xxo_Type;
-#define XxoObject_Check(v) (Py_TYPE(v) == &Xxo_Type)
+#define XxoObject_Check(v) (Py_TYPE(v) == Xxo_Type)
static XxoObject *
newXxoObject(PyObject *arg)
{
XxoObject *self;
- self = PyObject_New(XxoObject, &Xxo_Type);
+ self = PyObject_New(XxoObject, (PyTypeObject*)Xxo_Type);
if (self == NULL)
return NULL;
self->x_attr = NULL;
@@ -94,51 +94,22 @@
return PyDict_SetItemString(self->x_attr, name, v);
}
-static PyTypeObject Xxo_Type = {
- /* The ob_type field must be initialized in the module init function
- * to be portable to Windows without using C++. */
- PyVarObject_HEAD_INIT(NULL, 0)
- "xxmodule.Xxo", /*tp_name*/
- sizeof(XxoObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)Xxo_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- (getattrfunc)0, /*tp_getattr*/
- (setattrfunc)Xxo_setattr, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- (getattrofunc)Xxo_getattro, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- Xxo_methods, /*tp_methods*/
- 0, /*tp_members*/
- 0, /*tp_getset*/
- 0, /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- 0, /*tp_init*/
- 0, /*tp_alloc*/
- 0, /*tp_new*/
- 0, /*tp_free*/
- 0, /*tp_is_gc*/
+static PyType_Slot Xxo_Type_slots[] = {
+ {Py_tp_dealloc, Xxo_dealloc},
+ {Py_tp_getattro, Xxo_getattro},
+ {Py_tp_methods, Xxo_methods},
+ {0, 0},
};
+
+static PyType_Spec Xxo_Type_spec = {
+ "xxmodule.Xxo",
+ NULL,
+ sizeof(XxoObject),
+ 0,
+ Py_TPFLAGS_DEFAULT,
+ Xxo_Type_slots
+};
+
/* --------------------------------------------------------------------- */
/* Function of two integers returning integer */
@@ -175,27 +146,6 @@
return (PyObject *)rv;
}
-/* Example with subtle bug from extensions manual ("Thin Ice"). */
-
-static PyObject *
-xx_bug(PyObject *self, PyObject *args)
-{
- PyObject *list, *item;
-
- if (!PyArg_ParseTuple(args, "O:bug", &list))
- return NULL;
-
- item = PyList_GetItem(list, 0);
- /* Py_INCREF(item); */
- PyList_SetItem(list, 1, PyLong_FromLong(0L));
- PyObject_Print(item, stdout, 0);
- printf("\n");
- /* Py_DECREF(item); */
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
/* Test bad format character */
static PyObject *
@@ -212,50 +162,18 @@
/* ---------- */
-static PyTypeObject Str_Type = {
- /* The ob_type field must be initialized in the module init function
- * to be portable to Windows without using C++. */
- PyVarObject_HEAD_INIT(NULL, 0)
- "xxmodule.Str", /*tp_name*/
- 0, /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- 0, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- 0, /*tp_methods*/
- 0, /*tp_members*/
- 0, /*tp_getset*/
- 0, /* see PyInit_xx */ /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- 0, /*tp_init*/
- 0, /*tp_alloc*/
- 0, /*tp_new*/
- 0, /*tp_free*/
- 0, /*tp_is_gc*/
+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,
+ 0,
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ Str_Type_slots
};
/* ---------- */
@@ -267,56 +185,24 @@
return Py_NotImplemented;
}
-static PyTypeObject Null_Type = {
- /* The ob_type field must be initialized in the module init function
- * to be portable to Windows without using C++. */
- PyVarObject_HEAD_INIT(NULL, 0)
- "xxmodule.Null", /*tp_name*/
- 0, /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- 0, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- null_richcompare, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- 0, /*tp_methods*/
- 0, /*tp_members*/
- 0, /*tp_getset*/
- 0, /* see PyInit_xx */ /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- 0, /*tp_init*/
- 0, /*tp_alloc*/
- 0, /* see PyInit_xx */ /*tp_new*/
- 0, /*tp_free*/
- 0, /*tp_is_gc*/
+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",
+ NULL, /* doc */
+ 0, /* basicsize */
+ 0, /* itemsize */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ Null_Type_slots
+};
/* ---------- */
-
/* List of functions defined in the module */
static PyMethodDef xx_methods[] = {
@@ -326,8 +212,6 @@
xx_foo_doc},
{"new", xx_new, METH_VARARGS,
PyDoc_STR("new() -> new Xx object")},
- {"bug", xx_bug, METH_VARARGS,
- PyDoc_STR("bug(o) -> None")},
{NULL, NULL} /* sentinel */
};
@@ -350,20 +234,20 @@
};
PyMODINIT_FUNC
-PyInit_xx(void)
+PyInit_xxlimited(void)
{
PyObject *m = NULL;
+ 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.tp_base = &PyBaseObject_Type;
- Null_Type.tp_new = PyType_GenericNew;
- Str_Type.tp_base = &PyUnicode_Type;
-
- /* Finalize the type object including setting type of the new type
- * object; doing it here is required for portability, too. */
- if (PyType_Ready(&Xxo_Type) < 0)
+ 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;
/* Create the module and add the functions */
@@ -381,14 +265,16 @@
PyModule_AddObject(m, "error", ErrorObject);
/* Add Str */
- if (PyType_Ready(&Str_Type) < 0)
+ o = PyType_FromSpec(&Str_Type_spec);
+ if (o == NULL)
goto fail;
- PyModule_AddObject(m, "Str", (PyObject *)&Str_Type);
+ PyModule_AddObject(m, "Str", o);
/* Add Null */
- if (PyType_Ready(&Null_Type) < 0)
+ o = PyType_FromSpec(&Null_Type_spec);
+ if (o == NULL)
goto fail;
- PyModule_AddObject(m, "Null", (PyObject *)&Null_Type);
+ PyModule_AddObject(m, "Null", o);
return m;
fail:
Py_XDECREF(m);
Modified: python/branches/pep-0384/Objects/object.c
==============================================================================
--- python/branches/pep-0384/Objects/object.c (original)
+++ python/branches/pep-0384/Objects/object.c Sat Oct 30 11:57:04 2010
@@ -1739,7 +1739,6 @@
#endif
-
/* Hack to force loading of pycapsule.o */
PyTypeObject *_PyCapsule_hack = &PyCapsule_Type;
@@ -1884,6 +1883,18 @@
}
}
+#ifndef Py_TRACE_REFS
+/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
+ Define this here, so we can undefine the macro. */
+#undef _Py_Dealloc
+void
+_Py_Dealloc(PyObject *op)
+{
+ _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
+ (*Py_TYPE(op)->tp_dealloc)(op);
+}
+#endif
+
#ifdef __cplusplus
}
#endif
Modified: python/branches/pep-0384/setup.py
==============================================================================
--- python/branches/pep-0384/setup.py (original)
+++ python/branches/pep-0384/setup.py Sat Oct 30 11:57:04 2010
@@ -1552,6 +1552,9 @@
## # Uncomment these lines if you want to play with xxmodule.c
## ext = Extension('xx', ['xxmodule.c'])
## self.extensions.append(ext)
+ ext = Extension('xxlimited', ['xxlimited.c'],
+ define_macros=[('Py_LIMITED_API', 1)])
+ self.extensions.append(ext)
# XXX handle these, but how to detect?
# *** Uncomment and edit for PIL (TkImaging) extension only:
More information about the Python-checkins
mailing list