| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6562b29 commit a555cfc
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,6 +62,10 @@ PyAPI_FUNC(int) _Py_stat( | |||
| 62 | 62 | PyAPI_FUNC(int) _Py_open( | |
| 63 | 63 | const char *pathname, | |
| 64 | 64 | int flags); | |
| 65 | + | ||
| 66 | + PyAPI_FUNC(int) _Py_open_noraise( | ||
| 67 | + const char *pathname, | ||
| 68 | + int flags); | ||
| 65 | 69 | #endif | |
| 66 | 70 | ||
| 67 | 71 | PyAPI_FUNC(FILE *) _Py_wfopen( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -257,6 +257,7 @@ _close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep) | |||
| 257 | 257 | fd_dir_fd = _Py_open(FD_DIR, O_RDONLY); | |
| 258 | 258 | if (fd_dir_fd == -1) { | |
| 259 | 259 | /* No way to get a list of open fds. */ | |
| 260 | + PyErr_Clear(); | ||
| 260 | 261 | _close_fds_by_brute_force(start_fd, py_fds_to_keep); | |
| 261 | 262 | return; | |
| 262 | 263 | } else { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1221,7 +1221,6 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) | |||
| 1221 | 1221 | fd = devzero = _Py_open("/dev/zero", O_RDWR); | |
| 1222 | 1222 | if (devzero == -1) { | |
| 1223 | 1223 | Py_DECREF(m_obj); | |
| 1224 | - PyErr_SetFromErrno(PyExc_OSError); | ||
| 1225 | 1224 | return NULL; | |
| 1226 | 1225 | } | |
| 1227 | 1226 | #endif | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -116,11 +116,8 @@ newossobject(PyObject *arg) | |||
| 116 | 116 | provides a special ioctl() for non-blocking read/write, which is | |
| 117 | 117 | exposed via oss_nonblock() below. */ | |
| 118 | 118 | fd = _Py_open(devicename, imode|O_NONBLOCK); | |
| 119 | - | ||
| 120 | - if (fd == -1) { | ||
| 121 | - PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename); | ||
| 119 | + if (fd == -1) | ||
| 122 | 120 | return NULL; | |
| 123 | - } | ||
| 124 | 121 | ||
| 125 | 122 | /* And (try to) put it back in blocking mode so we get the | |
| 126 | 123 | expected write() semantics. */ | |
@@ -180,10 +177,8 @@ newossmixerobject(PyObject *arg) | |||
| 180 | 177 | } | |
| 181 | 178 | ||
| 182 | 179 | fd = _Py_open(devicename, O_RDWR); | |
| 183 | - if (fd == -1) { | ||
| 184 | - PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename); | ||
| 180 | + if (fd == -1) | ||
| 185 | 181 | return NULL; | |
| 186 | - } | ||
| 187 | 182 | ||
| 188 | 183 | if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) { | |
| 189 | 184 | close(fd); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7930,7 +7930,7 @@ os_openpty_impl(PyModuleDef *module) | |||
| 7930 | 7930 | ||
| 7931 | 7931 | slave_fd = _Py_open(slave_name, O_RDWR); | |
| 7932 | 7932 | if (slave_fd < 0) | |
| 7933 | - goto posix_error; | ||
| 7933 | + goto error; | ||
| 7934 | 7934 | ||
| 7935 | 7935 | #else | |
| 7936 | 7936 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ | |
@@ -7958,8 +7958,8 @@ os_openpty_impl(PyModuleDef *module) | |||
| 7958 | 7958 | goto posix_error; | |
| 7959 | 7959 | ||
| 7960 | 7960 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ | |
| 7961 | - if (slave_fd < 0) | ||
| 7962 | - goto posix_error; | ||
| 7961 | + if (slave_fd == -1) | ||
| 7962 | + goto error; | ||
| 7963 | 7963 | ||
| 7964 | 7964 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) | |
| 7965 | 7965 | goto posix_error; | |
@@ -7977,9 +7977,7 @@ os_openpty_impl(PyModuleDef *module) | |||
| 7977 | 7977 | ||
| 7978 | 7978 | posix_error: | |
| 7979 | 7979 | posix_error(); | |
| 7980 | - #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) | ||
| 7981 | 7980 | error: | |
| 7982 | - #endif | ||
| 7983 | 7981 | if (master_fd != -1) | |
| 7984 | 7982 | close(master_fd); | |
| 7985 | 7983 | if (slave_fd != -1) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1013,26 +1013,21 @@ newDevPollObject(void) | |||
| 1013 | 1013 | struct pollfd *fds; | |
| 1014 | 1014 | struct rlimit limit; | |
| 1015 | 1015 | ||
| 1016 | - Py_BEGIN_ALLOW_THREADS | ||
| 1017 | 1016 | /* | |
| 1018 | 1017 | ** If we try to process more that getrlimit() | |
| 1019 | 1018 | ** fds, the kernel will give an error, so | |
| 1020 | 1019 | ** we set the limit here. It is a dynamic | |
| 1021 | 1020 | ** value, because we can change rlimit() anytime. | |
| 1022 | 1021 | */ | |
| 1023 | 1022 | limit_result = getrlimit(RLIMIT_NOFILE, &limit); | |
| 1024 | - if (limit_result != -1) | ||
| 1025 | - fd_devpoll = _Py_open("/dev/poll", O_RDWR); | ||
| 1026 | - Py_END_ALLOW_THREADS | ||
| 1027 | - | ||
| 1028 | 1023 | if (limit_result == -1) { | |
| 1029 | 1024 | PyErr_SetFromErrno(PyExc_OSError); | |
| 1030 | 1025 | return NULL; | |
| 1031 | 1026 | } | |
| 1032 | - if (fd_devpoll == -1) { | ||
| 1033 | - PyErr_SetFromErrnoWithFilename(PyExc_IOError, "/dev/poll"); | ||
| 1027 | + | ||
| 1028 | + fd_devpoll = _Py_open("/dev/poll", O_RDWR); | ||
| 1029 | + if (fd_devpoll == -1) | ||
| 1034 | 1030 | return NULL; | |
| 1035 | - } | ||
| 1036 | 1031 | ||
| 1037 | 1032 | fds = PyMem_NEW(struct pollfd, limit.rlim_cur); | |
| 1038 | 1033 | if (fds == NULL) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,7 +30,8 @@ extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size); | |||
| 30 | 30 | 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23 | |
| 31 | 31 | 1: open() supports O_CLOEXEC flag, close-on-exec is set | |
| 32 | 32 | ||
| 33 | - The flag is used by _Py_open(), io.FileIO and os.open() */ | ||
| 33 | + The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO | ||
| 34 | + and os.open(). */ | ||
| 34 | 35 | int _Py_open_cloexec_works = -1; | |
| 35 | 36 | #endif | |
| 36 | 37 | ||
@@ -907,37 +908,74 @@ _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works) | |||
| 907 | 908 | return set_inheritable(fd, inheritable, 1, atomic_flag_works); | |
| 908 | 909 | } | |
| 909 | 910 | ||
| 910 | - /* Open a file with the specified flags (wrapper to open() function). | ||
| 911 | - The file descriptor is created non-inheritable. */ | ||
| 912 | - int | ||
| 913 | - _Py_open(const char *pathname, int flags) | ||
| 911 | + static int | ||
| 912 | + _Py_open_impl(const char *pathname, int flags, int gil_held) | ||
| 914 | 913 | { | |
| 915 | 914 | int fd; | |
| 916 | - #ifdef MS_WINDOWS | ||
| 917 | - fd = open(pathname, flags | O_NOINHERIT); | ||
| 918 | - if (fd < 0) | ||
| 919 | - return fd; | ||
| 920 | - #else | ||
| 921 | - | ||
| 915 | + #ifndef MS_WINDOWS | ||
| 922 | 916 | int *atomic_flag_works; | |
| 923 | - #ifdef O_CLOEXEC | ||
| 917 | + #endif | ||
| 918 | + | ||
| 919 | + #ifdef MS_WINDOWS | ||
| 920 | + flags |= O_NOINHERIT; | ||
| 921 | + #elif defined(O_CLOEXEC) | ||
| 924 | 922 | atomic_flag_works = &_Py_open_cloexec_works; | |
| 925 | 923 | flags |= O_CLOEXEC; | |
| 926 | 924 | #else | |
| 927 | 925 | atomic_flag_works = NULL; | |
| 928 | 926 | #endif | |
| 929 | - fd = open(pathname, flags); | ||
| 930 | - if (fd < 0) | ||
| 931 | - return fd; | ||
| 932 | 927 | ||
| 933 | - if (set_inheritable(fd, 0, 0, atomic_flag_works) < 0) { | ||
| 928 | + if (gil_held) { | ||
| 929 | + Py_BEGIN_ALLOW_THREADS | ||
| 930 | + fd = open(pathname, flags); | ||
| 931 | + Py_END_ALLOW_THREADS | ||
| 932 | + | ||
| 933 | + if (fd < 0) { | ||
| 934 | + PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname); | ||
| 935 | + return -1; | ||
| 936 | + } | ||
| 937 | + } | ||
| 938 | + else { | ||
| 939 | + fd = open(pathname, flags); | ||
| 940 | + if (fd < 0) | ||
| 941 | + return -1; | ||
| 942 | + } | ||
| 943 | + | ||
| 944 | + #ifndef MS_WINDOWS | ||
| 945 | + if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) { | ||
| 934 | 946 | close(fd); | |
| 935 | 947 | return -1; | |
| 936 | 948 | } | |
| 937 | - #endif /* !MS_WINDOWS */ | ||
| 949 | + #endif | ||
| 950 | + | ||
| 938 | 951 | return fd; | |
| 939 | 952 | } | |
| 940 | 953 | ||
| 954 | + /* Open a file with the specified flags (wrapper to open() function). | ||
| 955 | + Return a file descriptor on success. Raise an exception and return -1 on | ||
| 956 | + error. | ||
| 957 | + | ||
| 958 | + The file descriptor is created non-inheritable. | ||
| 959 | + | ||
| 960 | + The GIL must be held. Use _Py_open_noraise() if the GIL cannot be held. */ | ||
| 961 | + int | ||
| 962 | + _Py_open(const char *pathname, int flags) | ||
| 963 | + { | ||
| 964 | + /* _Py_open() must be called with the GIL held. */ | ||
| 965 | + assert(PyGILState_Check()); | ||
| 966 | + return _Py_open_impl(pathname, flags, 1); | ||
| 967 | + } | ||
| 968 | + | ||
| 969 | + /* Open a file with the specified flags (wrapper to open() function). | ||
| 970 | + Return a file descriptor on success. Set errno and return -1 on error. | ||
| 971 | + | ||
| 972 | + The file descriptor is created non-inheritable. */ | ||
| 973 | + int | ||
| 974 | + _Py_open_noraise(const char *pathname, int flags) | ||
| 975 | + { | ||
| 976 | + return _Py_open_impl(pathname, flags, 0); | ||
| 977 | + } | ||
| 978 | + | ||
| 941 | 979 | /* Open a file. Use _wfopen() on Windows, encode the path to the locale | |
| 942 | 980 | encoding and use fopen() otherwise. The file descriptor is created | |
| 943 | 981 | non-inheritable. */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -111,7 +111,7 @@ dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size) | |||
| 111 | 111 | ||
| 112 | 112 | assert (0 < size); | |
| 113 | 113 | ||
| 114 | - fd = _Py_open("/dev/urandom", O_RDONLY); | ||
| 114 | + fd = _Py_open_noraise("/dev/urandom", O_RDONLY); | ||
| 115 | 115 | if (fd < 0) | |
| 116 | 116 | Py_FatalError("Failed to open /dev/urandom"); | |
| 117 | 117 | ||
@@ -158,17 +158,13 @@ dev_urandom_python(char *buffer, Py_ssize_t size) | |||
| 158 | 158 | if (urandom_cache.fd >= 0) | |
| 159 | 159 | fd = urandom_cache.fd; | |
| 160 | 160 | else { | |
| 161 | - Py_BEGIN_ALLOW_THREADS | ||
| 162 | 161 | fd = _Py_open("/dev/urandom", O_RDONLY); | |
| 163 | - Py_END_ALLOW_THREADS | ||
| 164 | - if (fd < 0) | ||
| 165 | - { | ||
| 162 | + if (fd < 0) { | ||
| 166 | 163 | if (errno == ENOENT || errno == ENXIO || | |
| 167 | 164 | errno == ENODEV || errno == EACCES) | |
| 168 | 165 | PyErr_SetString(PyExc_NotImplementedError, | |
| 169 | 166 | "/dev/urandom (or equivalent) not found"); | |
| 170 | - else | ||
| 171 | - PyErr_SetFromErrno(PyExc_OSError); | ||
| 167 | + /* otherwise, keep the OSError exception raised by _Py_open() */ | ||
| 172 | 168 | return -1; | |
| 173 | 169 | } | |
| 174 | 170 | if (urandom_cache.fd >= 0) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments