annotate Python/fileutils.c @ 80415:f3e512b5ffb3

Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
author Victor Stinner <victor.stinner@gmail.com>
date Mon, 12 Nov 2012 23:32:21 +0100
parents 48fbdaf3a849
children 3b344eac6b0f
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
75353
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
7 #ifdef HAVE_LANGINFO_H
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
8 #include <langinfo.h>
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
9 #endif
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
10
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
11 #ifdef __APPLE__
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
12 extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
13 #endif
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
14
75353
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
15 PyObject *
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
16 _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
17 {
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
18 #if defined(MS_WINDOWS) || defined(MS_WIN64)
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
19 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
20 #endif
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
21 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
22 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
23 }
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
24 #if defined(MS_WINDOWS) || defined(MS_WIN64)
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
25 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
26 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
27 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
28 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
29 else
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
30 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
31 /* 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
32 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
33 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
34 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
35 #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
36 {
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
37 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
38 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
39 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
40 }
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
41 #endif
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 }
c80d9a0bd5a0 Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
Brett Cannon <brett@python.org>
parents: 74129
diff changeset
44
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
45 #ifdef HAVE_STAT
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
46
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
47 /* Decode a byte string from the locale encoding with the
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
48 surrogateescape error handler (undecodable bytes are decoded as characters
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
49 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
50 character, escape the bytes using the surrogateescape error handler instead
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
51 of decoding them.
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
52
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
53 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
54
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
55 Return a pointer to a newly allocated wide character string (use
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
56 PyMem_Free() to free the memory) and write the number of written wide
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
57 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
58 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
59 *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
60 error.
66094
76a1c5e88352 _Py_char2wchar() frees the memory on conversion error
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66092
diff changeset
61
76a1c5e88352 _Py_char2wchar() frees the memory on conversion error
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66092
diff changeset
62 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
63 library. */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
64 wchar_t*
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
65 _Py_char2wchar(const char* arg, size_t *size)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
66 {
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
67 #ifdef __APPLE__
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
68 wchar_t *wstr;
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
69 wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
80415
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
70 if (size != NULL) {
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
71 if (wstr != NULL)
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
72 *size = wcslen(wstr);
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
73 else
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
74 *size = (size_t)-1;
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
75 }
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
76 return wstr;
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
77 #else
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
78 wchar_t *res;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
79 #ifdef HAVE_BROKEN_MBSTOWCS
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
80 /* Some platforms have a broken implementation of
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
81 * mbstowcs which does not count the characters that
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
82 * would result from conversion. Use an upper bound.
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
83 */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
84 size_t argsize = strlen(arg);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
85 #else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
86 size_t argsize = mbstowcs(NULL, arg, 0);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
87 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
88 size_t count;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
89 unsigned char *in;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
90 wchar_t *out;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
91 #ifdef HAVE_MBRTOWC
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
92 mbstate_t mbs;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
93 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
94 if (argsize != (size_t)-1) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
95 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
96 if (!res)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
97 goto oom;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
98 count = mbstowcs(res, arg, argsize+1);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
99 if (count != (size_t)-1) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
100 wchar_t *tmp;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
101 /* Only use the result if it contains no
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
102 surrogate characters. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
103 for (tmp = res; *tmp != 0 &&
80057
13dd8199c112 Issue #16330: Use surrogate-related macros
Victor Stinner <victor.stinner@gmail.com>
parents: 75353
diff changeset
104 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
105 ;
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
106 if (*tmp == 0) {
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
107 if (size != NULL)
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
108 *size = count;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
109 return res;
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
110 }
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
111 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
112 PyMem_Free(res);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
113 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
114 /* Conversion failed. Fall back to escaping with surrogateescape. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
115 #ifdef HAVE_MBRTOWC
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
116 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
117
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
118 /* Overallocate; as multi-byte characters are in the argument, the
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
119 actual output could use less memory. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
120 argsize = strlen(arg) + 1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
121 res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
66094
76a1c5e88352 _Py_char2wchar() frees the memory on conversion error
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66092
diff changeset
122 if (!res)
76a1c5e88352 _Py_char2wchar() frees the memory on conversion error
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66092
diff changeset
123 goto oom;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
124 in = (unsigned char*)arg;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
125 out = res;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
126 memset(&mbs, 0, sizeof mbs);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
127 while (argsize) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
128 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
129 if (converted == 0)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
130 /* Reached end of string; null char stored. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
131 break;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
132 if (converted == (size_t)-2) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
133 /* Incomplete character. This should never happen,
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
134 since we provide everything that we have -
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
135 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
136 misunderstood how mbrtowc works. */
66094
76a1c5e88352 _Py_char2wchar() frees the memory on conversion error
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66092
diff changeset
137 PyMem_Free(res);
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
138 if (size != NULL)
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
139 *size = (size_t)-2;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
140 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
141 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
142 if (converted == (size_t)-1) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
143 /* Conversion error. Escape as UTF-8b, and start over
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
144 in the initial shift state. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
145 *out++ = 0xdc00 + *in++;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
146 argsize--;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
147 memset(&mbs, 0, sizeof mbs);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
148 continue;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
149 }
80057
13dd8199c112 Issue #16330: Use surrogate-related macros
Victor Stinner <victor.stinner@gmail.com>
parents: 75353
diff changeset
150 if (Py_UNICODE_IS_SURROGATE(*out)) {
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
151 /* Surrogate character. Escape the original
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
152 byte sequence with surrogateescape. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
153 argsize -= converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
154 while (converted--)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
155 *out++ = 0xdc00 + *in++;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
156 continue;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
157 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
158 /* successfully converted some bytes */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
159 in += converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
160 argsize -= converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
161 out++;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
162 }
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
163 #else /* HAVE_MBRTOWC */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
164 /* 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
165 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
166 correctly in the locale's charset, which must be an ASCII superset. */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
167 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
168 if (!res)
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
169 goto oom;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
170 in = (unsigned char*)arg;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
171 out = res;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
172 while(*in)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
173 if(*in < 128)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
174 *out++ = *in++;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
175 else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
176 *out++ = 0xdc00 + *in++;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
177 *out = 0;
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
178 #endif /* HAVE_MBRTOWC */
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
179 if (size != NULL)
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
180 *size = out - res;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
181 return res;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
182 oom:
74002
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
183 if (size != NULL)
279b0aee0cfb Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73593
diff changeset
184 *size = (size_t)-1;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
185 return NULL;
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
186 #endif /* __APPLE__ */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
187 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
188
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
189 /* 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
190 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
191 converted to bytes 0x80..0xFF).
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
192
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
193 This function is the reverse of _Py_char2wchar().
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
194
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
195 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
196 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
197
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
198 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
199 on encoding error, or (size_t)-1 otherwise. */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
200 char*
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
201 _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
202 {
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
203 #ifdef __APPLE__
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
204 Py_ssize_t len;
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
205 PyObject *unicode, *bytes = NULL;
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
206 char *cpath;
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
207
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
208 unicode = PyUnicode_FromWideChar(text, wcslen(text));
80415
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
209 if (unicode == NULL)
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
210 return NULL;
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
211
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
212 bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
213 Py_DECREF(unicode);
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
214 if (bytes == NULL) {
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
215 PyErr_Clear();
80415
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
216 if (error_pos != NULL)
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
217 *error_pos = (size_t)-1;
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
218 return NULL;
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
219 }
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
220
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
221 len = PyBytes_GET_SIZE(bytes);
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
222 cpath = PyMem_Malloc(len+1);
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
223 if (cpath == NULL) {
80415
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
224 PyErr_Clear();
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
225 Py_DECREF(bytes);
80415
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
226 if (error_pos != NULL)
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
227 *error_pos = (size_t)-1;
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
228 return NULL;
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
229 }
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
230 memcpy(cpath, PyBytes_AsString(bytes), len + 1);
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
231 Py_DECREF(bytes);
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
232 return cpath;
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
233 #else /* __APPLE__ */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
234 const size_t len = wcslen(text);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
235 char *result = NULL, *bytes = NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
236 size_t i, size, converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
237 wchar_t c, buf[2];
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
238
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
239 /* The function works in two steps:
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
240 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
241 2. outputs the bytes */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
242 size = 0;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
243 buf[1] = 0;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
244 while (1) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
245 for (i=0; i < len; i++) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
246 c = text[i];
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
247 if (c >= 0xdc80 && c <= 0xdcff) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
248 /* UTF-8b surrogate */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
249 if (bytes != NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
250 *bytes++ = c - 0xdc00;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
251 size--;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
252 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
253 else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
254 size++;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
255 continue;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
256 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
257 else {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
258 buf[0] = c;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
259 if (bytes != NULL)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
260 converted = wcstombs(bytes, buf, size);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
261 else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
262 converted = wcstombs(NULL, buf, 0);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
263 if (converted == (size_t)-1) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
264 if (result != NULL)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
265 PyMem_Free(result);
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
266 if (error_pos != NULL)
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
267 *error_pos = i;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
268 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
269 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
270 if (bytes != NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
271 bytes += converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
272 size -= converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
273 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
274 else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
275 size += converted;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
276 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
277 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
278 if (result != NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
279 *bytes = 0;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
280 break;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
281 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
282
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
283 size += 1; /* nul byte at the end */
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
284 result = PyMem_Malloc(size);
80415
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
285 if (result == NULL) {
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
286 if (error_pos != NULL)
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
287 *error_pos = (size_t)-1;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
288 return NULL;
80415
f3e512b5ffb3 Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 80411
diff changeset
289 }
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
290 bytes = result;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
291 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
292 return result;
80411
48fbdaf3a849 Issue #16416: OS data are now always encoded/decoded to/from
Victor Stinner <victor.stinner@gmail.com>
parents: 80407
diff changeset
293 #endif /* __APPLE__ */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
294 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
295
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
296 /* In principle, this should use HAVE__WSTAT, and _wstat
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
297 should be detected by autoconf. However, no current
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
298 POSIX system provides that function, so testing for
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
299 it is pointless.
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
300 Not sure whether the MS_WINDOWS guards are necessary:
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
301 perhaps for cygwin/mingw builds?
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
302 */
65164
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
303 #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
304
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
305 /* 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
306
65164
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
307 int
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
308 _Py_wstat(const wchar_t* path, struct stat *buf)
1f3919af8a53 Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65163
diff changeset
309 {
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
310 int err;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
311 char *fname;
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
312 fname = _Py_wchar2char(path, NULL);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
313 if (fname == NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
314 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
315 return -1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
316 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
317 err = stat(fname, buf);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
318 PyMem_Free(fname);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
319 return err;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
320 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
321 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
322
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
323 /* 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
324 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
325
74058
2a140b01d343 import.c now catchs _Py_stat() exceptions
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74002
diff changeset
326 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
327 raised. */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
328
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
329 int
65165
2147cc152227 _Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65164
diff changeset
330 _Py_stat(PyObject *path, struct stat *statbuf)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
331 {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
332 #ifdef MS_WINDOWS
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
333 int err;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
334 struct _stat wstatbuf;
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
335 wchar_t *wpath;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
336
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
337 wpath = PyUnicode_AsUnicode(path);
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
338 if (wpath == NULL)
74058
2a140b01d343 import.c now catchs _Py_stat() exceptions
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74002
diff changeset
339 return -2;
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
340 err = _wstat(wpath, &wstatbuf);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
341 if (!err)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
342 statbuf->st_mode = wstatbuf.st_mode;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
343 return err;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
344 #else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
345 int ret;
65165
2147cc152227 _Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65164
diff changeset
346 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
347 if (bytes == NULL)
74058
2a140b01d343 import.c now catchs _Py_stat() exceptions
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74002
diff changeset
348 return -2;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
349 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
350 Py_DECREF(bytes);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
351 return ret;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
352 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
353 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
354
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
355 /* Open a file. Use _wfopen() on Windows, encode the path to the locale
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
356 encoding and use fopen() otherwise. */
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
357
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
358 FILE *
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
359 _Py_wfopen(const wchar_t *path, const wchar_t *mode)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
360 {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
361 #ifndef MS_WINDOWS
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
362 FILE *f;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
363 char *cpath;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
364 char cmode[10];
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
365 size_t r;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
366 r = wcstombs(cmode, mode, 10);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
367 if (r == (size_t)-1 || r >= 10) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
368 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
369 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
370 }
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
371 cpath = _Py_wchar2char(path, NULL);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
372 if (cpath == NULL)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
373 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
374 f = fopen(cpath, cmode);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
375 PyMem_Free(cpath);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
376 return f;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
377 #else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
378 return _wfopen(path, mode);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
379 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
380 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
381
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
382 /* Call _wfopen() 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
383 call fopen() otherwise.
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
384
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
385 Return the new file object on success, or NULL if the file cannot be open or
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
386 (if PyErr_Occurred()) on unicode error */
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
387
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
388 FILE*
65165
2147cc152227 _Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65164
diff changeset
389 _Py_fopen(PyObject *path, const char *mode)
65162
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 #ifdef MS_WINDOWS
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
392 wchar_t *wpath;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
393 wchar_t wmode[10];
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
394 int usize;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
395
74129
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74085
diff changeset
396 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
397 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
398 "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
399 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
400 return NULL;
33dea851f918 Issue #13626: Add support for SSL Diffie-Hellman key exchange, through the
Antoine Pitrou <solipsis@pitrou.net>
parents: 74085
diff changeset
401 }
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
402 wpath = PyUnicode_AsUnicode(path);
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
403 if (wpath == NULL)
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
404 return NULL;
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
405
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
406 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
407 if (usize == 0)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
408 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
409
73593
94a38ad4aebf Catch PyUnicode_AS_UNICODE() errors in fileutils.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66094
diff changeset
410 return _wfopen(wpath, wmode);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
411 #else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
412 FILE *f;
74085
a882aa30a713 _Py_fopen now allows bytes filenames under non-Windows platforms.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74058
diff changeset
413 PyObject *bytes;
a882aa30a713 _Py_fopen now allows bytes filenames under non-Windows platforms.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74058
diff changeset
414 if (!PyUnicode_FSConverter(path, &bytes))
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
415 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
416 f = fopen(PyBytes_AS_STRING(bytes), mode);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
417 Py_DECREF(bytes);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
418 return f;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
419 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
420 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
421
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
422 #ifdef HAVE_READLINK
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
423
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
424 /* 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
425 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
426
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
427 int
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
428 _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
429 {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
430 char *cpath;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
431 char cbuf[PATH_MAX];
65409
e1dc9861b43f _Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65346
diff changeset
432 wchar_t *wbuf;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
433 int res;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
434 size_t r1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
435
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
436 cpath = _Py_wchar2char(path, NULL);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
437 if (cpath == NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
438 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
439 return -1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
440 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
441 res = (int)readlink(cpath, cbuf, PATH_MAX);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
442 PyMem_Free(cpath);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
443 if (res == -1)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
444 return -1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
445 if (res == PATH_MAX) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
446 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
447 return -1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
448 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
449 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
450 wbuf = _Py_char2wchar(cbuf, &r1);
65410
6dcba93d6262 _Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65409
diff changeset
451 if (wbuf == NULL) {
6dcba93d6262 _Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65409
diff changeset
452 errno = EINVAL;
6dcba93d6262 _Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65409
diff changeset
453 return -1;
6dcba93d6262 _Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65409
diff changeset
454 }
65409
e1dc9861b43f _Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65346
diff changeset
455 if (bufsiz <= r1) {
e1dc9861b43f _Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65346
diff changeset
456 PyMem_Free(wbuf);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
457 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
458 return -1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
459 }
65409
e1dc9861b43f _Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65346
diff changeset
460 wcsncpy(buf, wbuf, bufsiz);
e1dc9861b43f _Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65346
diff changeset
461 PyMem_Free(wbuf);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
462 return (int)r1;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
463 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
464 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
465
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
466 #ifdef HAVE_REALPATH
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
467
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
468 /* 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
469 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
470 Return NULL on error. */
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
471
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
472 wchar_t*
65166
8fd5d06462e6 _Py_wrealpath() requires the size of the output buffer
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65165
diff changeset
473 _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
474 wchar_t *resolved_path, size_t resolved_path_size)
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
475 {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
476 char *cpath;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
477 char cresolved_path[PATH_MAX];
65411
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
478 wchar_t *wresolved_path;
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
479 char *res;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
480 size_t r;
66092
1b5f47e2e0f0 PyUnicode_EncodeFS() raises an exception if _Py_wchar2char() fails
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65412
diff changeset
481 cpath = _Py_wchar2char(path, NULL);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
482 if (cpath == NULL) {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
483 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
484 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
485 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
486 res = realpath(cpath, cresolved_path);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
487 PyMem_Free(cpath);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
488 if (res == NULL)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
489 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
490
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
491 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
492 if (wresolved_path == NULL) {
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
493 errno = EINVAL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
494 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
495 }
65411
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
496 if (resolved_path_size <= r) {
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
497 PyMem_Free(wresolved_path);
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
498 errno = EINVAL;
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
499 return NULL;
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
500 }
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
501 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
df0e348e7176 _Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65410
diff changeset
502 PyMem_Free(wresolved_path);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
503 return resolved_path;
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 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
506
65312
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
507 /* 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
508 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
509 Return NULL on error. */
65168
1c4774b4e98a fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65166
diff changeset
510
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
511 wchar_t*
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
512 _Py_wgetcwd(wchar_t *buf, size_t size)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
513 {
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
514 #ifdef MS_WINDOWS
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
515 return _wgetcwd(buf, size);
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
516 #else
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
517 char fname[PATH_MAX];
65312
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
518 wchar_t *wname;
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
519 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
520
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
521 if (getcwd(fname, PATH_MAX) == NULL)
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
522 return NULL;
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
523 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
524 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
525 return NULL;
65412
3794c9b50824 Add an optional size argument to _Py_char2wchar()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65411
diff changeset
526 if (size <= len) {
65312
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
527 PyMem_Free(wname);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
528 return NULL;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
529 }
65312
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
530 wcsncpy(buf, wname, size);
a27fc0ff3678 _Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65168
diff changeset
531 PyMem_Free(wname);
65162
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
532 return buf;
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
533 #endif
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
534 }
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
535
39014f0abc32 Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff changeset
536 #endif