annotate Python/fileutils.c @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 37d36a029df0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
1 #include "Python.h"
80406
5db6d9ddf6e8 Issue #15835: Define PATH_MAX on HP-UX.
Stefan Krah <skrah@bytereef.org>
parents: 75353
diff changeset
2 #include "osdefs.h"
65164
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
3 #ifdef MS_WINDOWS
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
4 # include <windows.h>
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
5 #endif
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
6
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
7 #ifdef HAVE_LANGINFO_H
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
8 #include <locale.h>
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
9 #include <langinfo.h>
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
10 #endif
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
11
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
12 #ifdef HAVE_SYS_IOCTL_H
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
13 #include <sys/ioctl.h>
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
14 #endif
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
15
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
16 #ifdef HAVE_FCNTL_H
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
17 #include <fcntl.h>
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
18 #endif /* HAVE_FCNTL_H */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
19
80702
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
20 #ifdef __APPLE__
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
21 extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
22 #endif
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
23
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
24 #ifdef O_CLOEXEC
85584
98f82b124c7d Close #18954: Fix some typo in fileutils.c comments
Victor Stinner <victor.stinner@gmail.com>
parents: 85420
diff changeset
25 /* Does open() support the O_CLOEXEC flag? Possible values:
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
26
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
27 -1: unknown
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
28 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
29 1: open() supports O_CLOEXEC flag, close-on-exec is set
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
30
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
31 The flag is used by _Py_open(), io.FileIO and os.open() */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
32 int _Py_open_cloexec_works = -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
33 #endif
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
34
75353
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
35 PyObject *
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
36 _Py_device_encoding(int fd)
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
37 {
84331
dfc020b4b123 If MS_WIN64 is defined, MS_WINDOWS is also defined: #ifdef can be simplified.
Victor Stinner <victor.stinner@gmail.com>
parents: 84034
diff changeset
38 #if defined(MS_WINDOWS)
75353
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
39 UINT cp;
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
40 #endif
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
41 if (!_PyVerify_fd(fd) || !isatty(fd)) {
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
42 Py_RETURN_NONE;
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
43 }
84331
dfc020b4b123 If MS_WIN64 is defined, MS_WINDOWS is also defined: #ifdef can be simplified.
Victor Stinner <victor.stinner@gmail.com>
parents: 84034
diff changeset
44 #if defined(MS_WINDOWS)
75353
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
45 if (fd == 0)
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
46 cp = GetConsoleCP();
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
47 else if (fd == 1 || fd == 2)
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
48 cp = GetConsoleOutputCP();
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
49 else
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
50 cp = 0;
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
51 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
52 has no console */
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
53 if (cp != 0)
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
54 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
55 #elif defined(CODESET)
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
56 {
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
57 char *codeset = nl_langinfo(CODESET);
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
58 if (codeset != NULL && codeset[0] != 0)
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
59 return PyUnicode_FromString(codeset);
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
60 }
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
61 #endif
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
62 Py_RETURN_NONE;
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
63 }
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
64
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
65 #if !defined(__APPLE__) && !defined(MS_WINDOWS)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
66 extern int _Py_normalize_encoding(const char *, char *, size_t);
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
67
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
68 /* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale.
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
69 On these operating systems, nl_langinfo(CODESET) announces an alias of the
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
70 ASCII encoding, whereas mbstowcs() and wcstombs() functions use the
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
71 ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
72 locale.getpreferredencoding() codec. For example, if command line arguments
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
73 are decoded by mbstowcs() and encoded back by os.fsencode(), we get a
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
74 UnicodeEncodeError instead of retrieving the original byte string.
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
75
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
76 The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
77 nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
78 one byte in range 0x80-0xff can be decoded from the locale encoding. The
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
79 workaround is also enabled on error, for example if getting the locale
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
80 failed.
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
81
81535
Philip Jenvey <pjenvey@underboss.org>
parents: 81259
diff changeset
82 Values of force_ascii:
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
83
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
84 1: the workaround is used: _Py_wchar2char() uses
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
85 encode_ascii_surrogateescape() and _Py_char2wchar() uses
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
86 decode_ascii_surrogateescape()
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
87 0: the workaround is not used: _Py_wchar2char() uses wcstombs() and
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
88 _Py_char2wchar() uses mbstowcs()
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
89 -1: unknown, need to call check_force_ascii() to get the value
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
90 */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
91 static int force_ascii = -1;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
92
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
93 static int
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
94 check_force_ascii(void)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
95 {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
96 char *loc;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
97 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
98 char *codeset, **alias;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
99 char encoding[100];
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
100 int is_ascii;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
101 unsigned int i;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
102 char* ascii_aliases[] = {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
103 "ascii",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
104 "646",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
105 "ansi-x3.4-1968",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
106 "ansi-x3-4-1968",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
107 "ansi-x3.4-1986",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
108 "cp367",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
109 "csascii",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
110 "ibm367",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
111 "iso646-us",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
112 "iso-646.irv-1991",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
113 "iso-ir-6",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
114 "us",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
115 "us-ascii",
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
116 NULL
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
117 };
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
118 #endif
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
119
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
120 loc = setlocale(LC_CTYPE, NULL);
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
121 if (loc == NULL)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
122 goto error;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
123 if (strcmp(loc, "C") != 0) {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
124 /* the LC_CTYPE locale is different than C */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
125 return 0;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
126 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
127
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
128 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
129 codeset = nl_langinfo(CODESET);
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
130 if (!codeset || codeset[0] == '\0') {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
131 /* CODESET is not set or empty */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
132 goto error;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
133 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
134 if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding)))
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
135 goto error;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
136
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
137 is_ascii = 0;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
138 for (alias=ascii_aliases; *alias != NULL; alias++) {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
139 if (strcmp(encoding, *alias) == 0) {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
140 is_ascii = 1;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
141 break;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
142 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
143 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
144 if (!is_ascii) {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
145 /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
146 return 0;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
147 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
148
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
149 for (i=0x80; i<0xff; i++) {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
150 unsigned char ch;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
151 wchar_t wch;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
152 size_t res;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
153
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
154 ch = (unsigned char)i;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
155 res = mbstowcs(&wch, (char*)&ch, 1);
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
156 if (res != (size_t)-1) {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
157 /* decoding a non-ASCII character from the locale encoding succeed:
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
158 the locale encoding is not ASCII, force ASCII */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
159 return 1;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
160 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
161 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
162 /* None of the bytes in the range 0x80-0xff can be decoded from the locale
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
163 encoding: the locale encoding is really ASCII */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
164 return 0;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
165 #else
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
166 /* nl_langinfo(CODESET) is not available: always force ASCII */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
167 return 1;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
168 #endif
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
169
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
170 error:
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
171 /* if an error occured, force the ASCII encoding */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
172 return 1;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
173 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
174
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
175 static char*
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
176 encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
177 {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
178 char *result = NULL, *out;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
179 size_t len, i;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
180 wchar_t ch;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
181
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
182 if (error_pos != NULL)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
183 *error_pos = (size_t)-1;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
184
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
185 len = wcslen(text);
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
186
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
187 result = PyMem_Malloc(len + 1); /* +1 for NUL byte */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
188 if (result == NULL)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
189 return NULL;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
190
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
191 out = result;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
192 for (i=0; i<len; i++) {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
193 ch = text[i];
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
194
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
195 if (ch <= 0x7f) {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
196 /* ASCII character */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
197 *out++ = (char)ch;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
198 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
199 else if (0xdc80 <= ch && ch <= 0xdcff) {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
200 /* UTF-8b surrogate */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
201 *out++ = (char)(ch - 0xdc00);
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
202 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
203 else {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
204 if (error_pos != NULL)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
205 *error_pos = i;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
206 PyMem_Free(result);
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
207 return NULL;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
208 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
209 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
210 *out = '\0';
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
211 return result;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
212 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
213 #endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
214
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
215 #if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC))
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
216 static wchar_t*
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
217 decode_ascii_surrogateescape(const char *arg, size_t *size)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
218 {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
219 wchar_t *res;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
220 unsigned char *in;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
221 wchar_t *out;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
222
84493
fc01f9497da7 Issue #18203: Fix decode_ascii_surrogateescape(), use PyMem_RawMalloc() as _Py_char2wchar()
Victor Stinner <victor.stinner@gmail.com>
parents: 84490
diff changeset
223 res = PyMem_RawMalloc((strlen(arg)+1)*sizeof(wchar_t));
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
224 if (!res)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
225 return NULL;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
226
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
227 in = (unsigned char*)arg;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
228 out = res;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
229 while(*in)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
230 if(*in < 128)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
231 *out++ = *in++;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
232 else
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
233 *out++ = 0xdc00 + *in++;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
234 *out = 0;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
235 if (size != NULL)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
236 *size = out - res;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
237 return res;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
238 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
239 #endif
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
240
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
241
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
242 /* Decode a byte string from the locale encoding with the
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
243 surrogateescape error handler (undecodable bytes are decoded as characters
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
244 in range U+DC80..U+DCFF). If a byte sequence can be decoded as a surrogate
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
245 character, escape the bytes using the surrogateescape error handler instead
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
246 of decoding them.
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
247
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
248 Use _Py_wchar2char() to encode the character string back to a byte string.
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
249
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
250 Return a pointer to a newly allocated wide character string (use
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
251 PyMem_RawFree() to free the memory) and write the number of written wide
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
252 characters excluding the null character into *size if size is not NULL, or
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
253 NULL on error (decoding or memory allocation error). If size is not NULL,
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
254 *size is set to (size_t)-1 on memory error and (size_t)-2 on decoding
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
255 error.
66094
76a1c5e88352 _Py_char2wchar() frees the memory on conversion error
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66092
diff changeset
256
76a1c5e88352 _Py_char2wchar() frees the memory on conversion error
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66092
diff changeset
257 Conversion errors should never happen, unless there is a bug in the C
76a1c5e88352 _Py_char2wchar() frees the memory on conversion error
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66092
diff changeset
258 library. */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
259 wchar_t*
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
260 _Py_char2wchar(const char* arg, size_t *size)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
261 {
80702
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
262 #ifdef __APPLE__
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
263 wchar_t *wstr;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
264 wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
265 if (size != NULL) {
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
266 if (wstr != NULL)
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
267 *size = wcslen(wstr);
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
268 else
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
269 *size = (size_t)-1;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
270 }
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
271 return wstr;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
272 #else
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
273 wchar_t *res;
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
274 size_t argsize;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
275 size_t count;
83673
2ea629da4f54 Fix a compiler warning: in and out are unused in _Py_char2wchar() if
Victor Stinner <victor.stinner@gmail.com>
parents: 81535
diff changeset
276 #ifdef HAVE_MBRTOWC
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
277 unsigned char *in;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
278 wchar_t *out;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
279 mbstate_t mbs;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
280 #endif
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
281
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
282 #ifndef MS_WINDOWS
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
283 if (force_ascii == -1)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
284 force_ascii = check_force_ascii();
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
285
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
286 if (force_ascii) {
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
287 /* force ASCII encoding to workaround mbstowcs() issue */
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
288 res = decode_ascii_surrogateescape(arg, size);
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
289 if (res == NULL)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
290 goto oom;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
291 return res;
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
292 }
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
293 #endif
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
294
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
295 #ifdef HAVE_BROKEN_MBSTOWCS
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
296 /* Some platforms have a broken implementation of
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
297 * mbstowcs which does not count the characters that
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
298 * would result from conversion. Use an upper bound.
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
299 */
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
300 argsize = strlen(arg);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
301 #else
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
302 argsize = mbstowcs(NULL, arg, 0);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
303 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
304 if (argsize != (size_t)-1) {
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
305 res = (wchar_t *)PyMem_RawMalloc((argsize+1)*sizeof(wchar_t));
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
306 if (!res)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
307 goto oom;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
308 count = mbstowcs(res, arg, argsize+1);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
309 if (count != (size_t)-1) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
310 wchar_t *tmp;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
311 /* Only use the result if it contains no
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
312 surrogate characters. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
313 for (tmp = res; *tmp != 0 &&
80057
13dd8199c112 Issue #16330: Use surrogate-related macros
Victor Stinner <victor.stinner@gmail.com>
parents: 75353
diff changeset
314 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
315 ;
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
316 if (*tmp == 0) {
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
317 if (size != NULL)
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
318 *size = count;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
319 return res;
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
320 }
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
321 }
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
322 PyMem_RawFree(res);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
323 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
324 /* Conversion failed. Fall back to escaping with surrogateescape. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
325 #ifdef HAVE_MBRTOWC
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
326 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
327
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
328 /* Overallocate; as multi-byte characters are in the argument, the
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
329 actual output could use less memory. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
330 argsize = strlen(arg) + 1;
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
331 res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
66094
76a1c5e88352 _Py_char2wchar() frees the memory on conversion error
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66092
diff changeset
332 if (!res)
76a1c5e88352 _Py_char2wchar() frees the memory on conversion error
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66092
diff changeset
333 goto oom;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
334 in = (unsigned char*)arg;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
335 out = res;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
336 memset(&mbs, 0, sizeof mbs);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
337 while (argsize) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
338 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
339 if (converted == 0)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
340 /* Reached end of string; null char stored. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
341 break;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
342 if (converted == (size_t)-2) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
343 /* Incomplete character. This should never happen,
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
344 since we provide everything that we have -
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
345 unless there is a bug in the C library, or I
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
346 misunderstood how mbrtowc works. */
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
347 PyMem_RawFree(res);
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
348 if (size != NULL)
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
349 *size = (size_t)-2;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
350 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
351 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
352 if (converted == (size_t)-1) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
353 /* Conversion error. Escape as UTF-8b, and start over
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
354 in the initial shift state. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
355 *out++ = 0xdc00 + *in++;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
356 argsize--;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
357 memset(&mbs, 0, sizeof mbs);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
358 continue;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
359 }
80057
13dd8199c112 Issue #16330: Use surrogate-related macros
Victor Stinner <victor.stinner@gmail.com>
parents: 75353
diff changeset
360 if (Py_UNICODE_IS_SURROGATE(*out)) {
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
361 /* Surrogate character. Escape the original
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
362 byte sequence with surrogateescape. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
363 argsize -= converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
364 while (converted--)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
365 *out++ = 0xdc00 + *in++;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
366 continue;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
367 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
368 /* successfully converted some bytes */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
369 in += converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
370 argsize -= converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
371 out++;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
372 }
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
373 if (size != NULL)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
374 *size = out - res;
80702
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
375 #else /* HAVE_MBRTOWC */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
376 /* Cannot use C locale for escaping; manually escape as if charset
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
377 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
378 correctly in the locale's charset, which must be an ASCII superset. */
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
379 res = decode_ascii_surrogateescape(arg, size);
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
380 if (res == NULL)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
381 goto oom;
80702
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
382 #endif /* HAVE_MBRTOWC */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
383 return res;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
384 oom:
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
385 if (size != NULL)
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
386 *size = (size_t)-1;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
387 return NULL;
80702
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
388 #endif /* __APPLE__ */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
389 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
390
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
391 /* Encode a (wide) character string to the locale encoding with the
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
392 surrogateescape error handler (characters in range U+DC80..U+DCFF are
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
393 converted to bytes 0x80..0xFF).
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
394
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
395 This function is the reverse of _Py_char2wchar().
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
396
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
397 Return a pointer to a newly allocated byte string (use PyMem_Free() to free
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
398 the memory), or NULL on encoding or memory allocation error.
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
399
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
400 If error_pos is not NULL: *error_pos is the index of the invalid character
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
401 on encoding error, or (size_t)-1 otherwise. */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
402 char*
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
403 _Py_wchar2char(const wchar_t *text, size_t *error_pos)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
404 {
80702
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
405 #ifdef __APPLE__
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
406 Py_ssize_t len;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
407 PyObject *unicode, *bytes = NULL;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
408 char *cpath;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
409
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
410 unicode = PyUnicode_FromWideChar(text, wcslen(text));
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
411 if (unicode == NULL)
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
412 return NULL;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
413
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
414 bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
415 Py_DECREF(unicode);
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
416 if (bytes == NULL) {
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
417 PyErr_Clear();
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
418 if (error_pos != NULL)
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
419 *error_pos = (size_t)-1;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
420 return NULL;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
421 }
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
422
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
423 len = PyBytes_GET_SIZE(bytes);
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
424 cpath = PyMem_Malloc(len+1);
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
425 if (cpath == NULL) {
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
426 PyErr_Clear();
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
427 Py_DECREF(bytes);
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
428 if (error_pos != NULL)
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
429 *error_pos = (size_t)-1;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
430 return NULL;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
431 }
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
432 memcpy(cpath, PyBytes_AsString(bytes), len + 1);
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
433 Py_DECREF(bytes);
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
434 return cpath;
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
435 #else /* __APPLE__ */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
436 const size_t len = wcslen(text);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
437 char *result = NULL, *bytes = NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
438 size_t i, size, converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
439 wchar_t c, buf[2];
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
440
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
441 #ifndef MS_WINDOWS
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
442 if (force_ascii == -1)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
443 force_ascii = check_force_ascii();
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
444
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
445 if (force_ascii)
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
446 return encode_ascii_surrogateescape(text, error_pos);
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
447 #endif
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
448
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
449 /* The function works in two steps:
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
450 1. compute the length of the output buffer in bytes (size)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
451 2. outputs the bytes */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
452 size = 0;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
453 buf[1] = 0;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
454 while (1) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
455 for (i=0; i < len; i++) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
456 c = text[i];
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
457 if (c >= 0xdc80 && c <= 0xdcff) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
458 /* UTF-8b surrogate */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
459 if (bytes != NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
460 *bytes++ = c - 0xdc00;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
461 size--;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
462 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
463 else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
464 size++;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
465 continue;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
466 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
467 else {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
468 buf[0] = c;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
469 if (bytes != NULL)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
470 converted = wcstombs(bytes, buf, size);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
471 else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
472 converted = wcstombs(NULL, buf, 0);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
473 if (converted == (size_t)-1) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
474 if (result != NULL)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
475 PyMem_Free(result);
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
476 if (error_pos != NULL)
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
477 *error_pos = i;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
478 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
479 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
480 if (bytes != NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
481 bytes += converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
482 size -= converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
483 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
484 else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
485 size += converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
486 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
487 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
488 if (result != NULL) {
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
489 *bytes = '\0';
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
490 break;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
491 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
492
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
493 size += 1; /* nul byte at the end */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
494 result = PyMem_Malloc(size);
80702
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
495 if (result == NULL) {
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
496 if (error_pos != NULL)
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
497 *error_pos = (size_t)-1;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
498 return NULL;
80702
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
499 }
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
500 bytes = result;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
501 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
502 return result;
80702
c838c9b117f1 Issue #16416: On Mac OS X, operating system data are now always
Victor Stinner <victor.stinner@gmail.com>
parents: 66094
diff changeset
503 #endif /* __APPLE__ */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
504 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
505
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
506 /* In principle, this should use HAVE__WSTAT, and _wstat
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
507 should be detected by autoconf. However, no current
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
508 POSIX system provides that function, so testing for
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
509 it is pointless.
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
510 Not sure whether the MS_WINDOWS guards are necessary:
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
511 perhaps for cygwin/mingw builds?
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
512 */
65164
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
513 #if defined(HAVE_STAT) && !defined(MS_WINDOWS)
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
514
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
515 /* Get file status. Encode the path to the locale encoding. */
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
516
65164
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
517 int
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
518 _Py_wstat(const wchar_t* path, struct stat *buf)
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
519 {
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
520 int err;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
521 char *fname;
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
522 fname = _Py_wchar2char(path, NULL);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
523 if (fname == NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
524 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
525 return -1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
526 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
527 err = stat(fname, buf);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
528 PyMem_Free(fname);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
529 return err;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
530 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
531 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
532
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
533 #ifdef HAVE_STAT
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
534
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
535 /* Call _wstat() on Windows, or encode the path to the filesystem encoding and
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
536 call stat() otherwise. Only fill st_mode attribute on Windows.
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
537
74058
2a140b01d343 import.c now catchs _Py_stat() exceptions
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74002
diff changeset
538 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
2a140b01d343 import.c now catchs _Py_stat() exceptions
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74002
diff changeset
539 raised. */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
540
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
541 int
65165
2147cc152227 _Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65164
diff changeset
542 _Py_stat(PyObject *path, struct stat *statbuf)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
543 {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
544 #ifdef MS_WINDOWS
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
545 int err;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
546 struct _stat wstatbuf;
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
547 wchar_t *wpath;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
548
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
549 wpath = PyUnicode_AsUnicode(path);
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
550 if (wpath == NULL)
74058
2a140b01d343 import.c now catchs _Py_stat() exceptions
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74002
diff changeset
551 return -2;
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
552 err = _wstat(wpath, &wstatbuf);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
553 if (!err)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
554 statbuf->st_mode = wstatbuf.st_mode;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
555 return err;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
556 #else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
557 int ret;
65165
2147cc152227 _Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65164
diff changeset
558 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
559 if (bytes == NULL)
74058
2a140b01d343 import.c now catchs _Py_stat() exceptions
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74002
diff changeset
560 return -2;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
561 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
562 Py_DECREF(bytes);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
563 return ret;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
564 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
565 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
566
81257
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
567 #endif
c256764e2b3f Issue #16455: On FreeBSD and Solaris, if the locale is C, the
Victor Stinner <victor.stinner@gmail.com>
parents: 80704
diff changeset
568
86257
eeec58c57964 Don't export internal symbols ("make smelly")
Antoine Pitrou <solipsis@pitrou.net>
parents: 85584
diff changeset
569 static int
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
570 get_inheritable(int fd, int raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
571 {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
572 #ifdef MS_WINDOWS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
573 HANDLE handle;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
574 DWORD flags;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
575
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
576 if (!_PyVerify_fd(fd)) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
577 if (raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
578 PyErr_SetFromErrno(PyExc_OSError);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
579 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
580 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
581
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
582 handle = (HANDLE)_get_osfhandle(fd);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
583 if (handle == INVALID_HANDLE_VALUE) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
584 if (raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
585 PyErr_SetFromWindowsErr(0);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
586 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
587 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
588
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
589 if (!GetHandleInformation(handle, &flags)) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
590 if (raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
591 PyErr_SetFromWindowsErr(0);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
592 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
593 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
594
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
595 return (flags & HANDLE_FLAG_INHERIT);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
596 #else
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
597 int flags;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
598
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
599 flags = fcntl(fd, F_GETFD, 0);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
600 if (flags == -1) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
601 if (raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
602 PyErr_SetFromErrno(PyExc_OSError);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
603 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
604 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
605 return !(flags & FD_CLOEXEC);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
606 #endif
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
607 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
608
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
609 /* Get the inheritable flag of the specified file descriptor.
85584
98f82b124c7d Close #18954: Fix some typo in fileutils.c comments
Victor Stinner <victor.stinner@gmail.com>
parents: 85420
diff changeset
610 Return 1 if the file descriptor can be inherited, 0 if it cannot,
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
611 raise an exception and return -1 on error. */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
612 int
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
613 _Py_get_inheritable(int fd)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
614 {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
615 return get_inheritable(fd, 1);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
616 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
617
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
618 static int
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
619 set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
620 {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
621 #ifdef MS_WINDOWS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
622 HANDLE handle;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
623 DWORD flags;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
624 #elif defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
625 int request;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
626 int err;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
627 #elif defined(HAVE_FCNTL_H)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
628 int flags;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
629 int res;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
630 #endif
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
631
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
632 /* atomic_flag_works can only be used to make the file descriptor
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
633 non-inheritable */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
634 assert(!(atomic_flag_works != NULL && inheritable));
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
635
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
636 if (atomic_flag_works != NULL && !inheritable) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
637 if (*atomic_flag_works == -1) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
638 int inheritable = get_inheritable(fd, raise);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
639 if (inheritable == -1)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
640 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
641 *atomic_flag_works = !inheritable;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
642 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
643
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
644 if (*atomic_flag_works)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
645 return 0;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
646 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
647
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
648 #ifdef MS_WINDOWS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
649 if (!_PyVerify_fd(fd)) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
650 if (raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
651 PyErr_SetFromErrno(PyExc_OSError);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
652 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
653 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
654
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
655 handle = (HANDLE)_get_osfhandle(fd);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
656 if (handle == INVALID_HANDLE_VALUE) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
657 if (raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
658 PyErr_SetFromWindowsErr(0);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
659 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
660 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
661
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
662 if (inheritable)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
663 flags = HANDLE_FLAG_INHERIT;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
664 else
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
665 flags = 0;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
666 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
667 if (raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
668 PyErr_SetFromWindowsErr(0);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
669 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
670 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
671 return 0;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
672
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
673 #elif defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
674 if (inheritable)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
675 request = FIONCLEX;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
676 else
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
677 request = FIOCLEX;
87098
e539761d45e3 Add unused third arg for the benefit of Valgrind.
Stefan Krah <skrah@bytereef.org>
parents: 86257
diff changeset
678 err = ioctl(fd, request, NULL);
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
679 if (err) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
680 if (raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
681 PyErr_SetFromErrno(PyExc_OSError);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
682 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
683 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
684 return 0;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
685
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
686 #else
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
687 flags = fcntl(fd, F_GETFD);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
688 if (flags < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
689 if (raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
690 PyErr_SetFromErrno(PyExc_OSError);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
691 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
692 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
693
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
694 if (inheritable)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
695 flags &= ~FD_CLOEXEC;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
696 else
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
697 flags |= FD_CLOEXEC;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
698 res = fcntl(fd, F_SETFD, flags);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
699 if (res < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
700 if (raise)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
701 PyErr_SetFromErrno(PyExc_OSError);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
702 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
703 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
704 return 0;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
705 #endif
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
706 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
707
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
708 /* Make the file descriptor non-inheritable.
85584
98f82b124c7d Close #18954: Fix some typo in fileutils.c comments
Victor Stinner <victor.stinner@gmail.com>
parents: 85420
diff changeset
709 Return 0 on success, set errno and return -1 on error. */
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
710 static int
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
711 make_non_inheritable(int fd)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
712 {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
713 return set_inheritable(fd, 0, 0, NULL);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
714 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
715
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
716 /* Set the inheritable flag of the specified file descriptor.
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
717 On success: return 0, on error: raise an exception if raise is nonzero
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
718 and return -1.
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
719
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
720 If atomic_flag_works is not NULL:
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
721
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
722 * if *atomic_flag_works==-1, check if the inheritable is set on the file
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
723 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
724 set the inheritable flag
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
725 * if *atomic_flag_works==1: do nothing
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
726 * if *atomic_flag_works==0: set inheritable flag to False
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
727
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
728 Set atomic_flag_works to NULL if no atomic flag was used to create the
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
729 file descriptor.
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
730
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
731 atomic_flag_works can only be used to make a file descriptor
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
732 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
733 int
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
734 _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
735 {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
736 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
737 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
738
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
739 /* Open a file with the specified flags (wrapper to open() function).
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
740 The file descriptor is created non-inheritable. */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
741 int
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
742 _Py_open(const char *pathname, int flags)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
743 {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
744 int fd;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
745 #ifdef MS_WINDOWS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
746 fd = open(pathname, flags | O_NOINHERIT);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
747 if (fd < 0)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
748 return fd;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
749 #else
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
750
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
751 int *atomic_flag_works;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
752 #ifdef O_CLOEXEC
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
753 atomic_flag_works = &_Py_open_cloexec_works;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
754 flags |= O_CLOEXEC;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
755 #else
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
756 atomic_flag_works = NULL;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
757 #endif
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
758 fd = open(pathname, flags);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
759 if (fd < 0)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
760 return fd;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
761
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
762 if (set_inheritable(fd, 0, 0, atomic_flag_works) < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
763 close(fd);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
764 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
765 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
766 #endif /* !MS_WINDOWS */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
767 return fd;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
768 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
769
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
770 /* Open a file. Use _wfopen() on Windows, encode the path to the locale
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
771 encoding and use fopen() otherwise. The file descriptor is created
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
772 non-inheritable. */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
773 FILE *
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
774 _Py_wfopen(const wchar_t *path, const wchar_t *mode)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
775 {
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
776 FILE *f;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
777 #ifndef MS_WINDOWS
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
778 char *cpath;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
779 char cmode[10];
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
780 size_t r;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
781 r = wcstombs(cmode, mode, 10);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
782 if (r == (size_t)-1 || r >= 10) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
783 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
784 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
785 }
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
786 cpath = _Py_wchar2char(path, NULL);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
787 if (cpath == NULL)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
788 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
789 f = fopen(cpath, cmode);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
790 PyMem_Free(cpath);
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
791 #else
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
792 f = _wfopen(path, mode);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
793 #endif
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
794 if (f == NULL)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
795 return NULL;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
796 if (make_non_inheritable(fileno(f)) < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
797 fclose(f);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
798 return NULL;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
799 }
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
800 return f;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
801 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
802
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
803 /* Wrapper to fopen(). The file descriptor is created non-inheritable. */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
804 FILE*
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
805 _Py_fopen(const char *pathname, const char *mode)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
806 {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
807 FILE *f = fopen(pathname, mode);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
808 if (f == NULL)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
809 return NULL;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
810 if (make_non_inheritable(fileno(f)) < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
811 fclose(f);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
812 return NULL;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
813 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
814 return f;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
815 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
816
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
817 /* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
818 encoding and call fopen() otherwise. The file descriptor is created
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
819 non-inheritable.
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
820
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
821 Return the new file object on success, or NULL if the file cannot be open or
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
822 (if PyErr_Occurred()) on unicode error. */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
823 FILE*
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
824 _Py_fopen_obj(PyObject *path, const char *mode)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
825 {
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
826 FILE *f;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
827 #ifdef MS_WINDOWS
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
828 wchar_t *wpath;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
829 wchar_t wmode[10];
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
830 int usize;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
831
74129
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74085
diff changeset
832 if (!PyUnicode_Check(path)) {
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74085
diff changeset
833 PyErr_Format(PyExc_TypeError,
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74085
diff changeset
834 "str file path expected under Windows, got %R",
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74085
diff changeset
835 Py_TYPE(path));
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74085
diff changeset
836 return NULL;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74085
diff changeset
837 }
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
838 wpath = PyUnicode_AsUnicode(path);
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
839 if (wpath == NULL)
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
840 return NULL;
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
841
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
842 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
843 if (usize == 0)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
844 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
845
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
846 f = _wfopen(wpath, wmode);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
847 #else
74085
a882aa30a713 _Py_fopen now allows bytes filenames under non-Windows platforms.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74058
diff changeset
848 PyObject *bytes;
a882aa30a713 _Py_fopen now allows bytes filenames under non-Windows platforms.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74058
diff changeset
849 if (!PyUnicode_FSConverter(path, &bytes))
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
850 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
851 f = fopen(PyBytes_AS_STRING(bytes), mode);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
852 Py_DECREF(bytes);
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
853 #endif
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
854 if (f == NULL)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
855 return NULL;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
856 if (make_non_inheritable(fileno(f)) < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
857 fclose(f);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
858 return NULL;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
859 }
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
860 return f;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
861 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
862
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
863 #ifdef HAVE_READLINK
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
864
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
865 /* Read value of symbolic link. Encode the path to the locale encoding, decode
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
866 the result from the locale encoding. Return -1 on error. */
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
867
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
868 int
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
869 _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
870 {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
871 char *cpath;
87117
8bbd215940f1 fileutils.c: use MAXPATHLEN instead of PATH_MAX
Victor Stinner <victor.stinner@gmail.com>
parents: 81258
diff changeset
872 char cbuf[MAXPATHLEN];
65409
e1dc9861b43f _Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65346
diff changeset
873 wchar_t *wbuf;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
874 int res;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
875 size_t r1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
876
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
877 cpath = _Py_wchar2char(path, NULL);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
878 if (cpath == NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
879 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
880 return -1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
881 }
87117
8bbd215940f1 fileutils.c: use MAXPATHLEN instead of PATH_MAX
Victor Stinner <victor.stinner@gmail.com>
parents: 81258
diff changeset
882 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
883 PyMem_Free(cpath);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
884 if (res == -1)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
885 return -1;
87117
8bbd215940f1 fileutils.c: use MAXPATHLEN instead of PATH_MAX
Victor Stinner <victor.stinner@gmail.com>
parents: 81258
diff changeset
886 if (res == Py_ARRAY_LENGTH(cbuf)) {
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
887 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
888 return -1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
889 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
890 cbuf[res] = '\0'; /* buf will be null terminated */
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
891 wbuf = _Py_char2wchar(cbuf, &r1);
65410
6dcba93d6262 _Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65409
diff changeset
892 if (wbuf == NULL) {
6dcba93d6262 _Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65409
diff changeset
893 errno = EINVAL;
6dcba93d6262 _Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65409
diff changeset
894 return -1;
6dcba93d6262 _Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65409
diff changeset
895 }
65409
e1dc9861b43f _Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65346
diff changeset
896 if (bufsiz <= r1) {
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
897 PyMem_RawFree(wbuf);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
898 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
899 return -1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
900 }
65409
e1dc9861b43f _Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65346
diff changeset
901 wcsncpy(buf, wbuf, bufsiz);
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
902 PyMem_RawFree(wbuf);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
903 return (int)r1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
904 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
905 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
906
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
907 #ifdef HAVE_REALPATH
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
908
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
909 /* Return the canonicalized absolute pathname. Encode path to the locale
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
910 encoding, decode the result from the locale encoding.
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
911 Return NULL on error. */
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
912
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
913 wchar_t*
65166
8fd5d06462e6 _Py_wrealpath() requires the size of the output buffer
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65165
diff changeset
914 _Py_wrealpath(const wchar_t *path,
8fd5d06462e6 _Py_wrealpath() requires the size of the output buffer
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65165
diff changeset
915 wchar_t *resolved_path, size_t resolved_path_size)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
916 {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
917 char *cpath;
87117
8bbd215940f1 fileutils.c: use MAXPATHLEN instead of PATH_MAX
Victor Stinner <victor.stinner@gmail.com>
parents: 81258
diff changeset
918 char cresolved_path[MAXPATHLEN];
65411
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
919 wchar_t *wresolved_path;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
920 char *res;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
921 size_t r;
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
922 cpath = _Py_wchar2char(path, NULL);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
923 if (cpath == NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
924 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
925 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
926 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
927 res = realpath(cpath, cresolved_path);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
928 PyMem_Free(cpath);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
929 if (res == NULL)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
930 return NULL;
65411
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
931
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
932 wresolved_path = _Py_char2wchar(cresolved_path, &r);
65411
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
933 if (wresolved_path == NULL) {
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
934 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
935 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
936 }
65411
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
937 if (resolved_path_size <= r) {
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
938 PyMem_RawFree(wresolved_path);
65411
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
939 errno = EINVAL;
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
940 return NULL;
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
941 }
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
942 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
943 PyMem_RawFree(wresolved_path);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
944 return resolved_path;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
945 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
946 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
947
65312
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
948 /* Get the current directory. size is the buffer size in wide characters
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
949 including the null character. Decode the path from the locale encoding.
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
950 Return NULL on error. */
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
951
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
952 wchar_t*
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
953 _Py_wgetcwd(wchar_t *buf, size_t size)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
954 {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
955 #ifdef MS_WINDOWS
84034
88a21c5a97ef Issue #9566: Fix compiler warning on Windows 64-bit
Victor Stinner <victor.stinner@gmail.com>
parents: 83673
diff changeset
956 int isize = (int)Py_MIN(size, INT_MAX);
88a21c5a97ef Issue #9566: Fix compiler warning on Windows 64-bit
Victor Stinner <victor.stinner@gmail.com>
parents: 83673
diff changeset
957 return _wgetcwd(buf, isize);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
958 #else
87117
8bbd215940f1 fileutils.c: use MAXPATHLEN instead of PATH_MAX
Victor Stinner <victor.stinner@gmail.com>
parents: 81258
diff changeset
959 char fname[MAXPATHLEN];
65312
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
960 wchar_t *wname;
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
961 size_t len;
65312
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
962
87117
8bbd215940f1 fileutils.c: use MAXPATHLEN instead of PATH_MAX
Victor Stinner <victor.stinner@gmail.com>
parents: 81258
diff changeset
963 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
964 return NULL;
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
965 wname = _Py_char2wchar(fname, &len);
65312
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
966 if (wname == NULL)
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
967 return NULL;
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
968 if (size <= len) {
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
969 PyMem_RawFree(wname);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
970 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
971 }
65312
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
972 wcsncpy(buf, wname, size);
84490
18bb92b0c458 Issue #18203: Replace malloc() with PyMem_RawMalloc() at Python initialization
Victor Stinner <victor.stinner@gmail.com>
parents: 84331
diff changeset
973 PyMem_RawFree(wname);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
974 return buf;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
975 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
976 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
977
85420
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
978 /* Duplicate a file descriptor. The new file descriptor is created as
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
979 non-inheritable. Return a new file descriptor on success, raise an OSError
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
980 exception and return -1 on error.
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
981
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
982 The GIL is released to call dup(). The caller must hold the GIL. */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
983 int
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
984 _Py_dup(int fd)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
985 {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
986 #ifdef MS_WINDOWS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
987 HANDLE handle;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
988 DWORD ftype;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
989 #endif
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
990
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
991 if (!_PyVerify_fd(fd)) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
992 PyErr_SetFromErrno(PyExc_OSError);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
993 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
994 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
995
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
996 #ifdef MS_WINDOWS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
997 handle = (HANDLE)_get_osfhandle(fd);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
998 if (handle == INVALID_HANDLE_VALUE) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
999 PyErr_SetFromWindowsErr(0);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1000 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1001 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1002
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1003 /* get the file type, ignore the error if it failed */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1004 ftype = GetFileType(handle);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1005
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1006 Py_BEGIN_ALLOW_THREADS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1007 fd = dup(fd);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1008 Py_END_ALLOW_THREADS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1009 if (fd < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1010 PyErr_SetFromErrno(PyExc_OSError);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1011 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1012 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1013
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1014 /* Character files like console cannot be make non-inheritable */
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1015 if (ftype != FILE_TYPE_CHAR) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1016 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1017 close(fd);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1018 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1019 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1020 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1021 #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1022 Py_BEGIN_ALLOW_THREADS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1023 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1024 Py_END_ALLOW_THREADS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1025 if (fd < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1026 PyErr_SetFromErrno(PyExc_OSError);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1027 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1028 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1029
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1030 #else
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1031 Py_BEGIN_ALLOW_THREADS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1032 fd = dup(fd);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1033 Py_END_ALLOW_THREADS
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1034 if (fd < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1035 PyErr_SetFromErrno(PyExc_OSError);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1036 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1037 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1038
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1039 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1040 close(fd);
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1041 return -1;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1042 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1043 #endif
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1044 return fd;
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1045 }
ef889c3d5dc6 Issue #18571: Implementation of the PEP 446: file descriptors and file handles
Victor Stinner <victor.stinner@gmail.com>
parents: 84493
diff changeset
1046