Skip to content

Commit b378738

Browse files
committed
embed: Avoid redefining open() and close() to prevent conflicts with others.
1 parent 590ae53 commit b378738

File tree

11 files changed

+33
-99
lines changed

11 files changed

+33
-99
lines changed

Include/np_embed.h

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -136,84 +136,18 @@ NP_DECL(int) np_ftrylockfile(void *e);
136136

137137
#ifndef NUITKAPYTHON_EMBED_BUILD
138138

139-
/* Using a bunch of macros from
140-
* https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms
141-
*/
142-
143-
#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
144-
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
145-
146-
#define IIF(c) PRIMITIVE_CAT(IIF_, c)
147-
#define IIF_0(t, ...) __VA_ARGS__
148-
#define IIF_1(t, ...) t
149-
150-
#define COMPL(b) PRIMITIVE_CAT(COMPL_, b)
151-
#define COMPL_0 1
152-
#define COMPL_1 0
153-
154-
#define DEC(x) PRIMITIVE_CAT(DEC_, x)
155-
#define DEC_0 0
156-
#define DEC_1 0
157-
#define DEC_2 1
158-
#define DEC_3 2
159-
#define DEC_4 3
160-
#define DEC_5 4
161-
#define DEC_6 5
162-
#define DEC_7 6
163-
#define DEC_8 7
164-
#define DEC_9 8
165-
#define DEC_10 9
166-
#define DEC_11 10
167-
#define DEC_12 11
168-
#define DEC_13 12
169-
#define DEC_14 13
170-
#define DEC_15 14
171-
#define DEC_16 15
172-
#define DEC_17 16
173-
#define DEC_18 17
174-
#define DEC_19 18
175-
#define DEC_20 19
176-
177-
#define CHECK_N(x, n, ...) n
178-
#define CHECK(...) CHECK_N(__VA_ARGS__, 0,)
179-
#define PROBE(x) x, 1,
180-
181-
#define IS_PAREN(x) CHECK(IS_PAREN_PROBE x)
182-
#define IS_PAREN_PROBE(...) PROBE(~)
183-
184-
#define NOT(x) CHECK(PRIMITIVE_CAT(NOT_, x))
185-
#define NOT_0 PROBE(~)
186-
187-
#define BOOL(x) COMPL(NOT(x))
188-
#define IF(c) IIF(BOOL(c))
189-
190-
#define EAT(...)
191-
#define EXPAND(...) __VA_ARGS__
192-
#define WHEN(c) IF(c)(EXPAND, EAT)
193-
194-
#define EXPAND(...) __VA_ARGS__
195-
196-
#define NUM_ARGS1(_20,_19,_18,_17,_16,_15,_14,_13,_12,_11,_10,_9,_8,_7,_6,_5,_4,_3,_2,_1, n, ...) n
197-
#define NUM_ARGS0(...) NUM_ARGS1(__VA_ARGS__,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
198-
#define NUM_ARGS(...) IF(DEC(NUM_ARGS0(__VA_ARGS__)))(NUM_ARGS0(__VA_ARGS__),IF(IS_PAREN(__VA_ARGS__ ()))(0,1))
199-
200139
// Preprocessor Translation
201140
#define FILE EFILE
202141
/* File Opening and Closing */
203142
#define fopen np_fopen
204143
#define _fopen np_fopen
205-
// Basically this very complicated macro overrides only calls to open() functions that have 2 or more arguments to avoid compatibility issues.
206-
#define open(...) WHEN(DEC(NUM_ARGS( __VA_ARGS__ )))(np_open(__VA_ARGS__))WHEN(NOT(DEC(NUM_ARGS( __VA_ARGS__ ))))(open(__VA_ARGS__))
207144
#define _open np_open
208145
#define fdopen np_fdopen
209146
#define _fdopen np_fdopen
210147
#define freopen np_freopen
211148
#define _freopen np_freopen
212149
#define fclose np_fclose
213150
#define _fclose np_fclose
214-
#ifndef _WIN32
215-
#define close np_close
216-
#endif
217151
#define _close np_close
218152
#ifdef _WIN32
219153
#define wopen np_wopen

Modules/_io/fileio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ internal_close(fileio *self)
114114
/* fd is accessible and someone else may have closed it */
115115
Py_BEGIN_ALLOW_THREADS
116116
_Py_BEGIN_SUPPRESS_IPH
117-
err = close(fd);
117+
err = _close(fd);
118118
if (err < 0)
119119
save_errno = errno;
120120
_Py_END_SUPPRESS_IPH
@@ -381,7 +381,7 @@ _Py_COMP_DIAG_POP
381381
#ifdef MS_WINDOWS
382382
self->fd = _wopen(widename, flags, 0666);
383383
#else
384-
self->fd = open(name, flags, 0666);
384+
self->fd = _open(name, flags, 0666);
385385
#endif
386386
Py_END_ALLOW_THREADS
387387
} while (self->fd < 0 && errno == EINTR &&

Modules/_io/winconsoleio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ internal_close(winconsoleio *self)
187187
if (self->fd != -1) {
188188
if (self->closefd) {
189189
_Py_BEGIN_SUPPRESS_IPH
190-
close(self->fd);
190+
_close(self->fd);
191191
_Py_END_SUPPRESS_IPH
192192
}
193193
self->fd = -1;

Modules/_posixsubprocess.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,11 @@ _close_open_fds_safe(int start_fd, int *fds_to_keep, Py_ssize_t fds_to_keep_len)
350350
if (fd != fd_dir_fd && fd >= start_fd &&
351351
!_is_fd_in_sorted_fd_sequence(fd, fds_to_keep,
352352
fds_to_keep_len)) {
353-
close(fd);
353+
_close(fd);
354354
}
355355
}
356356
}
357-
close(fd_dir_fd);
357+
_close(fd_dir_fd);
358358
}
359359
}
360360

@@ -396,7 +396,7 @@ _close_open_fds_maybe_unsafe(int start_fd, int *fds_to_keep,
396396
* reuse that fd otherwise we might close opendir's file descriptor in
397397
* our loop. This trick assumes that fd's are allocated on a lowest
398398
* available basis. */
399-
close(start_fd);
399+
_close(start_fd);
400400
++start_fd;
401401
#endif
402402

@@ -425,7 +425,7 @@ _close_open_fds_maybe_unsafe(int start_fd, int *fds_to_keep,
425425
if (fd != fd_used_by_opendir && fd >= start_fd &&
426426
!_is_fd_in_sorted_fd_sequence(fd, fds_to_keep,
427427
fds_to_keep_len)) {
428-
close(fd);
428+
_close(fd);
429429
}
430430
errno = 0;
431431
}

Modules/expat/xmlparse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ writeRandomBytes_dev_urandom(void *target, size_t count) {
844844
}
845845
} while (! success && (errno == EINTR));
846846

847-
close(fd);
847+
_close(fd);
848848
return success;
849849
}
850850

Modules/mmapmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ mmap_object_dealloc(mmap_object *m_obj)
161161
#ifdef UNIX
162162
Py_BEGIN_ALLOW_THREADS
163163
if (m_obj->fd >= 0)
164-
(void) close(m_obj->fd);
164+
(void) _close(m_obj->fd);
165165
if (m_obj->data!=NULL) {
166166
munmap(m_obj->data, m_obj->size);
167167
}
@@ -217,7 +217,7 @@ mmap_close_method(mmap_object *self, PyObject *unused)
217217
self->data = NULL;
218218
Py_BEGIN_ALLOW_THREADS
219219
if (0 <= fd)
220-
(void) close(fd);
220+
(void) _close(fd);
221221
if (data != NULL) {
222222
munmap(data, self->size);
223223
}

Modules/posixmodule.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,7 +4211,7 @@ _posix_listdir(path_t *path, PyObject *list)
42114211
#ifdef HAVE_FDOPENDIR
42124212
if (fd != -1) {
42134213
Py_BEGIN_ALLOW_THREADS
4214-
close(fd);
4214+
_close(fd);
42154215
Py_END_ALLOW_THREADS
42164216
}
42174217
#endif
@@ -7497,9 +7497,9 @@ os_openpty_impl(PyObject *module)
74977497
posix_error();
74987498
error:
74997499
if (master_fd != -1)
7500-
close(master_fd);
7500+
_close(master_fd);
75017501
if (slave_fd != -1)
7502-
close(slave_fd);
7502+
_close(slave_fd);
75037503
return NULL;
75047504
}
75057505
#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
@@ -7547,7 +7547,7 @@ os_login_tty_impl(PyObject *module, int fd)
75477547
return posix_error();
75487548
}
75497549
if (fd > 2) {
7550-
close(fd);
7550+
_close(fd);
75517551
}
75527552
#endif /* HAVE_LOGIN_TTY */
75537553
Py_RETURN_NONE;
@@ -9377,7 +9377,7 @@ os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
93779377
}
93789378
} else
93799379
#endif /* HAVE_OPENAT */
9380-
fd = open(path->narrow, flags, mode);
9380+
fd = _open(path->narrow, flags, mode);
93819381
#endif /* !MS_WINDOWS */
93829382
Py_END_ALLOW_THREADS
93839383
} while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
@@ -9398,7 +9398,7 @@ os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
93989398

93999399
#ifndef MS_WINDOWS
94009400
if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
9401-
close(fd);
9401+
_close(fd);
94029402
return -1;
94039403
}
94049404
#endif
@@ -9426,7 +9426,7 @@ os_close_impl(PyObject *module, int fd)
94269426
*/
94279427
Py_BEGIN_ALLOW_THREADS
94289428
_Py_BEGIN_SUPPRESS_IPH
9429-
res = close(fd);
9429+
res = _close(fd);
94309430
_Py_END_SUPPRESS_IPH
94319431
Py_END_ALLOW_THREADS
94329432
if (res < 0)
@@ -9512,7 +9512,7 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
95129512

95139513
/* Character files like console cannot be make non-inheritable */
95149514
if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
9515-
close(fd2);
9515+
_close(fd2);
95169516
return -1;
95179517
}
95189518

@@ -9557,7 +9557,7 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
95579557
}
95589558

95599559
if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
9560-
close(fd2);
9560+
_close(fd2);
95619561
return -1;
95629562
}
95639563
#ifdef HAVE_DUP3
@@ -10373,13 +10373,13 @@ os_pipe_impl(PyObject *module)
1037310373

1037410374
if (res == 0) {
1037510375
if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
10376-
close(fds[0]);
10377-
close(fds[1]);
10376+
_close(fds[0]);
10377+
_close(fds[1]);
1037810378
return NULL;
1037910379
}
1038010380
if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
10381-
close(fds[0]);
10382-
close(fds[1]);
10381+
_close(fds[0]);
10382+
_close(fds[1]);
1038310383
return NULL;
1038410384
}
1038510385
}
@@ -11014,7 +11014,7 @@ os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
1101411014
result = -1;
1101511015
else {
1101611016
result = _chsize_s(fd, length);
11017-
close(fd);
11017+
_close(fd);
1101811018
if (result < 0)
1101911019
errno = result;
1102011020
}
@@ -14648,7 +14648,7 @@ os_scandir_impl(PyObject *module, path_t *path)
1464814648
#ifdef HAVE_FDOPENDIR
1464914649
if (fd != -1) {
1465014650
Py_BEGIN_ALLOW_THREADS
14651-
close(fd);
14651+
_close(fd);
1465214652
Py_END_ALLOW_THREADS
1465314653
}
1465414654
#endif

Modules/selectmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ devpoll_internal_close(devpollObject *self)
10291029
int fd = self->fd_devpoll;
10301030
self->fd_devpoll = -1;
10311031
Py_BEGIN_ALLOW_THREADS
1032-
if (close(fd) < 0)
1032+
if (_close(fd) < 0)
10331033
save_errno = errno;
10341034
Py_END_ALLOW_THREADS
10351035
}

Python/dup2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dup2(int fd1, int fd2)
2525
if (fcntl(fd1, F_GETFL) < 0)
2626
return BADEXIT;
2727
if (fcntl(fd2, F_GETFL) >= 0)
28-
close(fd2);
28+
_close(fd2);
2929
if (fcntl(fd1, F_DUPFD, fd2) < 0)
3030
return BADEXIT;
3131
#else

Python/fileutils.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ _Py_open_impl(const char *pathname, int flags, int gil_held)
15521552

15531553
#ifndef MS_WINDOWS
15541554
if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
1555-
close(fd);
1555+
_close(fd);
15561556
return -1;
15571557
}
15581558
#endif
@@ -2384,7 +2384,7 @@ _Py_dup(int fd)
23842384

23852385
if (_Py_set_inheritable(fd, 0, NULL) < 0) {
23862386
_Py_BEGIN_SUPPRESS_IPH
2387-
close(fd);
2387+
_close(fd);
23882388
_Py_END_SUPPRESS_IPH
23892389
return -1;
23902390
}
@@ -2412,7 +2412,7 @@ _Py_dup(int fd)
24122412

24132413
if (_Py_set_inheritable(fd, 0, NULL) < 0) {
24142414
_Py_BEGIN_SUPPRESS_IPH
2415-
close(fd);
2415+
_close(fd);
24162416
_Py_END_SUPPRESS_IPH
24172417
return -1;
24182418
}
@@ -2635,7 +2635,7 @@ _fdwalk_close_func(void *lohi, int fd)
26352635
}
26362636
else if (fd >= lo) {
26372637
/* Ignore errors */
2638-
(void)close(fd);
2638+
(void)_close(fd);
26392639
}
26402640
return 0;
26412641
}
@@ -2674,7 +2674,7 @@ _Py_closerange(int first, int last)
26742674
{
26752675
for (int i = first; i <= last; i++) {
26762676
/* Ignore errors */
2677-
(void)close(i);
2677+
(void)_close(i);
26782678
}
26792679
}
26802680
#endif /* USE_FDWALK */

0 commit comments

Comments
 (0)