diff options
author | Guido van Rossum <guido@python.org> | 2007-11-06 21:34:58 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-11-06 21:34:58 (GMT) |
commit | 98297ee7815939b124156e438b22bd652d67b5db (patch) | |
tree | a9d239ebd87c73af2571ab48003984c4e18e27e5 /Modules | |
parent | a19f80c6df2df5e8a5d0cff37131097835ef971e (diff) | |
download | cpython-98297ee7815939b124156e438b22bd652d67b5db.zip cpython-98297ee7815939b124156e438b22bd652d67b5db.tar.gz cpython-98297ee7815939b124156e438b22bd652d67b5db.tar.bz2 |
Merging the py3k-pep3137 branch back into the py3k branch.
No detailed change log; just check out the change log for the py3k-pep3137
branch. The most obvious changes:
- str8 renamed to bytes (PyString at the C level);
- bytes renamed to buffer (PyBytes at the C level);
- PyString and PyUnicode are no longer compatible.
I.e. we now have an immutable bytes type and a mutable bytes type.
The behavior of PyString was modified quite a bit, to make it more
bytes-like. Some changes are still on the to-do list.
Diffstat (limited to 'Modules')
31 files changed, 340 insertions, 368 deletions
diff --git a/Modules/_bsddb.c b/Modules/_bsddb.c index bd1c271..0587071 100644 --- a/Modules/_bsddb.c +++ b/Modules/_bsddb.c @@ -1171,13 +1171,16 @@ _db_associateCallback(DB* db, const DBT* priKey, const DBT* priData, else if (PyInt_Check(result)) { retval = PyInt_AsLong(result); } - else if (PyBytes_Check(result)) { + else if (PyBytes_Check(result) || PyString_Check(result)) { char* data; Py_ssize_t size; CLEAR_DBT(*secKey); - size = PyBytes_Size(result); - data = PyBytes_AsString(result); + size = Py_Size(result); + if (PyBytes_Check(result)) + data = PyBytes_AS_STRING(result); + else + data = PyString_AS_STRING(result); secKey->flags = DB_DBT_APPMALLOC; /* DB will free */ secKey->data = malloc(size); /* TODO, check this */ if (secKey->data) { @@ -1517,7 +1520,7 @@ DB_get(DBObject* self, PyObject* args, PyObject* kwargs) retval = Py_BuildValue("y#y#", key.data, key.size, data.data, data.size); else /* return just the data */ - retval = PyBytes_FromStringAndSize((char*)data.data, data.size); + retval = PyString_FromStringAndSize((char*)data.data, data.size); free_dbt(&data); } FREE_DBT_VIEW(key, keyobj, key_buf_view); @@ -1587,13 +1590,13 @@ DB_pget(DBObject* self, PyObject* args, PyObject* kwargs) else if (!err) { PyObject *pkeyObj; PyObject *dataObj; - dataObj = PyBytes_FromStringAndSize(data.data, data.size); + dataObj = PyString_FromStringAndSize(data.data, data.size); if (self->primaryDBType == DB_RECNO || self->primaryDBType == DB_QUEUE) pkeyObj = PyInt_FromLong(*(int *)pkey.data); else - pkeyObj = PyBytes_FromStringAndSize(pkey.data, pkey.size); + pkeyObj = PyString_FromStringAndSize(pkey.data, pkey.size); if (flags & DB_SET_RECNO) /* return key , pkey and data */ { @@ -1602,7 +1605,7 @@ DB_pget(DBObject* self, PyObject* args, PyObject* kwargs) if (type == DB_RECNO || type == DB_QUEUE) keyObj = PyInt_FromLong(*(int *)key.data); else - keyObj = PyBytes_FromStringAndSize(key.data, key.size); + keyObj = PyString_FromStringAndSize(key.data, key.size); #if (PY_VERSION_HEX >= 0x02040000) retval = PyTuple_Pack(3, keyObj, pkeyObj, dataObj); #else @@ -1729,7 +1732,8 @@ DB_get_both(DBObject* self, PyObject* args, PyObject* kwargs) else if (!err) { /* XXX(nnorwitz): can we do: retval = dataobj; Py_INCREF(retval); */ /* XXX(gps) I think not: buffer API input vs. bytes object output. */ - retval = PyBytes_FromStringAndSize((char*)data.data, data.size); + /* XXX(guido) But what if the input is PyString? */ + retval = PyString_FromStringAndSize((char*)data.data, data.size); /* Even though the flags require DB_DBT_MALLOC, data is not always allocated. 4.4: allocated, 4.5: *not* allocated. :-( */ @@ -2773,7 +2777,7 @@ PyObject* DB_subscript(DBObject* self, PyObject* keyobj) retval = NULL; } else { - retval = PyBytes_FromStringAndSize((char*)data.data, data.size); + retval = PyString_FromStringAndSize((char*)data.data, data.size); free_dbt(&data); } @@ -2928,7 +2932,7 @@ _DB_make_list(DBObject* self, DB_TXN* txn, int type) case DB_BTREE: case DB_HASH: default: - item = PyBytes_FromStringAndSize((char*)key.data, key.size); + item = PyString_FromStringAndSize((char*)key.data, key.size); break; case DB_RECNO: case DB_QUEUE: @@ -2938,7 +2942,7 @@ _DB_make_list(DBObject* self, DB_TXN* txn, int type) break; case _VALUES_LIST: - item = PyBytes_FromStringAndSize((char*)data.data, data.size); + item = PyString_FromStringAndSize((char*)data.data, data.size); break; case _ITEMS_LIST: @@ -3286,13 +3290,13 @@ DBC_pget(DBCursorObject* self, PyObject* args, PyObject *kwargs) else { PyObject *pkeyObj; PyObject *dataObj; - dataObj = PyBytes_FromStringAndSize(data.data, data.size); + dataObj = PyString_FromStringAndSize(data.data, data.size); if (self->mydb->primaryDBType == DB_RECNO || self->mydb->primaryDBType == DB_QUEUE) pkeyObj = PyInt_FromLong(*(int *)pkey.data); else - pkeyObj = PyBytes_FromStringAndSize(pkey.data, pkey.size); + pkeyObj = PyString_FromStringAndSize(pkey.data, pkey.size); if (key.data && key.size) /* return key, pkey and data */ { @@ -3301,7 +3305,7 @@ DBC_pget(DBCursorObject* self, PyObject* args, PyObject *kwargs) if (type == DB_RECNO || type == DB_QUEUE) keyObj = PyInt_FromLong(*(int *)key.data); else - keyObj = PyBytes_FromStringAndSize(key.data, key.size); + keyObj = PyString_FromStringAndSize(key.data, key.size); retval = PyTuple_Pack(3, keyObj, pkeyObj, dataObj); Py_DECREF(keyObj); } @@ -4909,7 +4913,7 @@ DBSequence_get_key(DBSequenceObject* self, PyObject* args) MYDB_END_ALLOW_THREADS if (!err) - retval = PyBytes_FromStringAndSize(key.data, key.size); + retval = PyString_FromStringAndSize(key.data, key.size); free_dbt(&key); RETURN_IF_ERR(); diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c index e3933e7..caee3fd 100644 --- a/Modules/_codecsmodule.c +++ b/Modules/_codecsmodule.c @@ -180,7 +180,7 @@ escape_encode(PyObject *self, "string is too large to encode"); return NULL; } - v = PyBytes_FromStringAndSize(NULL, newsize); + v = PyString_FromStringAndSize(NULL, newsize); if (v == NULL) { return NULL; @@ -188,11 +188,11 @@ escape_encode(PyObject *self, else { register Py_ssize_t i; register char c; - register char *p = PyBytes_AS_STRING(v); + register char *p = PyString_AS_STRING(v); for (i = 0; i < size; i++) { /* There's at least enough room for a hex escape */ - assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4); + assert(newsize - (p - PyString_AS_STRING(v)) >= 4); c = PyString_AS_STRING(str)[i]; if (c == '\'' || c == '\\') *p++ = '\\', *p++ = c; @@ -212,13 +212,12 @@ escape_encode(PyObject *self, *p++ = c; } *p = '\0'; - if (PyBytes_Resize(v, (p - PyBytes_AS_STRING(v)))) { - Py_DECREF(v); + if (_PyString_Resize(&v, (p - PyString_AS_STRING(v)))) { return NULL; } } - return codec_tuple(v, PyBytes_Size(v)); + return codec_tuple(v, PyString_Size(v)); } /* --- Decoder ------------------------------------------------------------ */ @@ -654,7 +653,7 @@ readbuffer_encode(PyObject *self, &data, &size, &errors)) return NULL; - return codec_tuple(PyBytes_FromStringAndSize(data, size), size); + return codec_tuple(PyString_FromStringAndSize(data, size), size); } static PyObject * @@ -669,7 +668,7 @@ charbuffer_encode(PyObject *self, &data, &size, &errors)) return NULL; - return codec_tuple(PyBytes_FromStringAndSize(data, size), size); + return codec_tuple(PyString_FromStringAndSize(data, size), size); } static PyObject * @@ -688,12 +687,12 @@ unicode_internal_encode(PyObject *self, if (PyUnicode_Check(obj)) { data = PyUnicode_AS_DATA(obj); size = PyUnicode_GET_DATA_SIZE(obj); - return codec_tuple(PyBytes_FromStringAndSize(data, size), size); + return codec_tuple(PyString_FromStringAndSize(data, size), size); } else { if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) return NULL; - return codec_tuple(PyBytes_FromStringAndSize(data, size), size); + return codec_tuple(PyString_FromStringAndSize(data, size), size); } } diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 81276fa..39dfdef 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -763,7 +763,7 @@ CharArray_set_raw(CDataObject *self, PyObject *value) static PyObject * CharArray_get_raw(CDataObject *self) { - return PyBytes_FromStringAndSize(self->b_ptr, self->b_size); + return PyString_FromStringAndSize(self->b_ptr, self->b_size); } static PyObject * @@ -774,7 +774,7 @@ CharArray_get_value(CDataObject *self) for (i = 0; i < self->b_size; ++i) if (*ptr++ == '\0') break; - return PyBytes_FromStringAndSize(self->b_ptr, i); + return PyString_FromStringAndSize(self->b_ptr, i); } static int @@ -789,14 +789,14 @@ CharArray_set_value(CDataObject *self, PyObject *value) conversion_mode_errors); if (!value) return -1; - } else if (!PyBytes_Check(value)) { + } else if (!PyString_Check(value)) { PyErr_Format(PyExc_TypeError, "str/bytes expected instead of %s instance", Py_Type(value)->tp_name); 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 @@ CharArray_set_value(CDataObject *self, PyObject *value) 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'; @@ -838,7 +838,7 @@ WCharArray_set_value(CDataObject *self, PyObject *value) { Py_ssize_t result = 0; - if (PyBytes_Check(value)) { + if (PyString_Check(value)) { value = PyUnicode_FromEncodedObject(value, conversion_mode_encoding, conversion_mode_errors); @@ -1106,7 +1106,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value) Py_INCREF(Py_None); return Py_None; } - if (PyUnicode_Check(value) || PyBytes_Check(value)) { + if (PyUnicode_Check(value) || PyString_Check(value)) { PyCArgObject *parg; struct fielddesc *fd = getentry("Z"); @@ -1167,7 +1167,7 @@ c_char_p_from_param(PyObject *type, PyObject *value) Py_INCREF(Py_None); return Py_None; } - if (PyBytes_Check(value) || PyUnicode_Check(value)) { + if (PyString_Check(value) || PyUnicode_Check(value)) { PyCArgObject *parg; struct fielddesc *fd = getentry("z"); @@ -1251,7 +1251,7 @@ c_void_p_from_param(PyObject *type, PyObject *value) } /* XXX struni: remove later */ /* string */ - if (PyBytes_Check(value)) { + if (PyString_Check(value)) { PyCArgObject *parg; struct fielddesc *fd = getentry("z"); @@ -2705,8 +2705,8 @@ _get_name(PyObject *obj, char **pname) return 1; } #endif - if (PyBytes_Check(obj)) { - *pname = PyBytes_AS_STRING(obj); + if (PyString_Check(obj)) { + *pname = PyString_AS_STRING(obj); return *pname ? 1 : 0; } if (PyUnicode_Check(obj)) { @@ -3734,9 +3734,9 @@ Array_subscript(PyObject *_self, PyObject *item) char *dest; if (slicelen <= 0) - return PyBytes_FromStringAndSize("", 0); + return PyString_FromStringAndSize("", 0); if (step == 1) { - return PyBytes_FromStringAndSize(ptr + start, + return PyString_FromStringAndSize(ptr + start, slicelen); } dest = (char *)PyMem_Malloc(slicelen); @@ -3749,7 +3749,7 @@ Array_subscript(PyObject *_self, PyObject *item) dest[i] = ptr[cur]; } - np = PyBytes_FromStringAndSize(dest, slicelen); + np = PyString_FromStringAndSize(dest, slicelen); PyMem_Free(dest); return np; } @@ -4411,9 +4411,9 @@ Pointer_subscript(PyObject *_self, PyObject *item) char *dest; if (len <= 0) - return PyBytes_FromStringAndSize("", 0); + return PyString_FromStringAndSize("", 0); if (step == 1) { - return PyBytes_FromStringAndSize(ptr + start, + return PyString_FromStringAndSize(ptr + start, len); } dest = (char *)PyMem_Malloc(len); @@ -4422,7 +4422,7 @@ Pointer_subscript(PyObject *_self, PyObject *item) for (cur = start, i = 0; i < len; cur += step, i++) { dest[i] = ptr[cur]; } - np = PyBytes_FromStringAndSize(dest, len); + np = PyString_FromStringAndSize(dest, len); PyMem_Free(dest); return np; } @@ -4658,8 +4658,8 @@ static PyObject * string_at(const char *ptr, int size) { if (size == -1) - return PyBytes_FromStringAndSize(ptr, strlen(ptr)); - return PyBytes_FromStringAndSize(ptr, size); + return PyString_FromStringAndSize(ptr, strlen(ptr)); + return PyString_FromStringAndSize(ptr, size); } static int diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index bc524f7..69129f7 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -507,9 +507,9 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa) return 0; } - if (PyBytes_Check(obj)) { + if (PyString_Check(obj)) { pa->ffi_type = &ffi_type_pointer; - pa->value.p = PyBytes_AsString(obj); + pa->value.p = PyString_AsString(obj); Py_INCREF(obj); pa->keep = obj; return 0; diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index 910470a..2ec7b3a 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -1157,16 +1157,20 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size) 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)) @@ -1187,7 +1191,7 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size) static PyObject * c_get(void *ptr, Py_ssize_t size) { - return PyBytes_FromStringAndSize((char *)ptr, 1); + return PyString_FromStringAndSize((char *)ptr, 1); } #ifdef CTYPES_UNICODE @@ -1196,7 +1200,7 @@ static PyObject * u_set(void *ptr, PyObject *value, Py_ssize_t size) { Py_ssize_t len; - if (PyBytes_Check(value)) { + if (PyString_Check(value)) { value = PyUnicode_FromEncodedObject(value, conversion_mode_encoding, conversion_mode_errors); @@ -1271,7 +1275,7 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length) /* It's easier to calculate in characters than in bytes */ length /= sizeof(wchar_t); - if (PyBytes_Check(value)) { + if (PyString_Check(value)) { value = PyUnicode_FromEncodedObject(value, conversion_mode_encoding, conversion_mode_errors); @@ -1327,8 +1331,8 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length) 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 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length) 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. @@ -1368,8 +1372,8 @@ z_set(void *ptr, PyObject *value, Py_ssize_t size) Py_INCREF(value); return value; } - if (PyBytes_Check(value)) { - *(char **)ptr = PyBytes_AsString(value); + if (PyString_Check(value)) { + *(char **)ptr = PyString_AsString(value); Py_INCREF(value); return value; } else if (PyUnicode_Check(value)) { @@ -1378,8 +1382,7 @@ z_set(void *ptr, PyObject *value, Py_ssize_t size) 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 @@ -1433,7 +1436,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size) Py_INCREF(Py_None); return Py_None; } - if (PyBytes_Check(value)) { + if (PyString_Check(value)) { value = PyUnicode_FromEncodedObject(value, conversion_mode_encoding, conversion_mode_errors); @@ -1516,7 +1519,7 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size) /* convert value into a PyUnicodeObject or NULL */ if (Py_None == value) { value = NULL; - } else if (PyBytes_Check(value)) { + } else if (PyString_Check(value)) { value = PyUnicode_FromEncodedObject(value, conversion_mode_encoding, conversion_mode_errors); diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index a1d7e2e..cf412d8 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1796,7 +1796,7 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream) remove(fn); return NULL; } - if (!PyBytes_Check(data)) { + if (!PyString_Check(data)) { PyErr_Format(PyExc_TypeError, "f.read() returned %.100s instead of bytes", data->ob_type->tp_name); @@ -1805,7 +1805,7 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *stream) remove(fn); return NULL; } - fwrite(PyBytes_AS_STRING(data), 1, PyBytes_GET_SIZE(data), fp); + fwrite(PyString_AS_STRING(data), 1, PyString_GET_SIZE(data), fp); Py_DECREF(data); fseek(fp, 0, 0); win = getwin(fp); diff --git a/Modules/_fileio.c b/Modules/_fileio.c index f02c5ef..c357a73 100644 --- a/Modules/_fileio.c +++ b/Modules/_fileio.c @@ -400,14 +400,14 @@ fileio_readall(PyFileIOObject *self) Py_ssize_t total = 0; int n; - result = PyBytes_FromStringAndSize(NULL, DEFAULT_BUFFER_SIZE); + result = PyString_FromStringAndSize(NULL, DEFAULT_BUFFER_SIZE); if (result == NULL) return NULL; while (1) { Py_ssize_t newsize = total + DEFAULT_BUFFER_SIZE; - if (PyBytes_GET_SIZE(result) < newsize) { - if (PyBytes_Resize(result, newsize) < 0) { + if (PyString_GET_SIZE(result) < newsize) { + if (_PyString_Resize(&result, newsize) < 0) { if (total == 0) { Py_DECREF(result); return NULL; @@ -419,7 +419,7 @@ fileio_readall(PyFileIOObject *self) Py_BEGIN_ALLOW_THREADS errno = 0; n = read(self->fd, - PyBytes_AS_STRING(result) + total, + PyString_AS_STRING(result) + total, newsize - total); Py_END_ALLOW_THREADS if (n == 0) @@ -438,8 +438,8 @@ fileio_readall(PyFileIOObject *self) total += n; } - if (PyBytes_GET_SIZE(result) > total) { - if (PyBytes_Resize(result, total) < 0) { + if (PyString_GET_SIZE(result) > total) { + if (_PyString_Resize(&result, total) < 0) { /* This should never happen, but just in case */ Py_DECREF(result); return NULL; @@ -468,10 +468,10 @@ fileio_read(PyFileIOObject *self, PyObject *args) return fileio_readall(self); } - bytes = PyBytes_FromStringAndSize(NULL, size); + bytes = PyString_FromStringAndSize(NULL, size); if (bytes == NULL) return NULL; - ptr = PyBytes_AsString(bytes); + ptr = PyString_AS_STRING(bytes); Py_BEGIN_ALLOW_THREADS errno = 0; @@ -486,7 +486,7 @@ fileio_read(PyFileIOObject *self, PyObject *args) } if (n != size) { - if (PyBytes_Resize(bytes, n) < 0) { + if (_PyString_Resize(&bytes, n) < 0) { Py_DECREF(bytes); return NULL; } diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 252a2ae..0f460bf 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -108,7 +108,7 @@ EVP_digest(EVPobject *self, PyObject *unused) digest_size = EVP_MD_CTX_size(&temp_ctx); EVP_DigestFinal(&temp_ctx, digest, NULL); - retval = PyBytes_FromStringAndSize((const char *)digest, digest_size); + retval = PyString_FromStringAndSize((const char *)digest, digest_size); EVP_MD_CTX_cleanup(&temp_ctx); return retval; } diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c index 829c175..2f50e6a 100644 --- a/Modules/_sqlite/cache.c +++ b/Modules/_sqlite/cache.c @@ -241,12 +241,12 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args) if (!fmt_args) { return NULL; } - template = PyString_FromString("%s <- %s ->%s\n"); + template = PyUnicode_FromString("%s <- %s ->%s\n"); if (!template) { Py_DECREF(fmt_args); return NULL; } - display_str = PyString_Format(template, fmt_args); + display_str = PyUnicode_Format(template, fmt_args); Py_DECREF(template); Py_DECREF(fmt_args); if (!display_str) { diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 5f899e8..d4318de 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -425,8 +425,6 @@ void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) sqlite3_result_int64(context, (PY_LONG_LONG)longval); } else if (PyFloat_Check(py_val)) { sqlite3_result_double(context, PyFloat_AsDouble(py_val)); - } else if (PyString_Check(py_val)) { - sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT); } else if (PyUnicode_Check(py_val)) { sqlite3_result_text(context, PyUnicode_AsString(py_val), -1, SQLITE_TRANSIENT); } else if (PyObject_CheckBuffer(py_val)) { @@ -467,7 +465,7 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_ break; case SQLITE_TEXT: val_str = (const char*)sqlite3_value_text(cur_value); - cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL); + cur_py_value = PyUnicode_FromString(val_str); /* TODO: have a way to show errors here */ if (!cur_py_value) { PyErr_Clear(); @@ -477,7 +475,7 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_ break; case SQLITE_BLOB: buflen = sqlite3_value_bytes(cur_value); - cur_py_value = PyBytes_FromStringAndSize( + cur_py_value = PyString_FromStringAndSize( sqlite3_value_blob(cur_value), buflen); break; case SQLITE_NULL: @@ -1023,8 +1021,8 @@ pysqlite_collation_callback( goto finally; } - string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length); - string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length); + string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length); + string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length); if (!string1 || !string2) { goto finally; /* failed to allocate strings */ @@ -1093,7 +1091,7 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args) goto finally; } - chk = PyString_AsString(uppercase_name); + chk = PyUnicode_AsString(uppercase_name); while (*chk) { if ((*chk >= '0' && *chk <= '9') || (*chk >= 'A' && *chk <= 'Z') @@ -1118,7 +1116,7 @@ pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args) } rc = sqlite3_create_collation(self->db, - PyString_AsString(uppercase_name), + PyUnicode_AsString(uppercase_name), SQLITE_UTF8, (callable != Py_None) ? callable : NULL, (callable != Py_None) ? pysqlite_collation_callback : NULL); diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index c789faf..c51c92e 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -272,11 +272,7 @@ PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize) } } - if (is_ascii) { - return PyString_FromString(val_str); - } else { - return PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL); - } + return PyUnicode_FromString(val_str); } /* @@ -379,7 +375,7 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self) } else { /* coltype == SQLITE_BLOB */ nbytes = sqlite3_column_bytes(self->statement->st, i); - buffer = PyBytes_FromStringAndSize( + buffer = PyString_FromStringAndSize( sqlite3_column_blob(self->statement->st, i), nbytes); if (!buffer) { break; @@ -436,8 +432,8 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* return NULL; } - if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { - PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); + if (!PyUnicode_Check(operation)) { + PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); return NULL; } @@ -458,8 +454,8 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* return NULL; } - if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { - PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); + if (!PyUnicode_Check(operation)) { + PyErr_SetString(PyExc_ValueError, "operation parameter must be str"); return NULL; } diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 61ac0a1..107d61a 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -146,7 +146,7 @@ static PyObject* module_register_converter(PyObject* self, PyObject* args, PyObj PyObject* callable; PyObject* retval = NULL; - if (!PyArg_ParseTuple(args, "SO", &orig_name, &callable)) { + if (!PyArg_ParseTuple(args, "UO", &orig_name, &callable)) { return NULL; } diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c index 2f3ba69..dfb6363 100644 --- a/Modules/_sqlite/row.c +++ b/Modules/_sqlite/row.c @@ -87,7 +87,7 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx) nitems = PyTuple_Size(self->description); for (i = 0; i < nitems; i++) { - compare_key = PyString_AsString(PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)); + compare_key = PyUnicode_AsString(PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)); if (!compare_key) { return NULL; } diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index 1cc3cdd..98cc68a 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -105,15 +105,10 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec #endif } else if (PyFloat_Check(parameter)) { rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter)); - } else if PyString_Check(parameter) { - 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_AsString(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); diff --git a/Modules/_struct.c b/Modules/_struct.c index 84aa828..8f66a96 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -12,11 +12,6 @@ static PyTypeObject PyStructType; -/* compatibility macros */ -#if (PY_VERSION_HEX < 0x02050000) -typedef int Py_ssize_t; -#endif - /* If PY_STRUCT_OVERFLOW_MASKING is defined, the struct module will wrap all input numbers for explicit endians such that they fit in the given type, much like explicit casting in C. A warning will be raised if the number did @@ -411,7 +406,7 @@ _range_error(const formatdef *f, int is_unsigned) if (msg == NULL) return -1; rval = PyErr_WarnEx(PyExc_DeprecationWarning, - PyString_AS_STRING(msg), 2); + PyUnicode_AsString(msg), 2); Py_DECREF(msg); if (rval == 0) return 0; @@ -1535,37 +1530,26 @@ Requires len(buffer) == self.size. See struct.__doc__ for more on format\n\ strings."); static PyObject * -s_unpack(PyObject *self, PyObject *inputstr) +s_unpack(PyObject *self, PyObject *input) { - char *start; - Py_ssize_t len; - PyObject *args=NULL, *result; + Py_buffer vbuf; + PyObject *result; PyStructObject *soself = (PyStructObject *)self; + assert(PyStruct_Check(self)); assert(soself->s_codes != NULL); - if (inputstr == NULL) - goto fail; - if (PyString_Check(inputstr) && - PyString_GET_SIZE(inputstr) == soself->s_size) { - return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); - } - args = PyTuple_Pack(1, inputstr); - if (args == NULL) + if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0) return NULL; - if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len)) - goto fail; - if (soself->s_size != len) - goto fail; - result = s_unpack_internal(soself, start); - Py_DECREF(args); + if (vbuf.len != soself->s_size) { + PyErr_Format(StructError, + "unpack requires a bytes argument of length %zd", + soself->s_size); + PyObject_ReleaseBuffer(input, &vbuf); + return NULL; + } + result = s_unpack_internal(soself, vbuf.buf); + PyObject_ReleaseBuffer(input, &vbuf); return result; - -fail: - Py_XDECREF(args); - PyErr_Format(StructError, - "unpack requires a string argument of length %zd", - soself->s_size); - return NULL; } PyDoc_STRVAR(s_unpack_from__doc__, @@ -1580,37 +1564,34 @@ static PyObject * s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"buffer", "offset", 0}; -#if (PY_VERSION_HEX < 0x02050000) - static char *fmt = "z#|i:unpack_from"; -#else - static char *fmt = "z#|n:unpack_from"; -#endif - Py_ssize_t buffer_len = 0, offset = 0; - char *buffer = NULL; + + PyObject *input; + Py_ssize_t offset = 0; + Py_buffer vbuf; + PyObject *result; PyStructObject *soself = (PyStructObject *)self; + assert(PyStruct_Check(self)); assert(soself->s_codes != NULL); - if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist, - &buffer, &buffer_len, &offset)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, + "O|n:unpack_from", kwlist, + &input, &offset)) return NULL; - - if (buffer == NULL) { - PyErr_Format(StructError, - "unpack_from requires a buffer argument"); + if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0) return NULL; - } - if (offset < 0) - offset += buffer_len; - - if (offset < 0 || (buffer_len - offset) < soself->s_size) { + offset += vbuf.len; + if (offset < 0 || vbuf.len - offset < soself->s_size) { PyErr_Format(StructError, "unpack_from requires a buffer of at least %zd bytes", soself->s_size); + PyObject_ReleaseBuffer(input, &vbuf); return NULL; } - return s_unpack_internal(soself, buffer + offset); + result = s_unpack_internal(soself, (char*)vbuf.buf + offset); + PyObject_ReleaseBuffer(input, &vbuf); + return result; } diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 8a24a7e..c7aeb5b 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1212,14 +1212,14 @@ array_fromfile(arrayobject *self, PyObject *args) if (b == NULL) return NULL; - if (!PyBytes_Check(b)) { + if (!PyString_Check(b)) { PyErr_SetString(PyExc_TypeError, "read() didn't return bytes"); Py_DECREF(b); return NULL; } - if (PyBytes_GET_SIZE(b) != nbytes) { + if (PyString_GET_SIZE(b) != nbytes) { PyErr_SetString(PyExc_EOFError, "read() didn't return enough bytes"); Py_DECREF(b); @@ -1263,7 +1263,7 @@ array_tofile(arrayobject *self, PyObject *f) PyObject *bytes, *res; if (i*BLOCKSIZE + size > nbytes) size = nbytes - i*BLOCKSIZE; - bytes = PyBytes_FromStringAndSize(ptr, size); + bytes = PyString_FromStringAndSize(ptr, size); if (bytes == NULL) return NULL; res = PyObject_CallMethod(f, "write", "O", bytes); @@ -1395,7 +1395,7 @@ values, as if it had been read from a file using the fromfile() method)."); static PyObject * array_tostring(arrayobject *self, PyObject *unused) { - return PyBytes_FromStringAndSize(self->ob_item, + return PyString_FromStringAndSize(self->ob_item, Py_Size(self) * self->ob_descr->itemsize); } @@ -1861,6 +1861,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (!(initial == NULL || PyList_Check(initial) || PyBytes_Check(initial) + || PyString_Check(initial) || PyTuple_Check(initial) || ((c=='u') && PyUnicode_Check(initial)))) { it = PyObject_GetIter(initial); @@ -1904,7 +1905,9 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } Py_DECREF(v); } - } else if (initial != NULL && PyBytes_Check(initial)) { + } + else if (initial != NULL && (PyBytes_Check(initial) || + PyString_Check(initial))) { PyObject *t_initial, *v; t_initial = PyTuple_Pack(1, initial); if (t_initial == NULL) { @@ -1919,7 +1922,8 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return NULL; } Py_DECREF(v); - } else if (initial != NULL && PyUnicode_Check(initial)) { + } + else if (initial != NULL && PyUnicode_Check(initial)) { Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial); if (n > 0) { arrayobject *self = (arrayobject *)a; diff --git a/Modules/binascii.c b/Modules/binascii.c index 3b55a35..62fc8c2 100644 --- a/Modules/binascii.c +++ b/Modules/binascii.c @@ -200,9 +200,9 @@ binascii_a2b_uu(PyObject *self, PyObject *args) ascii_len--; /* Allocate the buffer */ - if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL ) + if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL ) return NULL; - bin_data = (unsigned char *)PyBytes_AS_STRING(rv); + bin_data = (unsigned char *)PyString_AS_STRING(rv); for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) { /* XXX is it really best to add NULs if there's no more data */ @@ -277,9 +277,9 @@ binascii_b2a_uu(PyObject *self, PyObject *args) } /* We're lazy and allocate to much (fixed up later) */ - if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2+2)) == NULL ) + if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2+2)) == NULL ) return NULL; - ascii_data = (unsigned char *)PyBytes_AS_STRING(rv); + ascii_data = (unsigned char *)PyString_AS_STRING(rv); /* Store the length */ *ascii_data++ = ' ' + (bin_len & 077); @@ -301,9 +301,9 @@ binascii_b2a_uu(PyObject *self, PyObject *args) } *ascii_data++ = '\n'; /* Append a courtesy newline */ - if (PyBytes_Resize(rv, + if (_PyString_Resize(&rv, (ascii_data - - (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { + (unsigned char *)PyString_AS_STRING(rv))) < 0) { Py_DECREF(rv); rv = NULL; } @@ -355,9 +355,9 @@ binascii_a2b_base64(PyObject *self, PyObject *args) bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */ /* Allocate the buffer */ - if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL ) + if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL ) return NULL; - bin_data = (unsigned char *)PyBytes_AS_STRING(rv); + bin_data = (unsigned char *)PyString_AS_STRING(rv); bin_len = 0; for( ; ascii_len > 0; ascii_len--, ascii_data++) { @@ -416,17 +416,17 @@ binascii_a2b_base64(PyObject *self, PyObject *args) /* And set string size correctly. If the result string is empty ** (because the input was all invalid) return the shared empty - ** string instead; PyBytes_Resize() won't do this for us. + ** string instead; _PyString_Resize() won't do this for us. */ if (bin_len > 0) { - if (PyBytes_Resize(rv, bin_len) < 0) { + if (_PyString_Resize(&rv, bin_len) < 0) { Py_DECREF(rv); rv = NULL; } } else { Py_DECREF(rv); - rv = PyBytes_FromStringAndSize("", 0); + rv = PyString_FromStringAndSize("", 0); } return rv; } @@ -453,9 +453,9 @@ binascii_b2a_base64(PyObject *self, PyObject *args) /* We're lazy and allocate too much (fixed up later). "+3" leaves room for up to two pad characters and a trailing newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */ - if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL ) + if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL ) return NULL; - ascii_data = (unsigned char *)PyBytes_AS_STRING(rv); + ascii_data = (unsigned char *)PyString_AS_STRING(rv); for( ; bin_len > 0 ; bin_len--, bin_data++ ) { /* Shift the data into our buffer */ @@ -479,9 +479,9 @@ binascii_b2a_base64(PyObject *self, PyObject *args) } *ascii_data++ = '\n'; /* Append a courtesy newline */ - if (PyBytes_Resize(rv, + if (_PyString_Resize(&rv, (ascii_data - - (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { + (unsigned char *)PyString_AS_STRING(rv))) < 0) { Py_DECREF(rv); rv = NULL; } @@ -507,9 +507,9 @@ binascii_a2b_hqx(PyObject *self, PyObject *args) /* Allocate a string that is too big (fixed later) Add two to the initial length to prevent interning which would preclude subsequent resizing. */ - if ( (rv=PyBytes_FromStringAndSize(NULL, len+2)) == NULL ) + if ( (rv=PyString_FromStringAndSize(NULL, len+2)) == NULL ) return NULL; - bin_data = (unsigned char *)PyBytes_AS_STRING(rv); + bin_data = (unsigned char *)PyString_AS_STRING(rv); for( ; len > 0 ; len--, ascii_data++ ) { /* Get the byte and look it up */ @@ -543,9 +543,9 @@ binascii_a2b_hqx(PyObject *self, PyObject *args) Py_DECREF(rv); return NULL; } - if (PyBytes_Resize(rv, + if (_PyString_Resize(&rv, (bin_data - - (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { + (unsigned char *)PyString_AS_STRING(rv))) < 0) { Py_DECREF(rv); rv = NULL; } @@ -572,9 +572,9 @@ binascii_rlecode_hqx(PyObject *self, PyObject *args) return NULL; /* Worst case: output is twice as big as input (fixed later) */ - if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL ) + if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL ) return NULL; - out_data = (unsigned char *)PyBytes_AS_STRING(rv); + out_data = (unsigned char *)PyString_AS_STRING(rv); for( in=0; in<len; in++) { ch = in_data[in]; @@ -600,9 +600,9 @@ binascii_rlecode_hqx(PyObject *self, PyObject *args) } } } - if (PyBytes_Resize(rv, + if (_PyString_Resize(&rv, (out_data - - (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { + (unsigned char *)PyString_AS_STRING(rv))) < 0) { Py_DECREF(rv); rv = NULL; } @@ -625,9 +625,9 @@ binascii_b2a_hqx(PyObject *self, PyObject *args) return NULL; /* Allocate a buffer that is at least large enough */ - if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL ) + if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL ) return NULL; - ascii_data = (unsigned char *)PyBytes_AS_STRING(rv); + ascii_data = (unsigned char *)PyString_AS_STRING(rv); for( ; len > 0 ; len--, bin_data++ ) { /* Shift into our buffer, and output any 6bits ready */ @@ -644,9 +644,9 @@ binascii_b2a_hqx(PyObject *self, PyObject *args) leftchar <<= (6-leftbits); *ascii_data++ = table_b2a_hqx[leftchar & 0x3f]; } - if (PyBytes_Resize(rv, + if (_PyString_Resize(&rv, (ascii_data - - (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { + (unsigned char *)PyString_AS_STRING(rv))) < 0) { Py_DECREF(rv); rv = NULL; } @@ -668,14 +668,14 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args) /* Empty string is a special case */ if ( in_len == 0 ) - return PyBytes_FromStringAndSize("", 0); + return PyString_FromStringAndSize("", 0); /* Allocate a buffer of reasonable size. Resized when needed */ out_len = in_len*2; - if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL ) + if ( (rv=PyString_FromStringAndSize(NULL, out_len)) == NULL ) return NULL; out_len_left = out_len; - out_data = (unsigned char *)PyBytes_AS_STRING(rv); + out_data = (unsigned char *)PyString_AS_STRING(rv); /* ** We need two macros here to get/put bytes and handle @@ -694,9 +694,9 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args) #define OUTBYTE(b) \ do { \ if ( --out_len_left < 0 ) { \ - if (PyBytes_Resize(rv, 2*out_len) < 0) \ + if (_PyString_Resize(&rv, 2*out_len) < 0) \ { Py_DECREF(rv); return NULL; } \ - out_data = (unsigned char *)PyBytes_AS_STRING(rv) \ + out_data = (unsigned char *)PyString_AS_STRING(rv) \ + out_len; \ out_len_left = out_len-1; \ out_len = out_len * 2; \ @@ -744,9 +744,9 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args) OUTBYTE(in_byte); } } - if (PyBytes_Resize(rv, + if (_PyString_Resize(&rv, (out_data - - (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { + (unsigned char *)PyString_AS_STRING(rv))) < 0) { Py_DECREF(rv); rv = NULL; } @@ -940,10 +940,10 @@ binascii_hexlify(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "s#:b2a_hex", &argbuf, &arglen)) return NULL; - retval = PyBytes_FromStringAndSize(NULL, arglen*2); + retval = PyString_FromStringAndSize(NULL, arglen*2); if (!retval) return NULL; - retbuf = PyBytes_AS_STRING(retval); + retbuf = PyString_AS_STRING(retval); /* make hex version of string, taken from shamodule.c */ for (i=j=0; i < arglen; i++) { @@ -1000,10 +1000,10 @@ binascii_unhexlify(PyObject *self, PyObject *args) return NULL; } - retval = PyBytes_FromStringAndSize(NULL, (arglen/2)); + retval = PyString_FromStringAndSize(NULL, (arglen/2)); if (!retval) return NULL; - retbuf = PyBytes_AS_STRING(retval); + retbuf = PyString_AS_STRING(retval); for (i=j=0; i < arglen; i += 2) { int top = to_int(Py_CHARMASK(argbuf[i])); @@ -1115,7 +1115,7 @@ binascii_a2b_qp(PyObject *self, PyObject *args, PyObject *kwargs) out++; } } - if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) { + if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) { PyMem_Free(odata); return NULL; } @@ -1315,7 +1315,7 @@ binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs) } } } - if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) { + if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) { PyMem_Free(odata); return NULL; } diff --git a/Modules/bz2module.c b/Modules/bz2module.c index 15b6e44..e0fbb57 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -34,7 +34,7 @@ typedef fpos_t Py_off_t; #error "Large file support, but neither off_t nor fpos_t is large enough." #endif -#define BUF(v) PyBytes_AS_STRING(v) +#define BUF(v) PyString_AS_STRING(v) #define MODE_CLOSED 0 #define MODE_READ 1 @@ -232,7 +232,7 @@ Util_GetLine(BZ2FileObject *f, int n) int bytes_read; total_v_size = n > 0 ? n : 100; - v = PyBytes_FromStringAndSize((char *)NULL, total_v_size); + v = PyString_FromStringAndSize((char *)NULL, total_v_size); if (v == NULL) return NULL; @@ -272,8 +272,7 @@ Util_GetLine(BZ2FileObject *f, int n) Py_DECREF(v); return NULL; } - if (PyBytes_Resize(v, total_v_size) < 0) { - Py_DECREF(v); + if (_PyString_Resize(&v, total_v_size) < 0) { return NULL; } buf = BUF(v) + used_v_size; @@ -282,8 +281,7 @@ Util_GetLine(BZ2FileObject *f, int n) used_v_size = buf - BUF(v); if (used_v_size != total_v_size) { - if (PyBytes_Resize(v, used_v_size) < 0) { - Py_DECREF(v); + if (_PyString_Resize(&v, used_v_size) < 0) { v = NULL; } } @@ -340,10 +338,10 @@ Util_ReadAhead(BZ2FileObject *f, int bufsize) /* This is a hacked version of Python's * fileobject.c:readahead_get_line_skip(). */ -static PyBytesObject * +static PyStringObject * Util_ReadAheadGetLineSkip(BZ2FileObject *f, int skip, int bufsize) { - PyBytesObject* s; + PyStringObject* s; char *bufptr; char *buf; int len; @@ -354,17 +352,17 @@ Util_ReadAheadGetLineSkip(BZ2FileObject *f, int skip, int bufsize) len = f->f_bufend - f->f_bufptr; if (len == 0) - return (PyBytesObject *) - PyBytes_FromStringAndSize(NULL, skip); + return (PyStringObject *) + PyString_FromStringAndSize(NULL, skip); bufptr = memchr(f->f_bufptr, '\n', len); if (bufptr != NULL) { bufptr++; /* Count the '\n' */ len = bufptr - f->f_bufptr; - s = (PyBytesObject *) - PyBytes_FromStringAndSize(NULL, skip+len); + s = (PyStringObject *) + PyString_FromStringAndSize(NULL, skip+len); if (s == NULL) return NULL; - memcpy(PyBytes_AS_STRING(s)+skip, f->f_bufptr, len); + memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len); f->f_bufptr = bufptr; if (bufptr == f->f_bufend) Util_DropReadAhead(f); @@ -378,7 +376,7 @@ Util_ReadAheadGetLineSkip(BZ2FileObject *f, int skip, int bufsize) PyMem_Free(buf); return NULL; } - memcpy(PyBytes_AS_STRING(s)+skip, bufptr, len); + memcpy(PyString_AS_STRING(s)+skip, bufptr, len); PyMem_Free(buf); } return s; @@ -411,7 +409,7 @@ BZ2File_read(BZ2FileObject *self, PyObject *args) case MODE_READ: break; case MODE_READ_EOF: - ret = PyBytes_FromStringAndSize("", 0); + ret = PyString_FromStringAndSize("", 0); goto cleanup; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, @@ -433,7 +431,7 @@ BZ2File_read(BZ2FileObject *self, PyObject *args) "more than a Python string can hold"); goto cleanup; } - ret = PyBytes_FromStringAndSize((char *)NULL, buffersize); + ret = PyString_FromStringAndSize((char *)NULL, buffersize); if (ret == NULL || buffersize == 0) goto cleanup; bytesread = 0; @@ -458,8 +456,7 @@ BZ2File_read(BZ2FileObject *self, PyObject *args) } if (bytesrequested < 0) { buffersize = Util_NewBufferSize(buffersize); - if (PyBytes_Resize(ret, buffersize) < 0) { - Py_DECREF(ret); + if (_PyString_Resize(&ret, buffersize) < 0) { ret = NULL; goto cleanup; } @@ -468,8 +465,7 @@ BZ2File_read(BZ2FileObject *self, PyObject *args) } } if (bytesread != buffersize) { - if (PyBytes_Resize(ret, bytesread) < 0) { - Py_DECREF(ret); + if (_PyString_Resize(&ret, bytesread) < 0) { ret = NULL; } } @@ -502,7 +498,7 @@ BZ2File_readline(BZ2FileObject *self, PyObject *args) case MODE_READ: break; case MODE_READ_EOF: - ret = PyBytes_FromStringAndSize("", 0); + ret = PyString_FromStringAndSize("", 0); goto cleanup; case MODE_CLOSED: PyErr_SetString(PyExc_ValueError, @@ -515,7 +511,7 @@ BZ2File_readline(BZ2FileObject *self, PyObject *args) } if (sizehint == 0) - ret = PyBytes_FromStringAndSize("", 0); + ret = PyString_FromStringAndSize("", 0); else ret = Util_GetLine(self, (sizehint < 0) ? 0 : sizehint); @@ -608,21 +604,20 @@ BZ2File_readlines(BZ2FileObject *self, PyObject *args) } if (big_buffer == NULL) { /* Create the big buffer */ - big_buffer = PyBytes_FromStringAndSize( + big_buffer = PyString_FromStringAndSize( NULL, buffersize); if (big_buffer == NULL) goto error; - buffer = PyBytes_AS_STRING(big_buffer); + buffer = PyString_AS_STRING(big_buffer); memcpy(buffer, small_buffer, nfilled); } else { /* Grow the big buffer */ - if (PyBytes_Resize(big_buffer, buffersize) < 0){ - Py_DECREF(big_buffer); + if (_PyString_Resize(&big_buffer, buffersize) < 0){ big_buffer = NULL; goto error; } - buffer = PyBytes_AS_STRING(big_buffer); + buffer = PyString_AS_STRING(big_buffer); } continue; } @@ -631,7 +626,7 @@ BZ2File_readlines(BZ2FileObject *self, PyObject *args) while (p != NULL) { /* Process complete lines */ p++; - line = PyBytes_FromStringAndSize(q, p-q); + line = PyString_FromStringAndSize(q, p-q); if (line == NULL) goto error; err = PyList_Append(list, line); @@ -654,21 +649,18 @@ BZ2File_readlines(BZ2FileObject *self, PyObject *args) } if (nfilled != 0) { /* Partial last line */ - line = PyBytes_FromStringAndSize(buffer, nfilled); + line = PyString_FromStringAndSize(buffer, nfilled); if (line == NULL) goto error; if (sizehint > 0) { /* Need to complete the last line */ PyObject *rest = Util_GetLine(self, 0); - PyObject *new; if (rest == NULL) { Py_DECREF(line); goto error; } - new = PyBytes_Concat(line, rest); - Py_DECREF(line); + PyString_Concat(&line, rest); Py_DECREF(rest); - line = new; if (line == NULL) goto error; } @@ -702,7 +694,7 @@ BZ2File_write(BZ2FileObject *self, PyObject *args) int len; int bzerror; - if (!PyArg_ParseTuple(args, "s#:write", &buf, &len)) + if (!PyArg_ParseTuple(args, "y#:write", &buf, &len)) return NULL; ACQUIRE_LOCK(self); @@ -820,7 +812,7 @@ BZ2File_writelines(BZ2FileObject *self, PyObject *seq) could potentially execute Python code. */ for (i = 0; i < j; i++) { PyObject *v = PyList_GET_ITEM(list, i); - if (!PyBytes_Check(v)) { + if (!PyString_Check(v)) { const char *buffer; Py_ssize_t len; if (PyObject_AsCharBuffer(v, &buffer, &len)) { @@ -831,7 +823,7 @@ BZ2File_writelines(BZ2FileObject *self, PyObject *seq) "bytes objects"); goto error; } - line = PyBytes_FromStringAndSize(buffer, + line = PyString_FromStringAndSize(buffer, len); if (line == NULL) goto error; @@ -845,9 +837,9 @@ BZ2File_writelines(BZ2FileObject *self, PyObject *seq) Py_BEGIN_ALLOW_THREADS for (i = 0; i < j; i++) { line = PyList_GET_ITEM(list, i); - len = PyBytes_GET_SIZE(line); + len = PyString_GET_SIZE(line); BZ2_bzWrite (&bzerror, self->fp, - PyBytes_AS_STRING(line), len); + PyString_AS_STRING(line), len); if (bzerror != BZ_OK) { Py_BLOCK_THREADS Util_CatchBZ2Error(bzerror); @@ -1269,7 +1261,7 @@ BZ2File_getiter(BZ2FileObject *self) static PyObject * BZ2File_iternext(BZ2FileObject *self) { - PyBytesObject* ret; + PyStringObject* ret; ACQUIRE_LOCK(self); if (self->mode == MODE_CLOSED) { PyErr_SetString(PyExc_ValueError, @@ -1278,7 +1270,7 @@ BZ2File_iternext(BZ2FileObject *self) } ret = Util_ReadAheadGetLineSkip(self, 0, READAHEAD_BUFSIZE); RELEASE_LOCK(self); - if (ret == NULL || PyBytes_GET_SIZE(ret) == 0) { + if (ret == NULL || PyString_GET_SIZE(ret) == 0) { Py_XDECREF(ret); return NULL; } @@ -1367,11 +1359,11 @@ BZ2Comp_compress(BZ2CompObject *self, PyObject *args) bz_stream *bzs = &self->bzs; int bzerror; - if (!PyArg_ParseTuple(args, "s#:compress", &data, &datasize)) + if (!PyArg_ParseTuple(args, "y#:compress", &data, &datasize)) return NULL; if (datasize == 0) - return PyBytes_FromStringAndSize("", 0); + return PyString_FromStringAndSize("", 0); ACQUIRE_LOCK(self); if (!self->running) { @@ -1380,7 +1372,7 @@ BZ2Comp_compress(BZ2CompObject *self, PyObject *args) goto error; } - ret = PyBytes_FromStringAndSize(NULL, bufsize); + ret = PyString_FromStringAndSize(NULL, bufsize); if (!ret) goto error; @@ -1403,7 +1395,7 @@ BZ2Comp_compress(BZ2CompObject *self, PyObject *args) break; /* no more input data */ if (bzs->avail_out == 0) { bufsize = Util_NewBufferSize(bufsize); - if (PyBytes_Resize(ret, bufsize) < 0) { + if (_PyString_Resize(&ret, bufsize) < 0) { BZ2_bzCompressEnd(bzs); goto error; } @@ -1413,7 +1405,7 @@ BZ2Comp_compress(BZ2CompObject *self, PyObject *args) } } - if (PyBytes_Resize(ret, + if (_PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout)) < 0) goto error; @@ -1450,7 +1442,7 @@ BZ2Comp_flush(BZ2CompObject *self) } self->running = 0; - ret = PyBytes_FromStringAndSize(NULL, bufsize); + ret = PyString_FromStringAndSize(NULL, bufsize); if (!ret) goto error; @@ -1471,7 +1463,7 @@ BZ2Comp_flush(BZ2CompObject *self) } if (bzs->avail_out == 0) { bufsize = Util_NewBufferSize(bufsize); - if (PyBytes_Resize(ret, bufsize) < 0) + if (_PyString_Resize(&ret, bufsize) < 0) goto error; bzs->next_out = BUF(ret); bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs) @@ -1481,7 +1473,7 @@ BZ2Comp_flush(BZ2CompObject *self) } if (bzs->avail_out != 0) { - if (PyBytes_Resize(ret, + if (_PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout)) < 0) goto error; } @@ -1656,7 +1648,7 @@ BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args) bz_stream *bzs = &self->bzs; int bzerror; - if (!PyArg_ParseTuple(args, "s#:decompress", &data, &datasize)) + if (!PyArg_ParseTuple(args, "y#:decompress", &data, &datasize)) return NULL; ACQUIRE_LOCK(self); @@ -1666,7 +1658,7 @@ BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args) goto error; } - ret = PyBytes_FromStringAndSize(NULL, bufsize); + ret = PyString_FromStringAndSize(NULL, bufsize); if (!ret) goto error; @@ -1685,7 +1677,7 @@ BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args) if (bzs->avail_in != 0) { Py_DECREF(self->unused_data); self->unused_data = - PyBytes_FromStringAndSize(bzs->next_in, + PyString_FromStringAndSize(bzs->next_in, bzs->avail_in); } self->running = 0; @@ -1699,7 +1691,7 @@ BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args) break; /* no more input data */ if (bzs->avail_out == 0) { bufsize = Util_NewBufferSize(bufsize); - if (PyBytes_Resize(ret, bufsize) < 0) { + if (_PyString_Resize(&ret, bufsize) < 0) { BZ2_bzDecompressEnd(bzs); goto error; } @@ -1711,7 +1703,7 @@ BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args) } if (bzs->avail_out != 0) { - if (PyBytes_Resize(ret, + if (_PyString_Resize(&ret, (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout)) < 0) goto error; } @@ -1750,7 +1742,7 @@ BZ2Decomp_init(BZ2DecompObject *self, PyObject *args, PyObject *kwargs) } #endif - self->unused_data = PyBytes_FromStringAndSize("", 0); + self->unused_data = PyString_FromStringAndSize("", 0); if (!self->unused_data) goto error; @@ -1868,7 +1860,7 @@ bz2_compress(PyObject *self, PyObject *args, PyObject *kwargs) int bzerror; static char *kwlist[] = {"data", "compresslevel", 0}; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|i", + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y#|i", kwlist, &data, &datasize, &compresslevel)) return NULL; @@ -1883,7 +1875,7 @@ bz2_compress(PyObject *self, PyObject *args, PyObject *kwargs) * data in one shot. We will check it later anyway. */ bufsize = datasize + (datasize/100+1) + 600; - ret = PyBytes_FromStringAndSize(NULL, bufsize); + ret = PyString_FromStringAndSize(NULL, bufsize); if (!ret) return NULL; @@ -1915,9 +1907,8 @@ bz2_compress(PyObject *self, PyObject *args, PyObject *kwargs) } if (bzs->avail_out == 0) { bufsize = Util_NewBufferSize(bufsize); - if (PyBytes_Resize(ret, bufsize) < 0) { + if (_PyString_Resize(&ret, bufsize) < 0) { BZ2_bzCompressEnd(bzs); - Py_DECREF(ret); return NULL; } bzs->next_out = BUF(ret) + BZS_TOTAL_OUT(bzs); @@ -1926,8 +1917,7 @@ bz2_compress(PyObject *self, PyObject *args, PyObject *kwargs) } if (bzs->avail_out != 0) { - if (PyBytes_Resize(ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs)) < 0) { - Py_DECREF(ret); + if (_PyString_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs)) < 0) { ret = NULL; } } @@ -1954,13 +1944,13 @@ bz2_decompress(PyObject *self, PyObject *args) bz_stream *bzs = &_bzs; int bzerror; - if (!PyArg_ParseTuple(args, "s#:decompress", &data, &datasize)) + if (!PyArg_ParseTuple(args, "y#:decompress", &data, &datasize)) return NULL; if (datasize == 0) - return PyBytes_FromStringAndSize("", 0); + return PyString_FromStringAndSize("", 0); - ret = PyBytes_FromStringAndSize(NULL, bufsize); + ret = PyString_FromStringAndSize(NULL, bufsize); if (!ret) return NULL; @@ -1999,9 +1989,8 @@ bz2_decompress(PyObject *self, PyObject *args) } if (bzs->avail_out == 0) { bufsize = Util_NewBufferSize(bufsize); - if (PyBytes_Resize(ret, bufsize) < 0) { + if (_PyString_Resize(&ret, bufsize) < 0) { BZ2_bzDecompressEnd(bzs); - Py_DECREF(ret); return NULL; } bzs->next_out = BUF(ret) + BZS_TOTAL_OUT(bzs); @@ -2010,8 +1999,7 @@ bz2_decompress(PyObject *self, PyObject *args) } if (bzs->avail_out != 0) { - if (PyBytes_Resize(ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs)) < 0) { - Py_DECREF(ret); + if (_PyString_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs)) < 0) { ret = NULL; } } diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c index 7ab3145..701b112 100644 --- a/Modules/cjkcodecs/multibytecodec.c +++ b/Modules/cjkcodecs/multibytecodec.c @@ -175,15 +175,15 @@ expand_encodebuffer(MultibyteEncodeBuffer *buf, Py_ssize_t esize) Py_ssize_t orgpos, orgsize; orgpos = (Py_ssize_t)((char *)buf->outbuf - - PyBytes_AS_STRING(buf->outobj)); - orgsize = PyBytes_GET_SIZE(buf->outobj); - if (PyBytes_Resize(buf->outobj, orgsize + ( + PyString_AS_STRING(buf->outobj)); + orgsize = PyString_GET_SIZE(buf->outobj); + if (_PyString_Resize(&buf->outobj, orgsize + ( esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize)) == -1) return -1; - buf->outbuf = (unsigned char *)PyBytes_AS_STRING(buf->outobj) +orgpos; - buf->outbuf_end = (unsigned char *)PyBytes_AS_STRING(buf->outobj) - + PyBytes_GET_SIZE(buf->outobj); + buf->outbuf = (unsigned char *)PyString_AS_STRING(buf->outobj) +orgpos; + buf->outbuf_end = (unsigned char *)PyString_AS_STRING(buf->outobj) + + PyString_GET_SIZE(buf->outobj); return 0; } @@ -330,11 +330,11 @@ multibytecodec_encerror(MultibyteCodec *codec, goto errorexit; } - assert(PyBytes_Check(retstr)); - retstrsize = PyBytes_GET_SIZE(retstr); + assert(PyString_Check(retstr)); + retstrsize = PyString_GET_SIZE(retstr); REQUIRE_ENCODEBUFFER(buf, retstrsize); - memcpy(buf->outbuf, PyBytes_AS_STRING(retstr), retstrsize); + memcpy(buf->outbuf, PyString_AS_STRING(retstr), retstrsize); buf->outbuf += retstrsize; newpos = PyInt_AsSsize_t(PyTuple_GET_ITEM(retobj, 1)); @@ -476,16 +476,16 @@ multibytecodec_encode(MultibyteCodec *codec, Py_ssize_t finalsize, r = 0; if (datalen == 0) - return PyBytes_FromStringAndSize(NULL, 0); + return PyString_FromStringAndSize(NULL, 0); buf.excobj = NULL; buf.inbuf = buf.inbuf_top = *data; buf.inbuf_end = buf.inbuf_top + datalen; - buf.outobj = PyBytes_FromStringAndSize(NULL, datalen * 2 + 16); + buf.outobj = PyString_FromStringAndSize(NULL, datalen * 2 + 16); if (buf.outobj == NULL) goto errorexit; - buf.outbuf = (unsigned char *)PyBytes_AS_STRING(buf.outobj); - buf.outbuf_end = buf.outbuf + PyBytes_GET_SIZE(buf.outobj); + buf.outbuf = (unsigned char *)PyString_AS_STRING(buf.outobj); + buf.outbuf_end = buf.outbuf + PyString_GET_SIZE(buf.outobj); while (buf.inbuf < buf.inbuf_end) { Py_ssize_t inleft, outleft; @@ -520,10 +520,10 @@ multibytecodec_encode(MultibyteCodec *codec, } finalsize = (Py_ssize_t)((char *)buf.outbuf - - PyBytes_AS_STRING(buf.outobj)); + PyString_AS_STRING(buf.outobj)); - if (finalsize != PyBytes_GET_SIZE(buf.outobj)) - if (PyBytes_Resize(buf.outobj, finalsize) == -1) + if (finalsize != PyString_GET_SIZE(buf.outobj)) + if (_PyString_Resize(&buf.outobj, finalsize) == -1) goto errorexit; Py_XDECREF(buf.excobj); @@ -1611,8 +1611,8 @@ mbstreamwriter_reset(MultibyteStreamWriterObject *self) if (pwrt == NULL) return NULL; - assert(PyBytes_Check(pwrt)); - if (PyBytes_Size(pwrt) > 0) { + assert(PyString_Check(pwrt)); + if (PyString_Size(pwrt) > 0) { PyObject *wr; wr = PyObject_CallMethod(self->stream, "write", "O", pwrt); if (wr == NULL) { diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c index 6f13a85..6b2cd5a 100644 --- a/Modules/datetimemodule.c +++ b/Modules/datetimemodule.c @@ -1133,7 +1133,7 @@ make_Zreplacement(PyObject *object, PyObject *tzinfoarg) { PyObject *temp; PyObject *tzinfo = get_tzinfo_member(object); - PyObject *Zreplacement = PyBytes_FromStringAndSize("", 0); + PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0); if (Zreplacement == NULL) return NULL; if (tzinfo == Py_None || tzinfo == NULL) @@ -1158,14 +1158,7 @@ make_Zreplacement(PyObject *object, PyObject *tzinfoarg) Py_DECREF(temp); if (Zreplacement == NULL) return NULL; - if (PyUnicode_Check(Zreplacement)) { - PyObject *tmp = PyUnicode_AsUTF8String(Zreplacement); - Py_DECREF(Zreplacement); - if (tmp == NULL) - return NULL; - Zreplacement = tmp; - } - if (!PyBytes_Check(Zreplacement)) { + if (!PyUnicode_Check(Zreplacement)) { PyErr_SetString(PyExc_TypeError, "tzname.replace() did not return a string"); goto Error; @@ -1297,9 +1290,10 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple, goto Done; } assert(Zreplacement != NULL); - assert(PyBytes_Check(Zreplacement)); - ptoappend = PyBytes_AS_STRING(Zreplacement); - ntoappend = PyBytes_GET_SIZE(Zreplacement); + assert(PyUnicode_Check(Zreplacement)); + ptoappend = PyUnicode_AsStringAndSize(Zreplacement, + &ntoappend); + ntoappend = Py_Size(Zreplacement); } else { /* percent followed by neither z nor Z */ @@ -3194,7 +3188,7 @@ time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw) PyObject *tuple; static char *keywords[] = {"format", NULL}; - if (! PyArg_ParseTupleAndKeywords(args, kw, "S:strftime", keywords, + if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords, &format)) return NULL; diff --git a/Modules/dbmmodule.c b/Modules/dbmmodule.c index 5660882..6b05fad 100644 --- a/Modules/dbmmodule.c +++ b/Modules/dbmmodule.c @@ -219,14 +219,14 @@ dbm_contains(PyObject *self, PyObject *arg) if (arg == NULL) return -1; } - if (!PyBytes_Check(arg)) { + if (!PyString_Check(arg)) { PyErr_Format(PyExc_TypeError, "dbm key must be string, not %.100s", arg->ob_type->tp_name); return -1; } - key.dptr = PyBytes_AS_STRING(arg); - key.dsize = PyBytes_GET_SIZE(arg); + key.dptr = PyString_AS_STRING(arg); + key.dsize = PyString_GET_SIZE(arg); val = dbm_fetch(dp->di_dbm, key); return val.dptr != NULL; } diff --git a/Modules/gdbmmodule.c b/Modules/gdbmmodule.c index 5ee9c9a..86f98c0 100644 --- a/Modules/gdbmmodule.c +++ b/Modules/gdbmmodule.c @@ -251,14 +251,14 @@ dbm_contains(PyObject *self, PyObject *arg) "GDBM object has already been closed"); return -1; } - if (!PyBytes_Check(arg)) { + if (!PyString_Check(arg)) { PyErr_Format(PyExc_TypeError, "gdbm key must be bytes, not %.100s", arg->ob_type->tp_name); return -1; } - key.dptr = PyBytes_AsString(arg); - key.dsize = PyBytes_Size(arg); + key.dptr = PyString_AS_STRING(arg); + key.dsize = PyString_GET_SIZE(arg); return gdbm_exists(dp->di_dbm, key); } diff --git a/Modules/main.c b/Modules/main.c index b15f1714..ee4a1b8 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -44,7 +44,7 @@ static char **orig_argv; static int orig_argc; /* command line options */ -#define BASE_OPTS "c:dEhim:OStuvVW:xX?" +#define BASE_OPTS "bc:dEhim:OStuvVW:xX?" #define PROGRAM_OPTS BASE_OPTS @@ -55,32 +55,34 @@ static char *usage_line = /* Long usage message, split into parts < 512 bytes */ static char *usage_1 = "\ Options and arguments (and corresponding environment variables):\n\ +-b : issue warnings about str(bytes_instance), str(buffer_instance)\n\ + and comparing bytes/buffer with str. (-bb: issue errors)\n\ -c cmd : program passed in as string (terminates option list)\n\ -d : debug output from parser; also PYTHONDEBUG=x\n\ -E : ignore environment variables (such as PYTHONPATH)\n\ -h : print this help message and exit (also --help)\n\ --i : inspect interactively after running script; forces a prompt even\n\ - if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\ "; static char *usage_2 = "\ +-i : inspect interactively after running script; forces a prompt even\n\ + if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\ -m mod : run library module as a script (terminates option list)\n\ -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\ -OO : remove doc-strings in addition to the -O optimizations\n\ -S : don't imply 'import site' on initialization\n\ -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\ --u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\ "; static char *usage_3 = "\ +-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\ see man page for details on internal buffering relating to '-u'\n\ -v : verbose (trace import statements); also PYTHONVERBOSE=x\n\ can be supplied multiple times to increase verbosity\n\ -V : print the Python version number and exit (also --version)\n\ -W arg : warning control; arg is action:message:category:module:lineno\n\ -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ -file : program read from script file\n\ -- : program read from stdin (default; interactive mode if a tty)\n\ "; static char *usage_4 = "\ +file : program read from script file\n\ +- : program read from stdin (default; interactive mode if a tty)\n\ arg ...: arguments passed to program in sys.argv[1:]\n\n\ Other environment variables:\n\ PYTHONSTARTUP: file executed on interactive startup (no default)\n\ @@ -252,6 +254,9 @@ Py_Main(int argc, char **argv) } switch (c) { + case 'b': + Py_BytesWarningFlag++; + break; case 'd': Py_DebugFlag++; diff --git a/Modules/md5module.c b/Modules/md5module.c index 26f0e4c..c5770b3 100644 --- a/Modules/md5module.c +++ b/Modules/md5module.c @@ -363,7 +363,7 @@ MD5_digest(MD5object *self, PyObject *unused) temp = self->hash_state; md5_done(&temp, digest); - return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE); + return PyString_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE); } PyDoc_STRVAR(MD5_hexdigest__doc__, diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index e5d7711..1a58f3d 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -305,7 +305,7 @@ is_resizeable(mmap_object *self) return 0; } if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT)) - return 1; + return 1; PyErr_Format(PyExc_TypeError, "mmap can't resize a readonly or copy-on-write memory map."); return 0; @@ -621,10 +621,10 @@ static struct PyMethodDef mmap_object_methods[] = { /* Functions for treating an mmap'ed file as a buffer */ static int -mmap_buffer_getbuf(mmap_object *self, Py_buffer *view, int flags) +mmap_buffer_getbuf(mmap_object *self, Py_buffer *view, int flags) { CHECK_VALID(-1); - if (PyBuffer_FillInfo(view, self->data, self->size, + if (PyBuffer_FillInfo(view, self->data, self->size, (self->access == ACCESS_READ), flags) < 0) return -1; self->exports++; @@ -676,7 +676,7 @@ mmap_subscript(mmap_object *self, PyObject *item) "mmap index out of range"); return NULL; } - return PyBytes_FromStringAndSize(self->data + i, 1); + return PyInt_FromLong(Py_CHARMASK(self->data[i])); } else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelen; @@ -685,12 +685,12 @@ mmap_subscript(mmap_object *self, PyObject *item) &start, &stop, &step, &slicelen) < 0) { return NULL; } - + if (slicelen <= 0) - return PyBytes_FromStringAndSize("", 0); + return PyString_FromStringAndSize("", 0); else if (step == 1) - return PyBytes_FromStringAndSize(self->data + start, - slicelen); + return PyString_FromStringAndSize(self->data + start, + slicelen); else { char *result_buf = (char *)PyMem_Malloc(slicelen); Py_ssize_t cur, i; @@ -702,8 +702,8 @@ mmap_subscript(mmap_object *self, PyObject *item) cur += step, i++) { result_buf[i] = self->data[cur]; } - result = PyBytes_FromStringAndSize(result_buf, - slicelen); + result = PyString_FromStringAndSize(result_buf, + slicelen); PyMem_Free(result_buf); return result; } @@ -765,9 +765,12 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value) { CHECK_VALID(-1); + if (!is_writable(self)) + return -1; + if (PyIndex_Check(item)) { Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); - const char *buf; + Py_ssize_t v; if (i == -1 && PyErr_Occurred()) return -1; @@ -775,28 +778,35 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value) i += self->size; if (i < 0 || (size_t)i > self->size) { PyErr_SetString(PyExc_IndexError, - "mmap index out of range"); + "mmap index out of range"); return -1; } if (value == NULL) { PyErr_SetString(PyExc_TypeError, - "mmap object doesn't support item deletion"); + "mmap doesn't support item deletion"); return -1; } - if (!PyBytes_Check(value) || PyBytes_Size(value) != 1) { - PyErr_SetString(PyExc_IndexError, - "mmap assignment must be length-1 bytes()"); + if (!PyIndex_Check(value)) { + PyErr_SetString(PyExc_TypeError, + "mmap item value must be an int"); return -1; } - if (!is_writable(self)) + v = PyNumber_AsSsize_t(value, PyExc_TypeError); + if (v == -1 && PyErr_Occurred()) return -1; - buf = PyBytes_AsString(value); - self->data[i] = buf[0]; + if (v < 0 || v > 255) { + PyErr_SetString(PyExc_ValueError, + "mmap item value must be " + "in range(0, 256)"); + return -1; + } + self->data[i] = v; return 0; } else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelen; - + Py_buffer vbuf; + if (PySlice_GetIndicesEx((PySliceObject *)item, self->size, &start, &stop, &step, &slicelen) < 0) { @@ -807,41 +817,32 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value) "mmap object doesn't support slice deletion"); return -1; } - if (!PyBytes_Check(value)) { - PyErr_SetString(PyExc_IndexError, - "mmap slice assignment must be bytes"); + if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0) return -1; - } - if (PyBytes_Size(value) != slicelen) { + if (vbuf.len != slicelen) { PyErr_SetString(PyExc_IndexError, "mmap slice assignment is wrong size"); + PyObject_ReleaseBuffer(value, &vbuf); return -1; } - if (!is_writable(self)) - return -1; - if (slicelen == 0) - return 0; + if (slicelen == 0) { + } else if (step == 1) { - const char *buf = PyBytes_AsString(value); - - if (buf == NULL) - return -1; - memcpy(self->data + start, buf, slicelen); - return 0; + memcpy(self->data + start, vbuf.buf, slicelen); } else { Py_ssize_t cur, i; - const char *buf = PyBytes_AsString(value); - - if (buf == NULL) - return -1; - for (cur = start, i = 0; i < slicelen; - cur += step, i++) { - self->data[cur] = buf[i]; + + for (cur = start, i = 0; + i < slicelen; + cur += step, i++) + { + self->data[cur] = ((char *)vbuf.buf)[i]; } - return 0; } + PyObject_ReleaseBuffer(value, &vbuf); + return 0; } else { PyErr_SetString(PyExc_TypeError, @@ -908,9 +909,9 @@ _GetMapSize(PyObject *o, const char* param) return 0; if (PyIndex_Check(o)) { Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError); - if (i==-1 && PyErr_Occurred()) + if (i==-1 && PyErr_Occurred()) return -1; - if (i < 0) { + if (i < 0) { PyErr_Format(PyExc_OverflowError, "memory mapped %s must be positive", param); @@ -1151,8 +1152,8 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) (dwErr = GetLastError()) != NO_ERROR) { Py_DECREF(m_obj); return PyErr_SetFromWindowsErr(dwErr); - } - + } + #if SIZEOF_SIZE_T > 4 m_obj->size = (((size_t)high)<<32) + low; #else diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index acd01da..cb74f84 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -357,12 +357,12 @@ convertenviron(void) char *p = strchr(*e, '='); if (p == NULL) continue; - k = PyString_FromStringAndSize(*e, (int)(p-*e)); + k = PyUnicode_FromStringAndSize(*e, (int)(p-*e)); if (k == NULL) { PyErr_Clear(); continue; } - v = PyString_FromString(p+1); + v = PyUnicode_FromString(p+1); if (v == NULL) { PyErr_Clear(); Py_DECREF(k); diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index ae6f143..658569e 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -858,6 +858,7 @@ readinst(char *buf, int buf_size, PyObject *meth) PyObject *bytes = NULL; PyObject *str = NULL; int len = -1; + char *ptr; if ((bytes = PyInt_FromLong(buf_size)) == NULL) goto finally; @@ -877,14 +878,17 @@ readinst(char *buf, int buf_size, PyObject *meth) if (str == NULL) goto finally; - /* XXX what to do if it returns a Unicode string? */ - if (!PyBytes_Check(str)) { + if (PyString_Check(str)) + ptr = PyString_AS_STRING(str); + else if (PyBytes_Check(str)) + ptr = PyBytes_AS_STRING(str); + else { PyErr_Format(PyExc_TypeError, "read() did not return a bytes object (type=%.400s)", Py_Type(str)->tp_name); goto finally; } - len = PyBytes_GET_SIZE(str); + len = Py_Size(str); if (len > buf_size) { PyErr_Format(PyExc_ValueError, "read() returned too much data: " @@ -892,7 +896,7 @@ readinst(char *buf, int buf_size, PyObject *meth) buf_size, len); goto finally; } - memcpy(buf, PyBytes_AsString(str), len); + memcpy(buf, ptr, len); finally: Py_XDECREF(arg); Py_XDECREF(str); @@ -998,7 +1002,7 @@ xmlparse_GetInputContext(xmlparseobject *self, PyObject *unused) = XML_GetInputContext(self->itself, &offset, &size); if (buffer != NULL) - return PyBytes_FromStringAndSize(buffer + offset, + return PyString_FromStringAndSize(buffer + offset, size - offset); else Py_RETURN_NONE; diff --git a/Modules/sha1module.c b/Modules/sha1module.c index d96fe35..6ba03ad 100644 --- a/Modules/sha1module.c +++ b/Modules/sha1module.c @@ -339,7 +339,7 @@ SHA1_digest(SHA1object *self, PyObject *unused) temp = self->hash_state; sha1_done(&temp, digest); - return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE); + return PyString_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE); } PyDoc_STRVAR(SHA1_hexdigest__doc__, diff --git a/Modules/sha256module.c b/Modules/sha256module.c index da31d18..a5dc9ad 100644 --- a/Modules/sha256module.c +++ b/Modules/sha256module.c @@ -432,7 +432,7 @@ SHA256_digest(SHAobject *self, PyObject *unused) SHAcopy(self, &temp); sha_final(digest, &temp); - return PyBytes_FromStringAndSize((const char *)digest, self->digestsize); + return PyString_FromStringAndSize((const char *)digest, self->digestsize); } PyDoc_STRVAR(SHA256_hexdigest__doc__, diff --git a/Modules/sha512module.c b/Modules/sha512module.c index 8b33bb0..c330e1b 100644 --- a/Modules/sha512module.c +++ b/Modules/sha512module.c @@ -498,7 +498,7 @@ SHA512_digest(SHAobject *self, PyObject *unused) SHAcopy(self, &temp); sha512_final(digest, &temp); - return PyBytes_FromStringAndSize((const char *)digest, self->digestsize); + return PyString_FromStringAndSize((const char *)digest, self->digestsize); } PyDoc_STRVAR(SHA512_hexdigest__doc__, diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 7fe562e..78aeb55 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -3659,8 +3659,8 @@ socket_getaddrinfo(PyObject *self, PyObject *args) idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); if (!idna) return NULL; - assert(PyBytes_Check(idna)); - hptr = PyBytes_AsString(idna); + assert(PyString_Check(idna)); + hptr = PyString_AS_STRING(idna); } else if (PyString_Check(hobj)) { hptr = PyString_AsString(hobj); } else { |