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

Skip to content

Commit 39599dc

Browse files
committed
PyString_AsString is permissive and accepts unicode strings.
Replace it with PyUnicode_AsString when the argument is known to be a str.
1 parent 484fcd4 commit 39599dc

5 files changed

Lines changed: 9 additions & 9 deletions

File tree

Objects/setobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,7 @@ test_c_api(PySetObject *so)
23032303
/* Exercise direct iteration */
23042304
i = 0, count = 0;
23052305
while (_PySet_Next((PyObject *)dup, &i, &x)) {
2306-
s = PyString_AsString(x);
2306+
s = PyUnicode_AsString(x);
23072307
assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
23082308
count++;
23092309
}

Objects/stringobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,7 +3273,7 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
32733273
if (!result)
32743274
return NULL;
32753275

3276-
buf = PyString_AsString(result);
3276+
buf = PyUnicode_AsString(result);
32773277
if (!buf) {
32783278
Py_DECREF(result);
32793279
return NULL;
@@ -3284,7 +3284,7 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
32843284
PyErr_BadInternalCall();
32853285
return NULL;
32863286
}
3287-
llen = PyString_Size(result);
3287+
llen = PyUnicode_GetSize(result);
32883288
if (llen > INT_MAX) {
32893289
PyErr_SetString(PyExc_ValueError,
32903290
"string too large in _PyString_FormatLong");

Python/ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ ast_for_atom(struct compiling *c, const node *n)
12991299
if (errstr) {
13001300
char *s = "";
13011301
char buf[128];
1302-
s = PyString_AsString(errstr);
1302+
s = PyUnicode_AsString(errstr);
13031303
PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
13041304
ast_error(n, buf);
13051305
} else {

Python/getargs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
768768
else if (PyUnicode_Check(arg) &&
769769
PyUnicode_GET_SIZE(arg) == 1 &&
770770
PyUnicode_AS_UNICODE(arg)[0] < 256)
771-
*p = PyUnicode_AS_UNICODE(arg)[0];
771+
*p = (char)PyUnicode_AS_UNICODE(arg)[0];
772772
else
773773
return converterr("char < 256", arg, msgbuf, bufsize);
774774
break;
@@ -823,7 +823,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
823823
}
824824
else
825825
return converterr("string", arg, msgbuf, bufsize);
826-
if ((Py_ssize_t)strlen(*p) != PyString_Size(arg))
826+
if ((Py_ssize_t)strlen(*p) != PyUnicode_GetSize(arg))
827827
return converterr("string without null bytes",
828828
arg, msgbuf, bufsize);
829829
}
@@ -899,7 +899,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
899899
format++;
900900
}
901901
else if (*p != NULL &&
902-
(Py_ssize_t)strlen(*p) != PyString_Size(arg))
902+
(Py_ssize_t)strlen(*p) != PyUnicode_GetSize(arg))
903903
return converterr(
904904
"string without null bytes or None",
905905
arg, msgbuf, bufsize);
@@ -1596,7 +1596,7 @@ vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
15961596
"keywords must be strings");
15971597
return cleanreturn(0, freelist);
15981598
}
1599-
ks = PyString_AsString(key);
1599+
ks = PyUnicode_AsString(key);
16001600
for (i = 0; i < max; i++) {
16011601
if (!strcmp(ks, kwlist[i])) {
16021602
match = 1;

Python/peephole.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
407407
case LOAD_NAME:
408408
case LOAD_GLOBAL:
409409
j = GETARG(codestr, i);
410-
name = PyString_AsString(PyTuple_GET_ITEM(names, j));
410+
name = PyUnicode_AsString(PyTuple_GET_ITEM(names, j));
411411
h = load_global(codestr, i, name, consts);
412412
if (h < 0)
413413
goto exitUnchanged;

0 commit comments

Comments
 (0)