mirror of
https://github.com/python/cpython.git
synced 2025-07-13 06:15:17 +00:00
bpo-32030: Add _Py_EncodeLocaleRaw() (#4961)
Replace Py_EncodeLocale() with _Py_EncodeLocaleRaw() in: * _Py_wfopen() * _Py_wreadlink() * _Py_wrealpath() * _Py_wstat() * pymain_open_filename() These functions are called early during Python intialization, only the RAW memory allocator must be used.
This commit is contained in:
parent
4a02543cf9
commit
9dd762013f
5 changed files with 101 additions and 41 deletions
|
@ -5158,7 +5158,8 @@ _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size, size_t *p_wlen)
|
|||
On memory allocation failure, return NULL and write (size_t)-1 into
|
||||
*error_pos (if error_pos is set). */
|
||||
char*
|
||||
_Py_EncodeUTF8_surrogateescape(const wchar_t *text, size_t *error_pos)
|
||||
_Py_EncodeUTF8_surrogateescape(const wchar_t *text, size_t *error_pos,
|
||||
int raw_malloc)
|
||||
{
|
||||
const Py_ssize_t max_char_size = 4;
|
||||
Py_ssize_t len = wcslen(text);
|
||||
|
@ -5167,7 +5168,12 @@ _Py_EncodeUTF8_surrogateescape(const wchar_t *text, size_t *error_pos)
|
|||
|
||||
char *bytes;
|
||||
if (len <= PY_SSIZE_T_MAX / max_char_size - 1) {
|
||||
bytes = PyMem_Malloc((len + 1) * max_char_size);
|
||||
if (raw_malloc) {
|
||||
bytes = PyMem_RawMalloc((len + 1) * max_char_size);
|
||||
}
|
||||
else {
|
||||
bytes = PyMem_Malloc((len + 1) * max_char_size);
|
||||
}
|
||||
}
|
||||
else {
|
||||
bytes = NULL;
|
||||
|
@ -5221,7 +5227,13 @@ _Py_EncodeUTF8_surrogateescape(const wchar_t *text, size_t *error_pos)
|
|||
*p++ = '\0';
|
||||
|
||||
size_t final_size = (p - bytes);
|
||||
char *bytes2 = PyMem_Realloc(bytes, final_size);
|
||||
char *bytes2;
|
||||
if (raw_malloc) {
|
||||
bytes2 = PyMem_RawRealloc(bytes, final_size);
|
||||
}
|
||||
else {
|
||||
bytes2 = PyMem_Realloc(bytes, final_size);
|
||||
}
|
||||
if (bytes2 == NULL) {
|
||||
if (error_pos != NULL) {
|
||||
*error_pos = (size_t)-1;
|
||||
|
@ -5231,7 +5243,12 @@ _Py_EncodeUTF8_surrogateescape(const wchar_t *text, size_t *error_pos)
|
|||
return bytes2;
|
||||
|
||||
error:
|
||||
PyMem_Free(bytes);
|
||||
if (raw_malloc) {
|
||||
PyMem_RawFree(bytes);
|
||||
}
|
||||
else {
|
||||
PyMem_Free(bytes);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue