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:
Martin v. Löwis 2007-07-21 06:55:02 +00:00
parent b1994b4a5d
commit 6819210b9e
129 changed files with 1090 additions and 1250 deletions

View file

@ -421,7 +421,7 @@ PyObject *PyString_AsDecodedString(PyObject *str,
if (!PyString_Check(v)) {
PyErr_Format(PyExc_TypeError,
"decoder did not return a string object (type=%.400s)",
v->ob_type->tp_name);
Py_Type(v)->tp_name);
Py_DECREF(v);
goto onError;
}
@ -501,7 +501,7 @@ PyObject *PyString_AsEncodedString(PyObject *str,
if (!PyString_Check(v)) {
PyErr_Format(PyExc_TypeError,
"encoder did not return a string object (type=%.400s)",
v->ob_type->tp_name);
Py_Type(v)->tp_name);
Py_DECREF(v);
goto onError;
}
@ -521,7 +521,7 @@ string_dealloc(PyObject *op)
case SSTATE_INTERNED_MORTAL:
/* revive dead object temporarily for DelItem */
op->ob_refcnt = 3;
Py_Refcnt(op) = 3;
if (PyDict_DelItem(interned, op) != 0)
Py_FatalError(
"deletion of interned string failed");
@ -533,7 +533,7 @@ string_dealloc(PyObject *op)
default:
Py_FatalError("Inconsistent interned string state.");
}
op->ob_type->tp_free(op);
Py_Type(op)->tp_free(op);
}
/* Unescape a backslash-escaped string. If unicode is non-zero,
@ -717,7 +717,7 @@ PyString_Size(register PyObject *op)
{
if (!PyString_Check(op))
return string_getsize(op);
return ((PyStringObject *)op) -> ob_size;
return Py_Size(op);
}
/*const*/ char *
@ -750,7 +750,7 @@ PyString_AsStringAndSize(register PyObject *obj,
{
PyErr_Format(PyExc_TypeError,
"expected string or Unicode object, "
"%.200s found", obj->ob_type->tp_name);
"%.200s found", Py_Type(obj)->tp_name);
return -1;
}
}
@ -805,7 +805,7 @@ string_print(PyStringObject *op, FILE *fp, int flags)
}
if (flags & Py_PRINT_RAW) {
char *data = op->ob_sval;
Py_ssize_t size = op->ob_size;
Py_ssize_t size = Py_Size(op);
while (size > INT_MAX) {
/* Very long strings cannot be written atomically.
* But don't write exactly INT_MAX bytes at a time
@ -826,12 +826,12 @@ string_print(PyStringObject *op, FILE *fp, int flags)
/* figure out which quote to use; single is preferred */
quote = '\'';
if (memchr(op->ob_sval, '\'', op->ob_size) &&
!memchr(op->ob_sval, '"', op->ob_size))
if (memchr(op->ob_sval, '\'', Py_Size(op)) &&
!memchr(op->ob_sval, '"', Py_Size(op)))
quote = '"';
fputc(quote, fp);
for (i = 0; i < op->ob_size; i++) {
for (i = 0; i < Py_Size(op); i++) {
c = op->ob_sval[i];
if (c == quote || c == '\\')
fprintf(fp, "\\%c", c);
@ -854,9 +854,9 @@ PyObject *
PyString_Repr(PyObject *obj, int smartquotes)
{
register PyStringObject* op = (PyStringObject*) obj;
size_t newsize = 2 + 4 * op->ob_size;
size_t newsize = 2 + 4 * Py_Size(op);
PyObject *v;
if (newsize > PY_SSIZE_T_MAX || newsize / 4 != op->ob_size) {
if (newsize > PY_SSIZE_T_MAX || newsize / 4 != Py_Size(op)) {
PyErr_SetString(PyExc_OverflowError,
"string is too large to make repr");
}
@ -873,13 +873,13 @@ PyString_Repr(PyObject *obj, int smartquotes)
/* figure out which quote to use; single is preferred */
quote = '\'';
if (smartquotes &&
memchr(op->ob_sval, '\'', op->ob_size) &&
!memchr(op->ob_sval, '"', op->ob_size))
memchr(op->ob_sval, '\'', Py_Size(op)) &&
!memchr(op->ob_sval, '"', Py_Size(op)))
quote = '"';
p = PyString_AS_STRING(v);
*p++ = quote;
for (i = 0; i < op->ob_size; i++) {
for (i = 0; i < Py_Size(op); i++) {
/* There's at least enough room for a hex escape
and a closing quote. */
assert(newsize - (p - PyString_AS_STRING(v)) >= 5);
@ -928,14 +928,14 @@ string_str(PyObject *s)
else {
/* Subtype -- return genuine string with the same value. */
PyStringObject *t = (PyStringObject *) s;
return PyString_FromStringAndSize(t->ob_sval, t->ob_size);
return PyString_FromStringAndSize(t->ob_sval, Py_Size(t));
}
}
static Py_ssize_t
string_length(PyStringObject *a)
{
return a->ob_size;
return Py_Size(a);
}
static PyObject *
@ -950,21 +950,21 @@ string_concat(register PyStringObject *a, register PyObject *bb)
#endif
PyErr_Format(PyExc_TypeError,
"cannot concatenate 'str' and '%.200s' objects",
bb->ob_type->tp_name);
Py_Type(bb)->tp_name);
return NULL;
}
#define b ((PyStringObject *)bb)
/* Optimize cases with empty left or right operand */
if ((a->ob_size == 0 || b->ob_size == 0) &&
if ((Py_Size(a) == 0 || Py_Size(b) == 0) &&
PyString_CheckExact(a) && PyString_CheckExact(b)) {
if (a->ob_size == 0) {
if (Py_Size(a) == 0) {
Py_INCREF(bb);
return bb;
}
Py_INCREF(a);
return (PyObject *)a;
}
size = a->ob_size + b->ob_size;
size = Py_Size(a) + Py_Size(b);
if (size < 0) {
PyErr_SetString(PyExc_OverflowError,
"strings are too large to concat");
@ -978,8 +978,8 @@ string_concat(register PyStringObject *a, register PyObject *bb)
PyObject_INIT_VAR(op, &PyString_Type, size);
op->ob_shash = -1;
op->ob_sstate = SSTATE_NOT_INTERNED;
Py_MEMCPY(op->ob_sval, a->ob_sval, a->ob_size);
Py_MEMCPY(op->ob_sval + a->ob_size, b->ob_sval, b->ob_size);
Py_MEMCPY(op->ob_sval, a->ob_sval, Py_Size(a));
Py_MEMCPY(op->ob_sval + Py_Size(a), b->ob_sval, Py_Size(b));
op->ob_sval[size] = '\0';
return (PyObject *) op;
#undef b
@ -998,13 +998,13 @@ string_repeat(register PyStringObject *a, register Py_ssize_t n)
/* watch out for overflows: the size can overflow int,
* and the # of bytes needed can overflow size_t
*/
size = a->ob_size * n;
if (n && size / n != a->ob_size) {
size = Py_Size(a) * n;
if (n && size / n != Py_Size(a)) {
PyErr_SetString(PyExc_OverflowError,
"repeated string is too long");
return NULL;
}
if (size == a->ob_size && PyString_CheckExact(a)) {
if (size == Py_Size(a) && PyString_CheckExact(a)) {
Py_INCREF(a);
return (PyObject *)a;
}
@ -1022,14 +1022,14 @@ string_repeat(register PyStringObject *a, register Py_ssize_t n)
op->ob_shash = -1;
op->ob_sstate = SSTATE_NOT_INTERNED;
op->ob_sval[size] = '\0';
if (a->ob_size == 1 && n > 0) {
if (Py_Size(a) == 1 && n > 0) {
memset(op->ob_sval, a->ob_sval[0] , n);
return (PyObject *) op;
}
i = 0;
if (i < size) {
Py_MEMCPY(op->ob_sval, a->ob_sval, a->ob_size);
i = a->ob_size;
Py_MEMCPY(op->ob_sval, a->ob_sval, Py_Size(a));
i = Py_Size(a);
}
while (i < size) {
j = (i <= size-i) ? i : size-i;
@ -1050,9 +1050,9 @@ string_slice(register PyStringObject *a, register Py_ssize_t i,
i = 0;
if (j < 0)
j = 0; /* Avoid signed/unsigned bug in next line */
if (j > a->ob_size)
j = a->ob_size;
if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) {
if (j > Py_Size(a))
j = Py_Size(a);
if (i == 0 && j == Py_Size(a) && PyString_CheckExact(a)) {
/* It's the same as a */
Py_INCREF(a);
return (PyObject *)a;
@ -1073,7 +1073,7 @@ string_contains(PyObject *str_obj, PyObject *sub_obj)
if (!PyString_Check(sub_obj)) {
PyErr_Format(PyExc_TypeError,
"'in <string>' requires string as left operand, "
"not %.200s", sub_obj->ob_type->tp_name);
"not %.200s", Py_Type(sub_obj)->tp_name);
return -1;
}
}
@ -1086,7 +1086,7 @@ string_item(PyStringObject *a, register Py_ssize_t i)
{
char pchar;
PyObject *v;
if (i < 0 || i >= a->ob_size) {
if (i < 0 || i >= Py_Size(a)) {
PyErr_SetString(PyExc_IndexError, "string index out of range");
return NULL;
}
@ -1129,16 +1129,16 @@ string_richcompare(PyStringObject *a, PyStringObject *b, int op)
if (op == Py_EQ) {
/* Supporting Py_NE here as well does not save
much time, since Py_NE is rarely used. */
if (a->ob_size == b->ob_size
if (Py_Size(a) == Py_Size(b)
&& (a->ob_sval[0] == b->ob_sval[0]
&& memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0)) {
&& memcmp(a->ob_sval, b->ob_sval, Py_Size(a)) == 0)) {
result = Py_True;
} else {
result = Py_False;
}
goto out;
}
len_a = a->ob_size; len_b = b->ob_size;
len_a = Py_Size(a); len_b = Py_Size(b);
min_len = (len_a < len_b) ? len_a : len_b;
if (min_len > 0) {
c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
@ -1170,9 +1170,9 @@ _PyString_Eq(PyObject *o1, PyObject *o2)
{
PyStringObject *a = (PyStringObject*) o1;
PyStringObject *b = (PyStringObject*) o2;
return a->ob_size == b->ob_size
return Py_Size(a) == Py_Size(b)
&& *a->ob_sval == *b->ob_sval
&& memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0;
&& memcmp(a->ob_sval, b->ob_sval, Py_Size(a)) == 0;
}
static long
@ -1184,12 +1184,12 @@ string_hash(PyStringObject *a)
if (a->ob_shash != -1)
return a->ob_shash;
len = a->ob_size;
len = Py_Size(a);
p = (unsigned char *) a->ob_sval;
x = *p << 7;
while (--len >= 0)
x = (1000003*x) ^ *p++;
x ^= a->ob_size;
x ^= Py_Size(a);
if (x == -1)
x = -2;
a->ob_shash = x;
@ -1242,7 +1242,7 @@ string_subscript(PyStringObject* self, PyObject* item)
else {
PyErr_Format(PyExc_TypeError,
"string indices must be integers, not %.200s",
item->ob_type->tp_name);
Py_Type(item)->tp_name);
return NULL;
}
}
@ -1256,7 +1256,7 @@ string_buffer_getreadbuf(PyStringObject *self, Py_ssize_t index, const void **pt
return -1;
}
*ptr = (void *)self->ob_sval;
return self->ob_size;
return Py_Size(self);
}
static Py_ssize_t
@ -1271,7 +1271,7 @@ static Py_ssize_t
string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp)
{
if ( lenp )
*lenp = self->ob_size;
*lenp = Py_Size(self);
return 1;
}
@ -1284,7 +1284,7 @@ string_buffer_getcharbuf(PyStringObject *self, Py_ssize_t index, const char **pt
return -1;
}
*ptr = self->ob_sval;
return self->ob_size;
return Py_Size(self);
}
static PySequenceMethods string_as_sequence = {
@ -1373,7 +1373,7 @@ static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
count++; }
/* Always force the list to the expected size. */
#define FIX_PREALLOC_SIZE(list) ((PyListObject *)list)->ob_size = count
#define FIX_PREALLOC_SIZE(list) Py_Size(list) = count
#define SKIP_SPACE(s, i, len) { while (i<len && isspace(Py_CHARMASK(s[i]))) i++; }
#define SKIP_NONSPACE(s, i, len) { while (i<len && !isspace(Py_CHARMASK(s[i]))) i++; }
@ -1788,7 +1788,7 @@ string_join(PyStringObject *self, PyObject *orig)
PyErr_Format(PyExc_TypeError,
"sequence item %zd: expected string,"
" %.80s found",
i, item->ob_type->tp_name);
i, Py_Type(item)->tp_name);
Py_DECREF(seq);
return NULL;
}
@ -3248,7 +3248,7 @@ string_encode(PyStringObject *self, PyObject *args)
PyErr_Format(PyExc_TypeError,
"encoder did not return a string/unicode object "
"(type=%.400s)",
v->ob_type->tp_name);
Py_Type(v)->tp_name);
Py_DECREF(v);
return NULL;
}
@ -3285,7 +3285,7 @@ string_decode(PyStringObject *self, PyObject *args)
PyErr_Format(PyExc_TypeError,
"decoder did not return a string/unicode object "
"(type=%.400s)",
v->ob_type->tp_name);
Py_Type(v)->tp_name);
Py_DECREF(v);
return NULL;
}
@ -3843,7 +3843,7 @@ string_splitlines(PyStringObject *self, PyObject *args)
static PyObject *
string_getnewargs(PyStringObject *v)
{
return Py_BuildValue("(s#)", v->ob_sval, v->ob_size);
return Py_BuildValue("(s#)", v->ob_sval, Py_Size(v));
}
@ -3972,8 +3972,7 @@ static PyNumberMethods string_as_number = {
PyTypeObject PyBaseString_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"basestring",
0,
0,
@ -4021,8 +4020,7 @@ Return a nice string representation of the object.\n\
If the argument is a string, the return value is the same object.");
PyTypeObject PyString_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"str",
sizeof(PyStringObject),
sizeof(char),
@ -4108,7 +4106,7 @@ _PyString_Resize(PyObject **pv, Py_ssize_t newsize)
register PyObject *v;
register PyStringObject *sv;
v = *pv;
if (!PyString_Check(v) || v->ob_refcnt != 1 || newsize < 0 ||
if (!PyString_Check(v) || Py_Refcnt(v) != 1 || newsize < 0 ||
PyString_CHECK_INTERNED(v)) {
*pv = 0;
Py_DECREF(v);
@ -4127,7 +4125,7 @@ _PyString_Resize(PyObject **pv, Py_ssize_t newsize)
}
_Py_NewReference(*pv);
sv = (PyStringObject *) *pv;
sv->ob_size = newsize;
Py_Size(sv) = newsize;
sv->ob_sval[newsize] = '\0';
sv->ob_shash = -1; /* invalidate cached hash value */
return 0;
@ -4175,7 +4173,7 @@ formatfloat(char *buf, size_t buflen, int flags,
x = PyFloat_AsDouble(v);
if (x == -1.0 && PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError, "float argument required, "
"not %.200s", v->ob_type->tp_name);
"not %.200s", Py_Type(v)->tp_name);
return -1;
}
if (prec < 0)
@ -4249,15 +4247,15 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
switch (type) {
case 'd':
case 'u':
result = val->ob_type->tp_str(val);
result = Py_Type(val)->tp_str(val);
break;
case 'o':
result = val->ob_type->tp_as_number->nb_oct(val);
result = Py_Type(val)->tp_as_number->nb_oct(val);
break;
case 'x':
case 'X':
numnondigits = 2;
result = val->ob_type->tp_as_number->nb_hex(val);
result = Py_Type(val)->tp_as_number->nb_hex(val);
break;
default:
assert(!"'type' not in [duoxX]");
@ -4272,7 +4270,7 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
}
/* To modify the string in-place, there can only be one reference. */
if (result->ob_refcnt != 1) {
if (Py_Refcnt(result) != 1) {
PyErr_BadInternalCall();
return NULL;
}
@ -4372,7 +4370,7 @@ formatint(char *buf, size_t buflen, int flags,
x = PyInt_AsLong(v);
if (x == -1 && PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError, "int argument required, not %.200s",
v->ob_type->tp_name);
Py_Type(v)->tp_name);
return -1;
}
if (x < 0 && type == 'u') {
@ -4489,7 +4487,7 @@ PyString_Format(PyObject *format, PyObject *args)
arglen = -1;
argidx = -2;
}
if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) &&
if (Py_Type(args)->tp_as_mapping && !PyTuple_Check(args) &&
!PyObject_TypeCheck(args, &PyBaseString_Type))
dict = args;
while (--fmtcnt >= 0) {
@ -4956,7 +4954,7 @@ PyString_InternInPlace(PyObject **p)
}
/* The two references in interned are not counted by refcnt.
The string deallocator will take care of this */
s->ob_refcnt -= 2;
Py_Refcnt(s) -= 2;
PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL;
}
@ -5023,12 +5021,12 @@ void _Py_ReleaseInternedStrings(void)
/* XXX Shouldn't happen */
break;
case SSTATE_INTERNED_IMMORTAL:
s->ob_refcnt += 1;
immortal_size += s->ob_size;
Py_Refcnt(s) += 1;
immortal_size += Py_Size(s);
break;
case SSTATE_INTERNED_MORTAL:
s->ob_refcnt += 2;
mortal_size += s->ob_size;
Py_Refcnt(s) += 2;
mortal_size += Py_Size(s);
break;
default:
Py_FatalError("Inconsistent interned string state.");