mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and PyVarObject_HEAD_INIT.
This commit is contained in:
parent
b1994b4a5d
commit
6819210b9e
129 changed files with 1090 additions and 1250 deletions
|
@ -214,7 +214,7 @@ PyObject_Init(PyObject *op, PyTypeObject *tp)
|
|||
if (op == NULL)
|
||||
return PyErr_NoMemory();
|
||||
/* Any changes should be reflected in PyObject_INIT (objimpl.h) */
|
||||
op->ob_type = tp;
|
||||
Py_Type(op) = tp;
|
||||
_Py_NewReference(op);
|
||||
return op;
|
||||
}
|
||||
|
@ -226,7 +226,7 @@ PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
|
|||
return (PyVarObject *) PyErr_NoMemory();
|
||||
/* Any changes should be reflected in PyObject_INIT_VAR */
|
||||
op->ob_size = size;
|
||||
op->ob_type = tp;
|
||||
Py_Type(op) = tp;
|
||||
_Py_NewReference((PyObject *)op);
|
||||
return op;
|
||||
}
|
||||
|
@ -287,7 +287,7 @@ internal_print(PyObject *op, FILE *fp, int flags, int nesting)
|
|||
universally available */
|
||||
fprintf(fp, "<refcnt %ld at %p>",
|
||||
(long)op->ob_refcnt, op);
|
||||
else if (op->ob_type->tp_print == NULL) {
|
||||
else if (Py_Type(op)->tp_print == NULL) {
|
||||
PyObject *s;
|
||||
if (flags & Py_PRINT_RAW)
|
||||
s = PyObject_Str(op);
|
||||
|
@ -302,7 +302,7 @@ internal_print(PyObject *op, FILE *fp, int flags, int nesting)
|
|||
Py_XDECREF(s);
|
||||
}
|
||||
else
|
||||
ret = (*op->ob_type->tp_print)(op, fp, flags);
|
||||
ret = (*Py_Type(op)->tp_print)(op, fp, flags);
|
||||
}
|
||||
if (ret == 0) {
|
||||
if (ferror(fp)) {
|
||||
|
@ -335,7 +335,7 @@ void _PyObject_Dump(PyObject* op)
|
|||
"type : %s\n"
|
||||
"refcount: %ld\n"
|
||||
"address : %p\n",
|
||||
op->ob_type==NULL ? "NULL" : op->ob_type->tp_name,
|
||||
Py_Type(op)==NULL ? "NULL" : Py_Type(op)->tp_name,
|
||||
(long)op->ob_refcnt,
|
||||
op);
|
||||
}
|
||||
|
@ -354,12 +354,12 @@ PyObject_Repr(PyObject *v)
|
|||
#endif
|
||||
if (v == NULL)
|
||||
return PyString_FromString("<NULL>");
|
||||
else if (v->ob_type->tp_repr == NULL)
|
||||
else if (Py_Type(v)->tp_repr == NULL)
|
||||
return PyString_FromFormat("<%s object at %p>",
|
||||
v->ob_type->tp_name, v);
|
||||
Py_Type(v)->tp_name, v);
|
||||
else {
|
||||
PyObject *res;
|
||||
res = (*v->ob_type->tp_repr)(v);
|
||||
res = (*Py_Type(v)->tp_repr)(v);
|
||||
if (res == NULL)
|
||||
return NULL;
|
||||
#ifdef Py_USING_UNICODE
|
||||
|
@ -376,7 +376,7 @@ PyObject_Repr(PyObject *v)
|
|||
if (!PyString_Check(res)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"__repr__ returned non-string (type %.200s)",
|
||||
res->ob_type->tp_name);
|
||||
Py_Type(res)->tp_name);
|
||||
Py_DECREF(res);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -401,10 +401,10 @@ _PyObject_Str(PyObject *v)
|
|||
return v;
|
||||
}
|
||||
#endif
|
||||
if (v->ob_type->tp_str == NULL)
|
||||
if (Py_Type(v)->tp_str == NULL)
|
||||
return PyObject_Repr(v);
|
||||
|
||||
res = (*v->ob_type->tp_str)(v);
|
||||
res = (*Py_Type(v)->tp_str)(v);
|
||||
if (res == NULL)
|
||||
return NULL;
|
||||
type_ok = PyString_Check(res);
|
||||
|
@ -414,7 +414,7 @@ _PyObject_Str(PyObject *v)
|
|||
if (!type_ok) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"__str__ returned non-string (type %.200s)",
|
||||
res->ob_type->tp_name);
|
||||
Py_Type(res)->tp_name);
|
||||
Py_DECREF(res);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -488,8 +488,8 @@ PyObject_Unicode(PyObject *v)
|
|||
res = v;
|
||||
}
|
||||
else {
|
||||
if (v->ob_type->tp_str != NULL)
|
||||
res = (*v->ob_type->tp_str)(v);
|
||||
if (Py_Type(v)->tp_str != NULL)
|
||||
res = (*Py_Type(v)->tp_str)(v);
|
||||
else
|
||||
res = PyObject_Repr(v);
|
||||
}
|
||||
|
@ -1062,8 +1062,8 @@ PyObject_GetAttrString(PyObject *v, const char *name)
|
|||
{
|
||||
PyObject *w, *res;
|
||||
|
||||
if (v->ob_type->tp_getattr != NULL)
|
||||
return (*v->ob_type->tp_getattr)(v, (char*)name);
|
||||
if (Py_Type(v)->tp_getattr != NULL)
|
||||
return (*Py_Type(v)->tp_getattr)(v, (char*)name);
|
||||
w = PyString_InternFromString(name);
|
||||
if (w == NULL)
|
||||
return NULL;
|
||||
|
@ -1090,8 +1090,8 @@ PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
|
|||
PyObject *s;
|
||||
int res;
|
||||
|
||||
if (v->ob_type->tp_setattr != NULL)
|
||||
return (*v->ob_type->tp_setattr)(v, (char*)name, w);
|
||||
if (Py_Type(v)->tp_setattr != NULL)
|
||||
return (*Py_Type(v)->tp_setattr)(v, (char*)name, w);
|
||||
s = PyString_InternFromString(name);
|
||||
if (s == NULL)
|
||||
return -1;
|
||||
|
@ -1103,7 +1103,7 @@ PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
|
|||
PyObject *
|
||||
PyObject_GetAttr(PyObject *v, PyObject *name)
|
||||
{
|
||||
PyTypeObject *tp = v->ob_type;
|
||||
PyTypeObject *tp = Py_Type(v);
|
||||
|
||||
if (!PyString_Check(name)) {
|
||||
#ifdef Py_USING_UNICODE
|
||||
|
@ -1120,7 +1120,7 @@ PyObject_GetAttr(PyObject *v, PyObject *name)
|
|||
{
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"attribute name must be string, not '%.200s'",
|
||||
name->ob_type->tp_name);
|
||||
Py_Type(name)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
@ -1149,7 +1149,7 @@ PyObject_HasAttr(PyObject *v, PyObject *name)
|
|||
int
|
||||
PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
|
||||
{
|
||||
PyTypeObject *tp = v->ob_type;
|
||||
PyTypeObject *tp = Py_Type(v);
|
||||
int err;
|
||||
|
||||
if (!PyString_Check(name)){
|
||||
|
@ -1167,7 +1167,7 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
|
|||
{
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"attribute name must be string, not '%.200s'",
|
||||
name->ob_type->tp_name);
|
||||
Py_Type(name)->tp_name);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
@ -1209,7 +1209,7 @@ PyObject **
|
|||
_PyObject_GetDictPtr(PyObject *obj)
|
||||
{
|
||||
Py_ssize_t dictoffset;
|
||||
PyTypeObject *tp = obj->ob_type;
|
||||
PyTypeObject *tp = Py_Type(obj);
|
||||
|
||||
if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
|
||||
return NULL;
|
||||
|
@ -1244,7 +1244,7 @@ PyObject_SelfIter(PyObject *obj)
|
|||
PyObject *
|
||||
PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
|
||||
{
|
||||
PyTypeObject *tp = obj->ob_type;
|
||||
PyTypeObject *tp = Py_Type(obj);
|
||||
PyObject *descr = NULL;
|
||||
PyObject *res = NULL;
|
||||
descrgetfunc f;
|
||||
|
@ -1266,7 +1266,7 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
|
|||
{
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"attribute name must be string, not '%.200s'",
|
||||
name->ob_type->tp_name);
|
||||
Py_Type(name)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
@ -1346,7 +1346,7 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
|
|||
}
|
||||
|
||||
if (f != NULL) {
|
||||
res = f(descr, obj, (PyObject *)obj->ob_type);
|
||||
res = f(descr, obj, (PyObject *)Py_Type(obj));
|
||||
Py_DECREF(descr);
|
||||
goto done;
|
||||
}
|
||||
|
@ -1368,7 +1368,7 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
|
|||
int
|
||||
PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
|
||||
{
|
||||
PyTypeObject *tp = obj->ob_type;
|
||||
PyTypeObject *tp = Py_Type(obj);
|
||||
PyObject *descr;
|
||||
descrsetfunc f;
|
||||
PyObject **dictptr;
|
||||
|
@ -1389,7 +1389,7 @@ PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
|
|||
{
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"attribute name must be string, not '%.200s'",
|
||||
name->ob_type->tp_name);
|
||||
Py_Type(name)->tp_name);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
@ -1683,7 +1683,7 @@ _dir_locals(void)
|
|||
if (!PyList_Check(names)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"dir(): expected keys() of locals to be a list, "
|
||||
"not '%.200s'", names->ob_type->tp_name);
|
||||
"not '%.200s'", Py_Type(names)->tp_name);
|
||||
Py_DECREF(names);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1818,7 +1818,7 @@ _dir_object(PyObject *obj)
|
|||
if (!PyList_Check(result)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"__dir__() must return a list, not %.200s",
|
||||
result->ob_type->tp_name);
|
||||
Py_Type(result)->tp_name);
|
||||
Py_DECREF(result);
|
||||
result = NULL;
|
||||
}
|
||||
|
@ -1880,8 +1880,7 @@ none_dealloc(PyObject* ignore)
|
|||
|
||||
|
||||
static PyTypeObject PyNone_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0,
|
||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||
"NoneType",
|
||||
0,
|
||||
0,
|
||||
|
@ -1898,7 +1897,8 @@ static PyTypeObject PyNone_Type = {
|
|||
};
|
||||
|
||||
PyObject _Py_NoneStruct = {
|
||||
PyObject_HEAD_INIT(&PyNone_Type)
|
||||
_PyObject_EXTRA_INIT
|
||||
1, &PyNone_Type
|
||||
};
|
||||
|
||||
/* NotImplemented is an object that can be used to signal that an
|
||||
|
@ -1911,8 +1911,7 @@ NotImplemented_repr(PyObject *op)
|
|||
}
|
||||
|
||||
static PyTypeObject PyNotImplemented_Type = {
|
||||
PyObject_HEAD_INIT(&PyType_Type)
|
||||
0,
|
||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||
"NotImplementedType",
|
||||
0,
|
||||
0,
|
||||
|
@ -1929,7 +1928,8 @@ static PyTypeObject PyNotImplemented_Type = {
|
|||
};
|
||||
|
||||
PyObject _Py_NotImplementedStruct = {
|
||||
PyObject_HEAD_INIT(&PyNotImplemented_Type)
|
||||
_PyObject_EXTRA_INIT
|
||||
1, &PyNotImplemented_Type
|
||||
};
|
||||
|
||||
void
|
||||
|
@ -1997,7 +1997,7 @@ _Py_ForgetReference(register PyObject *op)
|
|||
void
|
||||
_Py_Dealloc(PyObject *op)
|
||||
{
|
||||
destructor dealloc = op->ob_type->tp_dealloc;
|
||||
destructor dealloc = Py_Type(op)->tp_dealloc;
|
||||
_Py_ForgetReference(op);
|
||||
(*dealloc)(op);
|
||||
}
|
||||
|
@ -2028,7 +2028,7 @@ _Py_PrintReferenceAddresses(FILE *fp)
|
|||
fprintf(fp, "Remaining object addresses:\n");
|
||||
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
|
||||
fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
|
||||
op->ob_refcnt, op->ob_type->tp_name);
|
||||
op->ob_refcnt, Py_Type(op)->tp_name);
|
||||
}
|
||||
|
||||
PyObject *
|
||||
|
@ -2046,7 +2046,7 @@ _Py_GetObjects(PyObject *self, PyObject *args)
|
|||
return NULL;
|
||||
for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
|
||||
while (op == self || op == args || op == res || op == t ||
|
||||
(t != NULL && op->ob_type != (PyTypeObject *) t)) {
|
||||
(t != NULL && Py_Type(op) != (PyTypeObject *) t)) {
|
||||
op = op->_ob_next;
|
||||
if (op == &refchain)
|
||||
return res;
|
||||
|
@ -2189,7 +2189,7 @@ _PyTrash_destroy_chain(void)
|
|||
{
|
||||
while (_PyTrash_delete_later) {
|
||||
PyObject *op = _PyTrash_delete_later;
|
||||
destructor dealloc = op->ob_type->tp_dealloc;
|
||||
destructor dealloc = Py_Type(op)->tp_dealloc;
|
||||
|
||||
_PyTrash_delete_later =
|
||||
(PyObject*) _Py_AS_GC(op)->gc.gc_prev;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue