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

Skip to content

Commit 80e7f27

Browse files
committed
Use unicode and remove support for some uses of str8.
1 parent da059e3 commit 80e7f27

2 files changed

Lines changed: 20 additions & 43 deletions

File tree

Objects/typeobject.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static int
4444
type_set_name(PyTypeObject *type, PyObject *value, void *context)
4545
{
4646
PyHeapTypeObject* et;
47+
char *tp_name;
4748

4849
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
4950
PyErr_Format(PyExc_TypeError,
@@ -61,11 +62,10 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
6162
type->tp_name, Py_Type(value)->tp_name);
6263
return -1;
6364
}
64-
value = _PyUnicode_AsDefaultEncodedString(value, NULL);
65-
if (value == NULL)
65+
tp_name = PyUnicode_AsString(value);
66+
if (tp_name == NULL)
6667
return -1;
67-
if (strlen(PyString_AS_STRING(value))
68-
!= (size_t)PyString_GET_SIZE(value)) {
68+
if (strlen(tp_name) != (size_t)PyUnicode_GET_SIZE(value)) {
6969
PyErr_Format(PyExc_ValueError,
7070
"__name__ must not contain null bytes");
7171
return -1;
@@ -78,7 +78,7 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
7878
Py_DECREF(et->ht_name);
7979
et->ht_name = value;
8080

81-
type->tp_name = PyString_AS_STRING(value);
81+
type->tp_name = tp_name;
8282

8383
return 0;
8484
}
@@ -1736,7 +1736,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
17361736
/* Have slots */
17371737

17381738
/* Make it into a tuple */
1739-
if (PyString_Check(slots) || PyUnicode_Check(slots))
1739+
if (PyUnicode_Check(slots))
17401740
slots = PyTuple_Pack(1, slots);
17411741
else
17421742
slots = PySequence_Tuple(slots);
@@ -1875,14 +1875,10 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
18751875
type->tp_as_sequence = &et->as_sequence;
18761876
type->tp_as_mapping = &et->as_mapping;
18771877
type->tp_as_buffer = &et->as_buffer;
1878-
if (PyString_Check(name))
1879-
type->tp_name = PyString_AsString(name);
1880-
else {
1881-
type->tp_name = PyUnicode_AsString(name);
1882-
if (!type->tp_name) {
1883-
Py_DECREF(type);
1884-
return NULL;
1885-
}
1878+
type->tp_name = PyUnicode_AsString(name);
1879+
if (!type->tp_name) {
1880+
Py_DECREF(type);
1881+
return NULL;
18861882
}
18871883

18881884
/* Set tp_base and tp_bases */

Python/import.c

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ PyImport_Cleanup(void)
453453
while (PyDict_Next(modules, &pos, &key, &value)) {
454454
if (value->ob_refcnt != 1)
455455
continue;
456-
if (PyString_Check(key) && PyModule_Check(value)) {
457-
name = PyString_AS_STRING(key);
456+
if (PyUnicode_Check(key) && PyModule_Check(value)) {
457+
name = PyUnicode_AsString(key);
458458
if (strcmp(name, "__builtin__") == 0)
459459
continue;
460460
if (strcmp(name, "sys") == 0)
@@ -472,8 +472,8 @@ PyImport_Cleanup(void)
472472
/* Next, delete all modules (still skipping __builtin__ and sys) */
473473
pos = 0;
474474
while (PyDict_Next(modules, &pos, &key, &value)) {
475-
if (PyString_Check(key) && PyModule_Check(value)) {
476-
name = PyString_AS_STRING(key);
475+
if (PyUnicode_Check(key) && PyModule_Check(value)) {
476+
name = PyUnicode_AsString(key);
477477
if (strcmp(name, "__builtin__") == 0)
478478
continue;
479479
if (strcmp(name, "sys") == 0)
@@ -2008,30 +2008,21 @@ get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
20082008
*buf = '\0';
20092009
*p_buflen = 0;
20102010
modname = PyDict_GetItem(globals, namestr);
2011-
if (modname == NULL || (!PyString_Check(modname) && !PyUnicode_Check(modname)))
2011+
if (modname == NULL || !PyUnicode_Check(modname))
20122012
return Py_None;
20132013

2014-
if (PyUnicode_Check(modname)) {
2015-
/* XXX need to support Unicode better */
2016-
modname = _PyUnicode_AsDefaultEncodedString(modname, NULL);
2017-
if (!modname) {
2018-
PyErr_Clear();
2019-
return NULL;
2020-
}
2021-
}
2022-
20232014
modpath = PyDict_GetItem(globals, pathstr);
20242015
if (modpath != NULL) {
2025-
Py_ssize_t len = PyString_GET_SIZE(modname);
2016+
Py_ssize_t len = PyUnicode_GET_SIZE(modname);
20262017
if (len > MAXPATHLEN) {
20272018
PyErr_SetString(PyExc_ValueError,
20282019
"Module name too long");
20292020
return NULL;
20302021
}
2031-
strcpy(buf, PyString_AS_STRING(modname));
2022+
strcpy(buf, PyUnicode_AsString(modname));
20322023
}
20332024
else {
2034-
char *start = PyString_AS_STRING(modname);
2025+
char *start = PyUnicode_AsString(modname);
20352026
char *lastdot = strrchr(start, '.');
20362027
size_t len;
20372028
if (lastdot == NULL && level > 0) {
@@ -2174,19 +2165,9 @@ ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
21742165
}
21752166
return 0;
21762167
}
2177-
if (PyString_Check(item)) {
2178-
/* XXX there shouldn't be any str8 objects here */
2179-
PyObject *uni = PyUnicode_DecodeASCII(PyString_AsString(item),
2180-
PyString_Size(item),
2181-
"strict");
2182-
Py_DECREF(item);
2183-
if (!uni)
2184-
return 0;
2185-
item = uni;
2186-
}
21872168
if (!PyUnicode_Check(item)) {
21882169
PyErr_SetString(PyExc_TypeError,
2189-
"Item in ``from list'' not a unicode string");
2170+
"Item in ``from list'' not a string");
21902171
Py_DECREF(item);
21912172
return 0;
21922173
}
@@ -2444,7 +2425,7 @@ PyImport_ReloadModule(PyObject *m)
24442425
done using whatever import hooks are installed in the current
24452426
environment, e.g. by "rexec".
24462427
A dummy list ["__doc__"] is passed as the 4th argument so that
2447-
e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
2428+
e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
24482429
will return <module "gencache"> instead of <module "win32com">. */
24492430

24502431
PyObject *

0 commit comments

Comments
 (0)