@@ -1638,10 +1638,10 @@ dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
16381638}
16391639
16401640static int
1641- fd_and_follow_symlinks_invalid(const char *function_name, int fd ,
1641+ fd_and_follow_symlinks_invalid(const char *function_name, int is_fd ,
16421642 int follow_symlinks)
16431643{
1644- if ((fd >= 0) && (!follow_symlinks)) {
1644+ if (is_fd && (!follow_symlinks)) {
16451645 PyErr_Format(PyExc_ValueError,
16461646 "%s: cannot use fd and follow_symlinks together",
16471647 function_name);
@@ -2880,12 +2880,13 @@ posix_do_stat(PyObject *module, const char *function_name, path_t *path,
28802880
28812881 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
28822882 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2883- fd_and_follow_symlinks_invalid("stat", path->fd , follow_symlinks))
2883+ fd_and_follow_symlinks_invalid("stat", path->is_fd , follow_symlinks))
28842884 return NULL;
28852885
28862886 Py_BEGIN_ALLOW_THREADS
2887- if (path->fd != -1)
2887+ if (path->is_fd) {
28882888 result = FSTAT(path->fd, &st);
2889+ }
28892890#ifdef MS_WINDOWS
28902891 else if (follow_symlinks)
28912892 result = win32_stat(path->wide, &st);
@@ -3647,7 +3648,7 @@ os_statx_impl(PyObject *module, path_t *path, unsigned int mask, int flags,
36473648{
36483649 if (path_and_dir_fd_invalid("statx", path, dir_fd) ||
36493650 dir_fd_and_fd_invalid("statx", dir_fd, path->fd) ||
3650- fd_and_follow_symlinks_invalid("statx", path->fd , follow_symlinks)) {
3651+ fd_and_follow_symlinks_invalid("statx", path->is_fd , follow_symlinks)) {
36513652 return NULL;
36523653 }
36533654
@@ -3677,7 +3678,7 @@ os_statx_impl(PyObject *module, path_t *path, unsigned int mask, int flags,
36773678
36783679 int result;
36793680 Py_BEGIN_ALLOW_THREADS
3680- if (path->fd != -1 ) {
3681+ if (path->is_fd ) {
36813682 result = statx(path->fd, "", flags | AT_EMPTY_PATH, mask, &v->stx);
36823683 }
36833684 else {
@@ -3934,7 +3935,7 @@ os_chdir_impl(PyObject *module, path_t *path)
39343935 result = !win32_wchdir(path->wide);
39353936#else
39363937#ifdef HAVE_FCHDIR
3937- if (path->fd != -1 )
3938+ if (path->is_fd )
39383939 result = fchdir(path->fd);
39393940 else
39403941#endif
@@ -4090,7 +4091,7 @@ os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
40904091#ifdef MS_WINDOWS
40914092 result = 0;
40924093 Py_BEGIN_ALLOW_THREADS
4093- if (path->fd != -1 ) {
4094+ if (path->is_fd ) {
40944095 result = win32_fchmod(path->fd, mode);
40954096 }
40964097 else if (follow_symlinks) {
@@ -4113,8 +4114,9 @@ os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
41134114#else /* MS_WINDOWS */
41144115 Py_BEGIN_ALLOW_THREADS
41154116#ifdef HAVE_FCHMOD
4116- if (path->fd != -1)
4117+ if (path->is_fd) {
41174118 result = fchmod(path->fd, mode);
4119+ }
41184120 else
41194121#endif /* HAVE_CHMOD */
41204122#ifdef HAVE_LCHMOD
@@ -4511,7 +4513,7 @@ os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
45114513 return NULL;
45124514#endif
45134515 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
4514- fd_and_follow_symlinks_invalid("chown", path->fd , follow_symlinks))
4516+ fd_and_follow_symlinks_invalid("chown", path->is_fd , follow_symlinks))
45154517 return NULL;
45164518
45174519 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid,
@@ -4521,7 +4523,7 @@ os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
45214523
45224524 Py_BEGIN_ALLOW_THREADS
45234525#ifdef HAVE_FCHOWN
4524- if (path->fd != -1 )
4526+ if (path->is_fd )
45254527 result = fchown(path->fd, uid, gid);
45264528 else
45274529#endif
@@ -4999,7 +5001,7 @@ _posix_listdir(path_t *path, PyObject *list)
49995001
50005002 errno = 0;
50015003#ifdef HAVE_FDOPENDIR
5002- if (path->fd != -1 ) {
5004+ if (path->is_fd ) {
50035005 if (HAVE_FDOPENDIR_RUNTIME) {
50045006 /* closedir() closes the FD, so we duplicate it */
50055007 fd = _Py_dup(path->fd);
@@ -5898,7 +5900,7 @@ _testFileExists(path_t *path, BOOL followLinks)
58985900 }
58995901
59005902 Py_BEGIN_ALLOW_THREADS
5901- if (path->fd != -1 ) {
5903+ if (path->is_fd ) {
59025904 HANDLE hfile = _Py_get_osfhandle_noraise(path->fd);
59035905 if (hfile != INVALID_HANDLE_VALUE) {
59045906 if (GetFileType(hfile) != FILE_TYPE_UNKNOWN || !GetLastError()) {
@@ -5924,7 +5926,7 @@ _testFileType(path_t *path, int testedType)
59245926 }
59255927
59265928 Py_BEGIN_ALLOW_THREADS
5927- if (path->fd != -1 ) {
5929+ if (path->is_fd ) {
59285930 HANDLE hfile = _Py_get_osfhandle_noraise(path->fd);
59295931 if (hfile != INVALID_HANDLE_VALUE) {
59305932 result = _testFileTypeByHandle(hfile, testedType, TRUE);
@@ -7141,7 +7143,7 @@ os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
71417143
71427144 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
71437145 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
7144- fd_and_follow_symlinks_invalid("utime", path->fd , follow_symlinks))
7146+ fd_and_follow_symlinks_invalid("utime", path->is_fd , follow_symlinks))
71457147 return NULL;
71467148
71477149#if !defined(HAVE_UTIMENSAT)
@@ -7200,7 +7202,7 @@ os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
72007202#endif
72017203
72027204#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
7203- if (path->fd != -1 )
7205+ if (path->is_fd )
72047206 result = utime_fd(&utime, path->fd);
72057207 else
72067208#endif
@@ -7569,7 +7571,7 @@ os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
75697571
75707572 _Py_BEGIN_SUPPRESS_IPH
75717573#ifdef HAVE_FEXECVE
7572- if (path->fd > -1 )
7574+ if (path->is_fd )
75737575 fexecve(path->fd, argvlist, envlist);
75747576 else
75757577#endif
@@ -13355,7 +13357,7 @@ os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
1335513357 int fd;
1335613358#endif
1335713359
13358- if (path->fd != -1 )
13360+ if (path->is_fd )
1335913361 return os_ftruncate_impl(module, path->fd, length);
1336013362
1336113363 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
@@ -14052,7 +14054,7 @@ os_statvfs_impl(PyObject *module, path_t *path)
1405214054 struct statfs st;
1405314055
1405414056 Py_BEGIN_ALLOW_THREADS
14055- if (path->fd != -1 ) {
14057+ if (path->is_fd ) {
1405614058 result = fstatfs(path->fd, &st);
1405714059 }
1405814060 else
@@ -14070,7 +14072,7 @@ os_statvfs_impl(PyObject *module, path_t *path)
1407014072
1407114073 Py_BEGIN_ALLOW_THREADS
1407214074#ifdef HAVE_FSTATVFS
14073- if (path->fd != -1 ) {
14075+ if (path->is_fd ) {
1407414076 result = fstatvfs(path->fd, &st);
1407514077 }
1407614078 else
@@ -15410,7 +15412,7 @@ os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
1541015412 int follow_symlinks)
1541115413/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
1541215414{
15413- if (fd_and_follow_symlinks_invalid("getxattr", path->fd , follow_symlinks))
15415+ if (fd_and_follow_symlinks_invalid("getxattr", path->is_fd , follow_symlinks))
1541415416 return NULL;
1541515417
1541615418 if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) {
@@ -15432,7 +15434,7 @@ os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
1543215434 void *ptr = PyBytesWriter_GetData(writer);
1543315435
1543415436 Py_BEGIN_ALLOW_THREADS;
15435- if (path->fd >= 0 )
15437+ if (path->is_fd )
1543615438 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
1543715439 else if (follow_symlinks)
1543815440 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
@@ -15481,7 +15483,7 @@ os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
1548115483{
1548215484 ssize_t result;
1548315485
15484- if (fd_and_follow_symlinks_invalid("setxattr", path->fd , follow_symlinks))
15486+ if (fd_and_follow_symlinks_invalid("setxattr", path->is_fd , follow_symlinks))
1548515487 return NULL;
1548615488
1548715489 if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object,
@@ -15490,7 +15492,7 @@ os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
1549015492 }
1549115493
1549215494 Py_BEGIN_ALLOW_THREADS;
15493- if (path->fd > -1 )
15495+ if (path->is_fd )
1549415496 result = fsetxattr(path->fd, attribute->narrow,
1549515497 value->buf, value->len, flags);
1549615498 else if (follow_symlinks)
@@ -15534,15 +15536,15 @@ os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
1553415536{
1553515537 ssize_t result;
1553615538
15537- if (fd_and_follow_symlinks_invalid("removexattr", path->fd , follow_symlinks))
15539+ if (fd_and_follow_symlinks_invalid("removexattr", path->is_fd , follow_symlinks))
1553815540 return NULL;
1553915541
1554015542 if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) {
1554115543 return NULL;
1554215544 }
1554315545
1554415546 Py_BEGIN_ALLOW_THREADS;
15545- if (path->fd > -1 )
15547+ if (path->is_fd )
1554615548 result = fremovexattr(path->fd, attribute->narrow);
1554715549 else if (follow_symlinks)
1554815550 result = removexattr(path->narrow, attribute->narrow);
@@ -15584,7 +15586,7 @@ os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
1558415586 const char *name;
1558515587 char *buffer = NULL;
1558615588
15587- if (fd_and_follow_symlinks_invalid("listxattr", path->fd , follow_symlinks))
15589+ if (fd_and_follow_symlinks_invalid("listxattr", path->is_fd , follow_symlinks))
1558815590 goto exit;
1558915591
1559015592 if (PySys_Audit("os.listxattr", "(O)",
@@ -15611,7 +15613,7 @@ os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
1561115613 }
1561215614
1561315615 Py_BEGIN_ALLOW_THREADS;
15614- if (path->fd > -1 )
15616+ if (path->is_fd )
1561515617 length = flistxattr(path->fd, buffer, buffer_size);
1561615618 else if (follow_symlinks)
1561715619 length = listxattr(name, buffer, buffer_size);
@@ -16664,7 +16666,7 @@ DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
1666416666 entry->stat = NULL;
1666516667 entry->lstat = NULL;
1666616668
16667- if (path->fd != -1 ) {
16669+ if (path->is_fd ) {
1666816670 entry->dir_fd = path->fd;
1666916671 joined_path = NULL;
1667016672 }
@@ -16689,7 +16691,7 @@ DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
1668916691 if (!entry->name)
1669016692 goto error;
1669116693
16692- if (path->fd != -1 ) {
16694+ if (path->is_fd ) {
1669316695 entry->path = Py_NewRef(entry->name);
1669416696 }
1669516697 else if (!entry->path)
@@ -16813,8 +16815,9 @@ ScandirIterator_closedir(ScandirIterator *iterator)
1681316815 iterator->dirp = NULL;
1681416816 Py_BEGIN_ALLOW_THREADS
1681516817#ifdef HAVE_FDOPENDIR
16816- if (iterator->path.fd != -1)
16818+ if (iterator->path.is_fd) {
1681716819 rewinddir(dirp);
16820+ }
1681816821#endif
1681916822 closedir(dirp);
1682016823 Py_END_ALLOW_THREADS
@@ -17034,7 +17037,7 @@ os_scandir_impl(PyObject *module, path_t *path)
1703417037#else /* POSIX */
1703517038 errno = 0;
1703617039#ifdef HAVE_FDOPENDIR
17037- if (iterator->path.fd != -1 ) {
17040+ if (iterator->path.is_fd ) {
1703817041 if (HAVE_FDOPENDIR_RUNTIME) {
1703917042 /* closedir() closes the FD, so we duplicate it */
1704017043 fd = _Py_dup(iterator->path.fd);
0 commit comments