summaryrefslogtreecommitdiffstats
path: root/Python/fileutils.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/fileutils.c')
-rw-r--r--Python/fileutils.c39
1 files changed, 20 insertions, 19 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c
index b7c42e8..6983855 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -16,13 +16,13 @@ extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
PyObject *
_Py_device_encoding(int fd)
{
-#if defined(MS_WINDOWS) || defined(MS_WIN64)
+#if defined(MS_WINDOWS)
UINT cp;
#endif
if (!_PyVerify_fd(fd) || !isatty(fd)) {
Py_RETURN_NONE;
}
-#if defined(MS_WINDOWS) || defined(MS_WIN64)
+#if defined(MS_WINDOWS)
if (fd == 0)
cp = GetConsoleCP();
else if (fd == 1 || fd == 2)
@@ -60,7 +60,7 @@ extern int _Py_normalize_encoding(const char *, char *, size_t);
workaround is also enabled on error, for example if getting the locale
failed.
- Values of locale_is_ascii:
+ Values of force_ascii:
1: the workaround is used: _Py_wchar2char() uses
encode_ascii_surrogateescape() and _Py_char2wchar() uses
@@ -201,7 +201,7 @@ decode_ascii_surrogateescape(const char *arg, size_t *size)
unsigned char *in;
wchar_t *out;
- res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
+ res = PyMem_RawMalloc((strlen(arg)+1)*sizeof(wchar_t));
if (!res)
return NULL;
@@ -229,7 +229,7 @@ decode_ascii_surrogateescape(const char *arg, size_t *size)
Use _Py_wchar2char() to encode the character string back to a byte string.
Return a pointer to a newly allocated wide character string (use
- PyMem_Free() to free the memory) and write the number of written wide
+ PyMem_RawFree() to free the memory) and write the number of written wide
characters excluding the null character into *size if size is not NULL, or
NULL on error (decoding or memory allocation error). If size is not NULL,
*size is set to (size_t)-1 on memory error and (size_t)-2 on decoding
@@ -254,9 +254,9 @@ _Py_char2wchar(const char* arg, size_t *size)
wchar_t *res;
size_t argsize;
size_t count;
+#ifdef HAVE_MBRTOWC
unsigned char *in;
wchar_t *out;
-#ifdef HAVE_MBRTOWC
mbstate_t mbs;
#endif
@@ -283,7 +283,7 @@ _Py_char2wchar(const char* arg, size_t *size)
argsize = mbstowcs(NULL, arg, 0);
#endif
if (argsize != (size_t)-1) {
- res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
+ res = (wchar_t *)PyMem_RawMalloc((argsize+1)*sizeof(wchar_t));
if (!res)
goto oom;
count = mbstowcs(res, arg, argsize+1);
@@ -292,7 +292,7 @@ _Py_char2wchar(const char* arg, size_t *size)
/* Only use the result if it contains no
surrogate characters. */
for (tmp = res; *tmp != 0 &&
- (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
+ !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
;
if (*tmp == 0) {
if (size != NULL)
@@ -300,7 +300,7 @@ _Py_char2wchar(const char* arg, size_t *size)
return res;
}
}
- PyMem_Free(res);
+ PyMem_RawFree(res);
}
/* Conversion failed. Fall back to escaping with surrogateescape. */
#ifdef HAVE_MBRTOWC
@@ -309,7 +309,7 @@ _Py_char2wchar(const char* arg, size_t *size)
/* Overallocate; as multi-byte characters are in the argument, the
actual output could use less memory. */
argsize = strlen(arg) + 1;
- res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
+ res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
if (!res)
goto oom;
in = (unsigned char*)arg;
@@ -325,7 +325,7 @@ _Py_char2wchar(const char* arg, size_t *size)
since we provide everything that we have -
unless there is a bug in the C library, or I
misunderstood how mbrtowc works. */
- PyMem_Free(res);
+ PyMem_RawFree(res);
if (size != NULL)
*size = (size_t)-2;
return NULL;
@@ -338,7 +338,7 @@ _Py_char2wchar(const char* arg, size_t *size)
memset(&mbs, 0, sizeof mbs);
continue;
}
- if (*out >= 0xd800 && *out <= 0xdfff) {
+ if (Py_UNICODE_IS_SURROGATE(*out)) {
/* Surrogate character. Escape the original
byte sequence with surrogateescape. */
argsize -= converted;
@@ -648,12 +648,12 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
return -1;
}
if (bufsiz <= r1) {
- PyMem_Free(wbuf);
+ PyMem_RawFree(wbuf);
errno = EINVAL;
return -1;
}
wcsncpy(buf, wbuf, bufsiz);
- PyMem_Free(wbuf);
+ PyMem_RawFree(wbuf);
return (int)r1;
}
#endif
@@ -689,12 +689,12 @@ _Py_wrealpath(const wchar_t *path,
return NULL;
}
if (resolved_path_size <= r) {
- PyMem_Free(wresolved_path);
+ PyMem_RawFree(wresolved_path);
errno = EINVAL;
return NULL;
}
wcsncpy(resolved_path, wresolved_path, resolved_path_size);
- PyMem_Free(wresolved_path);
+ PyMem_RawFree(wresolved_path);
return resolved_path;
}
#endif
@@ -707,7 +707,8 @@ wchar_t*
_Py_wgetcwd(wchar_t *buf, size_t size)
{
#ifdef MS_WINDOWS
- return _wgetcwd(buf, size);
+ int isize = (int)Py_MIN(size, INT_MAX);
+ return _wgetcwd(buf, isize);
#else
char fname[PATH_MAX];
wchar_t *wname;
@@ -719,11 +720,11 @@ _Py_wgetcwd(wchar_t *buf, size_t size)
if (wname == NULL)
return NULL;
if (size <= len) {
- PyMem_Free(wname);
+ PyMem_RawFree(wname);
return NULL;
}
wcsncpy(buf, wname, size);
- PyMem_Free(wname);
+ PyMem_RawFree(wname);
return buf;
#endif
}