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
|
|
|
|
# include <windows.h>
|
|
|
|
#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 */
|
|
|
|
|
2012-11-12 18:04:02 -04:00
|
|
|
#ifdef __APPLE__
|
|
|
|
extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
|
|
|
|
#endif
|
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
#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
|
|
|
|
|
|
|
|
The flag is used by _Py_open(), io.FileIO and os.open() */
|
|
|
|
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
|
|
|
|
if (!_PyVerify_fd(fd) || !isatty(fd)) {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:34:47 -04:00
|
|
|
#if !defined(__APPLE__) && !defined(MS_WINDOWS)
|
|
|
|
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
|
|
|
|
|
|
|
1: the workaround is used: _Py_wchar2char() uses
|
|
|
|
encode_ascii_surrogateescape() and _Py_char2wchar() uses
|
|
|
|
decode_ascii_surrogateescape()
|
|
|
|
0: the workaround is not used: _Py_wchar2char() uses wcstombs() and
|
|
|
|
_Py_char2wchar() uses mbstowcs()
|
|
|
|
-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;
|
|
|
|
char encoding[100];
|
|
|
|
int is_ascii;
|
|
|
|
unsigned int i;
|
|
|
|
char* ascii_aliases[] = {
|
|
|
|
"ascii",
|
|
|
|
"646",
|
|
|
|
"ansi-x3.4-1968",
|
|
|
|
"ansi-x3-4-1968",
|
|
|
|
"ansi-x3.4-1986",
|
|
|
|
"cp367",
|
|
|
|
"csascii",
|
|
|
|
"ibm367",
|
|
|
|
"iso646-us",
|
|
|
|
"iso-646.irv-1991",
|
|
|
|
"iso-ir-6",
|
|
|
|
"us",
|
|
|
|
"us-ascii",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
loc = setlocale(LC_CTYPE, NULL);
|
|
|
|
if (loc == NULL)
|
|
|
|
goto error;
|
|
|
|
if (strcmp(loc, "C") != 0) {
|
|
|
|
/* 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:
|
|
|
|
/* if an error occured, force the ASCII encoding */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos)
|
|
|
|
{
|
|
|
|
char *result = NULL, *out;
|
|
|
|
size_t len, i;
|
|
|
|
wchar_t ch;
|
|
|
|
|
|
|
|
if (error_pos != NULL)
|
|
|
|
*error_pos = (size_t)-1;
|
|
|
|
|
|
|
|
len = wcslen(text);
|
|
|
|
|
|
|
|
result = PyMem_Malloc(len + 1); /* +1 for NUL byte */
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
out = result;
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
ch = text[i];
|
|
|
|
|
|
|
|
if (ch <= 0x7f) {
|
|
|
|
/* ASCII character */
|
|
|
|
*out++ = (char)ch;
|
|
|
|
}
|
|
|
|
else if (0xdc80 <= ch && ch <= 0xdcff) {
|
|
|
|
/* UTF-8b surrogate */
|
|
|
|
*out++ = (char)(ch - 0xdc00);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (error_pos != NULL)
|
|
|
|
*error_pos = i;
|
|
|
|
PyMem_Free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */
|
|
|
|
|
|
|
|
#if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC))
|
|
|
|
static wchar_t*
|
|
|
|
decode_ascii_surrogateescape(const char *arg, size_t *size)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-01-04 18:03:17 -04:00
|
|
|
if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
|
|
|
|
return NULL;
|
2015-01-04 18:05:39 -04:00
|
|
|
res = PyMem_RawMalloc(argsize*sizeof(wchar_t));
|
2012-12-03 20:34:47 -04:00
|
|
|
if (!res)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
in = (unsigned char*)arg;
|
|
|
|
out = res;
|
|
|
|
while(*in)
|
|
|
|
if(*in < 128)
|
|
|
|
*out++ = *in++;
|
|
|
|
else
|
|
|
|
*out++ = 0xdc00 + *in++;
|
|
|
|
*out = 0;
|
|
|
|
if (size != NULL)
|
|
|
|
*size = out - res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-07 18:45:39 -03: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.
|
|
|
|
|
|
|
|
Use _Py_wchar2char() to encode the character string back to a byte string.
|
|
|
|
|
2010-10-16 20:16:16 -03:00
|
|
|
Return a pointer to a newly allocated wide character string (use
|
2013-07-07 11:25:15 -03:00
|
|
|
PyMem_RawFree() to free the memory) and write the number of written wide
|
2010-10-16 20:16:16 -03:00
|
|
|
characters excluding the null character into *size if size is not NULL, or
|
2011-12-16 18:56:01 -04:00
|
|
|
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
|
|
|
|
error.
|
2010-11-08 19:30:46 -04:00
|
|
|
|
|
|
|
Conversion errors should never happen, unless there is a bug in the C
|
|
|
|
library. */
|
2010-10-07 18:45:39 -03:00
|
|
|
wchar_t*
|
2010-10-16 20:16:16 -03:00
|
|
|
_Py_char2wchar(const char* arg, size_t *size)
|
2010-10-07 18:45:39 -03:00
|
|
|
{
|
2012-11-12 18:04:02 -04:00
|
|
|
#ifdef __APPLE__
|
|
|
|
wchar_t *wstr;
|
|
|
|
wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
|
2012-11-12 18:32:21 -04:00
|
|
|
if (size != NULL) {
|
|
|
|
if (wstr != NULL)
|
|
|
|
*size = wcslen(wstr);
|
|
|
|
else
|
|
|
|
*size = (size_t)-1;
|
|
|
|
}
|
2012-11-12 18:04:02 -04:00
|
|
|
return wstr;
|
|
|
|
#else
|
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
|
|
|
|
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
if (force_ascii == -1)
|
|
|
|
force_ascii = check_force_ascii();
|
|
|
|
|
|
|
|
if (force_ascii) {
|
|
|
|
/* force ASCII encoding to workaround mbstowcs() issue */
|
|
|
|
res = decode_ascii_surrogateescape(arg, size);
|
|
|
|
if (res == NULL)
|
|
|
|
goto oom;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#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) {
|
2015-01-04 18:03:17 -04:00
|
|
|
if (argsize == PY_SSIZE_T_MAX)
|
|
|
|
goto oom;
|
|
|
|
argsize += 1;
|
|
|
|
if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
|
|
|
|
goto oom;
|
2015-01-04 18:05:39 -04:00
|
|
|
res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t));
|
2010-10-07 18:45:39 -03:00
|
|
|
if (!res)
|
|
|
|
goto oom;
|
2015-01-04 18:03:17 -04:00
|
|
|
count = mbstowcs(res, arg, argsize);
|
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) {
|
|
|
|
if (size != NULL)
|
|
|
|
*size = count;
|
2010-10-07 18:45:39 -03:00
|
|
|
return res;
|
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
|
|
|
}
|
|
|
|
/* 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;
|
2015-01-04 18:03:17 -04:00
|
|
|
if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
|
|
|
|
goto oom;
|
2013-07-07 11:25:15 -03:00
|
|
|
res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
|
2010-11-08 19:30:46 -04:00
|
|
|
if (!res)
|
|
|
|
goto oom;
|
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);
|
|
|
|
if (converted == 0)
|
|
|
|
/* Reached end of string; null char stored. */
|
|
|
|
break;
|
|
|
|
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. */
|
2013-07-07 11:25:15 -03:00
|
|
|
PyMem_RawFree(res);
|
2011-12-16 18:56:01 -04:00
|
|
|
if (size != NULL)
|
|
|
|
*size = (size_t)-2;
|
2010-10-07 18:45:39 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (converted == (size_t)-1) {
|
|
|
|
/* Conversion error. Escape as UTF-8b, and start over
|
|
|
|
in the initial shift state. */
|
|
|
|
*out++ = 0xdc00 + *in++;
|
|
|
|
argsize--;
|
|
|
|
memset(&mbs, 0, sizeof mbs);
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-29 21:42:39 -03:00
|
|
|
if (Py_UNICODE_IS_SURROGATE(*out)) {
|
2010-10-07 18:45:39 -03:00
|
|
|
/* Surrogate character. Escape the original
|
|
|
|
byte sequence with surrogateescape. */
|
|
|
|
argsize -= converted;
|
|
|
|
while (converted--)
|
|
|
|
*out++ = 0xdc00 + *in++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* successfully converted some bytes */
|
|
|
|
in += converted;
|
|
|
|
argsize -= converted;
|
|
|
|
out++;
|
|
|
|
}
|
2012-12-03 20:34:47 -04:00
|
|
|
if (size != NULL)
|
|
|
|
*size = out - res;
|
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. */
|
2012-12-03 20:34:47 -04:00
|
|
|
res = decode_ascii_surrogateescape(arg, size);
|
|
|
|
if (res == NULL)
|
2011-12-16 18:56:01 -04:00
|
|
|
goto oom;
|
2012-11-12 18:04:02 -04:00
|
|
|
#endif /* HAVE_MBRTOWC */
|
2010-10-07 18:45:39 -03:00
|
|
|
return res;
|
|
|
|
oom:
|
2011-12-16 18:56:01 -04:00
|
|
|
if (size != NULL)
|
|
|
|
*size = (size_t)-1;
|
2010-10-07 18:45:39 -03:00
|
|
|
return NULL;
|
2012-11-12 18:04:02 -04:00
|
|
|
#endif /* __APPLE__ */
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Encode a (wide) character string to the locale encoding with the
|
|
|
|
surrogateescape error handler (characters in range U+DC80..U+DCFF are
|
|
|
|
converted to bytes 0x80..0xFF).
|
|
|
|
|
|
|
|
This function is the reverse of _Py_char2wchar().
|
|
|
|
|
|
|
|
Return a pointer to a newly allocated byte string (use PyMem_Free() to free
|
2011-12-16 18:56:01 -04:00
|
|
|
the memory), or NULL on encoding or memory allocation error.
|
2010-11-08 18:43:46 -04:00
|
|
|
|
|
|
|
If error_pos is not NULL: *error_pos is the index of the invalid character
|
2011-12-16 18:56:01 -04:00
|
|
|
on encoding error, or (size_t)-1 otherwise. */
|
2010-10-07 18:45:39 -03:00
|
|
|
char*
|
2010-11-08 18:43:46 -04:00
|
|
|
_Py_wchar2char(const wchar_t *text, size_t *error_pos)
|
2010-10-07 18:45:39 -03:00
|
|
|
{
|
2012-11-12 18:04:02 -04:00
|
|
|
#ifdef __APPLE__
|
|
|
|
Py_ssize_t len;
|
|
|
|
PyObject *unicode, *bytes = NULL;
|
|
|
|
char *cpath;
|
|
|
|
|
|
|
|
unicode = PyUnicode_FromWideChar(text, wcslen(text));
|
2012-11-12 18:32:21 -04:00
|
|
|
if (unicode == NULL)
|
2012-11-12 18:04:02 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
|
|
|
|
Py_DECREF(unicode);
|
|
|
|
if (bytes == NULL) {
|
|
|
|
PyErr_Clear();
|
2012-11-12 18:32:21 -04:00
|
|
|
if (error_pos != NULL)
|
|
|
|
*error_pos = (size_t)-1;
|
2012-11-12 18:04:02 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = PyBytes_GET_SIZE(bytes);
|
|
|
|
cpath = PyMem_Malloc(len+1);
|
|
|
|
if (cpath == NULL) {
|
2012-11-12 18:32:21 -04:00
|
|
|
PyErr_Clear();
|
2012-11-12 18:04:02 -04:00
|
|
|
Py_DECREF(bytes);
|
2012-11-12 18:32:21 -04:00
|
|
|
if (error_pos != NULL)
|
|
|
|
*error_pos = (size_t)-1;
|
2012-11-12 18:04:02 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(cpath, PyBytes_AsString(bytes), len + 1);
|
|
|
|
Py_DECREF(bytes);
|
|
|
|
return cpath;
|
|
|
|
#else /* __APPLE__ */
|
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];
|
|
|
|
|
2012-12-03 20:34:47 -04:00
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
if (force_ascii == -1)
|
|
|
|
force_ascii = check_force_ascii();
|
|
|
|
|
|
|
|
if (force_ascii)
|
|
|
|
return encode_ascii_surrogateescape(text, error_pos);
|
|
|
|
#endif
|
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
/* 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) {
|
|
|
|
/* UTF-8b surrogate */
|
|
|
|
if (bytes != NULL) {
|
|
|
|
*bytes++ = c - 0xdc00;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
size++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buf[0] = c;
|
|
|
|
if (bytes != NULL)
|
|
|
|
converted = wcstombs(bytes, buf, size);
|
|
|
|
else
|
|
|
|
converted = wcstombs(NULL, buf, 0);
|
|
|
|
if (converted == (size_t)-1) {
|
|
|
|
if (result != NULL)
|
|
|
|
PyMem_Free(result);
|
2010-11-08 18:43:46 -04:00
|
|
|
if (error_pos != NULL)
|
|
|
|
*error_pos = i;
|
2010-10-07 18:45:39 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (bytes != NULL) {
|
|
|
|
bytes += converted;
|
|
|
|
size -= converted;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
size += converted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 */
|
|
|
|
result = PyMem_Malloc(size);
|
2012-11-12 18:32:21 -04:00
|
|
|
if (result == NULL) {
|
|
|
|
if (error_pos != NULL)
|
|
|
|
*error_pos = (size_t)-1;
|
2010-10-07 18:45:39 -03:00
|
|
|
return NULL;
|
2012-11-12 18:32:21 -04:00
|
|
|
}
|
2010-10-07 18:45:39 -03:00
|
|
|
bytes = result;
|
|
|
|
}
|
|
|
|
return result;
|
2012-11-12 18:04:02 -04:00
|
|
|
#endif /* __APPLE__ */
|
2010-10-07 18:45:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* In principle, this should use HAVE__WSTAT, and _wstat
|
|
|
|
should be detected by autoconf. However, no current
|
|
|
|
POSIX system provides that function, so testing for
|
|
|
|
it is pointless.
|
|
|
|
Not sure whether the MS_WINDOWS guards are necessary:
|
|
|
|
perhaps for cygwin/mingw builds?
|
|
|
|
*/
|
2010-10-07 19:09:40 -03:00
|
|
|
#if defined(HAVE_STAT) && !defined(MS_WINDOWS)
|
2010-10-07 19:53:43 -03:00
|
|
|
|
|
|
|
/* Get file status. Encode the path to the locale encoding. */
|
|
|
|
|
2010-10-07 19:09:40 -03:00
|
|
|
int
|
|
|
|
_Py_wstat(const wchar_t* path, struct stat *buf)
|
|
|
|
{
|
2010-10-07 18:45:39 -03:00
|
|
|
int err;
|
|
|
|
char *fname;
|
2010-11-08 18:43:46 -04:00
|
|
|
fname = _Py_wchar2char(path, NULL);
|
2010-10-07 18:45:39 -03:00
|
|
|
if (fname == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
err = stat(fname, buf);
|
|
|
|
PyMem_Free(fname);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-03 20:34:47 -04:00
|
|
|
#ifdef HAVE_STAT
|
|
|
|
|
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;
|
2011-11-16 19:51:38 -04:00
|
|
|
wchar_t *wpath;
|
2010-10-07 18:45:39 -03:00
|
|
|
|
2011-11-16 19:51:38 -04:00
|
|
|
wpath = PyUnicode_AsUnicode(path);
|
|
|
|
if (wpath == NULL)
|
2011-12-18 15:47:30 -04:00
|
|
|
return -2;
|
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;
|
2010-10-07 19:23:10 -03:00
|
|
|
PyObject *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;
|
2010-10-07 18:45:39 -03:00
|
|
|
ret = stat(PyBytes_AS_STRING(bytes), statbuf);
|
|
|
|
Py_DECREF(bytes);
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:34:47 -04:00
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!_PyVerify_fd(fd)) {
|
|
|
|
if (raise)
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = (HANDLE)_get_osfhandle(fd);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2013-08-27 19:53:59 -03:00
|
|
|
int flags;
|
|
|
|
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
|
|
|
|
if (!_PyVerify_fd(fd)) {
|
|
|
|
if (raise)
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = (HANDLE)_get_osfhandle(fd);
|
|
|
|
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;
|
|
|
|
if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
|
|
|
|
if (raise)
|
|
|
|
PyErr_SetFromWindowsErr(0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
2014-09-02 06:41:04 -03:00
|
|
|
#else
|
|
|
|
|
|
|
|
#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
|
|
|
|
if (ioctl_works != 0) {
|
|
|
|
/* fast-path: ioctl() only requires one syscall */
|
|
|
|
if (inheritable)
|
|
|
|
request = FIONCLEX;
|
|
|
|
else
|
|
|
|
request = FIOCLEX;
|
|
|
|
err = ioctl(fd, request, NULL);
|
|
|
|
if (!err) {
|
|
|
|
ioctl_works = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno != ENOTTY) {
|
|
|
|
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
|
|
|
|
Illumos-based OS for example. */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inheritable)
|
|
|
|
flags &= ~FD_CLOEXEC;
|
|
|
|
else
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
res = fcntl(fd, F_SETFD, flags);
|
|
|
|
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.
|
|
|
|
On success: return 0, on error: raise an exception if raise is nonzero
|
|
|
|
and return -1.
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open a file with the specified flags (wrapper to open() function).
|
|
|
|
The file descriptor is created non-inheritable. */
|
|
|
|
int
|
|
|
|
_Py_open(const char *pathname, int flags)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
fd = open(pathname, flags | O_NOINHERIT);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
#else
|
|
|
|
|
|
|
|
int *atomic_flag_works;
|
|
|
|
#ifdef O_CLOEXEC
|
|
|
|
atomic_flag_works = &_Py_open_cloexec_works;
|
|
|
|
flags |= O_CLOEXEC;
|
|
|
|
#else
|
|
|
|
atomic_flag_works = NULL;
|
|
|
|
#endif
|
|
|
|
fd = open(pathname, flags);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
if (set_inheritable(fd, 0, 0, atomic_flag_works) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* !MS_WINDOWS */
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open a file. Use _wfopen() on Windows, encode the path to the locale
|
|
|
|
encoding and use fopen() otherwise. The file descriptor is created
|
|
|
|
non-inheritable. */
|
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;
|
|
|
|
}
|
2010-11-08 18:43:46 -04:00
|
|
|
cpath = _Py_wchar2char(path, NULL);
|
2010-10-07 18:45:39 -03:00
|
|
|
if (cpath == NULL)
|
|
|
|
return NULL;
|
|
|
|
f = fopen(cpath, cmode);
|
|
|
|
PyMem_Free(cpath);
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
/* Wrapper to fopen(). The file descriptor is created non-inheritable. */
|
|
|
|
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
|
|
|
|
encoding and call fopen() otherwise. The file descriptor is created
|
|
|
|
non-inheritable.
|
2010-10-07 18:45:39 -03:00
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
Return the new file object on success, or NULL if the file cannot be open or
|
|
|
|
(if PyErr_Occurred()) on unicode error. */
|
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;
|
2010-10-07 18:45:39 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2011-11-16 19:51:38 -04:00
|
|
|
wchar_t *wpath;
|
2010-10-07 18:45:39 -03:00
|
|
|
wchar_t wmode[10];
|
|
|
|
int usize;
|
|
|
|
|
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;
|
|
|
|
}
|
2011-11-16 19:51:38 -04:00
|
|
|
wpath = PyUnicode_AsUnicode(path);
|
|
|
|
if (wpath == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2010-10-07 18:45:39 -03:00
|
|
|
usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
|
|
|
|
if (usize == 0)
|
|
|
|
return NULL;
|
|
|
|
|
2013-08-27 19:53:59 -03:00
|
|
|
f = _wfopen(wpath, wmode);
|
2010-10-07 18:45:39 -03:00
|
|
|
#else
|
2011-12-19 13:19:06 -04:00
|
|
|
PyObject *bytes;
|
|
|
|
if (!PyUnicode_FSConverter(path, &bytes))
|
2010-10-07 18:45:39 -03:00
|
|
|
return NULL;
|
|
|
|
f = fopen(PyBytes_AS_STRING(bytes), mode);
|
|
|
|
Py_DECREF(bytes);
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2010-11-08 18:43:46 -04:00
|
|
|
cpath = _Py_wchar2char(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));
|
2010-10-07 18:45:39 -03:00
|
|
|
PyMem_Free(cpath);
|
|
|
|
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 */
|
2010-10-16 20:16:16 -03:00
|
|
|
wbuf = _Py_char2wchar(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;
|
2010-11-08 18:43:46 -04:00
|
|
|
cpath = _Py_wchar2char(path, NULL);
|
2010-10-07 18:45:39 -03:00
|
|
|
if (cpath == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
res = realpath(cpath, cresolved_path);
|
|
|
|
PyMem_Free(cpath);
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
2010-10-16 19:55:47 -03:00
|
|
|
|
2010-10-16 20:16:16 -03:00
|
|
|
wresolved_path = _Py_char2wchar(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;
|
2010-10-16 20:16:16 -03:00
|
|
|
wname = _Py_char2wchar(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;
|
|
|
|
DWORD ftype;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!_PyVerify_fd(fd)) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
handle = (HANDLE)_get_osfhandle(fd);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the file type, ignore the error if it failed */
|
|
|
|
ftype = GetFileType(handle);
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
fd = dup(fd);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (fd < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Character files like console cannot be make non-inheritable */
|
|
|
|
if (ftype != FILE_TYPE_CHAR) {
|
|
|
|
if (_Py_set_inheritable(fd, 0, NULL) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (fd < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
fd = dup(fd);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (fd < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_Py_set_inheritable(fd, 0, NULL) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|