| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0cfba09 commit 2f02a51
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,7 +10,8 @@ PyAPI_FUNC(wchar_t *) _Py_char2wchar( | |||
| 10 | 10 | size_t *size); | |
| 11 | 11 | ||
| 12 | 12 | PyAPI_FUNC(char*) _Py_wchar2char( | |
| 13 | - const wchar_t *text); | ||
| 13 | + const wchar_t *text, | ||
| 14 | + size_t *error_pos); | ||
| 14 | 15 | ||
| 15 | 16 | #if defined(HAVE_STAT) && !defined(MS_WINDOWS) | |
| 16 | 17 | PyAPI_FUNC(int) _Py_wstat( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -646,7 +646,7 @@ Py_Main(int argc, wchar_t **argv) | |||
| 646 | 646 | if (fp == NULL) { | |
| 647 | 647 | char *cfilename_buffer; | |
| 648 | 648 | const char *cfilename; | |
| 649 | - cfilename_buffer = _Py_wchar2char(filename); | ||
| 649 | + cfilename_buffer = _Py_wchar2char(filename, NULL); | ||
| 650 | 650 | if (cfilename_buffer != NULL) | |
| 651 | 651 | cfilename = cfilename_buffer; | |
| 652 | 652 | else | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1606,14 +1606,31 @@ PyUnicode_EncodeFSDefault(PyObject *unicode) | |||
| 1606 | 1606 | wchar_t *wchar; | |
| 1607 | 1607 | char *bytes; | |
| 1608 | 1608 | PyObject *bytes_obj; | |
| 1609 | + size_t error_pos; | ||
| 1609 | 1610 | ||
| 1610 | 1611 | wchar = PyUnicode_AsWideCharString(unicode, NULL); | |
| 1611 | 1612 | if (wchar == NULL) | |
| 1612 | 1613 | return NULL; | |
| 1613 | - bytes = _Py_wchar2char(wchar); | ||
| 1614 | - PyMem_Free(wchar); | ||
| 1615 | - if (bytes == NULL) | ||
| 1614 | + bytes = _Py_wchar2char(wchar, &error_pos); | ||
| 1615 | + if (bytes == NULL) { | ||
| 1616 | + if (error_pos != (size_t)-1) { | ||
| 1617 | + char *errmsg = strerror(errno); | ||
| 1618 | + PyObject *exc = NULL; | ||
| 1619 | + if (errmsg == NULL) | ||
| 1620 | + errmsg = "Py_wchar2char() failed"; | ||
| 1621 | + raise_encode_exception(&exc, | ||
| 1622 | + "filesystemencoding", | ||
| 1623 | + PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode), | ||
| 1624 | + error_pos, error_pos+1, | ||
| 1625 | + errmsg); | ||
| 1626 | + Py_XDECREF(exc); | ||
| 1627 | + } | ||
| 1628 | + else | ||
| 1629 | + PyErr_NoMemory(); | ||
| 1630 | + PyMem_Free(wchar); | ||
| 1616 | 1631 | return NULL; | |
| 1632 | + } | ||
| 1633 | + PyMem_Free(wchar); | ||
| 1617 | 1634 | ||
| 1618 | 1635 | bytes_obj = PyBytes_FromString(bytes); | |
| 1619 | 1636 | PyMem_Free(bytes); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -132,15 +132,21 @@ _Py_char2wchar(const char* arg, size_t *size) | |||
| 132 | 132 | This function is the reverse of _Py_char2wchar(). | |
| 133 | 133 | ||
| 134 | 134 | Return a pointer to a newly allocated byte string (use PyMem_Free() to free | |
| 135 | - the memory), or NULL on error (conversion error or memory error). */ | ||
| 135 | + the memory), or NULL on conversion or memory allocation error. | ||
| 136 | + | ||
| 137 | + If error_pos is not NULL: *error_pos is the index of the invalid character | ||
| 138 | + on conversion error, or (size_t)-1 otherwise. */ | ||
| 136 | 139 | char* | |
| 137 | - _Py_wchar2char(const wchar_t *text) | ||
| 140 | + _Py_wchar2char(const wchar_t *text, size_t *error_pos) | ||
| 138 | 141 | { | |
| 139 | 142 | const size_t len = wcslen(text); | |
| 140 | 143 | char *result = NULL, *bytes = NULL; | |
| 141 | 144 | size_t i, size, converted; | |
| 142 | 145 | wchar_t c, buf[2]; | |
| 143 | 146 | ||
| 147 | + if (error_pos != NULL) | ||
| 148 | + *error_pos = (size_t)-1; | ||
| 149 | + | ||
| 144 | 150 | /* The function works in two steps: | |
| 145 | 151 | 1. compute the length of the output buffer in bytes (size) | |
| 146 | 152 | 2. outputs the bytes */ | |
@@ -168,6 +174,8 @@ _Py_wchar2char(const wchar_t *text) | |||
| 168 | 174 | if (converted == (size_t)-1) { | |
| 169 | 175 | if (result != NULL) | |
| 170 | 176 | PyMem_Free(result); | |
| 177 | + if (error_pos != NULL) | ||
| 178 | + *error_pos = i; | ||
| 171 | 179 | return NULL; | |
| 172 | 180 | } | |
| 173 | 181 | if (bytes != NULL) { | |
@@ -208,7 +216,7 @@ _Py_wstat(const wchar_t* path, struct stat *buf) | |||
| 208 | 216 | { | |
| 209 | 217 | int err; | |
| 210 | 218 | char *fname; | |
| 211 | - fname = _Py_wchar2char(path); | ||
| 219 | + fname = _Py_wchar2char(path, NULL); | ||
| 212 | 220 | if (fname == NULL) { | |
| 213 | 221 | errno = EINVAL; | |
| 214 | 222 | return -1; | |
@@ -263,7 +271,7 @@ _Py_wfopen(const wchar_t *path, const wchar_t *mode) | |||
| 263 | 271 | errno = EINVAL; | |
| 264 | 272 | return NULL; | |
| 265 | 273 | } | |
| 266 | - cpath = _Py_wchar2char(path); | ||
| 274 | + cpath = _Py_wchar2char(path, NULL); | ||
| 267 | 275 | if (cpath == NULL) | |
| 268 | 276 | return NULL; | |
| 269 | 277 | f = fopen(cpath, cmode); | |
@@ -317,7 +325,7 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) | |||
| 317 | 325 | int res; | |
| 318 | 326 | size_t r1; | |
| 319 | 327 | ||
| 320 | - cpath = _Py_wchar2char(path); | ||
| 328 | + cpath = _Py_wchar2char(path, NULL); | ||
| 321 | 329 | if (cpath == NULL) { | |
| 322 | 330 | errno = EINVAL; | |
| 323 | 331 | return -1; | |
@@ -361,7 +369,7 @@ _Py_wrealpath(const wchar_t *path, | |||
| 361 | 369 | wchar_t *wresolved_path; | |
| 362 | 370 | char *res; | |
| 363 | 371 | size_t r; | |
| 364 | - cpath = _Py_wchar2char(path); | ||
| 372 | + cpath = _Py_wchar2char(path, NULL); | ||
| 365 | 373 | if (cpath == NULL) { | |
| 366 | 374 | errno = EINVAL; | |
| 367 | 375 | return NULL; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments