8000 bpo-46315: Use fopencookie() to avoid dup() in _PyTokenizer_FindEncodingFilename by tiran · Pull Request #32033 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
8000

bpo-46315: Use fopencookie() to avoid dup() in _PyTokenizer_FindEncodingFilename #32033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Shorten code even further
  • Loading branch information
tiran committed Mar 22, 2022
commit fa6ee5eec0129e8e344782a910bfed9b74ba778d
17 changes: 6 additions & 11 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2073,11 +2073,8 @@ _PyTokenizer_Get(struct tok_state *tok,
}

#if defined(__wasi__) || defined(__EMSCRIPTEN__)
/* fdopen() with borrowed fd

WASI does not provide dup() and Emscripten's dup() emulation with open()
is slow. Implement fdopen() with fd borrowing on top of fdopencookie().
*/
// fdopen() with borrowed fd. WASI does not provide dup() and Emscripten's
// dup() emulation with open() is slow.
typedef union {
void *cookie;
int fd;
Expand All @@ -2091,22 +2088,20 @@ borrow_read(void *cookie, char *buf, size_t size)
}

static FILE *
fdopen_borrow(int fd, const char *mode) {
fdopen_borrow(int fd) {
// supports only reading. seek fails. close and write are no-ops.
assert(strcmp(mode, "r") == 0);
cookie_io_functions_t io_cb = {borrow_read, NULL, NULL, NULL};
borrowed b = {.fd = fd};
return fopencookie(b.cookie, "r", io_cb);
}
#else
static FILE *
fdopen_borrow(int fd, const char *mode) {
assert(strcmp(mode, "r") == 0);
fdopen_borrow(int fd) {
fd = _Py_dup(fd);
if (fd < 0) {
return NULL;
}
return fdopen(fd, mode);
return fdopen(fd, "r");
}
#endif

Expand All @@ -2129,7 +2124,7 @@ _PyTokenizer_FindEncodingFilename(int fd, PyObject *filename)
const char *p_end = NULL;
char *encoding = NULL;

fp = fdopen_borrow(fd, "r");
fp = fdopen_borrow(fd);
if (fp == NULL) {
return NULL;
}
Expand Down
0