Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ea45d3

Browse files
committed
Use unicode and remove support for some uses of str8.
1 parent 5b0fdc9 commit 6ea45d3

8 files changed

Lines changed: 59 additions & 100 deletions

File tree

Lib/binhex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ def _readheader(self):
420420

421421
self.FName = fname
422422
self.FInfo = FInfo()
423-
self.FInfo.Creator = str8(creator)
424-
self.FInfo.Type = str8(type)
423+
self.FInfo.Creator = creator
424+
self.FInfo.Type = type
425425
self.FInfo.Flags = flags
426426

427427
self.state = _DID_HEADER

Modules/cjkcodecs/cjkcodecs.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,19 @@ getcodec(PyObject *self, PyObject *encoding)
261261
const MultibyteCodec *codec;
262262
const char *enc;
263263

264-
if (PyUnicode_Check(encoding)) {
265-
encoding = _PyUnicode_AsDefaultEncodedString(encoding, NULL);
266-
if (encoding == NULL)
267-
return NULL;
268-
}
269-
if (!PyString_Check(encoding)) {
264+
if (!PyUnicode_Check(encoding)) {
270265
PyErr_SetString(PyExc_TypeError,
271266
"encoding name must be a string.");
272267
return NULL;
273268
}
269+
enc = PyUnicode_AsString(encoding, NULL);
270+
if (enc == NULL)
271+
return NULL;
274272

275273
cofunc = getmultibytecodec();
276274
if (cofunc == NULL)
277275
return NULL;
278276

279-
enc = PyString_AS_STRING(encoding);
280277
for (codec = codec_list; codec->encoding[0]; codec++)
281278
if (strcmp(codec->encoding, enc) == 0)
282279
break;

Modules/cjkcodecs/multibytecodec.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,20 @@ internal_error_callback(const char *errors)
8585
else if (strcmp(errors, "replace") == 0)
8686
return ERROR_REPLACE;
8787
else
88-
return PyString_FromString(errors);
88+
return PyUnicode_FromString(errors);
8989
}
9090

9191
static PyObject *
9292
call_error_callback(PyObject *errors, PyObject *exc)
9393
{
9494
PyObject *args, *cb, *r;
95+
const char *str;
9596

96-
assert(PyString_Check(errors));
97-
cb = PyCodec_LookupError(PyString_AS_STRING(errors));
97+
assert(PyUnicode_Check(errors));
98+
str = PyUnicode_AsString(errors);
99+
if (str == NULL)
100+
return NULL;
101+
cb = PyCodec_LookupError(str);
98102
if (cb == NULL)
99103
return NULL;
100104

@@ -129,26 +133,26 @@ codecctx_errors_get(MultibyteStatefulCodecContext *self)
129133
return self->errors;
130134
}
131135

132-
return PyString_FromString(errors);
136+
return PyUnicode_FromString(errors);
133137
}
134138

135139
static int
136140
codecctx_errors_set(MultibyteStatefulCodecContext *self, PyObject *value,
137141
void *closure)
138142
{
139143
PyObject *cb;
144+
const char *str;
140145

141-
if (PyUnicode_Check(value)) {
142-
value = _PyUnicode_AsDefaultEncodedString(value, NULL);
143-
if (value == NULL)
144-
return -1;
145-
}
146-
if (!PyString_Check(value)) {
146+
if (!PyUnicode_Check(value)) {
147147
PyErr_SetString(PyExc_TypeError, "errors must be a string");
148148
return -1;
149149
}
150150

151-
cb = internal_error_callback(PyString_AS_STRING(value));
151+
str = PyUnicode_AsString(value);
152+
if (str == NULL)
153+
return -1;
154+
155+
cb = internal_error_callback(str);
152156
if (cb == NULL)
153157
return -1;
154158

Objects/abstract.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,13 +1281,6 @@ PyNumber_Long(PyObject *o)
12811281
}
12821282
if (PyLong_Check(o)) /* A long subclass without nb_long */
12831283
return _PyLong_Copy((PyLongObject *)o);
1284-
if (PyString_Check(o))
1285-
/* need to do extra error checking that PyLong_FromString()
1286-
* doesn't do. In particular long('9.5') must raise an
1287-
* exception, not truncate the float.
1288-
*/
1289-
return long_from_string(PyString_AS_STRING(o),
1290-
PyString_GET_SIZE(o));
12911284
if (PyUnicode_Check(o))
12921285
/* The above check is done in PyLong_FromUnicode(). */
12931286
return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),

Objects/floatobject.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ PyFloat_FromString(PyObject *v)
7474
Py_ssize_t len;
7575
PyObject *result = NULL;
7676

77-
if (PyString_Check(v)) {
78-
s = PyString_AS_STRING(v);
79-
len = PyString_GET_SIZE(v);
80-
}
81-
else if (PyUnicode_Check(v)) {
77+
if (PyUnicode_Check(v)) {
8278
s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
8379
if (s_buffer == NULL)
8480
return PyErr_NoMemory();
@@ -843,7 +839,7 @@ float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
843839
return float_subtype_new(type, args, kwds); /* Wimp out */
844840
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
845841
return NULL;
846-
if (PyString_Check(x))
842+
if (PyUnicode_Check(x))
847843
return PyFloat_FromString(x);
848844
return PyNumber_Float(x);
849845
}
@@ -894,18 +890,15 @@ float_getformat(PyTypeObject *v, PyObject* arg)
894890
char* s;
895891
float_format_type r;
896892

897-
if (PyUnicode_Check(arg)) {
898-
arg = _PyUnicode_AsDefaultEncodedString(arg, NULL);
899-
if (arg == NULL)
900-
return NULL;
901-
}
902-
if (!PyString_Check(arg)) {
893+
if (!PyUnicode_Check(arg)) {
903894
PyErr_Format(PyExc_TypeError,
904895
"__getformat__() argument must be string, not %.500s",
905896
Py_Type(arg)->tp_name);
906897
return NULL;
907898
}
908-
s = PyString_AS_STRING(arg);
899+
s = PyUnicode_AsString(arg);
900+
if (s == NULL)
901+
return NULL;
909902
if (strcmp(s, "double") == 0) {
910903
r = double_format;
911904
}

Objects/object.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ _PyObject_Dump(PyObject* op)
357357
PyObject *
358358
PyObject_Repr(PyObject *v)
359359
{
360-
PyObject *ress, *resu;
360+
PyObject *res;
361361
if (PyErr_CheckSignals())
362362
return NULL;
363363
#ifdef USE_STACKCHECK
@@ -371,21 +371,15 @@ PyObject_Repr(PyObject *v)
371371
else if (Py_Type(v)->tp_repr == NULL)
372372
return PyUnicode_FromFormat("<%s object at %p>", v->ob_type->tp_name, v);
373373
else {
374-
ress = (*v->ob_type->tp_repr)(v);
375-
if (!ress)
376-
return NULL;
377-
if (PyUnicode_Check(ress))
378-
return ress;
379-
if (!PyString_Check(ress)) {
374+
res = (*v->ob_type->tp_repr)(v);
375+
if (res != NULL && !PyUnicode_Check(res)) {
380376
PyErr_Format(PyExc_TypeError,
381377
"__repr__ returned non-string (type %.200s)",
382-
ress->ob_type->tp_name);
383-
Py_DECREF(ress);
378+
res->ob_type->tp_name);
379+
Py_DECREF(res);
384380
return NULL;
385381
}
386-
resu = PyUnicode_FromObject(ress);
387-
Py_DECREF(ress);
388-
return resu;
382+
return res;
389383
}
390384
}
391385

@@ -413,7 +407,7 @@ _PyObject_Str(PyObject *v)
413407
{
414408
PyObject *res;
415409
if (v == NULL)
416-
return PyString_FromString("<NULL>");
410+
return PyUnicode_FromString("<NULL>");
417411
if (PyString_CheckExact(v)) {
418412
Py_INCREF(v);
419413
return v;

Objects/typeobject.c

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,15 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
5555
"can't delete %s.__name__", type->tp_name);
5656
return -1;
5757
}
58-
if (PyUnicode_Check(value)) {
59-
value = _PyUnicode_AsDefaultEncodedString(value, NULL);
60-
if (value == NULL)
61-
return -1;
62-
}
63-
if (!PyString_Check(value)) {
58+
if (!PyUnicode_Check(value)) {
6459
PyErr_Format(PyExc_TypeError,
6560
"can only assign string to %s.__name__, not '%s'",
6661
type->tp_name, Py_Type(value)->tp_name);
6762
return -1;
6863
}
64+
value = _PyUnicode_AsDefaultEncodedString(value, NULL);
65+
if (value == NULL)
66+
return -1;
6967
if (strlen(PyString_AS_STRING(value))
7068
!= (size_t)PyString_GET_SIZE(value)) {
7169
PyErr_Format(PyExc_ValueError,
@@ -1918,30 +1916,22 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
19181916
*/
19191917
{
19201918
PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1921-
if (doc != NULL) {
1922-
char *tp_doc;
1923-
const char *str = NULL;
1919+
if (doc != NULL && PyUnicode_Check(doc)) {
19241920
size_t n;
1925-
if (PyString_Check(doc)) {
1926-
str = PyString_AS_STRING(doc);
1927-
n = (size_t)PyString_GET_SIZE(doc);
1928-
} else if (PyUnicode_Check(doc)) {
1929-
str = PyUnicode_AsString(doc);
1930-
if (str == NULL) {
1931-
Py_DECREF(type);
1932-
return NULL;
1933-
}
1934-
n = strlen(str);
1921+
char *tp_doc;
1922+
const char *str = PyUnicode_AsString(doc);
1923+
if (str == NULL) {
1924+
Py_DECREF(type);
1925+
return NULL;
19351926
}
1936-
if (str != NULL) {
1937-
tp_doc = (char *)PyObject_MALLOC(n+1);
1938-
if (tp_doc == NULL) {
1939-
Py_DECREF(type);
1940-
return NULL;
1941-
}
1942-
memcpy(tp_doc, str, n+1);
1943-
type->tp_doc = tp_doc;
1927+
n = strlen(str);
1928+
tp_doc = (char *)PyObject_MALLOC(n+1);
1929+
if (tp_doc == NULL) {
1930+
Py_DECREF(type);
1931+
return NULL;
19441932
}
1933+
memcpy(tp_doc, str, n+1);
1934+
type->tp_doc = tp_doc;
19451935
}
19461936
}
19471937

Python/bltinmodule.c

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
4040
}
4141
func = PyTuple_GET_ITEM(args, 0); /* Better be callable */
4242
name = PyTuple_GET_ITEM(args, 1);
43-
if ((!PyString_Check(name) && !PyUnicode_Check(name))) {
43+
if (!PyUnicode_Check(name)) {
4444
PyErr_SetString(PyExc_TypeError,
4545
"__build_class__: name is not a string");
4646
return NULL;
@@ -631,8 +631,7 @@ builtin_exec(PyObject *self, PyObject *args)
631631
}
632632
else if (locals == Py_None)
633633
locals = globals;
634-
if (!PyString_Check(prog) &&
635-
!PyUnicode_Check(prog) &&
634+
if (!PyUnicode_Check(prog) &&
636635
!PyCode_Check(prog)) {
637636
PyErr_Format(PyExc_TypeError,
638637
"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.");
695694
static PyObject *
696695
builtin_getattr(PyObject *self, PyObject *args)
697696
{
698-
PyObject *v, *result, *dflt = NULL, *release = NULL;
697+
PyObject *v, *result, *dflt = NULL;
699698
PyObject *name;
700699

701700
if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
702701
return NULL;
703702

704-
if (PyString_Check(name)) {
705-
release = PyString_AsDecodedObject(name, NULL, NULL);
706-
if (!release)
707-
return NULL;
708-
name = release;
709-
}
710-
711703
if (!PyUnicode_Check(name)) {
712704
PyErr_SetString(PyExc_TypeError,
713705
"getattr(): attribute name must be string");
714-
Py_XDECREF(release);
715706
return NULL;
716707
}
717708
result = PyObject_GetAttr(v, name);
@@ -722,7 +713,6 @@ builtin_getattr(PyObject *self, PyObject *args)
722713
Py_INCREF(dflt);
723714
result = dflt;
724715
}
725-
Py_XDECREF(release);
726716
return result;
727717
}
728718

@@ -1221,17 +1211,15 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
12211211
if (file == NULL || file == Py_None)
12221212
file = PySys_GetObject("stdout");
12231213

1224-
if (sep && sep != Py_None && !PyString_Check(sep) &&
1225-
!PyUnicode_Check(sep)) {
1214+
if (sep && sep != Py_None && !PyUnicode_Check(sep)) {
12261215
PyErr_Format(PyExc_TypeError,
1227-
"sep must be None, str or unicode, not %.200s",
1216+
"sep must be None or a string, not %.200s",
12281217
sep->ob_type->tp_name);
12291218
return NULL;
12301219
}
1231-
if (end && end != Py_None && !PyString_Check(end) &&
1232-
!PyUnicode_Check(end)) {
1220+
if (end && end != Py_None && !PyUnicode_Check(end)) {
12331221
PyErr_Format(PyExc_TypeError,
1234-
"end must be None, str or unicode, not %.200s",
1222+
"end must be None or a string, not %.200s",
12351223
end->ob_type->tp_name);
12361224
return NULL;
12371225
}
@@ -1383,7 +1371,7 @@ builtin_input(PyObject *self, PyObject *args)
13831371
result = NULL;
13841372
}
13851373
else {
1386-
result = PyString_FromStringAndSize(s, len-1);
1374+
result = PyUnicode_FromStringAndSize(s, len-1);
13871375
}
13881376
}
13891377
PyMem_FREE(s);

0 commit comments

Comments
 (0)