Message410198
dup() is required by _PyTokenizer_FindEncodingFilename(). I came up with this hack:
// from wasi-libc libc-top-half/musl/src/internal/stdio_impl.h
struct _IO_FILE {
unsigned flags;
unsigned char *rpos, *rend;
int (*close)(FILE *);
unsigned char *wend, *wpos;
// incomplete
};
static int
dummy_close(FILE *fp) {
return 0;
};
static FILE *
_Py_fdopen_borrow(int fd, const char *mode) {
FILE *fp = fdopen(fd, mode);
((struct _IO_FILE*)fp)->close = dummy_close;
return fp;
}
keithw on #wasi pointed out that fopencookie() can archive the same outcome without resorting to ABI-specific hack. A trivial implementation is straight forward:
typedef union {
void *cookie;
int fd;
} borrowed;
static ssize_t
borrow_read(void *cookie, char *buf, size_t size)
{
borrowed b;
b.cookie = cookie;
return read(b.fd, (void *)buf, size);
}
static ssize_t
borrow_write(void *cookie, const char *buf, size_t size)
{
errno = ENOTSUP;
return -1;
}
static int
borrow_seek(void *cookie, off_t *off, int whence)
{
borrowed b;
b.cookie = cookie;
off_t pos;
pos = lseek(b.fd, *off, whence);
if (pos == (off_t)-1) {
return -1;
} else {
*off = pos;
return 0;
}
}
static int
borrow_close(void *cookie)
{
// does not close(fd)
return 0;
}
FILE *
_Py_fdopen_borrow(int fd, const char *mode) {
// only support read for now
if (strcmp(mode, "r") != 0) {
return NULL;
}
cookie_io_functions_t cookie_io = {
borrow_read, borrow_write, borrow_seek, borrow_close
};
// cookie is just the fd
borrowed b;
b.fd = fd;
return fopencookie(b.cookie, "r", cookie_io);
} |
|
Date |
User |
Action |
Args |
2022-01-10 10:59:10 | christian.heimes | set | recipients:
+ christian.heimes, pmpp, kumaraditya |
2022-01-10 10:59:10 | christian.heimes | set | messageid: <1641812350.53.0.779693256804.issue46315@roundup.psfhosted.org> |
2022-01-10 10:59:10 | christian.heimes | link | issue46315 messages |
2022-01-10 10:59:10 | christian.heimes | create | |
|