Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:46

0001 #ifndef Py_INTERNAL_FILEUTILS_H
0002 #define Py_INTERNAL_FILEUTILS_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006 
0007 #ifndef Py_BUILD_CORE
0008 #  error "this header requires Py_BUILD_CORE define"
0009 #endif
0010 
0011 #include <locale.h>               // struct lconv
0012 
0013 
0014 /* A routine to check if a file descriptor can be select()-ed. */
0015 #ifdef _MSC_VER
0016     /* On Windows, any socket fd can be select()-ed, no matter how high */
0017     #define _PyIsSelectable_fd(FD) (1)
0018 #else
0019     #define _PyIsSelectable_fd(FD) ((unsigned int)(FD) < (unsigned int)FD_SETSIZE)
0020 #endif
0021 
0022 struct _fileutils_state {
0023     int force_ascii;
0024 };
0025 
0026 typedef enum {
0027     _Py_ERROR_UNKNOWN=0,
0028     _Py_ERROR_STRICT,
0029     _Py_ERROR_SURROGATEESCAPE,
0030     _Py_ERROR_REPLACE,
0031     _Py_ERROR_IGNORE,
0032     _Py_ERROR_BACKSLASHREPLACE,
0033     _Py_ERROR_SURROGATEPASS,
0034     _Py_ERROR_XMLCHARREFREPLACE,
0035     _Py_ERROR_OTHER
0036 } _Py_error_handler;
0037 
0038 // Export for '_testinternalcapi' shared extension
0039 PyAPI_FUNC(_Py_error_handler) _Py_GetErrorHandler(const char *errors);
0040 
0041 // Export for '_testinternalcapi' shared extension
0042 PyAPI_FUNC(int) _Py_DecodeLocaleEx(
0043     const char *arg,
0044     wchar_t **wstr,
0045     size_t *wlen,
0046     const char **reason,
0047     int current_locale,
0048     _Py_error_handler errors);
0049 
0050 // Export for '_testinternalcapi' shared extension
0051 PyAPI_FUNC(int) _Py_EncodeLocaleEx(
0052     const wchar_t *text,
0053     char **str,
0054     size_t *error_pos,
0055     const char **reason,
0056     int current_locale,
0057     _Py_error_handler errors);
0058 
0059 extern char* _Py_EncodeLocaleRaw(
0060     const wchar_t *text,
0061     size_t *error_pos);
0062 
0063 extern PyObject* _Py_device_encoding(int);
0064 
0065 #if defined(MS_WINDOWS) || defined(__APPLE__)
0066     /* On Windows, the count parameter of read() is an int (bpo-9015, bpo-9611).
0067        On macOS 10.13, read() and write() with more than INT_MAX bytes
0068        fail with EINVAL (bpo-24658). */
0069 #   define _PY_READ_MAX  INT_MAX
0070 #   define _PY_WRITE_MAX INT_MAX
0071 #else
0072     /* write() should truncate the input to PY_SSIZE_T_MAX bytes,
0073        but it's safer to do it ourself to have a portable behaviour */
0074 #   define _PY_READ_MAX  PY_SSIZE_T_MAX
0075 #   define _PY_WRITE_MAX PY_SSIZE_T_MAX
0076 #endif
0077 
0078 #ifdef MS_WINDOWS
0079 struct _Py_stat_struct {
0080     uint64_t st_dev;
0081     uint64_t st_ino;
0082     unsigned short st_mode;
0083     int st_nlink;
0084     int st_uid;
0085     int st_gid;
0086     unsigned long st_rdev;
0087     __int64 st_size;
0088     time_t st_atime;
0089     int st_atime_nsec;
0090     time_t st_mtime;
0091     int st_mtime_nsec;
0092     time_t st_ctime;
0093     int st_ctime_nsec;
0094     time_t st_birthtime;
0095     int st_birthtime_nsec;
0096     unsigned long st_file_attributes;
0097     unsigned long st_reparse_tag;
0098     uint64_t st_ino_high;
0099 };
0100 #else
0101 #  define _Py_stat_struct stat
0102 #endif
0103 
0104 // Export for 'mmap' shared extension
0105 PyAPI_FUNC(int) _Py_fstat(
0106     int fd,
0107     struct _Py_stat_struct *status);
0108 
0109 // Export for 'mmap' shared extension
0110 PyAPI_FUNC(int) _Py_fstat_noraise(
0111     int fd,
0112     struct _Py_stat_struct *status);
0113 
0114 // Export for '_tkinter' shared extension
0115 PyAPI_FUNC(int) _Py_stat(
0116     PyObject *path,
0117     struct stat *status);
0118 
0119 // Export for 'select' shared extension (Solaris newDevPollObject())
0120 PyAPI_FUNC(int) _Py_open(
0121     const char *pathname,
0122     int flags);
0123 
0124 // Export for '_posixsubprocess' shared extension
0125 PyAPI_FUNC(int) _Py_open_noraise(
0126     const char *pathname,
0127     int flags);
0128 
0129 extern FILE* _Py_wfopen(
0130     const wchar_t *path,
0131     const wchar_t *mode);
0132 
0133 extern Py_ssize_t _Py_read(
0134     int fd,
0135     void *buf,
0136     size_t count);
0137 
0138 // Export for 'select' shared extension (Solaris devpoll_flush())
0139 PyAPI_FUNC(Py_ssize_t) _Py_write(
0140     int fd,
0141     const void *buf,
0142     size_t count);
0143 
0144 // Export for '_posixsubprocess' shared extension
0145 PyAPI_FUNC(Py_ssize_t) _Py_write_noraise(
0146     int fd,
0147     const void *buf,
0148     size_t count);
0149 
0150 #ifdef HAVE_READLINK
0151 extern int _Py_wreadlink(
0152     const wchar_t *path,
0153     wchar_t *buf,
0154     /* Number of characters of 'buf' buffer
0155        including the trailing NUL character */
0156     size_t buflen);
0157 #endif
0158 
0159 #ifdef HAVE_REALPATH
0160 extern wchar_t* _Py_wrealpath(
0161     const wchar_t *path,
0162     wchar_t *resolved_path,
0163     /* Number of characters of 'resolved_path' buffer
0164        including the trailing NUL character */
0165     size_t resolved_path_len);
0166 #endif
0167 
0168 extern wchar_t* _Py_wgetcwd(
0169     wchar_t *buf,
0170     /* Number of characters of 'buf' buffer
0171        including the trailing NUL character */
0172     size_t buflen);
0173 
0174 extern int _Py_get_inheritable(int fd);
0175 
0176 // Export for '_socket' shared extension
0177 PyAPI_FUNC(int) _Py_set_inheritable(int fd, int inheritable,
0178                                     int *atomic_flag_works);
0179 
0180 // Export for '_posixsubprocess' shared extension
0181 PyAPI_FUNC(int) _Py_set_inheritable_async_safe(int fd, int inheritable,
0182                                                int *atomic_flag_works);
0183 
0184 // Export for '_socket' shared extension
0185 PyAPI_FUNC(int) _Py_dup(int fd);
0186 
0187 extern int _Py_get_blocking(int fd);
0188 
0189 extern int _Py_set_blocking(int fd, int blocking);
0190 
0191 #ifdef MS_WINDOWS
0192 extern void* _Py_get_osfhandle_noraise(int fd);
0193 
0194 // Export for '_testconsole' shared extension
0195 PyAPI_FUNC(void*) _Py_get_osfhandle(int fd);
0196 
0197 extern int _Py_open_osfhandle_noraise(void *handle, int flags);
0198 
0199 extern int _Py_open_osfhandle(void *handle, int flags);
0200 #endif  /* MS_WINDOWS */
0201 
0202 // This is used after getting NULL back from Py_DecodeLocale().
0203 #define DECODE_LOCALE_ERR(NAME, LEN) \
0204     ((LEN) == (size_t)-2) \
0205      ? _PyStatus_ERR("cannot decode " NAME) \
0206      : _PyStatus_NO_MEMORY()
0207 
0208 extern int _Py_HasFileSystemDefaultEncodeErrors;
0209 
0210 extern int _Py_DecodeUTF8Ex(
0211     const char *arg,
0212     Py_ssize_t arglen,
0213     wchar_t **wstr,
0214     size_t *wlen,
0215     const char **reason,
0216     _Py_error_handler errors);
0217 
0218 extern int _Py_EncodeUTF8Ex(
0219     const wchar_t *text,
0220     char **str,
0221     size_t *error_pos,
0222     const char **reason,
0223     int raw_malloc,
0224     _Py_error_handler errors);
0225 
0226 extern wchar_t* _Py_DecodeUTF8_surrogateescape(
0227     const char *arg,
0228     Py_ssize_t arglen,
0229     size_t *wlen);
0230 
0231 extern int
0232 _Py_wstat(const wchar_t *, struct stat *);
0233 
0234 extern int _Py_GetForceASCII(void);
0235 
0236 /* Reset "force ASCII" mode (if it was initialized).
0237 
0238    This function should be called when Python changes the LC_CTYPE locale,
0239    so the "force ASCII" mode can be detected again on the new locale
0240    encoding. */
0241 extern void _Py_ResetForceASCII(void);
0242 
0243 
0244 extern int _Py_GetLocaleconvNumeric(
0245     struct lconv *lc,
0246     PyObject **decimal_point,
0247     PyObject **thousands_sep);
0248 
0249 // Export for '_posixsubprocess' (on macOS)
0250 PyAPI_FUNC(void) _Py_closerange(int first, int last);
0251 
0252 extern wchar_t* _Py_GetLocaleEncoding(void);
0253 extern PyObject* _Py_GetLocaleEncodingObject(void);
0254 
0255 #ifdef HAVE_NON_UNICODE_WCHAR_T_REPRESENTATION
0256 extern int _Py_LocaleUsesNonUnicodeWchar(void);
0257 
0258 extern wchar_t* _Py_DecodeNonUnicodeWchar(
0259     const wchar_t* native,
0260     Py_ssize_t size);
0261 
0262 extern int _Py_EncodeNonUnicodeWchar_InPlace(
0263     wchar_t* unicode,
0264     Py_ssize_t size);
0265 #endif
0266 
0267 extern int _Py_isabs(const wchar_t *path);
0268 extern int _Py_abspath(const wchar_t *path, wchar_t **abspath_p);
0269 #ifdef MS_WINDOWS
0270 extern int _PyOS_getfullpathname(const wchar_t *path, wchar_t **abspath_p);
0271 #endif
0272 extern wchar_t* _Py_join_relfile(const wchar_t *dirname,
0273                                  const wchar_t *relfile);
0274 extern int _Py_add_relfile(wchar_t *dirname,
0275                            const wchar_t *relfile,
0276                            size_t bufsize);
0277 extern size_t _Py_find_basename(const wchar_t *filename);
0278 
0279 // Export for '_testinternalcapi' shared extension
0280 PyAPI_FUNC(wchar_t*) _Py_normpath(wchar_t *path, Py_ssize_t size);
0281 
0282 extern wchar_t *_Py_normpath_and_size(wchar_t *path, Py_ssize_t size, Py_ssize_t *length);
0283 
0284 // The Windows Games API family does not provide these functions
0285 // so provide our own implementations. Remove them in case they get added
0286 // to the Games API family
0287 #if defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP)
0288 #include <winerror.h>             // HRESULT
0289 
0290 extern HRESULT PathCchSkipRoot(const wchar_t *pszPath, const wchar_t **ppszRootEnd);
0291 #endif /* defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP) */
0292 
0293 extern void _Py_skiproot(const wchar_t *path, Py_ssize_t size, Py_ssize_t *drvsize, Py_ssize_t *rootsize);
0294 
0295 // Macros to protect CRT calls against instant termination when passed an
0296 // invalid parameter (bpo-23524). IPH stands for Invalid Parameter Handler.
0297 // Usage:
0298 //
0299 //      _Py_BEGIN_SUPPRESS_IPH
0300 //      ...
0301 //      _Py_END_SUPPRESS_IPH
0302 #if defined _MSC_VER && _MSC_VER >= 1900
0303 
0304 #  include <stdlib.h>   // _set_thread_local_invalid_parameter_handler()
0305 
0306    extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
0307 #  define _Py_BEGIN_SUPPRESS_IPH \
0308     { _invalid_parameter_handler _Py_old_handler = \
0309       _set_thread_local_invalid_parameter_handler(_Py_silent_invalid_parameter_handler);
0310 #  define _Py_END_SUPPRESS_IPH \
0311     _set_thread_local_invalid_parameter_handler(_Py_old_handler); }
0312 #else
0313 #  define _Py_BEGIN_SUPPRESS_IPH
0314 #  define _Py_END_SUPPRESS_IPH
0315 #endif /* _MSC_VER >= 1900 */
0316 
0317 // Export for 'select' shared extension (Argument Clinic code)
0318 PyAPI_FUNC(int) _PyLong_FileDescriptor_Converter(PyObject *, void *);
0319 
0320 // Export for test_peg_generator
0321 PyAPI_FUNC(char*) _Py_UniversalNewlineFgetsWithSize(char *, int, FILE*, PyObject *, size_t*);
0322 
0323 extern int _PyFile_Flush(PyObject *);
0324 
0325 #ifndef MS_WINDOWS
0326 extern int _Py_GetTicksPerSecond(long *ticks_per_second);
0327 #endif
0328 
0329 // Export for '_testcapi' shared extension
0330 PyAPI_FUNC(int) _Py_IsValidFD(int fd);
0331 
0332 #ifdef __cplusplus
0333 }
0334 #endif
0335 #endif /* !Py_INTERNAL_FILEUTILS_H */