Mercurial > cpython-lru_cache-weakref
annotate Python/fileutils.c @ 65411:df0e348e7176
_Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
surrogate characters.
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Sat, 16 Oct 2010 22:55:47 +0000 |
| parents | 6dcba93d6262 |
| children | 3794c9b50824 |
| rev | line source |
|---|---|
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
1 #include "Python.h" |
|
65164
1f3919af8a53
Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65163
diff
changeset
|
2 #ifdef MS_WINDOWS |
|
1f3919af8a53
Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65163
diff
changeset
|
3 # include <windows.h> |
|
1f3919af8a53
Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65163
diff
changeset
|
4 #endif |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
5 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
6 #ifdef HAVE_STAT |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
7 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
8 /* Decode a byte string from the locale encoding with the |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
9 surrogateescape error handler (undecodable bytes are decoded as characters |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
10 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
|
11 character, escape the bytes using the surrogateescape error handler instead |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
12 of decoding them. |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
13 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
14 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
|
15 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
16 Return a pointer to a newly allocated (wide) character string (use |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
17 PyMem_Free() to free the memory), or NULL on error (conversion error or |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
18 memory error). */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
19 wchar_t* |
|
65346
cc1d8a2db348
Mark _Py_char2wchar() input argument as constant
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65312
diff
changeset
|
20 _Py_char2wchar(const char* arg) |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
21 { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
22 wchar_t *res; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
23 #ifdef HAVE_BROKEN_MBSTOWCS |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
24 /* Some platforms have a broken implementation of |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
25 * mbstowcs which does not count the characters that |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
26 * would result from conversion. Use an upper bound. |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
27 */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
28 size_t argsize = strlen(arg); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
29 #else |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
30 size_t argsize = mbstowcs(NULL, arg, 0); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
31 #endif |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
32 size_t count; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
33 unsigned char *in; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
34 wchar_t *out; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
35 #ifdef HAVE_MBRTOWC |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
36 mbstate_t mbs; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
37 #endif |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
38 if (argsize != (size_t)-1) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
39 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t)); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
40 if (!res) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
41 goto oom; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
42 count = mbstowcs(res, arg, argsize+1); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
43 if (count != (size_t)-1) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
44 wchar_t *tmp; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
45 /* Only use the result if it contains no |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
46 surrogate characters. */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
47 for (tmp = res; *tmp != 0 && |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
48 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
49 ; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
50 if (*tmp == 0) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
51 return res; |
|
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 PyMem_Free(res); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
54 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
55 /* Conversion failed. Fall back to escaping with surrogateescape. */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
56 #ifdef HAVE_MBRTOWC |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
57 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
58 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
59 /* Overallocate; as multi-byte characters are in the argument, the |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
60 actual output could use less memory. */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
61 argsize = strlen(arg) + 1; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
62 res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t)); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
63 if (!res) goto oom; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
64 in = (unsigned char*)arg; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
65 out = res; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
66 memset(&mbs, 0, sizeof mbs); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
67 while (argsize) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
68 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
69 if (converted == 0) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
70 /* Reached end of string; null char stored. */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
71 break; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
72 if (converted == (size_t)-2) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
73 /* Incomplete character. This should never happen, |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
74 since we provide everything that we have - |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
75 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
|
76 misunderstood how mbrtowc works. */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
77 fprintf(stderr, "unexpected mbrtowc result -2\n"); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
78 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
79 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
80 if (converted == (size_t)-1) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
81 /* Conversion error. Escape as UTF-8b, and start over |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
82 in the initial shift state. */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
83 *out++ = 0xdc00 + *in++; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
84 argsize--; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
85 memset(&mbs, 0, sizeof mbs); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
86 continue; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
87 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
88 if (*out >= 0xd800 && *out <= 0xdfff) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
89 /* Surrogate character. Escape the original |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
90 byte sequence with surrogateescape. */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
91 argsize -= converted; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
92 while (converted--) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
93 *out++ = 0xdc00 + *in++; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
94 continue; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
95 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
96 /* successfully converted some bytes */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
97 in += converted; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
98 argsize -= converted; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
99 out++; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
100 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
101 #else |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
102 /* 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
|
103 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
|
104 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
|
105 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t)); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
106 if (!res) goto oom; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
107 in = (unsigned char*)arg; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
108 out = res; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
109 while(*in) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
110 if(*in < 128) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
111 *out++ = *in++; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
112 else |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
113 *out++ = 0xdc00 + *in++; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
114 *out = 0; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
115 #endif |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
116 return res; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
117 oom: |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
118 fprintf(stderr, "out of memory\n"); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
119 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
120 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
121 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
122 /* 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
|
123 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
|
124 converted to bytes 0x80..0xFF). |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
125 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
126 This function is the reverse of _Py_char2wchar(). |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
127 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
128 Return a pointer to a newly allocated byte string (use PyMem_Free() to free |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
129 the memory), or NULL on error (conversion error or memory error). */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
130 char* |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
131 _Py_wchar2char(const wchar_t *text) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
132 { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
133 const size_t len = wcslen(text); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
134 char *result = NULL, *bytes = NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
135 size_t i, size, converted; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
136 wchar_t c, buf[2]; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
137 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
138 /* The function works in two steps: |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
139 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
|
140 2. outputs the bytes */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
141 size = 0; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
142 buf[1] = 0; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
143 while (1) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
144 for (i=0; i < len; i++) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
145 c = text[i]; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
146 if (c >= 0xdc80 && c <= 0xdcff) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
147 /* UTF-8b surrogate */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
148 if (bytes != NULL) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
149 *bytes++ = c - 0xdc00; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
150 size--; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
151 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
152 else |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
153 size++; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
154 continue; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
155 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
156 else { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
157 buf[0] = c; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
158 if (bytes != NULL) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
159 converted = wcstombs(bytes, buf, size); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
160 else |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
161 converted = wcstombs(NULL, buf, 0); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
162 if (converted == (size_t)-1) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
163 if (result != NULL) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
164 PyMem_Free(result); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
165 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
166 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
167 if (bytes != NULL) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
168 bytes += converted; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
169 size -= converted; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
170 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
171 else |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
172 size += converted; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
173 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
174 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
175 if (result != NULL) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
176 *bytes = 0; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
177 break; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
178 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
179 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
180 size += 1; /* nul byte at the end */ |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
181 result = PyMem_Malloc(size); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
182 if (result == NULL) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
183 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
184 bytes = result; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
185 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
186 return result; |
|
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 /* In principle, this should use HAVE__WSTAT, and _wstat |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
190 should be detected by autoconf. However, no current |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
191 POSIX system provides that function, so testing for |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
192 it is pointless. |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
193 Not sure whether the MS_WINDOWS guards are necessary: |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
194 perhaps for cygwin/mingw builds? |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
195 */ |
|
65164
1f3919af8a53
Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65163
diff
changeset
|
196 #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
|
197 |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
198 /* 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
|
199 |
|
65164
1f3919af8a53
Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65163
diff
changeset
|
200 int |
|
1f3919af8a53
Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65163
diff
changeset
|
201 _Py_wstat(const wchar_t* path, struct stat *buf) |
|
1f3919af8a53
Fix fileutils for Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65163
diff
changeset
|
202 { |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
203 int err; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
204 char *fname; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
205 fname = _Py_wchar2char(path); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
206 if (fname == NULL) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
207 errno = EINVAL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
208 return -1; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
209 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
210 err = stat(fname, buf); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
211 PyMem_Free(fname); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
212 return err; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
213 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
214 #endif |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
215 |
|
65168
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
216 /* 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
|
217 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
|
218 |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
219 Return 0 on success, -1 on _wstat() / stat() error or (if PyErr_Occurred()) |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
220 unicode error. */ |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
221 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
222 int |
|
65165
2147cc152227
_Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65164
diff
changeset
|
223 _Py_stat(PyObject *path, struct stat *statbuf) |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
224 { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
225 #ifdef MS_WINDOWS |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
226 int err; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
227 struct _stat wstatbuf; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
228 |
|
65165
2147cc152227
_Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65164
diff
changeset
|
229 err = _wstat(PyUnicode_AS_UNICODE(path), &wstatbuf); |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
230 if (!err) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
231 statbuf->st_mode = wstatbuf.st_mode; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
232 return err; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
233 #else |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
234 int ret; |
|
65165
2147cc152227
_Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65164
diff
changeset
|
235 PyObject *bytes = PyUnicode_EncodeFSDefault(path); |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
236 if (bytes == NULL) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
237 return -1; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
238 ret = stat(PyBytes_AS_STRING(bytes), statbuf); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
239 Py_DECREF(bytes); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
240 return ret; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
241 #endif |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
242 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
243 |
|
65168
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
244 /* 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
|
245 encoding and use fopen() otherwise. */ |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
246 |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
247 FILE * |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
248 _Py_wfopen(const wchar_t *path, const wchar_t *mode) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
249 { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
250 #ifndef MS_WINDOWS |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
251 FILE *f; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
252 char *cpath; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
253 char cmode[10]; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
254 size_t r; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
255 r = wcstombs(cmode, mode, 10); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
256 if (r == (size_t)-1 || r >= 10) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
257 errno = EINVAL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
258 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
259 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
260 cpath = _Py_wchar2char(path); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
261 if (cpath == NULL) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
262 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
263 f = fopen(cpath, cmode); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
264 PyMem_Free(cpath); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
265 return f; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
266 #else |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
267 return _wfopen(path, mode); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
268 #endif |
|
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 |
|
65168
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
271 /* 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
|
272 call fopen() otherwise. |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
273 |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
274 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
|
275 (if PyErr_Occurred()) on unicode error */ |
|
65162
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 FILE* |
|
65165
2147cc152227
_Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65164
diff
changeset
|
278 _Py_fopen(PyObject *path, const char *mode) |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
279 { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
280 #ifdef MS_WINDOWS |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
281 wchar_t wmode[10]; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
282 int usize; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
283 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
284 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
285 if (usize == 0) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
286 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
287 |
|
65165
2147cc152227
_Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65164
diff
changeset
|
288 return _wfopen(PyUnicode_AS_UNICODE(path), wmode); |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
289 #else |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
290 FILE *f; |
|
65165
2147cc152227
_Py_stat() and _Py_fopen(): avoid PyUnicode_AsWideCharString() on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65164
diff
changeset
|
291 PyObject *bytes = PyUnicode_EncodeFSDefault(path); |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
292 if (bytes == NULL) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
293 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
294 f = fopen(PyBytes_AS_STRING(bytes), mode); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
295 Py_DECREF(bytes); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
296 return f; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
297 #endif |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
298 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
299 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
300 #ifdef HAVE_READLINK |
|
65168
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
301 |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
302 /* Read value of symbolic link. Encode the path to the locale encoding, decode |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
303 the result from the locale encoding. */ |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
304 |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
305 int |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
306 _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
|
307 { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
308 char *cpath; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
309 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
|
310 wchar_t *wbuf; |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
311 int res; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
312 size_t r1; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
313 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
314 cpath = _Py_wchar2char(path); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
315 if (cpath == NULL) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
316 errno = EINVAL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
317 return -1; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
318 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
319 res = (int)readlink(cpath, cbuf, PATH_MAX); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
320 PyMem_Free(cpath); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
321 if (res == -1) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
322 return -1; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
323 if (res == PATH_MAX) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
324 errno = EINVAL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
325 return -1; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
326 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
327 cbuf[res] = '\0'; /* buf will be null terminated */ |
|
65409
e1dc9861b43f
_Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65346
diff
changeset
|
328 wbuf = _Py_char2wchar(cbuf); |
|
65410
6dcba93d6262
_Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65409
diff
changeset
|
329 if (wbuf == NULL) { |
|
6dcba93d6262
_Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65409
diff
changeset
|
330 errno = EINVAL; |
|
6dcba93d6262
_Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65409
diff
changeset
|
331 return -1; |
|
6dcba93d6262
_Py_wreadlink(): catch _Py_char2wchar() failure
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65409
diff
changeset
|
332 } |
|
65409
e1dc9861b43f
_Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65346
diff
changeset
|
333 r1 = wcslen(wbuf); |
|
e1dc9861b43f
_Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65346
diff
changeset
|
334 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
|
335 PyMem_Free(wbuf); |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
336 errno = EINVAL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
337 return -1; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
338 } |
|
65409
e1dc9861b43f
_Py_wreadlink() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65346
diff
changeset
|
339 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
|
340 PyMem_Free(wbuf); |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
341 return (int)r1; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
342 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
343 #endif |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
344 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
345 #ifdef HAVE_REALPATH |
|
65168
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
346 |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
347 /* Return the canonicalized absolute pathname. Encode path to the locale |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
348 encoding, decode the result from the locale encoding. */ |
|
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
349 |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
350 wchar_t* |
|
65166
8fd5d06462e6
_Py_wrealpath() requires the size of the output buffer
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65165
diff
changeset
|
351 _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
|
352 wchar_t *resolved_path, size_t resolved_path_size) |
|
65162
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 char *cpath; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
355 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
|
356 wchar_t *wresolved_path; |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
357 char *res; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
358 size_t r; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
359 cpath = _Py_wchar2char(path); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
360 if (cpath == NULL) { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
361 errno = EINVAL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
362 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
363 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
364 res = realpath(cpath, cresolved_path); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
365 PyMem_Free(cpath); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
366 if (res == NULL) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
367 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
|
368 |
|
df0e348e7176
_Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65410
diff
changeset
|
369 wresolved_path = _Py_char2wchar(cresolved_path); |
|
df0e348e7176
_Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65410
diff
changeset
|
370 if (wresolved_path == NULL) { |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
371 errno = EINVAL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
372 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
373 } |
|
65411
df0e348e7176
_Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65410
diff
changeset
|
374 r = wcslen(wresolved_path); |
|
df0e348e7176
_Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65410
diff
changeset
|
375 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
|
376 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
|
377 errno = EINVAL; |
|
df0e348e7176
_Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65410
diff
changeset
|
378 return NULL; |
|
df0e348e7176
_Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65410
diff
changeset
|
379 } |
|
df0e348e7176
_Py_wrealpath() uses _Py_char2wchar() to decode the result, to support
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65410
diff
changeset
|
380 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
|
381 PyMem_Free(wresolved_path); |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
382 return resolved_path; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
383 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
384 #endif |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
385 |
|
65312
a27fc0ff3678
_Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65168
diff
changeset
|
386 /* Get the current directory. size is the buffer size in wide characters |
|
a27fc0ff3678
_Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65168
diff
changeset
|
387 including the null character. Decode the path from the locale encoding. */ |
|
65168
1c4774b4e98a
fileutils.c: document which encodings are used
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65166
diff
changeset
|
388 |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
389 wchar_t* |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
390 _Py_wgetcwd(wchar_t *buf, size_t size) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
391 { |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
392 #ifdef MS_WINDOWS |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
393 return _wgetcwd(buf, size); |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
394 #else |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
395 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
|
396 wchar_t *wname; |
|
a27fc0ff3678
_Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65168
diff
changeset
|
397 |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
398 if (getcwd(fname, PATH_MAX) == NULL) |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
399 return NULL; |
|
65312
a27fc0ff3678
_Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65168
diff
changeset
|
400 wname = _Py_char2wchar(fname); |
|
a27fc0ff3678
_Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65168
diff
changeset
|
401 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
|
402 return NULL; |
|
a27fc0ff3678
_Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65168
diff
changeset
|
403 if (size <= wcslen(wname)) { |
|
a27fc0ff3678
_Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65168
diff
changeset
|
404 PyMem_Free(wname); |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
405 return NULL; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
406 } |
|
65312
a27fc0ff3678
_Py_wgetcwd() decodes the path using _Py_char2wchar() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65168
diff
changeset
|
407 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
|
408 PyMem_Free(wname); |
|
65162
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
409 return buf; |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
410 #endif |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
411 } |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
412 |
|
39014f0abc32
Create fileutils.c/.h
Victor Stinner <victor.stinner@haypocalc.com>
parents:
diff
changeset
|
413 #endif |
