Skip to content

Commit 8c3aee6

Browse files
authored
bpo-35134: Add Include/cpython/fileutils.h header file (GH-18493)
Move CPython C API from Include/fileutils.h into a new Include/cpython/fileutils.h header file which is included by Include/fileutils.h. Exclude the following private symbols from the limited C API: * _Py_error_handler * _Py_GetErrorHandler() * _Py_DecodeLocaleEx() * _Py_EncodeLocaleEx()
1 parent 98921ae commit 8c3aee6

File tree

5 files changed

+173
-164
lines changed

5 files changed

+173
-164
lines changed

Include/cpython/fileutils.h

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#ifndef Py_CPYTHON_FILEUTILS_H
2+
# error "this header file must not be included directly"
3+
#endif
4+
5+
typedef enum {
6+
_Py_ERROR_UNKNOWN=0,
7+
_Py_ERROR_STRICT,
8+
_Py_ERROR_SURROGATEESCAPE,
9+
_Py_ERROR_REPLACE,
10+
_Py_ERROR_IGNORE,
11+
_Py_ERROR_BACKSLASHREPLACE,
12+
_Py_ERROR_SURROGATEPASS,
13+
_Py_ERROR_XMLCHARREFREPLACE,
14+
_Py_ERROR_OTHER
15+
} _Py_error_handler;
16+
17+
PyAPI_FUNC(_Py_error_handler) _Py_GetErrorHandler(const char *errors);
18+
19+
PyAPI_FUNC(int) _Py_DecodeLocaleEx(
20+
const char *arg,
21+
wchar_t **wstr,
22+
size_t *wlen,
23+
const char **reason,
24+
int current_locale,
25+
_Py_error_handler errors);
26+
27+
PyAPI_FUNC(int) _Py_EncodeLocaleEx(
28+
const wchar_t *text,
29+
char **str,
30+
size_t *error_pos,
31+
const char **reason,
32+
int current_locale,
33+
_Py_error_handler errors);
34+
35+
36+
PyAPI_FUNC(PyObject *) _Py_device_encoding(int);
37+
38+
#if defined(MS_WINDOWS) || defined(__APPLE__)
39+
/* On Windows, the count parameter of read() is an int (bpo-9015, bpo-9611).
40+
On macOS 10.13, read() and write() with more than INT_MAX bytes
41+
fail with EINVAL (bpo-24658). */
42+
# define _PY_READ_MAX INT_MAX
43+
# define _PY_WRITE_MAX INT_MAX
44+
#else
45+
/* write() should truncate the input to PY_SSIZE_T_MAX bytes,
46+
but it's safer to do it ourself to have a portable behaviour */
47+
# define _PY_READ_MAX PY_SSIZE_T_MAX
48+
# define _PY_WRITE_MAX PY_SSIZE_T_MAX
49+
#endif
50+
51+
#ifdef MS_WINDOWS
52+
struct _Py_stat_struct {
53+
unsigned long st_dev;
54+
uint64_t st_ino;
55+
unsigned short st_mode;
56+
int st_nlink;
57+
int st_uid;
58+
int st_gid;
59+
unsigned long st_rdev;
60+
__int64 st_size;
61+
time_t st_atime;
62+
int st_atime_nsec;
63+
time_t st_mtime;
64+
int st_mtime_nsec;
65+
time_t st_ctime;
66+
int st_ctime_nsec;
67+
unsigned long st_file_attributes;
68+
unsigned long st_reparse_tag;
69+
};
70+
#else
71+
# define _Py_stat_struct stat
72+
#endif
73+
74+
PyAPI_FUNC(int) _Py_fstat(
75+
int fd,
76+
struct _Py_stat_struct *status);
77+
78+
PyAPI_FUNC(int) _Py_fstat_noraise(
79+
int fd,
80+
struct _Py_stat_struct *status);
81+
82+
PyAPI_FUNC(int) _Py_stat(
83+
PyObject *path,
84+
struct stat *status);
85+
86+
PyAPI_FUNC(int) _Py_open(
87+
const char *pathname,
88+
int flags);
89+
90+
PyAPI_FUNC(int) _Py_open_noraise(
91+
const char *pathname,
92+
int flags);
93+
94+
PyAPI_FUNC(FILE *) _Py_wfopen(
95+
const wchar_t *path,
96+
const wchar_t *mode);
97+
98+
PyAPI_FUNC(FILE*) _Py_fopen(
99+
const char *pathname,
100+
const char *mode);
101+
102+
PyAPI_FUNC(FILE*) _Py_fopen_obj(
103+
PyObject *path,
104+
const char *mode);
105+
106+
PyAPI_FUNC(Py_ssize_t) _Py_read(
107+
int fd,
108+
void *buf,
109+
size_t count);
110+
111+
PyAPI_FUNC(Py_ssize_t) _Py_write(
112+
int fd,
113+
const void *buf,
114+
size_t count);
115+
116+
PyAPI_FUNC(Py_ssize_t) _Py_write_noraise(
117+
int fd,
118+
const void *buf,
119+
size_t count);
120+
121+
#ifdef HAVE_READLINK
122+
PyAPI_FUNC(int) _Py_wreadlink(
123+
const wchar_t *path,
124+
wchar_t *buf,
125+
/* Number of characters of 'buf' buffer
126+
including the trailing NUL character */
127+
size_t buflen);
128+
#endif
129+
130+
#ifdef HAVE_REALPATH
131+
PyAPI_FUNC(wchar_t*) _Py_wrealpath(
132+
const wchar_t *path,
133+
wchar_t *resolved_path,
134+
/* Number of characters of 'resolved_path' buffer
135+
including the trailing NUL character */
136+
size_t resolved_path_len);
137+
#endif
138+
139+
#ifndef MS_WINDOWS
140+
PyAPI_FUNC(int) _Py_isabs(const wchar_t *path);
141+
#endif
142+
143+
PyAPI_FUNC(int) _Py_abspath(const wchar_t *path, wchar_t **abspath_p);
144+
145+
PyAPI_FUNC(wchar_t*) _Py_wgetcwd(
146+
wchar_t *buf,
147+
/* Number of characters of 'buf' buffer
148+
including the trailing NUL character */
149+
size_t buflen);
150+
151+
PyAPI_FUNC(int) _Py_get_inheritable(int fd);
152+
153+
PyAPI_FUNC(int) _Py_set_inheritable(int fd, int inheritable,
154+
int *atomic_flag_works);
155+
156+
PyAPI_FUNC(int) _Py_set_inheritable_async_safe(int fd, int inheritable,
157+
int *atomic_flag_works);
158+
159+
PyAPI_FUNC(int) _Py_dup(int fd);
160+
161+
#ifndef MS_WINDOWS
162+
PyAPI_FUNC(int) _Py_get_blocking(int fd);
163+
164+
PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
165+
#endif /* !MS_WINDOWS */

Include/fileutils.h

Lines changed: 3 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -18,173 +18,12 @@ PyAPI_FUNC(char*) _Py_EncodeLocaleRaw(
1818
size_t *error_pos);
1919
#endif
2020

21-
22-
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000
23-
typedef enum {
24-
_Py_ERROR_UNKNOWN=0,
25-
_Py_ERROR_STRICT,
26-
_Py_ERROR_SURROGATEESCAPE,
27-
_Py_ERROR_REPLACE,
28-
_Py_ERROR_IGNORE,
29-
_Py_ERROR_BACKSLASHREPLACE,
30-
_Py_ERROR_SURROGATEPASS,
31-
_Py_ERROR_XMLCHARREFREPLACE,
32-
_Py_ERROR_OTHER
33-
} _Py_error_handler;
34-
35-
PyAPI_FUNC(_Py_error_handler) _Py_GetErrorHandler(const char *errors);
36-
37-
PyAPI_FUNC(int) _Py_DecodeLocaleEx(
38-
const char *arg,
39-
wchar_t **wstr,
40-
size_t *wlen,
41-
const char **reason,
42-
int current_locale,
43-
_Py_error_handler errors);
44-
45-
PyAPI_FUNC(int) _Py_EncodeLocaleEx(
46-
const wchar_t *text,
47-
char **str,
48-
size_t *error_pos,
49-
const char **reason,
50-
int current_locale,
51-
_Py_error_handler errors);
52-
#endif
53-
5421
#ifndef Py_LIMITED_API
55-
PyAPI_FUNC(PyObject *) _Py_device_encoding(int);
56-
57-
#if defined(MS_WINDOWS) || defined(__APPLE__)
58-
/* On Windows, the count parameter of read() is an int (bpo-9015, bpo-9611).
59-
On macOS 10.13, read() and write() with more than INT_MAX bytes
60-
fail with EINVAL (bpo-24658). */
61-
# define _PY_READ_MAX INT_MAX
62-
# define _PY_WRITE_MAX INT_MAX
63-
#else
64-
/* write() should truncate the input to PY_SSIZE_T_MAX bytes,
65-
but it's safer to do it ourself to have a portable behaviour */
66-
# define _PY_READ_MAX PY_SSIZE_T_MAX
67-
# define _PY_WRITE_MAX PY_SSIZE_T_MAX
22+
# define Py_CPYTHON_FILEUTILS_H
23+
# include "cpython/fileutils.h"
24+
# undef Py_CPYTHON_FILEUTILS_H
6825
#endif
6926

70-
#ifdef MS_WINDOWS
71-
struct _Py_stat_struct {
72-
unsigned long st_dev;
73-
uint64_t st_ino;
74-
unsigned short st_mode;
75-
int st_nlink;
76-
int st_uid;
77-
int st_gid;
78-
unsigned long st_rdev;
79-
__int64 st_size;
80-
time_t st_atime;
81-
int st_atime_nsec;
82-
time_t st_mtime;
83-
int st_mtime_nsec;
84-
time_t st_ctime;
85-
int st_ctime_nsec;
86-
unsigned long st_file_attributes;
87-
unsigned long st_reparse_tag;
88-
};
89-
#else
90-
# define _Py_stat_struct stat
91-
#endif
92-
93-
PyAPI_FUNC(int) _Py_fstat(
94-
int fd,
95-
struct _Py_stat_struct *status);
96-
97-
PyAPI_FUNC(int) _Py_fstat_noraise(
98-
int fd,
99-
struct _Py_stat_struct *status);
100-
101-
PyAPI_FUNC(int) _Py_stat(
102-
PyObject *path,
103-
struct stat *status);
104-
105-
PyAPI_FUNC(int) _Py_open(
106-
const char *pathname,
107-
int flags);
108-
109-
PyAPI_FUNC(int) _Py_open_noraise(
110-
const char *pathname,
111-
int flags);
112-
113-
PyAPI_FUNC(FILE *) _Py_wfopen(
114-
const wchar_t *path,
115-
const wchar_t *mode);
116-
117-
PyAPI_FUNC(FILE*) _Py_fopen(
118-
const char *pathname,
119-
const char *mode);
120-
121-
PyAPI_FUNC(FILE*) _Py_fopen_obj(
122-
PyObject *path,
123-
const char *mode);
124-
125-
PyAPI_FUNC(Py_ssize_t) _Py_read(
126-
int fd,
127-
void *buf,
128-
size_t count);
129-
130-
PyAPI_FUNC(Py_ssize_t) _Py_write(
131-
int fd,
132-
const void *buf,
133-
size_t count);
134-
135-
PyAPI_FUNC(Py_ssize_t) _Py_write_noraise(
136-
int fd,
137-
const void *buf,
138-
size_t count);
139-
140-
#ifdef HAVE_READLINK
141-
PyAPI_FUNC(int) _Py_wreadlink(
142-
const wchar_t *path,
143-
wchar_t *buf,
144-
/* Number of characters of 'buf' buffer
145-
including the trailing NUL character */
146-
size_t buflen);
147-
#endif
148-
149-
#ifdef HAVE_REALPATH
150-
PyAPI_FUNC(wchar_t*) _Py_wrealpath(
151-
const wchar_t *path,
152-
wchar_t *resolved_path,
153-
/* Number of characters of 'resolved_path' buffer
154-
including the trailing NUL character */
155-
size_t resolved_path_len);
156-
#endif
157-
158-
#ifndef MS_WINDOWS
159-
PyAPI_FUNC(int) _Py_isabs(const wchar_t *path);
160-
#endif
161-
162-
PyAPI_FUNC(int) _Py_abspath(const wchar_t *path, wchar_t **abspath_p);
163-
164-
PyAPI_FUNC(wchar_t*) _Py_wgetcwd(
165-
wchar_t *buf,
166-
/* Number of characters of 'buf' buffer
167-
including the trailing NUL character */
168-
size_t buflen);
169-
170-
PyAPI_FUNC(int) _Py_get_inheritable(int fd);
171-
172-
PyAPI_FUNC(int) _Py_set_inheritable(int fd, int inheritable,
173-
int *atomic_flag_works);
174-
175-
PyAPI_FUNC(int) _Py_set_inheritable_async_safe(int fd, int inheritable,
176-
int *atomic_flag_works);
177-
178-
PyAPI_FUNC(int) _Py_dup(int fd);
179-
180-
#ifndef MS_WINDOWS
181-
PyAPI_FUNC(int) _Py_get_blocking(int fd);
182-
183-
PyAPI_FUNC(int) _Py_set_blocking(int fd, int blocking);
184-
#endif /* !MS_WINDOWS */
185-
186-
#endif /* Py_LIMITED_API */
187-
18827
#ifdef __cplusplus
18928
}
19029
#endif

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,7 @@ PYTHON_HEADERS= \
10601060
$(srcdir)/Include/cpython/ceval.h \
10611061
$(srcdir)/Include/cpython/dictobject.h \
10621062
$(srcdir)/Include/cpython/fileobject.h \
1063+
$(srcdir)/Include/cpython/fileutils.h \
10631064
$(srcdir)/Include/cpython/import.h \
10641065
$(srcdir)/Include/cpython/initconfig.h \
10651066
$(srcdir)/Include/cpython/interpreteridobject.h \

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
<ClInclude Include="..\Include\cpython\ceval.h" />
132132
<ClInclude Include="..\Include\cpython\dictobject.h" />
133133
<ClInclude Include="..\Include\cpython\fileobject.h" />
134+
<ClInclude Include="..\Include\cpython\fileutils.h" />
134135
<ClInclude Include="..\Include\cpython\import.h" />
135136
<ClInclude Include="..\Include\cpython\initconfig.h" />
136137
<ClInclude Include="..\Include\cpython\listobject.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
<ClInclude Include="..\Include\cpython\fileobject.h">
9797
<Filter>Include</Filter>
9898
</ClInclude>
99+
<ClInclude Include="..\Include\cpython\fileutils.h">
100+
<Filter>Include</Filter>
101+
</ClInclude>
99102
<ClInclude Include="..\Include\cpython\import.h">
100103
<Filter>Include</Filter>
101104
</ClInclude>

0 commit comments

Comments
 (0)