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

Skip to content

Commit beac78b

Browse files
author
Victor Stinner
committed
Use PyUnicode_AsUnicodeAndSize() instead of PyUnicode_GET_SIZE()
1 parent e459a08 commit beac78b

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

Modules/posixmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,10 +2529,9 @@ posix_listdir(PyObject *self, PyObject *args)
25292529
po_wchars = L".";
25302530
len = 1;
25312531
} else {
2532-
po_wchars = PyUnicode_AsUnicode(po);
2532+
po_wchars = PyUnicode_AsUnicodeAndSize(po, &len);
25332533
if (po_wchars == NULL)
25342534
return NULL;
2535-
len = PyUnicode_GET_SIZE(po);
25362535
}
25372536
/* Overallocate for \\*.*\0 */
25382537
wnamebuf = malloc((len + 5) * sizeof(wchar_t));

Python/getargs.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -982,10 +982,11 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
982982
STORE_SIZE(0);
983983
}
984984
else if (PyUnicode_Check(arg)) {
985-
*p = PyUnicode_AS_UNICODE(arg);
985+
Py_ssize_t len;
986+
*p = PyUnicode_AsUnicodeAndSize(arg, &len);
986987
if (*p == NULL)
987988
RETURN_ERR_OCCURRED;
988-
STORE_SIZE(PyUnicode_GET_SIZE(arg));
989+
STORE_SIZE(len);
989990
}
990991
else
991992
return converterr("str or None", arg, msgbuf, bufsize);
@@ -995,10 +996,11 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
995996
if (c == 'Z' && arg == Py_None)
996997
*p = NULL;
997998
else if (PyUnicode_Check(arg)) {
998-
*p = PyUnicode_AS_UNICODE(arg);
999+
Py_ssize_t len;
1000+
*p = PyUnicode_AsUnicodeAndSize(arg, &len);
9991001
if (*p == NULL)
10001002
RETURN_ERR_OCCURRED;
1001-
if (Py_UNICODE_strlen(*p) != PyUnicode_GET_SIZE(arg))
1003+
if (Py_UNICODE_strlen(*p) != len)
10021004
return converterr(
10031005
"str without null character or None",
10041006
arg, msgbuf, bufsize);

Python/import.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,8 @@ case_ok(PyObject *filename, Py_ssize_t prefix_delta, PyObject *name)
22822282
WIN32_FIND_DATAW data;
22832283
HANDLE h;
22842284
int cmp;
2285+
wchar_t *wname;
2286+
Py_ssizet wname_len;
22852287

22862288
if (Py_GETENV("PYTHONCASEOK") != NULL)
22872289
return 1;
@@ -2294,9 +2296,12 @@ case_ok(PyObject *filename, Py_ssize_t prefix_delta, PyObject *name)
22942296
return 0;
22952297
}
22962298
FindClose(h);
2297-
cmp = wcsncmp(data.cFileName,
2298-
PyUnicode_AS_UNICODE(name),
2299-
PyUnicode_GET_SIZE(name));
2299+
2300+
wname = PyUnicode_AsUnicodeAndSize(name, &wname_len);
2301+
if (wname == NULL)
2302+
return -1;
2303+
2304+
cmp = wcsncmp(data.cFileName, wname, wname_len);
23002305
return cmp == 0;
23012306
#elif defined(USE_CASE_OK_BYTES)
23022307
int match;

0 commit comments

Comments
 (0)