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.
This commit is contained in:
Guido van Rossum 2007-11-06 21:34:58 +00:00
parent a19f80c6df
commit 98297ee781
148 changed files with 2533 additions and 3517 deletions

View file

@ -119,8 +119,8 @@ static int import_all_from(PyObject *, PyObject *);
static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
static void reset_exc_info(PyThreadState *);
static void format_exc_check_arg(PyObject *, const char *, PyObject *);
static PyObject * string_concatenate(PyObject *, PyObject *,
PyFrameObject *, unsigned char *);
static PyObject * unicode_concatenate(PyObject *, PyObject *,
PyFrameObject *, unsigned char *);
#define NAME_ERROR_MSG \
"name '%.200s' is not defined"
@ -1127,10 +1127,10 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
goto slow_add;
x = PyInt_FromLong(i);
}
else if (PyString_CheckExact(v) &&
PyString_CheckExact(w)) {
x = string_concatenate(v, w, f, next_instr);
/* string_concatenate consumed the ref to v */
else if (PyUnicode_CheckExact(v) &&
PyUnicode_CheckExact(w)) {
x = unicode_concatenate(v, w, f, next_instr);
/* unicode_concatenate consumed the ref to v */
goto skip_decref_vx;
}
else {
@ -1328,10 +1328,10 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
goto slow_iadd;
x = PyInt_FromLong(i);
}
else if (PyString_CheckExact(v) &&
PyString_CheckExact(w)) {
x = string_concatenate(v, w, f, next_instr);
/* string_concatenate consumed the ref to v */
else if (PyUnicode_CheckExact(v) &&
PyUnicode_CheckExact(w)) {
x = unicode_concatenate(v, w, f, next_instr);
/* unicode_concatenate consumed the ref to v */
goto skip_decref_v;
}
else {
@ -1564,8 +1564,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
break;
}
PyErr_Format(PyExc_SystemError,
"no locals found when storing %s",
PyObject_REPR(w));
"no locals found when storing %R", w);
break;
case DELETE_NAME:
@ -1578,8 +1577,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
break;
}
PyErr_Format(PyExc_SystemError,
"no locals when deleting %s",
PyObject_REPR(w));
"no locals when deleting %R", w);
break;
PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
@ -1668,8 +1666,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
w = GETITEM(names, oparg);
if ((v = f->f_locals) == NULL) {
PyErr_Format(PyExc_SystemError,
"no locals when loading %s",
PyObject_REPR(w));
"no locals when loading %R", w);
break;
}
if (PyDict_CheckExact(v)) {
@ -1854,19 +1851,6 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
PUSH(x);
if (x != NULL) continue;
break;
case MAKE_BYTES:
w = POP();
if (PyString_Check(w))
x = PyBytes_FromStringAndSize(
PyString_AS_STRING(w),
PyString_GET_SIZE(w));
else
x = NULL;
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case LOAD_ATTR:
w = GETITEM(names, oparg);
@ -3961,13 +3945,13 @@ format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
}
static PyObject *
string_concatenate(PyObject *v, PyObject *w,
unicode_concatenate(PyObject *v, PyObject *w,
PyFrameObject *f, unsigned char *next_instr)
{
/* This function implements 'variable += expr' when both arguments
are strings. */
Py_ssize_t v_len = PyString_GET_SIZE(v);
Py_ssize_t w_len = PyString_GET_SIZE(w);
are (Unicode) strings. */
Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Py_ssize_t new_len = v_len + w_len;
if (new_len < 0) {
PyErr_SetString(PyExc_OverflowError,
@ -4016,12 +4000,12 @@ string_concatenate(PyObject *v, PyObject *w,
}
}
if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
/* Now we own the last reference to 'v', so we can resize it
* in-place.
*/
if (_PyString_Resize(&v, new_len) != 0) {
/* XXX if _PyString_Resize() fails, 'v' has been
if (PyUnicode_Resize(&v, new_len) != 0) {
/* XXX if PyUnicode_Resize() fails, 'v' has been
* deallocated so it cannot be put back into
* 'variable'. The MemoryError is raised when there
* is no value in 'variable', which might (very
@ -4030,14 +4014,15 @@ string_concatenate(PyObject *v, PyObject *w,
return NULL;
}
/* copy 'w' into the newly allocated area of 'v' */
memcpy(PyString_AS_STRING(v) + v_len,
PyString_AS_STRING(w), w_len);
memcpy(PyUnicode_AS_UNICODE(v) + v_len,
PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
return v;
}
else {
/* When in-place resizing is not an option. */
PyString_Concat(&v, w);
return v;
w = PyUnicode_Concat(v, w);
Py_DECREF(v);
return w;
}
}