Use unicode and remove support for some uses of str8.

This commit is contained in:
Neal Norwitz 2007-08-26 04:19:43 +00:00
parent 5b0fdc9d0a
commit 6ea45d3341
8 changed files with 59 additions and 100 deletions

View File

@ -420,8 +420,8 @@ class HexBin:
self.FName = fname
self.FInfo = FInfo()
self.FInfo.Creator = str8(creator)
self.FInfo.Type = str8(type)
self.FInfo.Creator = creator
self.FInfo.Type = type
self.FInfo.Flags = flags
self.state = _DID_HEADER

View File

@ -261,22 +261,19 @@ getcodec(PyObject *self, PyObject *encoding)
const MultibyteCodec *codec;
const char *enc;
if (PyUnicode_Check(encoding)) {
encoding = _PyUnicode_AsDefaultEncodedString(encoding, NULL);
if (encoding == NULL)
return NULL;
}
if (!PyString_Check(encoding)) {
if (!PyUnicode_Check(encoding)) {
PyErr_SetString(PyExc_TypeError,
"encoding name must be a string.");
return NULL;
}
enc = PyUnicode_AsString(encoding, NULL);
if (enc == NULL)
return NULL;
cofunc = getmultibytecodec();
if (cofunc == NULL)
return NULL;
enc = PyString_AS_STRING(encoding);
for (codec = codec_list; codec->encoding[0]; codec++)
if (strcmp(codec->encoding, enc) == 0)
break;

View File

@ -85,16 +85,20 @@ internal_error_callback(const char *errors)
else if (strcmp(errors, "replace") == 0)
return ERROR_REPLACE;
else
return PyString_FromString(errors);
return PyUnicode_FromString(errors);
}
static PyObject *
call_error_callback(PyObject *errors, PyObject *exc)
{
PyObject *args, *cb, *r;
const char *str;
assert(PyString_Check(errors));
cb = PyCodec_LookupError(PyString_AS_STRING(errors));
assert(PyUnicode_Check(errors));
str = PyUnicode_AsString(errors);
if (str == NULL)
return NULL;
cb = PyCodec_LookupError(str);
if (cb == NULL)
return NULL;
@ -129,7 +133,7 @@ codecctx_errors_get(MultibyteStatefulCodecContext *self)
return self->errors;
}
return PyString_FromString(errors);
return PyUnicode_FromString(errors);
}
static int
@ -137,18 +141,18 @@ codecctx_errors_set(MultibyteStatefulCodecContext *self, PyObject *value,
void *closure)
{
PyObject *cb;
const char *str;
if (PyUnicode_Check(value)) {
value = _PyUnicode_AsDefaultEncodedString(value, NULL);
if (value == NULL)
return -1;
}
if (!PyString_Check(value)) {
if (!PyUnicode_Check(value)) {
PyErr_SetString(PyExc_TypeError, "errors must be a string");
return -1;
}
cb = internal_error_callback(PyString_AS_STRING(value));
str = PyUnicode_AsString(value);
if (str == NULL)
return -1;
cb = internal_error_callback(str);
if (cb == NULL)
return -1;

View File

@ -1281,13 +1281,6 @@ PyNumber_Long(PyObject *o)
}
if (PyLong_Check(o)) /* A long subclass without nb_long */
return _PyLong_Copy((PyLongObject *)o);
if (PyString_Check(o))
/* need to do extra error checking that PyLong_FromString()
* doesn't do. In particular long('9.5') must raise an
* exception, not truncate the float.
*/
return long_from_string(PyString_AS_STRING(o),
PyString_GET_SIZE(o));
if (PyUnicode_Check(o))
/* The above check is done in PyLong_FromUnicode(). */
return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),

View File

@ -74,11 +74,7 @@ PyFloat_FromString(PyObject *v)
Py_ssize_t len;
PyObject *result = NULL;
if (PyString_Check(v)) {
s = PyString_AS_STRING(v);
len = PyString_GET_SIZE(v);
}
else if (PyUnicode_Check(v)) {
if (PyUnicode_Check(v)) {
s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
if (s_buffer == NULL)
return PyErr_NoMemory();
@ -843,7 +839,7 @@ float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return float_subtype_new(type, args, kwds); /* Wimp out */
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
return NULL;
if (PyString_Check(x))
if (PyUnicode_Check(x))
return PyFloat_FromString(x);
return PyNumber_Float(x);
}
@ -894,18 +890,15 @@ float_getformat(PyTypeObject *v, PyObject* arg)
char* s;
float_format_type r;
if (PyUnicode_Check(arg)) {
arg = _PyUnicode_AsDefaultEncodedString(arg, NULL);
if (arg == NULL)
return NULL;
}
if (!PyString_Check(arg)) {
if (!PyUnicode_Check(arg)) {
PyErr_Format(PyExc_TypeError,
"__getformat__() argument must be string, not %.500s",
Py_Type(arg)->tp_name);
return NULL;
}
s = PyString_AS_STRING(arg);
s = PyUnicode_AsString(arg);
if (s == NULL)
return NULL;
if (strcmp(s, "double") == 0) {
r = double_format;
}

View File

@ -357,7 +357,7 @@ _PyObject_Dump(PyObject* op)
PyObject *
PyObject_Repr(PyObject *v)
{
PyObject *ress, *resu;
PyObject *res;
if (PyErr_CheckSignals())
return NULL;
#ifdef USE_STACKCHECK
@ -371,21 +371,15 @@ PyObject_Repr(PyObject *v)
else if (Py_Type(v)->tp_repr == NULL)
return PyUnicode_FromFormat("<%s object at %p>", v->ob_type->tp_name, v);
else {
ress = (*v->ob_type->tp_repr)(v);
if (!ress)
return NULL;
if (PyUnicode_Check(ress))
return ress;
if (!PyString_Check(ress)) {
res = (*v->ob_type->tp_repr)(v);
if (res != NULL && !PyUnicode_Check(res)) {
PyErr_Format(PyExc_TypeError,
"__repr__ returned non-string (type %.200s)",
ress->ob_type->tp_name);
Py_DECREF(ress);
res->ob_type->tp_name);
Py_DECREF(res);
return NULL;
}
resu = PyUnicode_FromObject(ress);
Py_DECREF(ress);
return resu;
return res;
}
}
@ -413,7 +407,7 @@ _PyObject_Str(PyObject *v)
{
PyObject *res;
if (v == NULL)
return PyString_FromString("<NULL>");
return PyUnicode_FromString("<NULL>");
if (PyString_CheckExact(v)) {
Py_INCREF(v);
return v;

View File

@ -55,17 +55,15 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
"can't delete %s.__name__", type->tp_name);
return -1;
}
if (PyUnicode_Check(value)) {
value = _PyUnicode_AsDefaultEncodedString(value, NULL);
if (value == NULL)
return -1;
}
if (!PyString_Check(value)) {
if (!PyUnicode_Check(value)) {
PyErr_Format(PyExc_TypeError,
"can only assign string to %s.__name__, not '%s'",
type->tp_name, Py_Type(value)->tp_name);
return -1;
}
value = _PyUnicode_AsDefaultEncodedString(value, NULL);
if (value == NULL)
return -1;
if (strlen(PyString_AS_STRING(value))
!= (size_t)PyString_GET_SIZE(value)) {
PyErr_Format(PyExc_ValueError,
@ -1918,30 +1916,22 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
*/
{
PyObject *doc = PyDict_GetItemString(dict, "__doc__");
if (doc != NULL) {
char *tp_doc;
const char *str = NULL;
if (doc != NULL && PyUnicode_Check(doc)) {
size_t n;
if (PyString_Check(doc)) {
str = PyString_AS_STRING(doc);
n = (size_t)PyString_GET_SIZE(doc);
} else if (PyUnicode_Check(doc)) {
str = PyUnicode_AsString(doc);
if (str == NULL) {
Py_DECREF(type);
return NULL;
}
n = strlen(str);
char *tp_doc;
const char *str = PyUnicode_AsString(doc);
if (str == NULL) {
Py_DECREF(type);
return NULL;
}
if (str != NULL) {
tp_doc = (char *)PyObject_MALLOC(n+1);
if (tp_doc == NULL) {
Py_DECREF(type);
return NULL;
}
memcpy(tp_doc, str, n+1);
type->tp_doc = tp_doc;
n = strlen(str);
tp_doc = (char *)PyObject_MALLOC(n+1);
if (tp_doc == NULL) {
Py_DECREF(type);
return NULL;
}
memcpy(tp_doc, str, n+1);
type->tp_doc = tp_doc;
}
}

View File

@ -40,7 +40,7 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
}
func = PyTuple_GET_ITEM(args, 0); /* Better be callable */
name = PyTuple_GET_ITEM(args, 1);
if ((!PyString_Check(name) && !PyUnicode_Check(name))) {
if (!PyUnicode_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"__build_class__: name is not a string");
return NULL;
@ -631,8 +631,7 @@ builtin_exec(PyObject *self, PyObject *args)
}
else if (locals == Py_None)
locals = globals;
if (!PyString_Check(prog) &&
!PyUnicode_Check(prog) &&
if (!PyUnicode_Check(prog) &&
!PyCode_Check(prog)) {
PyErr_Format(PyExc_TypeError,
"exec() arg 1 must be a string, file, or code "
@ -695,23 +694,15 @@ globals and locals. If only globals is given, locals defaults to it.");
static PyObject *
builtin_getattr(PyObject *self, PyObject *args)
{
PyObject *v, *result, *dflt = NULL, *release = NULL;
PyObject *v, *result, *dflt = NULL;
PyObject *name;
if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
return NULL;
if (PyString_Check(name)) {
release = PyString_AsDecodedObject(name, NULL, NULL);
if (!release)
return NULL;
name = release;
}
if (!PyUnicode_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"getattr(): attribute name must be string");
Py_XDECREF(release);
return NULL;
}
result = PyObject_GetAttr(v, name);
@ -722,7 +713,6 @@ builtin_getattr(PyObject *self, PyObject *args)
Py_INCREF(dflt);
result = dflt;
}
Py_XDECREF(release);
return result;
}
@ -1221,17 +1211,15 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
if (file == NULL || file == Py_None)
file = PySys_GetObject("stdout");
if (sep && sep != Py_None && !PyString_Check(sep) &&
!PyUnicode_Check(sep)) {
if (sep && sep != Py_None && !PyUnicode_Check(sep)) {
PyErr_Format(PyExc_TypeError,
"sep must be None, str or unicode, not %.200s",
"sep must be None or a string, not %.200s",
sep->ob_type->tp_name);
return NULL;
}
if (end && end != Py_None && !PyString_Check(end) &&
!PyUnicode_Check(end)) {
if (end && end != Py_None && !PyUnicode_Check(end)) {
PyErr_Format(PyExc_TypeError,
"end must be None, str or unicode, not %.200s",
"end must be None or a string, not %.200s",
end->ob_type->tp_name);
return NULL;
}
@ -1383,7 +1371,7 @@ builtin_input(PyObject *self, PyObject *args)
result = NULL;
}
else {
result = PyString_FromStringAndSize(s, len-1);
result = PyUnicode_FromStringAndSize(s, len-1);
}
}
PyMem_FREE(s);