[Python-3000-checkins] r58763 - in python/branches/py3k-pep3137: Modules/_ctypes/_ctypes.c Modules/_ctypes/cfield.c Modules/_sqlite/statement.c Objects/bytesobject.c Objects/unicodeobject.c Parser/tokenizer.c Python/ast.c Python/codecs.c Python/getargs.c Python/import.c Python/marshal.c Python/pythonrun.c
guido.van.rossum
python-3000-checkins at python.org
Fri Nov 2 02:02:02 CET 2007
Author: guido.van.rossum
Date: Fri Nov 2 02:02:01 2007
New Revision: 58763
Modified:
python/branches/py3k-pep3137/Modules/_ctypes/_ctypes.c
python/branches/py3k-pep3137/Modules/_ctypes/cfield.c
python/branches/py3k-pep3137/Modules/_sqlite/statement.c
python/branches/py3k-pep3137/Objects/bytesobject.c
python/branches/py3k-pep3137/Objects/unicodeobject.c
python/branches/py3k-pep3137/Parser/tokenizer.c
python/branches/py3k-pep3137/Python/ast.c
python/branches/py3k-pep3137/Python/codecs.c
python/branches/py3k-pep3137/Python/getargs.c
python/branches/py3k-pep3137/Python/import.c
python/branches/py3k-pep3137/Python/marshal.c
python/branches/py3k-pep3137/Python/pythonrun.c
Log:
Another mega checking, purely a checkpoint.
This makes str().encode() return a bytes (PyString) again, even though most
codecs still return buffer (PyBytes). Lots of tests fail, but it's mostly
shallow, and I don't see any crashes any more (a few may still be hidden).
Modified: python/branches/py3k-pep3137/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/branches/py3k-pep3137/Modules/_ctypes/_ctypes.c (original)
+++ python/branches/py3k-pep3137/Modules/_ctypes/_ctypes.c Fri Nov 2 02:02:01 2007
@@ -796,7 +796,7 @@
return -1;
} else
Py_INCREF(value);
- size = PyBytes_GET_SIZE(value);
+ size = PyString_GET_SIZE(value);
if (size > self->b_size) {
PyErr_SetString(PyExc_ValueError,
"string too long");
@@ -804,7 +804,7 @@
return -1;
}
- ptr = PyBytes_AS_STRING(value);
+ ptr = PyString_AS_STRING(value);
memcpy(self->b_ptr, ptr, size);
if (size < self->b_size)
self->b_ptr[size] = '\0';
Modified: python/branches/py3k-pep3137/Modules/_ctypes/cfield.c
==============================================================================
--- python/branches/py3k-pep3137/Modules/_ctypes/cfield.c (original)
+++ python/branches/py3k-pep3137/Modules/_ctypes/cfield.c Fri Nov 2 02:02:01 2007
@@ -1157,16 +1157,20 @@
conversion_mode_errors);
if (value == NULL)
return NULL;
- if (PyBytes_GET_SIZE(value) != 1) {
+ if (PyString_GET_SIZE(value) != 1) {
Py_DECREF(value);
goto error;
}
- *(char *)ptr = PyBytes_AsString(value)[0];
+ *(char *)ptr = PyString_AS_STRING(value)[0];
Py_DECREF(value);
_RET(value);
}
+ if (PyString_Check(value) && PyString_GET_SIZE(value) == 1) {
+ *(char *)ptr = PyString_AS_STRING(value)[0];
+ _RET(value);
+ }
if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) {
- *(char *)ptr = PyBytes_AsString(value)[0];
+ *(char *)ptr = PyBytes_AS_STRING(value)[0];
_RET(value);
}
if (PyInt_Check(value))
@@ -1327,8 +1331,8 @@
conversion_mode_errors);
if (value == NULL)
return NULL;
- assert(PyBytes_Check(value));
- } else if(PyBytes_Check(value)) {
+ assert(PyString_Check(value));
+ } else if(PyString_Check(value)) {
Py_INCREF(value);
} else {
PyErr_Format(PyExc_TypeError,
@@ -1337,10 +1341,10 @@
return NULL;
}
- data = PyBytes_AsString(value);
+ data = PyString_AS_STRING(value);
if (!data)
return NULL;
- size = strlen(data);
+ size = strlen(data); /* XXX Why not Py_Size(value)? */
if (size < length) {
/* This will copy the leading NUL character
* if there is space for it.
@@ -1378,8 +1382,7 @@
conversion_mode_errors);
if (str == NULL)
return NULL;
- assert(PyBytes_Check(str));
- *(char **)ptr = PyBytes_AS_STRING(str);
+ *(char **)ptr = PyString_AS_STRING(str);
return str;
} else if (PyInt_Check(value)) {
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
Modified: python/branches/py3k-pep3137/Modules/_sqlite/statement.c
==============================================================================
--- python/branches/py3k-pep3137/Modules/_sqlite/statement.c (original)
+++ python/branches/py3k-pep3137/Modules/_sqlite/statement.c Fri Nov 2 02:02:01 2007
@@ -109,11 +109,9 @@
string = PyString_AsString(parameter);
rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
} else if PyUnicode_Check(parameter) {
- stringval = PyUnicode_AsUTF8String(parameter);
- string = PyBytes_AsString(stringval);
+ string = PyUnicode_AsUTF8String(parameter);
rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
- Py_DECREF(stringval);
} else if (PyObject_CheckBuffer(parameter)) {
if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
Modified: python/branches/py3k-pep3137/Objects/bytesobject.c
==============================================================================
--- python/branches/py3k-pep3137/Objects/bytesobject.c (original)
+++ python/branches/py3k-pep3137/Objects/bytesobject.c Fri Nov 2 02:02:01 2007
@@ -724,9 +724,9 @@
encoded = PyCodec_Encode(arg, encoding, errors);
if (encoded == NULL)
return -1;
- if (!PyBytes_Check(encoded) && !PyString_Check(encoded)) {
+ if (!PyString_Check(encoded)) {
PyErr_Format(PyExc_TypeError,
- "encoder did not return a str8 or bytes object (type=%.400s)",
+ "encoder did not return a bytes object (type=%.400s)",
Py_Type(encoded)->tp_name);
Py_DECREF(encoded);
return -1;
Modified: python/branches/py3k-pep3137/Objects/unicodeobject.c
==============================================================================
--- python/branches/py3k-pep3137/Objects/unicodeobject.c (original)
+++ python/branches/py3k-pep3137/Objects/unicodeobject.c Fri Nov 2 02:02:01 2007
@@ -1222,12 +1222,13 @@
v = PyCodec_Encode(unicode, encoding, errors);
if (v == NULL)
goto onError;
- if (!PyBytes_Check(v)) {
- if (PyString_Check(v)) {
- /* Old codec, turn it into bytes */
- PyObject *b = PyBytes_FromObject(v);
+ if (!PyString_Check(v)) {
+ if (PyBytes_Check(v)) {
+ /* Turn it into PyString */
+ PyObject *s = PyString_FromStringAndSize(
+ PyBytes_AS_STRING(v), Py_Size(v));
Py_DECREF(v);
- return b;
+ return s;
}
PyErr_Format(PyExc_TypeError,
"encoder did not return a bytes object "
@@ -1248,19 +1249,15 @@
const char *errors)
{
PyObject *v = ((PyUnicodeObject *)unicode)->defenc;
- PyObject *b;
if (v)
return v;
if (errors != NULL)
Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString");
- b = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
+ v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
PyUnicode_GET_SIZE(unicode),
NULL);
- if (!b)
+ if (!v)
return NULL;
- v = PyString_FromStringAndSize(PyBytes_AsString(b),
- PyBytes_Size(b));
- Py_DECREF(b);
((PyUnicodeObject *)unicode)->defenc = v;
return v;
}
@@ -1673,7 +1670,7 @@
int encodeWhiteSpace,
const char *errors)
{
- PyObject *v;
+ PyObject *v, *result;
/* It might be possible to tighten this worst case */
Py_ssize_t cbAllocated = 5 * size;
int inShift = 0;
@@ -1684,7 +1681,7 @@
char * start;
if (size == 0)
- return PyBytes_FromStringAndSize(NULL, 0);
+ return PyString_FromStringAndSize(NULL, 0);
v = PyBytes_FromStringAndSize(NULL, cbAllocated);
if (v == NULL)
@@ -1756,11 +1753,9 @@
*out++ = '-';
}
- if (PyBytes_Resize(v, out - start)) {
- Py_DECREF(v);
- return NULL;
- }
- return v;
+ result = PyString_FromStringAndSize(PyBytes_AS_STRING(v), out - start);
+ Py_DECREF(v);
+ return result;
}
#undef SPECIAL
@@ -2000,11 +1995,11 @@
{
#define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */
- Py_ssize_t i; /* index into s of next input byte */
- PyObject *v; /* result string object */
- char *p; /* next free byte in output buffer */
- Py_ssize_t nallocated; /* number of result bytes allocated */
- Py_ssize_t nneeded; /* number of result bytes needed */
+ Py_ssize_t i; /* index into s of next input byte */
+ PyObject *v, *result; /* result string object */
+ char *p; /* next free byte in output buffer */
+ Py_ssize_t nallocated; /* number of result bytes allocated */
+ Py_ssize_t nneeded; /* number of result bytes needed */
char stackbuf[MAX_SHORT_UNICHARS * 4];
assert(s != NULL);
@@ -2075,15 +2070,16 @@
/* This was stack allocated. */
nneeded = p - stackbuf;
assert(nneeded <= nallocated);
- v = PyBytes_FromStringAndSize(stackbuf, nneeded);
+ result = PyString_FromStringAndSize(stackbuf, nneeded);
}
else {
/* Cut back to size actually needed. */
nneeded = p - PyBytes_AS_STRING(v);
assert(nneeded <= nallocated);
- PyBytes_Resize(v, nneeded);
+ result = PyString_FromStringAndSize(PyBytes_AS_STRING(v), nneeded);
+ Py_DECREF(v);
}
- return v;
+ return result;
#undef MAX_SHORT_UNICHARS
}
@@ -2278,7 +2274,7 @@
const char *errors,
int byteorder)
{
- PyObject *v;
+ PyObject *v, *result;
unsigned char *p;
#ifndef Py_UNICODE_WIDE
int i, pairs;
@@ -2318,7 +2314,7 @@
if (byteorder == 0)
STORECHAR(0xFEFF);
if (size == 0)
- return v;
+ goto done;
if (byteorder == -1) {
/* force LE */
@@ -2349,7 +2345,11 @@
#endif
STORECHAR(ch);
}
- return v;
+
+ done:
+ result = PyString_FromStringAndSize(PyBytes_AS_STRING(v), Py_Size(v));
+ Py_DECREF(v);
+ return result;
#undef STORECHAR
}
@@ -2548,7 +2548,7 @@
const char *errors,
int byteorder)
{
- PyObject *v;
+ PyObject *v, *result;
unsigned char *p;
#ifdef Py_UNICODE_WIDE
int i, pairs;
@@ -2583,7 +2583,7 @@
if (byteorder == 0)
STORECHAR(0xFEFF);
if (size == 0)
- return v;
+ goto done;
if (byteorder == -1) {
/* force LE */
@@ -2609,7 +2609,11 @@
if (ch2)
STORECHAR(ch2);
}
- return v;
+
+ done:
+ result = PyString_FromStringAndSize(PyBytes_AS_STRING(v), Py_Size(v));
+ Py_DECREF(v);
+ return result;
#undef STORECHAR
}
@@ -2899,7 +2903,7 @@
PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s,
Py_ssize_t size)
{
- PyObject *repr;
+ PyObject *repr, *result;
char *p;
/* XXX(nnorwitz): rather than over-allocating, it would be
@@ -3022,12 +3026,10 @@
*p++ = (char) ch;
}
- *p = '\0';
- if (PyBytes_Resize(repr, p - PyBytes_AS_STRING(repr))) {
- Py_DECREF(repr);
- return NULL;
- }
- return repr;
+ result = PyString_FromStringAndSize(PyBytes_AS_STRING(repr),
+ p - PyBytes_AS_STRING(repr));
+ Py_DECREF(repr);
+ return result;
}
PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
@@ -3158,7 +3160,7 @@
PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
Py_ssize_t size)
{
- PyObject *repr;
+ PyObject *repr, *result;
char *p;
char *q;
@@ -3170,7 +3172,7 @@
if (repr == NULL)
return NULL;
if (size == 0)
- return repr;
+ goto done;
p = q = PyBytes_AS_STRING(repr);
while (size-- > 0) {
@@ -3204,12 +3206,12 @@
else
*p++ = (char) ch;
}
- *p = '\0';
- if (PyBytes_Resize(repr, p - q)) {
- Py_DECREF(repr);
- return NULL;
- }
- return repr;
+ size = p - q;
+
+ done:
+ result = PyString_FromStringAndSize(PyBytes_AS_STRING(repr), size);
+ Py_DECREF(repr);
+ return result;
}
PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
@@ -3444,23 +3446,23 @@
/* pointer into the output */
char *str;
/* current output position */
- Py_ssize_t respos = 0;
Py_ssize_t ressize;
const char *encoding = (limit == 256) ? "latin-1" : "ascii";
const char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)";
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
+ PyObject *result = NULL;
/* the following variable is used for caching string comparisons
* -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */
int known_errorHandler = -1;
/* allocate enough for a simple encoding without
replacements, if we need more, we'll resize */
+ if (size == 0)
+ return PyString_FromStringAndSize(NULL, 0);
res = PyBytes_FromStringAndSize(NULL, size);
if (res == NULL)
- goto onError;
- if (size == 0)
- return res;
+ return NULL;
str = PyBytes_AS_STRING(res);
ressize = size;
@@ -3588,20 +3590,13 @@
}
}
}
- /* Resize if we allocated to much */
- respos = str - PyBytes_AS_STRING(res);
- if (respos<ressize)
- /* If this falls res will be NULL */
- PyBytes_Resize(res, respos);
- Py_XDECREF(errorHandler);
- Py_XDECREF(exc);
- return res;
-
- onError:
- Py_XDECREF(res);
+ result = PyString_FromStringAndSize(PyBytes_AS_STRING(res),
+ str - PyBytes_AS_STRING(res));
+ onError:
+ Py_DECREF(res);
Py_XDECREF(errorHandler);
Py_XDECREF(exc);
- return NULL;
+ return result;
}
PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p,
@@ -6511,7 +6506,7 @@
v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors);
if (v == NULL)
goto onError;
- if (!PyBytes_Check(v)) {
+ if (!PyString_Check(v)) {
PyErr_Format(PyExc_TypeError,
"encoder did not return a bytes object "
"(type=%.400s)",
Modified: python/branches/py3k-pep3137/Parser/tokenizer.c
==============================================================================
--- python/branches/py3k-pep3137/Parser/tokenizer.c (original)
+++ python/branches/py3k-pep3137/Parser/tokenizer.c Fri Nov 2 02:02:01 2007
@@ -646,7 +646,7 @@
"unknown encoding: %s", tok->enc);
return error_ret(tok);
}
- str = PyBytes_AsString(utf8);
+ str = PyString_AS_STRING(utf8);
}
assert(tok->decoding_buffer == NULL);
tok->decoding_buffer = utf8; /* CAUTION */
@@ -765,8 +765,8 @@
tok->done = E_DECODE;
return EOF;
}
- buflen = PyBytes_Size(u);
- buf = PyBytes_AsString(u);
+ buflen = PyString_GET_SIZE(u);
+ buf = PyString_AS_STRING(u);
if (!buf) {
Py_DECREF(u);
tok->done = E_DECODE;
@@ -1550,7 +1550,7 @@
#else
static PyObject *
dec_utf8(const char *enc, const char *text, size_t len) {
- PyObject *ret = NULL;
+ PyObject *ret = NULL;
PyObject *unicode_text = PyUnicode_DecodeUTF8(text, len, "replace");
if (unicode_text) {
ret = PyUnicode_AsEncodedString(unicode_text, enc, "replace");
@@ -1560,7 +1560,7 @@
PyErr_Clear();
}
else {
- assert(PyBytes_Check(ret));
+ assert(PyString_Check(ret));
}
return ret;
}
@@ -1573,8 +1573,8 @@
/* convert source to original encondig */
PyObject *lineobj = dec_utf8(tok->encoding, tok->buf, len);
if (lineobj != NULL) {
- int linelen = PyBytes_GET_SIZE(lineobj);
- const char *line = PyBytes_AS_STRING(lineobj);
+ int linelen = PyString_GET_SIZE(lineobj);
+ const char *line = PyString_AS_STRING(lineobj);
text = PyObject_MALLOC(linelen + 1);
if (text != NULL && line != NULL) {
if (linelen)
@@ -1582,19 +1582,18 @@
text[linelen] = '\0';
}
Py_DECREF(lineobj);
-
+
/* adjust error offset */
if (*offset > 1) {
- PyObject *offsetobj = dec_utf8(tok->encoding,
+ PyObject *offsetobj = dec_utf8(tok->encoding,
tok->buf,
*offset-1);
if (offsetobj) {
- *offset = 1 +
- PyBytes_GET_SIZE(offsetobj);
+ *offset = 1 + Py_Size(offsetobj);
Py_DECREF(offsetobj);
}
}
-
+
}
}
return text;
Modified: python/branches/py3k-pep3137/Python/ast.c
==============================================================================
--- python/branches/py3k-pep3137/Python/ast.c (original)
+++ python/branches/py3k-pep3137/Python/ast.c Fri Nov 2 02:02:01 2007
@@ -3147,9 +3147,8 @@
Py_DECREF(u);
return NULL;
}
- assert(PyBytes_Check(w));
- r = PyBytes_AsString(w);
- rn = PyBytes_Size(w);
+ r = PyString_AS_STRING(w);
+ rn = Py_Size(w);
assert(rn % 2 == 0);
for (i = 0; i < rn; i += 2) {
sprintf(p, "\\u%02x%02x",
Modified: python/branches/py3k-pep3137/Python/codecs.c
==============================================================================
--- python/branches/py3k-pep3137/Python/codecs.c (original)
+++ python/branches/py3k-pep3137/Python/codecs.c Fri Nov 2 02:02:01 2007
@@ -333,18 +333,18 @@
args = args_tuple(object, errors);
if (args == NULL)
goto onError;
-
+
result = PyEval_CallObject(encoder,args);
if (result == NULL)
goto onError;
- if (!PyTuple_Check(result) ||
+ if (!PyTuple_Check(result) ||
PyTuple_GET_SIZE(result) != 2) {
PyErr_SetString(PyExc_TypeError,
"encoder must return a tuple (object,integer)");
goto onError;
}
- v = PyTuple_GET_ITEM(result,0);
+ v = PyTuple_GET_ITEM(result, 0);
Py_INCREF(v);
/* We don't check or use the second (integer) entry. */
@@ -352,7 +352,7 @@
Py_DECREF(encoder);
Py_DECREF(result);
return v;
-
+
onError:
Py_XDECREF(result);
Py_XDECREF(args);
Modified: python/branches/py3k-pep3137/Python/getargs.c
==============================================================================
--- python/branches/py3k-pep3137/Python/getargs.c (original)
+++ python/branches/py3k-pep3137/Python/getargs.c Fri Nov 2 02:02:01 2007
@@ -1008,14 +1008,14 @@
if (s == NULL)
return converterr("(encoding failed)",
arg, msgbuf, bufsize);
- if (!PyBytes_Check(s)) {
+ if (!PyString_Check(s)) {
Py_DECREF(s);
return converterr(
"(encoder failed to return bytes)",
arg, msgbuf, bufsize);
}
- size = PyBytes_GET_SIZE(s);
- ptr = PyBytes_AS_STRING(s);
+ size = PyString_GET_SIZE(s);
+ ptr = PyString_AS_STRING(s);
if (ptr == NULL)
ptr = "";
}
Modified: python/branches/py3k-pep3137/Python/import.c
==============================================================================
--- python/branches/py3k-pep3137/Python/import.c (original)
+++ python/branches/py3k-pep3137/Python/import.c Fri Nov 2 02:02:01 2007
@@ -2213,14 +2213,14 @@
PyUnicode_GetSize(item),
NULL);
} else {
- item8 = PyUnicode_AsEncodedString(item,
- Py_FileSystemDefaultEncoding, NULL);
+ item8 = PyUnicode_AsEncodedString(item,
+ Py_FileSystemDefaultEncoding, NULL);
}
if (!item8) {
PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
return 0;
}
- subname = PyBytes_AsString(item8);
+ subname = PyString_AS_STRING(item8);
if (buflen + strlen(subname) >= MAXPATHLEN) {
PyErr_SetString(PyExc_ValueError,
"Module name too long");
Modified: python/branches/py3k-pep3137/Python/marshal.c
==============================================================================
--- python/branches/py3k-pep3137/Python/marshal.c (original)
+++ python/branches/py3k-pep3137/Python/marshal.c Fri Nov 2 02:02:01 2007
@@ -249,14 +249,14 @@
return;
}
w_byte(TYPE_UNICODE, p);
- n = PyBytes_GET_SIZE(utf8);
+ n = PyString_GET_SIZE(utf8);
if (n > INT_MAX) {
p->depth--;
p->error = 1;
return;
}
w_long((long)n, p);
- w_string(PyBytes_AS_STRING(utf8), (int)n, p);
+ w_string(PyString_AS_STRING(utf8), (int)n, p);
Py_DECREF(utf8);
}
else if (PyTuple_Check(v)) {
Modified: python/branches/py3k-pep3137/Python/pythonrun.c
==============================================================================
--- python/branches/py3k-pep3137/Python/pythonrun.c (original)
+++ python/branches/py3k-pep3137/Python/pythonrun.c Fri Nov 2 02:02:01 2007
@@ -743,7 +743,7 @@
PySys_SetObject("stdout", std);
Py_DECREF(std);
-#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
+#if 0 /* Disable this if you have trouble debugging bootstrap stuff */
/* Set sys.stderr, replaces the preliminary stderr */
if (!(std = PyFile_FromFd(fileno(stderr), "<stderr>", "w", -1,
NULL, "\n", 0))) {
More information about the Python-3000-checkins
mailing list