mirror of
https://github.com/python/cpython.git
synced 2025-07-24 19:54:21 +00:00

svn+ssh://pythondev@svn.python.org/python/trunk ........ r60850 | amaury.forgeotdarc | 2008-02-16 01:16:50 +0100 (Sat, 16 Feb 2008) | 3 lines mmap.PROT_READ does not exists on win32; Skip this test created by r60830. ........ r60851 | raymond.hettinger | 2008-02-16 02:22:54 +0100 (Sat, 16 Feb 2008) | 1 line Add __all__ to logging module. ........ r60855 | georg.brandl | 2008-02-16 10:37:32 +0100 (Sat, 16 Feb 2008) | 2 lines #2120: broken links in advocacy document. ........ r60860 | amaury.forgeotdarc | 2008-02-16 15:34:57 +0100 (Sat, 16 Feb 2008) | 23 lines Crashers of the day: Py_CLEAR must be used when there is a chance that the function can be called recursively. This was discussed in issue1020188. In python codebase, all occurrences of Py_[X]DECREF(xxx->yyy) are suspect, except when they appear in tp_new or tp_dealloc functions, or when the member cannot be of a user-defined class. Note that tp_init is not safe. I do have a (crashing) example for every changed line. Is it worth adding them to the test suite? Example: class SpecialStr(str): def __del__(self): s.close() import cStringIO s = cStringIO.StringIO(SpecialStr("text")) s.close() # Segfault ........ r60871 | amaury.forgeotdarc | 2008-02-16 21:55:24 +0100 (Sat, 16 Feb 2008) | 3 lines Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer is still present in the containing structure. ........ r60872 | brett.cannon | 2008-02-17 02:59:18 +0100 (Sun, 17 Feb 2008) | 4 lines Move test_logging over to doctest. Thanks to Christopher White from GHOP. ........ r60873 | georg.brandl | 2008-02-17 12:33:38 +0100 (Sun, 17 Feb 2008) | 2 lines #2131: note that codecs.open() always opens files in binary mode. ........
127 lines
2.6 KiB
C
127 lines
2.6 KiB
C
/* Cell object implementation */
|
|
|
|
#include "Python.h"
|
|
|
|
PyObject *
|
|
PyCell_New(PyObject *obj)
|
|
{
|
|
PyCellObject *op;
|
|
|
|
op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
|
|
if (op == NULL)
|
|
return NULL;
|
|
op->ob_ref = obj;
|
|
Py_XINCREF(obj);
|
|
|
|
_PyObject_GC_TRACK(op);
|
|
return (PyObject *)op;
|
|
}
|
|
|
|
PyObject *
|
|
PyCell_Get(PyObject *op)
|
|
{
|
|
if (!PyCell_Check(op)) {
|
|
PyErr_BadInternalCall();
|
|
return NULL;
|
|
}
|
|
Py_XINCREF(((PyCellObject*)op)->ob_ref);
|
|
return PyCell_GET(op);
|
|
}
|
|
|
|
int
|
|
PyCell_Set(PyObject *op, PyObject *obj)
|
|
{
|
|
PyObject* oldobj;
|
|
if (!PyCell_Check(op)) {
|
|
PyErr_BadInternalCall();
|
|
return -1;
|
|
}
|
|
oldobj = PyCell_GET(op);
|
|
Py_XINCREF(obj);
|
|
PyCell_SET(op, obj);
|
|
Py_XDECREF(oldobj);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
cell_dealloc(PyCellObject *op)
|
|
{
|
|
_PyObject_GC_UNTRACK(op);
|
|
Py_XDECREF(op->ob_ref);
|
|
PyObject_GC_Del(op);
|
|
}
|
|
|
|
static PyObject *
|
|
cell_repr(PyCellObject *op)
|
|
{
|
|
if (op->ob_ref == NULL)
|
|
return PyUnicode_FromFormat("<cell at %p: empty>", op);
|
|
|
|
return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
|
|
op, op->ob_ref->ob_type->tp_name,
|
|
op->ob_ref);
|
|
}
|
|
|
|
static int
|
|
cell_traverse(PyCellObject *op, visitproc visit, void *arg)
|
|
{
|
|
Py_VISIT(op->ob_ref);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
cell_clear(PyCellObject *op)
|
|
{
|
|
Py_CLEAR(op->ob_ref);
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
cell_get_contents(PyCellObject *op, void *closure)
|
|
{
|
|
if (op->ob_ref == NULL)
|
|
{
|
|
PyErr_SetString(PyExc_ValueError, "Cell is empty");
|
|
return NULL;
|
|
}
|
|
Py_INCREF(op->ob_ref);
|
|
return op->ob_ref;
|
|
}
|
|
|
|
static PyGetSetDef cell_getsetlist[] = {
|
|
{"cell_contents", (getter)cell_get_contents, NULL},
|
|
{NULL} /* sentinel */
|
|
};
|
|
|
|
PyTypeObject PyCell_Type = {
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
"cell",
|
|
sizeof(PyCellObject),
|
|
0,
|
|
(destructor)cell_dealloc, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
(reprfunc)cell_repr, /* tp_repr */
|
|
0, /* tp_as_number */
|
|
0, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
0, /* tp_hash */
|
|
0, /* tp_call */
|
|
0, /* tp_str */
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
0, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
|
0, /* tp_doc */
|
|
(traverseproc)cell_traverse, /* tp_traverse */
|
|
(inquiry)cell_clear, /* tp_clear */
|
|
0, /* tp_richcompare */
|
|
0, /* tp_weaklistoffset */
|
|
0, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
0, /* tp_methods */
|
|
0, /* tp_members */
|
|
cell_getsetlist, /* tp_getset */
|
|
};
|