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

Skip to content

Commit 21431e8

Browse files
committed
This is the uncontroversial half of patch 1263 by Thomas Lee:
changes to codecs.c and structmember.c to use PyUnicode instead of PyString.
1 parent 630e464 commit 21431e8

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

Python/codecs.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,15 @@ PyObject *normalizestring(const char *string)
5555
size_t len = strlen(string);
5656
char *p;
5757
PyObject *v;
58-
58+
5959
if (len > PY_SSIZE_T_MAX) {
6060
PyErr_SetString(PyExc_OverflowError, "string is too large");
6161
return NULL;
6262
}
63-
64-
v = PyString_FromStringAndSize(NULL, len);
65-
if (v == NULL)
66-
return NULL;
67-
p = PyString_AS_STRING(v);
63+
64+
p = PyMem_Malloc(len + 1);
65+
if (p == NULL)
66+
return NULL;
6867
for (i = 0; i < len; i++) {
6968
register char ch = string[i];
7069
if (ch == ' ')
@@ -73,6 +72,11 @@ PyObject *normalizestring(const char *string)
7372
ch = tolower(Py_CHARMASK(ch));
7473
p[i] = ch;
7574
}
75+
p[i] = '\0';
76+
v = PyUnicode_FromString(p);
77+
if (v == NULL)
78+
return NULL;
79+
PyMem_Free(p);
7680
return v;
7781
}
7882

@@ -112,7 +116,7 @@ PyObject *_PyCodec_Lookup(const char *encoding)
112116
v = normalizestring(encoding);
113117
if (v == NULL)
114118
goto onError;
115-
PyString_InternInPlace(&v);
119+
PyUnicode_InternInPlace(&v);
116120

117121
/* First, try to lookup the name in the registry dictionary */
118122
result = PyDict_GetItem(interp->codec_search_cache, v);
@@ -193,7 +197,7 @@ PyObject *args_tuple(PyObject *object,
193197
if (errors) {
194198
PyObject *v;
195199

196-
v = PyString_FromString(errors);
200+
v = PyUnicode_FromString(errors);
197201
if (v == NULL) {
198202
Py_DECREF(args);
199203
return NULL;

Python/structmember.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ PyMember_GetOne(const char *addr, PyMemberDef *l)
5151
v = Py_None;
5252
}
5353
else
54-
v = PyString_FromString(*(char**)addr);
54+
v = PyUnicode_FromString(*(char**)addr);
5555
break;
5656
case T_STRING_INPLACE:
57-
v = PyString_FromString((char*)addr);
57+
v = PyUnicode_FromString((char*)addr);
5858
break;
5959
case T_CHAR:
60-
v = PyString_FromStringAndSize((char*)addr, 1);
60+
v = PyUnicode_FromStringAndSize((char*)addr, 1);
6161
break;
6262
case T_OBJECT:
6363
v = *(PyObject **)addr;
@@ -225,8 +225,8 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
225225
Py_XDECREF(oldv);
226226
break;
227227
case T_CHAR:
228-
if (PyString_Check(v) && PyString_Size(v) == 1) {
229-
*(char*)addr = PyString_AsString(v)[0];
228+
if (PyUnicode_Check(v) && PyUnicode_GetSize(v) == 1) {
229+
*(char*)addr = PyUnicode_AsString(v)[0];
230230
}
231231
else {
232232
PyErr_BadArgument();

0 commit comments

Comments
 (0)