2010-10-07 18:45:39 -03:00
|
|
|
#include "Python.h"
|
2012-11-12 15:14:36 -04:00
|
|
|
#include "osdefs.h"
|
2014-01-20 10:31:08 -04:00
|
|
|
#include <locale.h>
|
|
|
|
|
2010-10-07 19:09:40 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2015-03-06 18:47:02 -04:00
|
|
|
# include <malloc.h>
|
2010-10-07 19:09:40 -03:00
|
|
|
# include <windows.h>
|
2015-04-12 01:26:27 -03:00
|
|
|
extern int winerror_to_errno(int);
|
2010-10-07 19:09:40 -03:00
|
|
|
#endif
|
2010-10-07 18:45:39 -03:00
|
|
|
|
2012-02-29 19:31:31 -04:00
|
|
|
#ifdef HAVE_LANGINFO_H
|
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif /* HAVE_FCNTL_H */
|
|
|
|
|
|
|
|
#ifdef O_CLOEXEC
|
2013-09-07 05:36:04 -03:00
|
|
|
/* Does open() support the O_CLOEXEC flag? Possible values:
|
2013-08-27 19:53:59 -03:00
|
|
|
|
|
|
|
-1: unknown
|
|
|
|
0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
|
|
|
|
1: open() supports O_CLOEXEC flag, close-on-exec is set
|
|
|
|
|
2015-03-17 20:22:14 -03:00
|
|
|
The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO
|
|
|
|
and os.open(). */
|
2013-08-27 19:53:59 -03:00
|
|
|
int _Py_open_cloexec_works = -1;
|
|
|
|
#endif
|
|
|
|
|
2012-02-29 19:31:31 -04:00
|
|
|
PyObject *
|
|
|
|
_Py_device_encoding(int fd)
|
|
|
|
{
|
2013-06-24 19:37:25 -03:00
|
|
|
#if defined(MS_WINDOWS)
|
2012-02-29 19:31:31 -04:00
|
|
|
UINT cp;
|
|
|
|
#endif
|
2015-04-12 01:26:27 -03:00
|
|
|
int valid;
|
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2016-09-08 15:21:54 -03:00
|
|
|
valid = isatty(fd);
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
|
|
|
if (!valid)
|
2012-02-29 19:31:31 -04:00
|
|
|
Py_RETURN_NONE;
|
2015-04-12 01:26:27 -03:00
|
|
|
|
2013-06-24 19:37:25 -03:00
|
|
|
#if defined(MS_WINDOWS)
|
2012-02-29 19:31:31 -04:00
|
|
|
if (fd == 0)
|
|
|
|
cp = GetConsoleCP();
|
|
|
|
else if (fd == 1 || fd == 2)
|
|
|
|
cp = GetConsoleOutputCP();
|
|
|
|
else
|
|
|
|
cp = 0;
|
|
|
|
/* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
|
|
|
|
has no console */
|
|
|
|
if (cp != 0)
|
|
|
|
return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
|
|
|
|
#elif defined(CODESET)
|
|
|
|
{
|
|
|
|
char *codeset = nl_langinfo(CODESET);
|
|
|
|
if (codeset != NULL && codeset[0] != 0)
|
|
|
|
return PyUnicode_FromString(codeset);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
#if !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS)
|
|
|
|
|
|
|
|
#define USE_FORCE_ASCII
|
|
|
|
|
2012-12-03 20:34:47 -04:00
|
|
|
extern int _Py_normalize_encoding(const char *, char *, size_t);
|
|
|
|
|
|
|
|
/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale.
|
|
|
|
On these operating systems, nl_langinfo(CODESET) announces an alias of the
|
|
|
|
ASCII encoding, whereas mbstowcs() and wcstombs() functions use the
|
|
|
|
ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use
|
|
|
|
locale.getpreferredencoding() codec. For example, if command line arguments
|
|
|
|
are decoded by mbstowcs() and encoded back by os.fsencode(), we get a
|
|
|
|
UnicodeEncodeError instead of retrieving the original byte string.
|
|
|
|
|
|
|
|
The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C",
|
|
|
|
nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least
|
|
|
|
one byte in range 0x80-0xff can be decoded from the locale encoding. The
|
|
|
|
workaround is also enabled on error, for example if getting the locale
|
|
|
|
failed.
|
|
|
|
|
2013-01-15 17:24:12 -04:00
|
|
|
Values of force_ascii:
|
2012-12-03 20:34:47 -04:00
|
|
|
|
2014-08-01 07:28:48 -03:00
|
|
|
1: the workaround is used: Py_EncodeLocale() uses
|
|
|
|
encode_ascii_surrogateescape() and Py_DecodeLocale() uses
|
2018-01-15 05:45:49 -04:00
|
|
|
decode_ascii()
|
2014-08-01 07:28:48 -03:00
|
|
|
0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and
|
|
|
|
Py_DecodeLocale() uses mbstowcs()
|
2012-12-03 20:34:47 -04:00
|
|
|
-1: unknown, need to call check_force_ascii() to get the value
|
|
|
|
*/
|
|
|
|
static int force_ascii = -1;
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_force_ascii(void)
|
|
|
|
{
|
|
|
|
char *loc;
|
|
|
|
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
|
|
|
|
char *codeset, **alias;
|
2016-09-10 03:11:52 -03:00
|
|
|
char encoding[20]; /* longest name: "iso_646.irv_1991\0" */
|
2012-12-03 20:34:47 -04:00
|
|
|
int is_ascii;
|
|
|
|
unsigned int i;
|
|
|
|
char* ascii_aliases[] = {
|
|
|
|
"ascii",
|
2016-09-10 03:11:52 -03:00
|
|
|
/* Aliases from Lib/encodings/aliases.py */
|
2012-12-03 20:34:47 -04:00
|
|
|
"646",
|
2016-09-10 03:11:52 -03:00
|
|
|
"ansi_x3.4_1968",
|
|
|
|
"ansi_x3.4_1986",
|
|
|
|
"ansi_x3_4_1968",
|
2012-12-03 20:34:47 -04:00
|
|
|
"cp367",
|
|
|
|
"csascii",
|
|
|
|
"ibm367",
|
2016-09-10 03:11:52 -03:00
|
|
|
"iso646_us",
|
|
|
|
"iso_646.irv_1991",
|
|
|
|
"iso_ir_6",
|
2012-12-03 20:34:47 -04:00
|
|
|
"us",
|
2016-09-10 03:11:52 -03:00
|
|
|
"us_ascii",
|
2012-12-03 20:34:47 -04:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
loc = setlocale(LC_CTYPE, NULL);
|
|
|
|
if (loc == NULL)
|
|
|
|
goto error;
|
2018-08-28 08:51:20 -03:00
|
|
|
if (strcmp(loc, "C") != 0 && strcmp(loc, "POSIX") != 0) {
|
2012-12-03 20:34:47 -04:00
|
|
|
/* the LC_CTYPE locale is different than C */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
|
|
|
|
codeset = nl_langinfo(CODESET);
|
|
|
|
if (!codeset || codeset[0] == '\0') {
|
|
|
|
/* CODESET is not set or empty */
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
is_ascii = 0;
|
|
|
|
for (alias=ascii_aliases; *alias != NULL; alias++) {
|
|
|
|
if (strcmp(encoding, *alias) == 0) {
|
|
|
|
is_ascii = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_ascii) {
|
|
|
|
/* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0x80; i<0xff; i++) {
|
|
|
|
unsigned char ch;
|
|
|
|
wchar_t wch;
|
|
|
|
size_t res;
|
|
|
|
|
|
|
|
ch = (unsigned char)i;
|
|
|
|
res = mbstowcs(&wch, (char*)&ch, 1);
|
|
|
|
if (res != (size_t)-1) {
|
|
|
|
/* decoding a non-ASCII character from the locale encoding succeed:
|
|
|
|
the locale encoding is not ASCII, force ASCII */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* None of the bytes in the range 0x80-0xff can be decoded from the locale
|
|
|
|
encoding: the locale encoding is really ASCII */
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
/* nl_langinfo(CODESET) is not available: always force ASCII */
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
error:
|
2016-05-26 02:35:26 -03:00
|
|
|
/* if an error occurred, force the ASCII encoding */
|
2012-12-03 20:34:47 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-30 08:59:20 -03:00
|
|
|
|
|
|
|
int
|
|
|
|
_Py_GetForceASCII(void)
|
|
|
|
{
|
|
|
|
if (force_ascii == -1) {
|
|
|
|
force_ascii = check_force_ascii();
|
|
|
|
}
|
|
|
|
return force_ascii;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-23 08:37:42 -04:00
|
|
|
void
|
|
|
|
_Py_ResetForceASCII(void)
|
|
|
|
{
|
|
|
|
force_ascii = -1;
|
|
|
|
}
|
|
|
|
|
2018-10-30 08:59:20 -03:00
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
static int
|
|
|
|
encode_ascii(const wchar_t *text, char **str,
|
|
|
|
size_t *error_pos, const char **reason,
|
|
|
|
int raw_malloc, int surrogateescape)
|
2012-12-03 20:34:47 -04:00
|
|
|
{
|
|
|
|
char *result = NULL, *out;
|
|
|
|
size_t len, i;
|
|
|
|
wchar_t ch;
|
|
|
|
|
|
|
|
len = wcslen(text);
|
|
|
|
|
2017-12-21 11:49:13 -04:00
|
|
|
/* +1 for NULL byte */
|
2017-12-21 11:20:32 -04:00
|
|
|
if (raw_malloc) {
|
|
|
|
result = PyMem_RawMalloc(len + 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = PyMem_Malloc(len + 1);
|
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
if (result == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-03 20:34:47 -04:00
|
|
|
|
|
|
|
out = result;
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
ch = text[i];
|
|
|
|
|
|
|
|
if (ch <= 0x7f) {
|
|
|
|
/* ASCII character */
|
|
|
|
*out++ = (char)ch;
|
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
else if (surrogateescape && 0xdc80 <= ch && ch <= 0xdcff) {
|
2012-12-03 20:34:47 -04:00
|
|
|
/* UTF-8b surrogate */
|
|
|
|
*out++ = (char)(ch - 0xdc00);
|
|
|
|
}
|
|
|
|
else {
|
2017-12-21 11:20:32 -04:00
|
|
|
if (raw_malloc) {
|
|
|
|
PyMem_RawFree(result);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyMem_Free(result);
|
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
if (error_pos != NULL) {
|
|
|
|
*error_pos = i;
|
|
|
|
}
|
|
|
|
if (reason) {
|
|
|
|
*reason = "encoding error";
|
|
|
|
}
|
|
|
|
return -2;
|
2012-12-03 20:34:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*out = '\0';
|
2018-01-15 05:45:49 -04:00
|
|
|
*str = result;
|
|
|
|
return 0;
|
2012-12-03 20:34:47 -04:00
|
|
|
}
|
2018-10-30 10:32:01 -03:00
|
|
|
#else
|
|
|
|
int
|
|
|
|
_Py_GetForceASCII(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-23 08:37:42 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
_Py_ResetForceASCII(void)
|
|
|
|
{
|
|
|
|
/* nothing to do */
|
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
#endif /* !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS) */
|
2012-12-03 20:34:47 -04:00
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
|
|
|
|
#if !defined(HAVE_MBRTOWC) || defined(USE_FORCE_ASCII)
|
|
|
|
static int
|
|
|
|
decode_ascii(const char *arg, wchar_t **wstr, size_t *wlen,
|
|
|
|
const char **reason, int surrogateescape)
|
2012-12-03 20:34:47 -04:00
|
|
|
{
|
|
|
|
wchar_t *res;
|
|
|
|
unsigned char *in;
|
|
|
|
wchar_t *out;
|
2015-01-04 18:03:17 -04:00
|
|
|
size_t argsize = strlen(arg) + 1;
|
2012-12-03 20:34:47 -04:00
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res = PyMem_RawMalloc(argsize * sizeof(wchar_t));
|
|
|
|
if (!res) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-03 20:34:47 -04:00
|
|
|
|
|
|
|
out = res;
|
2018-01-15 05:45:49 -04:00
|
|
|
for (in = (unsigned char*)arg; *in; in++) {
|
|
|
|
unsigned char ch = *in;
|
|
|
|
if (ch < 128) {
|
|
|
|
*out++ = ch;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!surrogateescape) {
|
|
|
|
PyMem_RawFree(res);
|
|
|
|
if (wlen) {
|
|
|
|
*wlen = in - (unsigned char*)arg;
|
|
|
|
}
|
|
|
|
if (reason) {
|
|
|
|
*reason = "decoding error";
|
|
|
|
}
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
*out++ = 0xdc00 + ch;
|
|
|
|
}
|
|
|
|
}
|
2012-12-03 20:34:47 -04:00
|
|
|
*out = 0;
|
2018-01-15 05:45:49 -04:00
|
|
|
|
|
|
|
if (wlen != NULL) {
|
|
|
|
*wlen = out - res;
|
|
|
|
}
|
|
|
|
*wstr = res;
|
|
|
|
return 0;
|
2012-12-03 20:34:47 -04:00
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
#endif /* !HAVE_MBRTOWC */
|
2012-12-03 20:34:47 -04:00
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
static int
|
|
|
|
decode_current_locale(const char* arg, wchar_t **wstr, size_t *wlen,
|
|
|
|
const char **reason, int surrogateescape)
|
2010-10-07 18:45:39 -03:00
|
|
|
{
|
|
|
|
wchar_t *res;
|
2012-12-03 20:34:47 -04:00
|
|
|
size_t argsize;
|
|
|
|
size_t count;
|
2013-05-07 18:48:56 -03:00
|
|
|
#ifdef HAVE_MBRTOWC
|
2012-12-03 20:34:47 -04:00
|
|
|
unsigned char *in;
|
|
|
|
wchar_t *out;
|
|
|
|
mbstate_t mbs;
|
|
|
|
#endif
|
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
#ifdef HAVE_BROKEN_MBSTOWCS
|
|
|
|
/* Some platforms have a broken implementation of
|
|
|
|
* mbstowcs which does not count the characters that
|
|
|
|
* would result from conversion. Use an upper bound.
|
|
|
|
*/
|
2012-12-03 20:34:47 -04:00
|
|
|
argsize = strlen(arg);
|
2010-10-07 18:45:39 -03:00
|
|
|
#else
|
2012-12-03 20:34:47 -04:00
|
|
|
argsize = mbstowcs(NULL, arg, 0);
|
2010-10-07 18:45:39 -03:00
|
|
|
#endif
|
|
|
|
if (argsize != (size_t)-1) {
|
2018-01-15 05:45:49 -04:00
|
|
|
if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res = (wchar_t *)PyMem_RawMalloc((argsize + 1) * sizeof(wchar_t));
|
|
|
|
if (!res) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = mbstowcs(res, arg, argsize + 1);
|
2010-10-07 18:45:39 -03:00
|
|
|
if (count != (size_t)-1) {
|
|
|
|
wchar_t *tmp;
|
|
|
|
/* Only use the result if it contains no
|
|
|
|
surrogate characters. */
|
|
|
|
for (tmp = res; *tmp != 0 &&
|
2012-10-29 21:42:39 -03:00
|
|
|
!Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
|
2010-10-07 18:45:39 -03:00
|
|
|
;
|
2010-10-16 20:16:16 -03:00
|
|
|
if (*tmp == 0) {
|
2018-01-15 05:45:49 -04:00
|
|
|
if (wlen != NULL) {
|
|
|
|
*wlen = count;
|
|
|
|
}
|
|
|
|
*wstr = res;
|
|
|
|
return 0;
|
2010-10-16 20:16:16 -03:00
|
|
|
}
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
2013-07-07 11:25:15 -03:00
|
|
|
PyMem_RawFree(res);
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
/* Conversion failed. Fall back to escaping with surrogateescape. */
|
|
|
|
#ifdef HAVE_MBRTOWC
|
|
|
|
/* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
|
|
|
|
|
|
|
|
/* Overallocate; as multi-byte characters are in the argument, the
|
|
|
|
actual output could use less memory. */
|
|
|
|
argsize = strlen(arg) + 1;
|
2018-01-15 05:45:49 -04:00
|
|
|
if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res = (wchar_t*)PyMem_RawMalloc(argsize * sizeof(wchar_t));
|
|
|
|
if (!res) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
in = (unsigned char*)arg;
|
|
|
|
out = res;
|
|
|
|
memset(&mbs, 0, sizeof mbs);
|
|
|
|
while (argsize) {
|
|
|
|
size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
|
2018-01-15 05:45:49 -04:00
|
|
|
if (converted == 0) {
|
2010-10-07 18:45:39 -03:00
|
|
|
/* Reached end of string; null char stored. */
|
|
|
|
break;
|
2018-01-15 05:45:49 -04:00
|
|
|
}
|
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
if (converted == (size_t)-2) {
|
|
|
|
/* Incomplete character. This should never happen,
|
|
|
|
since we provide everything that we have -
|
|
|
|
unless there is a bug in the C library, or I
|
|
|
|
misunderstood how mbrtowc works. */
|
2018-01-15 05:45:49 -04:00
|
|
|
goto decode_error;
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
if (converted == (size_t)-1) {
|
2018-01-15 05:45:49 -04:00
|
|
|
if (!surrogateescape) {
|
|
|
|
goto decode_error;
|
|
|
|
}
|
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
/* Conversion error. Escape as UTF-8b, and start over
|
|
|
|
in the initial shift state. */
|
|
|
|
*out++ = 0xdc00 + *in++;
|
|
|
|
argsize--;
|
|
|
|
memset(&mbs, 0, sizeof mbs);
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
|
2012-10-29 21:42:39 -03:00
|
|
|
if (Py_UNICODE_IS_SURROGATE(*out)) {
|
2018-01-15 05:45:49 -04:00
|
|
|
if (!surrogateescape) {
|
|
|
|
goto decode_error;
|
|
|
|
}
|
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
/* Surrogate character. Escape the original
|
|
|
|
byte sequence with surrogateescape. */
|
|
|
|
argsize -= converted;
|
2018-01-15 05:45:49 -04:00
|
|
|
while (converted--) {
|
2010-10-07 18:45:39 -03:00
|
|
|
*out++ = 0xdc00 + *in++;
|
2018-01-15 05:45:49 -04:00
|
|
|
}
|
2010-10-07 18:45:39 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* successfully converted some bytes */
|
|
|
|
in += converted;
|
|
|
|
argsize -= converted;
|
|
|
|
out++;
|
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
if (wlen != NULL) {
|
|
|
|
*wlen = out - res;
|
|
|
|
}
|
|
|
|
*wstr = res;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
decode_error:
|
|
|
|
PyMem_RawFree(res);
|
|
|
|
if (wlen) {
|
|
|
|
*wlen = in - (unsigned char*)arg;
|
|
|
|
}
|
|
|
|
if (reason) {
|
|
|
|
*reason = "decoding error";
|
|
|
|
}
|
|
|
|
return -2;
|
2012-11-12 18:04:02 -04:00
|
|
|
#else /* HAVE_MBRTOWC */
|
2010-10-07 18:45:39 -03:00
|
|
|
/* Cannot use C locale for escaping; manually escape as if charset
|
|
|
|
is ASCII (i.e. escape all bytes > 128. This will still roundtrip
|
|
|
|
correctly in the locale's charset, which must be an ASCII superset. */
|
2018-01-15 05:45:49 -04:00
|
|
|
return decode_ascii(arg, wstr, wlen, reason, surrogateescape);
|
2012-11-12 18:04:02 -04:00
|
|
|
#endif /* HAVE_MBRTOWC */
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
/* Decode a byte string from the locale encoding.
|
|
|
|
|
|
|
|
Use the strict error handler if 'surrogateescape' is zero. Use the
|
|
|
|
surrogateescape error handler if 'surrogateescape' is non-zero: undecodable
|
|
|
|
bytes are decoded as characters in range U+DC80..U+DCFF. If a byte sequence
|
|
|
|
can be decoded as a surrogate character, escape the bytes using the
|
|
|
|
surrogateescape error handler instead of decoding them.
|
|
|
|
|
2018-04-20 18:00:41 -03:00
|
|
|
On success, return 0 and write the newly allocated wide character string into
|
2018-01-15 05:45:49 -04:00
|
|
|
*wstr (use PyMem_RawFree() to free the memory). If wlen is not NULL, write
|
|
|
|
the number of wide characters excluding the null character into *wlen.
|
|
|
|
|
|
|
|
On memory allocation failure, return -1.
|
|
|
|
|
|
|
|
On decoding error, return -2. If wlen is not NULL, write the start of
|
|
|
|
invalid byte sequence in the input string into *wlen. If reason is not NULL,
|
|
|
|
write the decoding error message into *reason.
|
|
|
|
|
|
|
|
Use the Py_EncodeLocaleEx() function to encode the character string back to
|
|
|
|
a byte string. */
|
|
|
|
int
|
|
|
|
_Py_DecodeLocaleEx(const char* arg, wchar_t **wstr, size_t *wlen,
|
|
|
|
const char **reason,
|
|
|
|
int current_locale, int surrogateescape)
|
2010-10-07 18:45:39 -03:00
|
|
|
{
|
2018-01-15 05:45:49 -04:00
|
|
|
if (current_locale) {
|
2018-01-22 14:07:32 -04:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
|
|
|
|
surrogateescape);
|
|
|
|
#else
|
2018-01-15 05:45:49 -04:00
|
|
|
return decode_current_locale(arg, wstr, wlen, reason, surrogateescape);
|
2018-01-22 14:07:32 -04:00
|
|
|
#endif
|
2018-01-15 05:45:49 -04:00
|
|
|
}
|
|
|
|
|
2016-12-15 15:59:58 -04:00
|
|
|
#if defined(__APPLE__) || defined(__ANDROID__)
|
2018-01-15 05:45:49 -04:00
|
|
|
return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
|
|
|
|
surrogateescape);
|
2017-12-13 07:29:09 -04:00
|
|
|
#else
|
2018-01-15 05:45:49 -04:00
|
|
|
if (Py_UTF8Mode == 1) {
|
|
|
|
return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
|
|
|
|
surrogateescape);
|
2017-12-13 07:29:09 -04:00
|
|
|
}
|
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
#ifdef USE_FORCE_ASCII
|
|
|
|
if (force_ascii == -1) {
|
2017-12-13 07:29:09 -04:00
|
|
|
force_ascii = check_force_ascii();
|
2018-01-15 05:45:49 -04:00
|
|
|
}
|
2017-12-13 07:29:09 -04:00
|
|
|
|
|
|
|
if (force_ascii) {
|
|
|
|
/* force ASCII encoding to workaround mbstowcs() issue */
|
2018-01-15 05:45:49 -04:00
|
|
|
return decode_ascii(arg, wstr, wlen, reason, surrogateescape);
|
2017-12-13 07:29:09 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
return decode_current_locale(arg, wstr, wlen, reason, surrogateescape);
|
2017-12-13 07:29:09 -04:00
|
|
|
#endif /* __APPLE__ or __ANDROID__ */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-10 17:46:15 -04:00
|
|
|
/* Decode a byte string from the locale encoding with the
|
|
|
|
surrogateescape error handler: undecodable bytes are decoded as characters
|
|
|
|
in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
|
|
|
|
character, escape the bytes using the surrogateescape error handler instead
|
|
|
|
of decoding them.
|
|
|
|
|
|
|
|
Return a pointer to a newly allocated wide character string, use
|
|
|
|
PyMem_RawFree() to free the memory. If size is not NULL, write the number of
|
|
|
|
wide characters excluding the null character into *size
|
|
|
|
|
|
|
|
Return NULL on decoding error or memory allocation error. If *size* is not
|
|
|
|
NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
|
|
|
|
decoding error.
|
|
|
|
|
|
|
|
Decoding errors should never happen, unless there is a bug in the C
|
|
|
|
library.
|
|
|
|
|
|
|
|
Use the Py_EncodeLocale() function to encode the character string back to a
|
|
|
|
byte string. */
|
|
|
|
wchar_t*
|
2018-01-15 05:45:49 -04:00
|
|
|
Py_DecodeLocale(const char* arg, size_t *wlen)
|
2018-01-10 17:46:15 -04:00
|
|
|
{
|
2018-01-15 05:45:49 -04:00
|
|
|
wchar_t *wstr;
|
|
|
|
int res = _Py_DecodeLocaleEx(arg, &wstr, wlen, NULL, 0, 1);
|
|
|
|
if (res != 0) {
|
|
|
|
if (wlen != NULL) {
|
|
|
|
*wlen = (size_t)res;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return wstr;
|
2018-01-10 17:46:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
static int
|
|
|
|
encode_current_locale(const wchar_t *text, char **str,
|
|
|
|
size_t *error_pos, const char **reason,
|
|
|
|
int raw_malloc, int surrogateescape)
|
2017-12-13 07:29:09 -04:00
|
|
|
{
|
2010-10-07 18:45:39 -03:00
|
|
|
const size_t len = wcslen(text);
|
|
|
|
char *result = NULL, *bytes = NULL;
|
|
|
|
size_t i, size, converted;
|
|
|
|
wchar_t c, buf[2];
|
|
|
|
|
|
|
|
/* The function works in two steps:
|
|
|
|
1. compute the length of the output buffer in bytes (size)
|
|
|
|
2. outputs the bytes */
|
|
|
|
size = 0;
|
|
|
|
buf[1] = 0;
|
|
|
|
while (1) {
|
|
|
|
for (i=0; i < len; i++) {
|
|
|
|
c = text[i];
|
|
|
|
if (c >= 0xdc80 && c <= 0xdcff) {
|
2018-01-15 05:45:49 -04:00
|
|
|
if (!surrogateescape) {
|
|
|
|
goto encode_error;
|
|
|
|
}
|
2010-10-07 18:45:39 -03:00
|
|
|
/* UTF-8b surrogate */
|
|
|
|
if (bytes != NULL) {
|
|
|
|
*bytes++ = c - 0xdc00;
|
|
|
|
size--;
|
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
else {
|
2010-10-07 18:45:39 -03:00
|
|
|
size++;
|
2018-01-15 05:45:49 -04:00
|
|
|
}
|
2010-10-07 18:45:39 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buf[0] = c;
|
2018-01-15 05:45:49 -04:00
|
|
|
if (bytes != NULL) {
|
2010-10-07 18:45:39 -03:00
|
|
|
converted = wcstombs(bytes, buf, size);
|
2018-01-15 05:45:49 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-10-07 18:45:39 -03:00
|
|
|
converted = wcstombs(NULL, buf, 0);
|
2018-01-15 05:45:49 -04:00
|
|
|
}
|
2010-10-07 18:45:39 -03:00
|
|
|
if (converted == (size_t)-1) {
|
2018-01-15 05:45:49 -04:00
|
|
|
goto encode_error;
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
|
|
|
if (bytes != NULL) {
|
|
|
|
bytes += converted;
|
|
|
|
size -= converted;
|
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
else {
|
2010-10-07 18:45:39 -03:00
|
|
|
size += converted;
|
2018-01-15 05:45:49 -04:00
|
|
|
}
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result != NULL) {
|
2012-12-03 20:34:47 -04:00
|
|
|
*bytes = '\0';
|
2010-10-07 18:45:39 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size += 1; /* nul byte at the end */
|
2017-12-21 11:20:32 -04:00
|
|
|
if (raw_malloc) {
|
|
|
|
result = PyMem_RawMalloc(size);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = PyMem_Malloc(size);
|
|
|
|
}
|
2012-11-12 18:32:21 -04:00
|
|
|
if (result == NULL) {
|
2018-01-15 05:45:49 -04:00
|
|
|
return -1;
|
2012-11-12 18:32:21 -04:00
|
|
|
}
|
2010-10-07 18:45:39 -03:00
|
|
|
bytes = result;
|
|
|
|
}
|
2018-01-15 05:45:49 -04:00
|
|
|
*str = result;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
encode_error:
|
|
|
|
if (raw_malloc) {
|
|
|
|
PyMem_RawFree(result);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyMem_Free(result);
|
|
|
|
}
|
|
|
|
if (error_pos != NULL) {
|
|
|
|
*error_pos = i;
|
|
|
|
}
|
|
|
|
if (reason) {
|
|
|
|
*reason = "encoding error";
|
|
|
|
}
|
|
|
|
return -2;
|
2017-12-13 07:29:09 -04:00
|
|
|
}
|
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
static int
|
|
|
|
encode_locale_ex(const wchar_t *text, char **str, size_t *error_pos,
|
|
|
|
const char **reason,
|
|
|
|
int raw_malloc, int current_locale, int surrogateescape)
|
2017-12-21 11:20:32 -04:00
|
|
|
{
|
2018-01-15 05:45:49 -04:00
|
|
|
if (current_locale) {
|
2018-01-22 14:07:32 -04:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
|
|
|
|
raw_malloc, surrogateescape);
|
|
|
|
#else
|
2018-01-15 05:45:49 -04:00
|
|
|
return encode_current_locale(text, str, error_pos, reason,
|
|
|
|
raw_malloc, surrogateescape);
|
2018-01-22 14:07:32 -04:00
|
|
|
#endif
|
2018-01-15 05:45:49 -04:00
|
|
|
}
|
|
|
|
|
2017-12-21 11:20:32 -04:00
|
|
|
#if defined(__APPLE__) || defined(__ANDROID__)
|
2018-01-15 05:45:49 -04:00
|
|
|
return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
|
|
|
|
raw_malloc, surrogateescape);
|
2017-12-21 11:20:32 -04:00
|
|
|
#else /* __APPLE__ */
|
2018-01-15 05:45:49 -04:00
|
|
|
if (Py_UTF8Mode == 1) {
|
|
|
|
return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
|
|
|
|
raw_malloc, surrogateescape);
|
2017-12-21 11:20:32 -04:00
|
|
|
}
|
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
#ifdef USE_FORCE_ASCII
|
|
|
|
if (force_ascii == -1) {
|
2017-12-21 11:20:32 -04:00
|
|
|
force_ascii = check_force_ascii();
|
2018-01-15 05:45:49 -04:00
|
|
|
}
|
2017-12-21 11:20:32 -04:00
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
if (force_ascii) {
|
|
|
|
return encode_ascii(text, str, error_pos, reason,
|
|
|
|
raw_malloc, surrogateescape);
|
|
|
|
}
|
2017-12-21 11:20:32 -04:00
|
|
|
#endif
|
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
return encode_current_locale(text, str, error_pos, reason,
|
|
|
|
raw_malloc, surrogateescape);
|
2017-12-21 11:20:32 -04:00
|
|
|
#endif /* __APPLE__ or __ANDROID__ */
|
|
|
|
}
|
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
static char*
|
|
|
|
encode_locale(const wchar_t *text, size_t *error_pos,
|
|
|
|
int raw_malloc, int current_locale)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
int res = encode_locale_ex(text, &str, error_pos, NULL,
|
|
|
|
raw_malloc, current_locale, 1);
|
|
|
|
if (res != -2 && error_pos) {
|
|
|
|
*error_pos = (size_t)-1;
|
|
|
|
}
|
|
|
|
if (res != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-12-13 07:29:09 -04:00
|
|
|
/* Encode a wide character string to the locale encoding with the
|
|
|
|
surrogateescape error handler: surrogate characters in the range
|
|
|
|
U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
|
|
|
|
|
|
|
|
Return a pointer to a newly allocated byte string, use PyMem_Free() to free
|
|
|
|
the memory. Return NULL on encoding or memory allocation error.
|
|
|
|
|
|
|
|
If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set
|
|
|
|
to the index of the invalid character on encoding error.
|
|
|
|
|
|
|
|
Use the Py_DecodeLocale() function to decode the bytes string back to a wide
|
|
|
|
character string. */
|
|
|
|
char*
|
|
|
|
Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
|
|
|
|
{
|
2018-01-10 17:46:15 -04:00
|
|
|
return encode_locale(text, error_pos, 0, 0);
|
2017-12-21 11:20:32 -04:00
|
|
|
}
|
2017-12-13 07:29:09 -04:00
|
|
|
|
|
|
|
|
2017-12-21 11:20:32 -04:00
|
|
|
/* Similar to Py_EncodeLocale(), but result must be freed by PyMem_RawFree()
|
|
|
|
instead of PyMem_Free(). */
|
|
|
|
char*
|
|
|
|
_Py_EncodeLocaleRaw(const wchar_t *text, size_t *error_pos)
|
|
|
|
{
|
2018-01-10 17:46:15 -04:00
|
|
|
return encode_locale(text, error_pos, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-15 05:45:49 -04:00
|
|
|
int
|
|
|
|
_Py_EncodeLocaleEx(const wchar_t *text, char **str,
|
|
|
|
size_t *error_pos, const char **reason,
|
|
|
|
int current_locale, int surrogateescape)
|
2018-01-10 17:46:15 -04:00
|
|
|
{
|
2018-01-15 05:45:49 -04:00
|
|
|
return encode_locale_ex(text, str, error_pos, reason, 1,
|
|
|
|
current_locale, surrogateescape);
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
|
|
|
|
2010-10-07 19:53:43 -03:00
|
|
|
|
2015-02-21 12:44:05 -04:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
|
|
|
|
|
|
|
|
static void
|
|
|
|
FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
|
|
|
|
{
|
|
|
|
/* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
|
|
|
|
/* Cannot simply cast and dereference in_ptr,
|
|
|
|
since it might not be aligned properly */
|
|
|
|
__int64 in;
|
|
|
|
memcpy(&in, in_ptr, sizeof(in));
|
|
|
|
*nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
|
|
|
|
*time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-21 19:26:02 -04:00
|
|
|
_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
|
2015-02-21 12:44:05 -04:00
|
|
|
{
|
|
|
|
/* XXX endianness */
|
|
|
|
__int64 out;
|
|
|
|
out = time_in + secs_between_epochs;
|
|
|
|
out = out * 10000000 + nsec_in / 100;
|
|
|
|
memcpy(out_ptr, &out, sizeof(out));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Below, we *know* that ugo+r is 0444 */
|
|
|
|
#if _S_IREAD != 0400
|
|
|
|
#error Unsupported C library
|
|
|
|
#endif
|
|
|
|
static int
|
|
|
|
attributes_to_mode(DWORD attr)
|
|
|
|
{
|
|
|
|
int m = 0;
|
|
|
|
if (attr & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
|
|
|
|
else
|
|
|
|
m |= _S_IFREG;
|
|
|
|
if (attr & FILE_ATTRIBUTE_READONLY)
|
|
|
|
m |= 0444;
|
|
|
|
else
|
|
|
|
m |= 0666;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2015-02-21 19:26:02 -04:00
|
|
|
void
|
2015-03-30 05:09:31 -03:00
|
|
|
_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
|
|
|
|
struct _Py_stat_struct *result)
|
2015-02-21 12:44:05 -04:00
|
|
|
{
|
|
|
|
memset(result, 0, sizeof(*result));
|
|
|
|
result->st_mode = attributes_to_mode(info->dwFileAttributes);
|
|
|
|
result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
|
|
|
|
result->st_dev = info->dwVolumeSerialNumber;
|
|
|
|
result->st_rdev = result->st_dev;
|
|
|
|
FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
|
|
|
|
FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
|
|
|
|
FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
|
|
|
|
result->st_nlink = info->nNumberOfLinks;
|
2017-03-09 12:34:28 -04:00
|
|
|
result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow;
|
2015-02-21 12:44:05 -04:00
|
|
|
if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
|
|
|
|
/* first clear the S_IFMT bits */
|
|
|
|
result->st_mode ^= (result->st_mode & S_IFMT);
|
|
|
|
/* now set the bits that make this a symlink */
|
|
|
|
result->st_mode |= S_IFLNK;
|
|
|
|
}
|
|
|
|
result->st_file_attributes = info->dwFileAttributes;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Return information about a file.
|
|
|
|
|
|
|
|
On POSIX, use fstat().
|
|
|
|
|
|
|
|
On Windows, use GetFileType() and GetFileInformationByHandle() which support
|
2017-11-08 18:44:44 -04:00
|
|
|
files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
|
|
|
|
than 2 GiB because the file size type is a signed 32-bit integer: see issue
|
2015-02-21 12:44:05 -04:00
|
|
|
#23152.
|
2015-03-30 05:09:31 -03:00
|
|
|
|
|
|
|
On Windows, set the last Windows error and return nonzero on error. On
|
|
|
|
POSIX, set errno and return nonzero on error. Fill status and return 0 on
|
|
|
|
success. */
|
2015-02-21 12:44:05 -04:00
|
|
|
int
|
2015-03-30 05:09:31 -03:00
|
|
|
_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
|
2015-02-21 12:44:05 -04:00
|
|
|
{
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
BY_HANDLE_FILE_INFORMATION info;
|
|
|
|
HANDLE h;
|
|
|
|
int type;
|
|
|
|
|
2016-09-08 15:21:54 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
|
|
|
h = (HANDLE)_get_osfhandle(fd);
|
|
|
|
_Py_END_SUPPRESS_IPH
|
2015-02-21 12:44:05 -04:00
|
|
|
|
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
2015-04-12 01:26:27 -03:00
|
|
|
/* errno is already set by _get_osfhandle, but we also set
|
|
|
|
the Win32 error for callers who expect that */
|
2015-03-07 22:14:07 -04:00
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
2015-02-21 12:44:05 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2015-03-30 05:09:31 -03:00
|
|
|
memset(status, 0, sizeof(*status));
|
2015-02-21 12:44:05 -04:00
|
|
|
|
|
|
|
type = GetFileType(h);
|
|
|
|
if (type == FILE_TYPE_UNKNOWN) {
|
|
|
|
DWORD error = GetLastError();
|
2015-04-12 01:26:27 -03:00
|
|
|
if (error != 0) {
|
|
|
|
errno = winerror_to_errno(error);
|
2015-02-21 12:44:05 -04:00
|
|
|
return -1;
|
2015-04-12 01:26:27 -03:00
|
|
|
}
|
2015-02-21 12:44:05 -04:00
|
|
|
/* else: valid but unknown file */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type != FILE_TYPE_DISK) {
|
|
|
|
if (type == FILE_TYPE_CHAR)
|
2015-03-30 05:09:31 -03:00
|
|
|
status->st_mode = _S_IFCHR;
|
2015-02-21 12:44:05 -04:00
|
|
|
else if (type == FILE_TYPE_PIPE)
|
2015-03-30 05:09:31 -03:00
|
|
|
status->st_mode = _S_IFIFO;
|
2015-02-21 12:44:05 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetFileInformationByHandle(h, &info)) {
|
2015-04-12 01:26:27 -03:00
|
|
|
/* The Win32 error is already set, but we also set errno for
|
|
|
|
callers who expect it */
|
|
|
|
errno = winerror_to_errno(GetLastError());
|
2015-02-21 12:44:05 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-03-30 05:09:31 -03:00
|
|
|
_Py_attribute_data_to_stat(&info, 0, status);
|
2015-02-21 12:44:05 -04:00
|
|
|
/* specific to fstat() */
|
2017-03-09 12:34:28 -04:00
|
|
|
status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow;
|
2015-02-21 12:44:05 -04:00
|
|
|
return 0;
|
|
|
|
#else
|
2015-03-30 05:09:31 -03:00
|
|
|
return fstat(fd, status);
|
2015-02-21 12:44:05 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-30 05:09:31 -03:00
|
|
|
/* Return information about a file.
|
|
|
|
|
|
|
|
On POSIX, use fstat().
|
|
|
|
|
|
|
|
On Windows, use GetFileType() and GetFileInformationByHandle() which support
|
2017-11-08 18:44:44 -04:00
|
|
|
files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
|
|
|
|
than 2 GiB because the file size type is a signed 32-bit integer: see issue
|
2015-03-30 05:09:31 -03:00
|
|
|
#23152.
|
|
|
|
|
|
|
|
Raise an exception and return -1 on error. On Windows, set the last Windows
|
|
|
|
error on error. On POSIX, set errno on error. Fill status and return 0 on
|
|
|
|
success.
|
|
|
|
|
2015-04-01 13:34:32 -03:00
|
|
|
Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
|
|
|
|
to call fstat(). The caller must hold the GIL. */
|
2015-03-30 05:09:31 -03:00
|
|
|
int
|
|
|
|
_Py_fstat(int fd, struct _Py_stat_struct *status)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2016-03-14 18:07:55 -03:00
|
|
|
assert(PyGILState_Check());
|
|
|
|
|
2015-03-30 05:09:31 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
res = _Py_fstat_noraise(fd, status);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (res != 0) {
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
PyErr_SetFromWindowsErr(0);
|
|
|
|
#else
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-21 12:44:05 -04:00
|
|
|
|
2010-10-07 19:53:43 -03:00
|
|
|
/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
|
|
|
|
call stat() otherwise. Only fill st_mode attribute on Windows.
|
|
|
|
|
2011-12-18 15:47:30 -04:00
|
|
|
Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
|
|
|
|
raised. */
|
2010-10-07 18:45:39 -03:00
|
|
|
|
|
|
|
int
|
2010-10-07 19:23:10 -03:00
|
|
|
_Py_stat(PyObject *path, struct stat *statbuf)
|
2010-10-07 18:45:39 -03:00
|
|
|
{
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
int err;
|
|
|
|
struct _stat wstatbuf;
|
2017-06-28 02:30:06 -03:00
|
|
|
const wchar_t *wpath;
|
2010-10-07 18:45:39 -03:00
|
|
|
|
2017-06-28 02:30:06 -03:00
|
|
|
wpath = _PyUnicode_AsUnicode(path);
|
2011-11-16 19:51:38 -04:00
|
|
|
if (wpath == NULL)
|
2011-12-18 15:47:30 -04:00
|
|
|
return -2;
|
2017-06-28 02:30:06 -03:00
|
|
|
|
2011-11-16 19:51:38 -04:00
|
|
|
err = _wstat(wpath, &wstatbuf);
|
2010-10-07 18:45:39 -03:00
|
|
|
if (!err)
|
|
|
|
statbuf->st_mode = wstatbuf.st_mode;
|
|
|
|
return err;
|
|
|
|
#else
|
|
|
|
int ret;
|
2017-06-28 02:30:06 -03:00
|
|
|
PyObject *bytes;
|
|
|
|
char *cpath;
|
|
|
|
|
|
|
|
bytes = PyUnicode_EncodeFSDefault(path);
|
2010-10-07 18:45:39 -03:00
|
|
|
if (bytes == NULL)
|
2011-12-18 15:47:30 -04:00
|
|
|
return -2;
|
2017-06-28 02:30:06 -03:00
|
|
|
|
|
|
|
/* check for embedded null bytes */
|
|
|
|
if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) {
|
|
|
|
Py_DECREF(bytes);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = stat(cpath, statbuf);
|
2010-10-07 18:45:39 -03:00
|
|
|
Py_DECREF(bytes);
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:34:47 -04:00
|
|
|
|
2018-02-06 02:31:22 -04:00
|
|
|
/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
|
2013-10-12 17:41:17 -03:00
|
|
|
static int
|
2013-08-27 19:53:59 -03:00
|
|
|
get_inheritable(int fd, int raise)
|
|
|
|
{
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
HANDLE handle;
|
|
|
|
DWORD flags;
|
|
|
|
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
handle = (HANDLE)_get_osfhandle(fd);
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
|
|
if (raise)
|
2015-03-14 15:38:27 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
2013-08-27 19:53:59 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetHandleInformation(handle, &flags)) {
|
|
|
|
if (raise)
|
|
|
|
PyErr_SetFromWindowsErr(0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (flags & HANDLE_FLAG_INHERIT);
|
|
|
|
#else
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = fcntl(fd, F_GETFD, 0);
|
|
|
|
if (flags == -1) {
|
|
|
|
if (raise)
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return !(flags & FD_CLOEXEC);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the inheritable flag of the specified file descriptor.
|
2013-09-07 05:36:04 -03:00
|
|
|
Return 1 if the file descriptor can be inherited, 0 if it cannot,
|
2013-08-27 19:53:59 -03:00
|
|
|
raise an exception and return -1 on error. */
|
|
|
|
int
|
|
|
|
_Py_get_inheritable(int fd)
|
|
|
|
{
|
|
|
|
return get_inheritable(fd, 1);
|
|
|
|
}
|
|
|
|
|
2018-02-06 02:31:22 -04:00
|
|
|
|
|
|
|
/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
|
2013-08-27 19:53:59 -03:00
|
|
|
static int
|
|
|
|
set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
|
|
|
|
{
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
HANDLE handle;
|
|
|
|
DWORD flags;
|
2014-09-02 06:41:04 -03:00
|
|
|
#else
|
|
|
|
#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
|
|
|
|
static int ioctl_works = -1;
|
2013-08-27 19:53:59 -03:00
|
|
|
int request;
|
|
|
|
int err;
|
2014-09-02 06:41:04 -03:00
|
|
|
#endif
|
2016-04-17 11:51:52 -03:00
|
|
|
int flags, new_flags;
|
2013-08-27 19:53:59 -03:00
|
|
|
int res;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* atomic_flag_works can only be used to make the file descriptor
|
|
|
|
non-inheritable */
|
|
|
|
assert(!(atomic_flag_works != NULL && inheritable));
|
|
|
|
|
|
|
|
if (atomic_flag_works != NULL && !inheritable) {
|
|
|
|
if (*atomic_flag_works == -1) {
|
2015-03-14 15:38:27 -03:00
|
|
|
int isInheritable = get_inheritable(fd, raise);
|
|
|
|
if (isInheritable == -1)
|
2013-08-27 19:53:59 -03:00
|
|
|
return -1;
|
2015-03-14 15:38:27 -03:00
|
|
|
*atomic_flag_works = !isInheritable;
|
2013-08-27 19:53:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*atomic_flag_works)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
handle = (HANDLE)_get_osfhandle(fd);
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
|
|
if (raise)
|
2015-03-14 15:38:27 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
2013-08-27 19:53:59 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inheritable)
|
|
|
|
flags = HANDLE_FLAG_INHERIT;
|
|
|
|
else
|
|
|
|
flags = 0;
|
2019-08-23 16:01:38 -03:00
|
|
|
|
|
|
|
/* This check can be removed once support for Windows 7 ends. */
|
|
|
|
#define CONSOLE_PSEUDOHANDLE(handle) (((ULONG_PTR)(handle) & 0x3) == 0x3 && \
|
|
|
|
GetFileType(handle) == FILE_TYPE_CHAR)
|
|
|
|
|
|
|
|
if (!CONSOLE_PSEUDOHANDLE(handle) &&
|
|
|
|
!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
|
2013-08-27 19:53:59 -03:00
|
|
|
if (raise)
|
|
|
|
PyErr_SetFromWindowsErr(0);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-23 16:01:38 -03:00
|
|
|
#undef CONSOLE_PSEUDOHANDLE
|
2013-08-27 19:53:59 -03:00
|
|
|
return 0;
|
|
|
|
|
2014-09-02 06:41:04 -03:00
|
|
|
#else
|
|
|
|
|
|
|
|
#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
|
2018-02-06 02:31:22 -04:00
|
|
|
if (ioctl_works != 0 && raise != 0) {
|
2014-09-02 06:41:04 -03:00
|
|
|
/* fast-path: ioctl() only requires one syscall */
|
2018-02-06 02:31:22 -04:00
|
|
|
/* caveat: raise=0 is an indicator that we must be async-signal-safe
|
|
|
|
* thus avoid using ioctl() so we skip the fast-path. */
|
2014-09-02 06:41:04 -03:00
|
|
|
if (inheritable)
|
|
|
|
request = FIONCLEX;
|
|
|
|
else
|
|
|
|
request = FIOCLEX;
|
|
|
|
err = ioctl(fd, request, NULL);
|
|
|
|
if (!err) {
|
|
|
|
ioctl_works = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:46:18 -03:00
|
|
|
if (errno != ENOTTY && errno != EACCES) {
|
2014-09-02 06:41:04 -03:00
|
|
|
if (raise)
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
|
|
|
|
device". The ioctl is declared but not supported by the kernel.
|
|
|
|
Remember that ioctl() doesn't work. It is the case on
|
2016-05-19 11:46:18 -03:00
|
|
|
Illumos-based OS for example.
|
|
|
|
|
|
|
|
Issue #27057: When SELinux policy disallows ioctl it will fail
|
|
|
|
with EACCES. While FIOCLEX is safe operation it may be
|
|
|
|
unavailable because ioctl was denied altogether.
|
|
|
|
This can be the case on Android. */
|
2014-09-02 06:41:04 -03:00
|
|
|
ioctl_works = 0;
|
|
|
|
}
|
|
|
|
/* fallback to fcntl() if ioctl() does not work */
|
2013-08-27 19:53:59 -03:00
|
|
|
}
|
2014-09-02 06:41:04 -03:00
|
|
|
#endif
|
2013-08-27 19:53:59 -03:00
|
|
|
|
2014-09-02 06:41:04 -03:00
|
|
|
/* slow-path: fcntl() requires two syscalls */
|
2013-08-27 19:53:59 -03:00
|
|
|
flags = fcntl(fd, F_GETFD);
|
|
|
|
if (flags < 0) {
|
|
|
|
if (raise)
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-04-17 11:51:52 -03:00
|
|
|
if (inheritable) {
|
|
|
|
new_flags = flags & ~FD_CLOEXEC;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_flags = flags | FD_CLOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_flags == flags) {
|
|
|
|
/* FD_CLOEXEC flag already set/cleared: nothing to do */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-19 11:19:29 -04:00
|
|
|
res = fcntl(fd, F_SETFD, new_flags);
|
2013-08-27 19:53:59 -03:00
|
|
|
if (res < 0) {
|
|
|
|
if (raise)
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the file descriptor non-inheritable.
|
2013-09-07 05:36:04 -03:00
|
|
|
Return 0 on success, set errno and return -1 on error. */
|
2013-08-27 19:53:59 -03:00
|
|
|
static int
|
|
|
|
make_non_inheritable(int fd)
|
|
|
|
{
|
|
|
|
return set_inheritable(fd, 0, 0, NULL);
|
|
|
|
}
|
2010-10-07 19:53:43 -03:00
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
/* Set the inheritable flag of the specified file descriptor.
|
2018-02-06 02:31:22 -04:00
|
|
|
On success: return 0, on error: raise an exception and return -1.
|
2013-08-27 19:53:59 -03:00
|
|
|
|
|
|
|
If atomic_flag_works is not NULL:
|
|
|
|
|
|
|
|
* if *atomic_flag_works==-1, check if the inheritable is set on the file
|
|
|
|
descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
|
|
|
|
set the inheritable flag
|
|
|
|
* if *atomic_flag_works==1: do nothing
|
|
|
|
* if *atomic_flag_works==0: set inheritable flag to False
|
|
|
|
|
|
|
|
Set atomic_flag_works to NULL if no atomic flag was used to create the
|
|
|
|
file descriptor.
|
|
|
|
|
|
|
|
atomic_flag_works can only be used to make a file descriptor
|
|
|
|
non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
|
|
|
|
int
|
|
|
|
_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
|
|
|
|
{
|
|
|
|
return set_inheritable(fd, inheritable, 1, atomic_flag_works);
|
|
|
|
}
|
|
|
|
|
2018-02-06 02:31:22 -04:00
|
|
|
/* Same as _Py_set_inheritable() but on error, set errno and
|
|
|
|
don't raise an exception.
|
|
|
|
This function is async-signal-safe. */
|
|
|
|
int
|
|
|
|
_Py_set_inheritable_async_safe(int fd, int inheritable, int *atomic_flag_works)
|
|
|
|
{
|
|
|
|
return set_inheritable(fd, inheritable, 0, atomic_flag_works);
|
|
|
|
}
|
|
|
|
|
2015-03-17 20:22:14 -03:00
|
|
|
static int
|
|
|
|
_Py_open_impl(const char *pathname, int flags, int gil_held)
|
2013-08-27 19:53:59 -03:00
|
|
|
{
|
|
|
|
int fd;
|
2015-03-18 05:52:54 -03:00
|
|
|
int async_err = 0;
|
2015-03-17 20:22:14 -03:00
|
|
|
#ifndef MS_WINDOWS
|
2013-08-27 19:53:59 -03:00
|
|
|
int *atomic_flag_works;
|
2015-03-17 20:22:14 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
flags |= O_NOINHERIT;
|
|
|
|
#elif defined(O_CLOEXEC)
|
2013-08-27 19:53:59 -03:00
|
|
|
atomic_flag_works = &_Py_open_cloexec_works;
|
|
|
|
flags |= O_CLOEXEC;
|
|
|
|
#else
|
|
|
|
atomic_flag_works = NULL;
|
|
|
|
#endif
|
|
|
|
|
2015-03-17 20:22:14 -03:00
|
|
|
if (gil_held) {
|
2015-03-18 05:52:54 -03:00
|
|
|
do {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
fd = open(pathname, flags);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
} while (fd < 0
|
|
|
|
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
|
|
|
|
if (async_err)
|
|
|
|
return -1;
|
2015-03-17 20:22:14 -03:00
|
|
|
if (fd < 0) {
|
|
|
|
PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fd = open(pathname, flags);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
|
2013-08-27 19:53:59 -03:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-03-17 20:22:14 -03:00
|
|
|
#endif
|
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2015-03-17 20:22:14 -03:00
|
|
|
/* Open a file with the specified flags (wrapper to open() function).
|
|
|
|
Return a file descriptor on success. Raise an exception and return -1 on
|
|
|
|
error.
|
|
|
|
|
|
|
|
The file descriptor is created non-inheritable.
|
|
|
|
|
2015-03-18 05:52:54 -03:00
|
|
|
When interrupted by a signal (open() fails with EINTR), retry the syscall,
|
|
|
|
except if the Python signal handler raises an exception.
|
|
|
|
|
2015-04-01 13:34:32 -03:00
|
|
|
Release the GIL to call open(). The caller must hold the GIL. */
|
2015-03-17 20:22:14 -03:00
|
|
|
int
|
|
|
|
_Py_open(const char *pathname, int flags)
|
|
|
|
{
|
|
|
|
/* _Py_open() must be called with the GIL held. */
|
|
|
|
assert(PyGILState_Check());
|
|
|
|
return _Py_open_impl(pathname, flags, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open a file with the specified flags (wrapper to open() function).
|
|
|
|
Return a file descriptor on success. Set errno and return -1 on error.
|
|
|
|
|
2015-03-18 05:52:54 -03:00
|
|
|
The file descriptor is created non-inheritable.
|
|
|
|
|
|
|
|
If interrupted by a signal, fail with EINTR. */
|
2015-03-17 20:22:14 -03:00
|
|
|
int
|
|
|
|
_Py_open_noraise(const char *pathname, int flags)
|
|
|
|
{
|
|
|
|
return _Py_open_impl(pathname, flags, 0);
|
|
|
|
}
|
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
/* Open a file. Use _wfopen() on Windows, encode the path to the locale
|
2015-03-17 21:39:23 -03:00
|
|
|
encoding and use fopen() otherwise.
|
|
|
|
|
2015-03-18 05:52:54 -03:00
|
|
|
The file descriptor is created non-inheritable.
|
|
|
|
|
|
|
|
If interrupted by a signal, fail with EINTR. */
|
2010-10-07 18:45:39 -03:00
|
|
|
FILE *
|
|
|
|
_Py_wfopen(const wchar_t *path, const wchar_t *mode)
|
|
|
|
{
|
|
|
|
FILE *f;
|
2013-08-27 19:53:59 -03:00
|
|
|
#ifndef MS_WINDOWS
|
2010-10-07 18:45:39 -03:00
|
|
|
char *cpath;
|
|
|
|
char cmode[10];
|
|
|
|
size_t r;
|
|
|
|
r = wcstombs(cmode, mode, 10);
|
|
|
|
if (r == (size_t)-1 || r >= 10) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-21 11:20:32 -04:00
|
|
|
cpath = _Py_EncodeLocaleRaw(path, NULL);
|
|
|
|
if (cpath == NULL) {
|
2010-10-07 18:45:39 -03:00
|
|
|
return NULL;
|
2017-12-21 11:20:32 -04:00
|
|
|
}
|
2010-10-07 18:45:39 -03:00
|
|
|
f = fopen(cpath, cmode);
|
2017-12-21 11:20:32 -04:00
|
|
|
PyMem_RawFree(cpath);
|
2010-10-07 18:45:39 -03:00
|
|
|
#else
|
2013-08-27 19:53:59 -03:00
|
|
|
f = _wfopen(path, mode);
|
2010-10-07 18:45:39 -03:00
|
|
|
#endif
|
2013-08-27 19:53:59 -03:00
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (make_non_inheritable(fileno(f)) < 0) {
|
|
|
|
fclose(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return f;
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:39:23 -03:00
|
|
|
/* Wrapper to fopen().
|
|
|
|
|
2015-03-18 05:52:54 -03:00
|
|
|
The file descriptor is created non-inheritable.
|
|
|
|
|
|
|
|
If interrupted by a signal, fail with EINTR. */
|
2013-08-27 19:53:59 -03:00
|
|
|
FILE*
|
|
|
|
_Py_fopen(const char *pathname, const char *mode)
|
|
|
|
{
|
|
|
|
FILE *f = fopen(pathname, mode);
|
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (make_non_inheritable(fileno(f)) < 0) {
|
|
|
|
fclose(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
2010-10-07 19:53:43 -03:00
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
|
2015-03-17 21:39:23 -03:00
|
|
|
encoding and call fopen() otherwise.
|
|
|
|
|
|
|
|
Return the new file object on success. Raise an exception and return NULL
|
|
|
|
on error.
|
|
|
|
|
|
|
|
The file descriptor is created non-inheritable.
|
2010-10-07 18:45:39 -03:00
|
|
|
|
2015-03-18 05:52:54 -03:00
|
|
|
When interrupted by a signal (open() fails with EINTR), retry the syscall,
|
|
|
|
except if the Python signal handler raises an exception.
|
|
|
|
|
2015-04-01 13:34:32 -03:00
|
|
|
Release the GIL to call _wfopen() or fopen(). The caller must hold
|
|
|
|
the GIL. */
|
2010-10-07 18:45:39 -03:00
|
|
|
FILE*
|
2013-08-27 19:53:59 -03:00
|
|
|
_Py_fopen_obj(PyObject *path, const char *mode)
|
2010-10-07 18:45:39 -03:00
|
|
|
{
|
2013-08-27 19:53:59 -03:00
|
|
|
FILE *f;
|
2015-03-18 05:52:54 -03:00
|
|
|
int async_err = 0;
|
2010-10-07 18:45:39 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2017-06-28 02:30:06 -03:00
|
|
|
const wchar_t *wpath;
|
2010-10-07 18:45:39 -03:00
|
|
|
wchar_t wmode[10];
|
|
|
|
int usize;
|
|
|
|
|
2015-03-17 21:39:23 -03:00
|
|
|
assert(PyGILState_Check());
|
|
|
|
|
2011-12-22 05:03:38 -04:00
|
|
|
if (!PyUnicode_Check(path)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"str file path expected under Windows, got %R",
|
|
|
|
Py_TYPE(path));
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-06-28 02:30:06 -03:00
|
|
|
wpath = _PyUnicode_AsUnicode(path);
|
2011-11-16 19:51:38 -04:00
|
|
|
if (wpath == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2018-02-18 14:40:07 -04:00
|
|
|
usize = MultiByteToWideChar(CP_ACP, 0, mode, -1,
|
|
|
|
wmode, Py_ARRAY_LENGTH(wmode));
|
2015-03-17 21:39:23 -03:00
|
|
|
if (usize == 0) {
|
|
|
|
PyErr_SetFromWindowsErr(0);
|
2010-10-07 18:45:39 -03:00
|
|
|
return NULL;
|
2015-03-17 21:39:23 -03:00
|
|
|
}
|
2010-10-07 18:45:39 -03:00
|
|
|
|
2015-03-18 05:52:54 -03:00
|
|
|
do {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
f = _wfopen(wpath, wmode);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
} while (f == NULL
|
|
|
|
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
|
2010-10-07 18:45:39 -03:00
|
|
|
#else
|
2011-12-19 13:19:06 -04:00
|
|
|
PyObject *bytes;
|
2015-03-17 21:39:23 -03:00
|
|
|
char *path_bytes;
|
|
|
|
|
|
|
|
assert(PyGILState_Check());
|
|
|
|
|
2011-12-19 13:19:06 -04:00
|
|
|
if (!PyUnicode_FSConverter(path, &bytes))
|
2010-10-07 18:45:39 -03:00
|
|
|
return NULL;
|
2015-03-17 21:39:23 -03:00
|
|
|
path_bytes = PyBytes_AS_STRING(bytes);
|
|
|
|
|
2015-03-18 05:52:54 -03:00
|
|
|
do {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
f = fopen(path_bytes, mode);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
} while (f == NULL
|
|
|
|
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
|
2015-03-17 21:39:23 -03:00
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
Py_DECREF(bytes);
|
|
|
|
#endif
|
2015-03-18 05:52:54 -03:00
|
|
|
if (async_err)
|
|
|
|
return NULL;
|
|
|
|
|
2015-03-17 21:39:23 -03:00
|
|
|
if (f == NULL) {
|
|
|
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
|
2013-08-27 19:53:59 -03:00
|
|
|
return NULL;
|
2015-03-17 21:39:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
|
2013-08-27 19:53:59 -03:00
|
|
|
fclose(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return f;
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
|
|
|
|
2015-03-19 18:53:20 -03:00
|
|
|
/* Read count bytes from fd into buf.
|
2015-04-01 13:34:45 -03:00
|
|
|
|
|
|
|
On success, return the number of read bytes, it can be lower than count.
|
|
|
|
If the current file offset is at or past the end of file, no bytes are read,
|
|
|
|
and read() returns zero.
|
|
|
|
|
|
|
|
On error, raise an exception, set errno and return -1.
|
|
|
|
|
|
|
|
When interrupted by a signal (read() fails with EINTR), retry the syscall.
|
|
|
|
If the Python signal handler raises an exception, the function returns -1
|
|
|
|
(the syscall is not retried).
|
|
|
|
|
|
|
|
Release the GIL to call read(). The caller must hold the GIL. */
|
2015-03-19 18:53:20 -03:00
|
|
|
Py_ssize_t
|
|
|
|
_Py_read(int fd, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
Py_ssize_t n;
|
2015-03-20 07:58:18 -03:00
|
|
|
int err;
|
2015-03-19 18:53:20 -03:00
|
|
|
int async_err = 0;
|
|
|
|
|
2016-03-14 18:07:55 -03:00
|
|
|
assert(PyGILState_Check());
|
|
|
|
|
2015-03-19 18:53:20 -03:00
|
|
|
/* _Py_read() must not be called with an exception set, otherwise the
|
|
|
|
* caller may think that read() was interrupted by a signal and the signal
|
|
|
|
* handler raised an exception. */
|
|
|
|
assert(!PyErr_Occurred());
|
|
|
|
|
2018-10-18 03:58:40 -03:00
|
|
|
if (count > _PY_READ_MAX) {
|
|
|
|
count = _PY_READ_MAX;
|
2015-03-19 18:53:20 -03:00
|
|
|
}
|
|
|
|
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2015-03-19 18:53:20 -03:00
|
|
|
do {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
n = read(fd, buf, (int)count);
|
|
|
|
#else
|
|
|
|
n = read(fd, buf, count);
|
|
|
|
#endif
|
2015-03-20 07:58:18 -03:00
|
|
|
/* save/restore errno because PyErr_CheckSignals()
|
|
|
|
* and PyErr_SetFromErrno() can modify it */
|
|
|
|
err = errno;
|
2015-03-19 18:53:20 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
2015-03-20 07:58:18 -03:00
|
|
|
} while (n < 0 && err == EINTR &&
|
2015-03-19 18:53:20 -03:00
|
|
|
!(async_err = PyErr_CheckSignals()));
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
2015-03-19 18:53:20 -03:00
|
|
|
|
|
|
|
if (async_err) {
|
|
|
|
/* read() was interrupted by a signal (failed with EINTR)
|
|
|
|
* and the Python signal handler raised an exception */
|
2015-03-20 07:58:18 -03:00
|
|
|
errno = err;
|
|
|
|
assert(errno == EINTR && PyErr_Occurred());
|
2015-03-19 18:53:20 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (n < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
2015-03-20 07:58:18 -03:00
|
|
|
errno = err;
|
2015-03-19 18:53:20 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-04-01 13:34:45 -03:00
|
|
|
static Py_ssize_t
|
|
|
|
_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
|
2015-03-19 18:53:20 -03:00
|
|
|
{
|
|
|
|
Py_ssize_t n;
|
2015-03-20 07:58:18 -03:00
|
|
|
int err;
|
2015-03-19 18:53:20 -03:00
|
|
|
int async_err = 0;
|
|
|
|
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2015-03-19 18:53:20 -03:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (count > 32767 && isatty(fd)) {
|
|
|
|
/* Issue #11395: the Windows console returns an error (12: not
|
|
|
|
enough space error) on writing into stdout if stdout mode is
|
|
|
|
binary and the length is greater than 66,000 bytes (or less,
|
|
|
|
depending on heap usage). */
|
|
|
|
count = 32767;
|
|
|
|
}
|
|
|
|
#endif
|
2018-10-18 03:58:40 -03:00
|
|
|
if (count > _PY_WRITE_MAX) {
|
|
|
|
count = _PY_WRITE_MAX;
|
|
|
|
}
|
2015-03-19 18:53:20 -03:00
|
|
|
|
2015-04-01 13:34:45 -03:00
|
|
|
if (gil_held) {
|
|
|
|
do {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
2015-03-19 18:53:20 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2015-04-01 13:34:45 -03:00
|
|
|
n = write(fd, buf, (int)count);
|
2015-03-19 18:53:20 -03:00
|
|
|
#else
|
2015-04-01 13:34:45 -03:00
|
|
|
n = write(fd, buf, count);
|
2015-03-19 18:53:20 -03:00
|
|
|
#endif
|
2015-04-01 13:34:45 -03:00
|
|
|
/* save/restore errno because PyErr_CheckSignals()
|
|
|
|
* and PyErr_SetFromErrno() can modify it */
|
|
|
|
err = errno;
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
} while (n < 0 && err == EINTR &&
|
|
|
|
!(async_err = PyErr_CheckSignals()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
do {
|
|
|
|
errno = 0;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
n = write(fd, buf, (int)count);
|
|
|
|
#else
|
|
|
|
n = write(fd, buf, count);
|
|
|
|
#endif
|
|
|
|
err = errno;
|
|
|
|
} while (n < 0 && err == EINTR);
|
|
|
|
}
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
2015-03-19 18:53:20 -03:00
|
|
|
|
|
|
|
if (async_err) {
|
|
|
|
/* write() was interrupted by a signal (failed with EINTR)
|
2015-04-01 13:34:45 -03:00
|
|
|
and the Python signal handler raised an exception (if gil_held is
|
|
|
|
nonzero). */
|
2015-03-20 07:58:18 -03:00
|
|
|
errno = err;
|
2015-04-01 13:34:45 -03:00
|
|
|
assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
|
2015-03-19 18:53:20 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (n < 0) {
|
2015-04-01 13:34:45 -03:00
|
|
|
if (gil_held)
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
2015-03-20 07:58:18 -03:00
|
|
|
errno = err;
|
2015-03-19 18:53:20 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-04-01 13:34:45 -03:00
|
|
|
/* Write count bytes of buf into fd.
|
|
|
|
|
|
|
|
On success, return the number of written bytes, it can be lower than count
|
|
|
|
including 0. On error, raise an exception, set errno and return -1.
|
|
|
|
|
|
|
|
When interrupted by a signal (write() fails with EINTR), retry the syscall.
|
|
|
|
If the Python signal handler raises an exception, the function returns -1
|
|
|
|
(the syscall is not retried).
|
|
|
|
|
|
|
|
Release the GIL to call write(). The caller must hold the GIL. */
|
|
|
|
Py_ssize_t
|
|
|
|
_Py_write(int fd, const void *buf, size_t count)
|
|
|
|
{
|
2016-03-14 18:07:55 -03:00
|
|
|
assert(PyGILState_Check());
|
|
|
|
|
2015-04-01 13:34:45 -03:00
|
|
|
/* _Py_write() must not be called with an exception set, otherwise the
|
|
|
|
* caller may think that write() was interrupted by a signal and the signal
|
|
|
|
* handler raised an exception. */
|
|
|
|
assert(!PyErr_Occurred());
|
|
|
|
|
|
|
|
return _Py_write_impl(fd, buf, count, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write count bytes of buf into fd.
|
|
|
|
*
|
|
|
|
* On success, return the number of written bytes, it can be lower than count
|
|
|
|
* including 0. On error, set errno and return -1.
|
|
|
|
*
|
|
|
|
* When interrupted by a signal (write() fails with EINTR), retry the syscall
|
|
|
|
* without calling the Python signal handler. */
|
|
|
|
Py_ssize_t
|
|
|
|
_Py_write_noraise(int fd, const void *buf, size_t count)
|
|
|
|
{
|
|
|
|
return _Py_write_impl(fd, buf, count, 0);
|
|
|
|
}
|
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
#ifdef HAVE_READLINK
|
2010-10-07 19:53:43 -03:00
|
|
|
|
|
|
|
/* Read value of symbolic link. Encode the path to the locale encoding, decode
|
2011-12-16 18:56:01 -04:00
|
|
|
the result from the locale encoding. Return -1 on error. */
|
2010-10-07 19:53:43 -03:00
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
int
|
|
|
|
_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
|
|
|
|
{
|
|
|
|
char *cpath;
|
2013-11-15 13:14:11 -04:00
|
|
|
char cbuf[MAXPATHLEN];
|
2010-10-16 19:47:37 -03:00
|
|
|
wchar_t *wbuf;
|
2010-10-07 18:45:39 -03:00
|
|
|
int res;
|
|
|
|
size_t r1;
|
|
|
|
|
2017-12-21 11:20:32 -04:00
|
|
|
cpath = _Py_EncodeLocaleRaw(path, NULL);
|
2010-10-07 18:45:39 -03:00
|
|
|
if (cpath == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-15 13:14:11 -04:00
|
|
|
res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
|
2017-12-21 11:20:32 -04:00
|
|
|
PyMem_RawFree(cpath);
|
2010-10-07 18:45:39 -03:00
|
|
|
if (res == -1)
|
|
|
|
return -1;
|
2013-11-15 13:14:11 -04:00
|
|
|
if (res == Py_ARRAY_LENGTH(cbuf)) {
|
2010-10-07 18:45:39 -03:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cbuf[res] = '\0'; /* buf will be null terminated */
|
2014-08-01 07:28:48 -03:00
|
|
|
wbuf = Py_DecodeLocale(cbuf, &r1);
|
2010-10-16 19:52:09 -03:00
|
|
|
if (wbuf == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-10-16 19:47:37 -03:00
|
|
|
if (bufsiz <= r1) {
|
2013-07-07 11:25:15 -03:00
|
|
|
PyMem_RawFree(wbuf);
|
2010-10-07 18:45:39 -03:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-10-16 19:47:37 -03:00
|
|
|
wcsncpy(buf, wbuf, bufsiz);
|
2013-07-07 11:25:15 -03:00
|
|
|
PyMem_RawFree(wbuf);
|
2010-10-07 18:45:39 -03:00
|
|
|
return (int)r1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_REALPATH
|
2010-10-07 19:53:43 -03:00
|
|
|
|
|
|
|
/* Return the canonicalized absolute pathname. Encode path to the locale
|
2011-12-16 18:56:01 -04:00
|
|
|
encoding, decode the result from the locale encoding.
|
|
|
|
Return NULL on error. */
|
2010-10-07 19:53:43 -03:00
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
wchar_t*
|
2010-10-07 19:29:53 -03:00
|
|
|
_Py_wrealpath(const wchar_t *path,
|
|
|
|
wchar_t *resolved_path, size_t resolved_path_size)
|
2010-10-07 18:45:39 -03:00
|
|
|
{
|
|
|
|
char *cpath;
|
2013-11-15 13:14:11 -04:00
|
|
|
char cresolved_path[MAXPATHLEN];
|
2010-10-16 19:55:47 -03:00
|
|
|
wchar_t *wresolved_path;
|
2010-10-07 18:45:39 -03:00
|
|
|
char *res;
|
|
|
|
size_t r;
|
2017-12-21 11:20:32 -04:00
|
|
|
cpath = _Py_EncodeLocaleRaw(path, NULL);
|
2010-10-07 18:45:39 -03:00
|
|
|
if (cpath == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
res = realpath(cpath, cresolved_path);
|
2017-12-21 11:20:32 -04:00
|
|
|
PyMem_RawFree(cpath);
|
2010-10-07 18:45:39 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
2010-10-16 19:55:47 -03:00
|
|
|
|
2014-08-01 07:28:48 -03:00
|
|
|
wresolved_path = Py_DecodeLocale(cresolved_path, &r);
|
2010-10-16 19:55:47 -03:00
|
|
|
if (wresolved_path == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (resolved_path_size <= r) {
|
2013-07-07 11:25:15 -03:00
|
|
|
PyMem_RawFree(wresolved_path);
|
2010-10-07 18:45:39 -03:00
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-10-16 19:55:47 -03:00
|
|
|
wcsncpy(resolved_path, wresolved_path, resolved_path_size);
|
2013-07-07 11:25:15 -03:00
|
|
|
PyMem_RawFree(wresolved_path);
|
2010-10-07 18:45:39 -03:00
|
|
|
return resolved_path;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-14 09:37:19 -03:00
|
|
|
/* Get the current directory. size is the buffer size in wide characters
|
2011-12-16 18:56:01 -04:00
|
|
|
including the null character. Decode the path from the locale encoding.
|
|
|
|
Return NULL on error. */
|
2010-10-07 19:53:43 -03:00
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
wchar_t*
|
|
|
|
_Py_wgetcwd(wchar_t *buf, size_t size)
|
|
|
|
{
|
|
|
|
#ifdef MS_WINDOWS
|
2013-06-04 19:46:29 -03:00
|
|
|
int isize = (int)Py_MIN(size, INT_MAX);
|
|
|
|
return _wgetcwd(buf, isize);
|
2010-10-07 18:45:39 -03:00
|
|
|
#else
|
2013-11-15 13:14:11 -04:00
|
|
|
char fname[MAXPATHLEN];
|
2010-10-14 09:37:19 -03:00
|
|
|
wchar_t *wname;
|
2010-10-16 20:16:16 -03:00
|
|
|
size_t len;
|
2010-10-14 09:37:19 -03:00
|
|
|
|
2013-11-15 13:14:11 -04:00
|
|
|
if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
|
2010-10-07 18:45:39 -03:00
|
|
|
return NULL;
|
2014-08-01 07:28:48 -03:00
|
|
|
wname = Py_DecodeLocale(fname, &len);
|
2010-10-14 09:37:19 -03:00
|
|
|
if (wname == NULL)
|
|
|
|
return NULL;
|
2010-10-16 20:16:16 -03:00
|
|
|
if (size <= len) {
|
2013-07-07 11:25:15 -03:00
|
|
|
PyMem_RawFree(wname);
|
2010-10-07 18:45:39 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-10-14 09:37:19 -03:00
|
|
|
wcsncpy(buf, wname, size);
|
2013-07-07 11:25:15 -03:00
|
|
|
PyMem_RawFree(wname);
|
2010-10-07 18:45:39 -03:00
|
|
|
return buf;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
/* Duplicate a file descriptor. The new file descriptor is created as
|
|
|
|
non-inheritable. Return a new file descriptor on success, raise an OSError
|
|
|
|
exception and return -1 on error.
|
|
|
|
|
|
|
|
The GIL is released to call dup(). The caller must hold the GIL. */
|
|
|
|
int
|
|
|
|
_Py_dup(int fd)
|
|
|
|
{
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
HANDLE handle;
|
|
|
|
#endif
|
|
|
|
|
2016-03-14 18:07:55 -03:00
|
|
|
assert(PyGILState_Check());
|
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
handle = (HANDLE)_get_osfhandle(fd);
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
2015-03-14 15:38:27 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
2013-08-27 19:53:59 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
fd = dup(fd);
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (fd < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-17 05:45:04 -03:00
|
|
|
if (_Py_set_inheritable(fd, 0, NULL) < 0) {
|
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
|
|
|
close(fd);
|
|
|
|
_Py_END_SUPPRESS_IPH
|
|
|
|
return -1;
|
2013-08-27 19:53:59 -03:00
|
|
|
}
|
|
|
|
#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (fd < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
fd = dup(fd);
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (fd < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_Py_set_inheritable(fd, 0, NULL) < 0) {
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
close(fd);
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_END_SUPPRESS_IPH
|
2013-08-27 19:53:59 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2014-07-29 17:32:47 -03:00
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
/* Get the blocking mode of the file descriptor.
|
|
|
|
Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
|
|
|
|
raise an exception and return -1 on error. */
|
|
|
|
int
|
|
|
|
_Py_get_blocking(int fd)
|
|
|
|
{
|
2015-04-12 01:26:27 -03:00
|
|
|
int flags;
|
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
_Py_END_SUPPRESS_IPH
|
2014-07-29 17:32:47 -03:00
|
|
|
if (flags < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !(flags & O_NONBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the blocking mode of the specified file descriptor.
|
|
|
|
|
|
|
|
Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
|
|
|
|
otherwise.
|
|
|
|
|
|
|
|
Return 0 on success, raise an exception and return -1 on error. */
|
|
|
|
int
|
|
|
|
_Py_set_blocking(int fd, int blocking)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
|
|
|
|
int arg = !blocking;
|
|
|
|
if (ioctl(fd, FIONBIO, &arg) < 0)
|
|
|
|
goto error;
|
|
|
|
#else
|
|
|
|
int flags, res;
|
|
|
|
|
2015-04-12 01:26:27 -03:00
|
|
|
_Py_BEGIN_SUPPRESS_IPH
|
2014-07-29 17:32:47 -03:00
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
2015-04-12 01:26:27 -03:00
|
|
|
if (flags >= 0) {
|
|
|
|
if (blocking)
|
|
|
|
flags = flags & (~O_NONBLOCK);
|
|
|
|
else
|
|
|
|
flags = flags | O_NONBLOCK;
|
2014-07-29 17:32:47 -03:00
|
|
|
|
2015-04-12 01:26:27 -03:00
|
|
|
res = fcntl(fd, F_SETFL, flags);
|
|
|
|
} else {
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
_Py_END_SUPPRESS_IPH
|
2014-07-29 17:32:47 -03:00
|
|
|
|
|
|
|
if (res < 0)
|
|
|
|
goto error;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2018-01-15 10:58:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
_Py_GetLocaleconvNumeric(PyObject **decimal_point, PyObject **thousands_sep,
|
|
|
|
const char **grouping)
|
|
|
|
{
|
|
|
|
int res = -1;
|
|
|
|
|
|
|
|
struct lconv *lc = localeconv();
|
|
|
|
|
|
|
|
int change_locale = 0;
|
|
|
|
if (decimal_point != NULL &&
|
|
|
|
(strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127))
|
|
|
|
{
|
|
|
|
change_locale = 1;
|
|
|
|
}
|
|
|
|
if (thousands_sep != NULL &&
|
|
|
|
(strlen(lc->thousands_sep) > 1 || ((unsigned char)lc->thousands_sep[0]) > 127))
|
|
|
|
{
|
|
|
|
change_locale = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep a copy of the LC_CTYPE locale */
|
|
|
|
char *oldloc = NULL, *loc = NULL;
|
|
|
|
if (change_locale) {
|
|
|
|
oldloc = setlocale(LC_CTYPE, NULL);
|
|
|
|
if (!oldloc) {
|
2018-11-20 17:06:21 -04:00
|
|
|
PyErr_SetString(PyExc_RuntimeWarning, "failed to get LC_CTYPE locale");
|
2018-01-15 10:58:02 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
oldloc = _PyMem_Strdup(oldloc);
|
|
|
|
if (!oldloc) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
loc = setlocale(LC_NUMERIC, NULL);
|
|
|
|
if (loc != NULL && strcmp(loc, oldloc) == 0) {
|
|
|
|
loc = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loc != NULL) {
|
2018-11-20 17:06:21 -04:00
|
|
|
/* Only set the locale temporarily the LC_CTYPE locale
|
2018-01-15 10:58:02 -04:00
|
|
|
if LC_NUMERIC locale is different than LC_CTYPE locale and
|
|
|
|
decimal_point and/or thousands_sep are non-ASCII or longer than
|
|
|
|
1 byte */
|
|
|
|
setlocale(LC_CTYPE, loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decimal_point != NULL) {
|
|
|
|
*decimal_point = PyUnicode_DecodeLocale(lc->decimal_point, NULL);
|
|
|
|
if (*decimal_point == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (thousands_sep != NULL) {
|
|
|
|
*thousands_sep = PyUnicode_DecodeLocale(lc->thousands_sep, NULL);
|
|
|
|
if (*thousands_sep == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grouping != NULL) {
|
|
|
|
*grouping = lc->grouping;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (loc != NULL) {
|
|
|
|
setlocale(LC_CTYPE, oldloc);
|
|
|
|
}
|
|
|
|
PyMem_Free(oldloc);
|
|
|
|
return res;
|
|
|
|
}
|