summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-12-21 15:20:32 (GMT)
committerGitHub <noreply@github.com>2017-12-21 15:20:32 (GMT)
commit9dd762013fd9fcf975ad51700b55d050ca9ed60e (patch)
tree67e0bcd6b3884cfa23d983bb722ee492c8c5aad9 /Objects
parent4a02543cf97e8cbf9293741379f977b85531e4c2 (diff)
downloadcpython-9dd762013fd9fcf975ad51700b55d050ca9ed60e.zip
cpython-9dd762013fd9fcf975ad51700b55d050ca9ed60e.tar.gz
cpython-9dd762013fd9fcf975ad51700b55d050ca9ed60e.tar.bz2
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.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 716e352..92a6ad6 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -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;
}