Undiamond Python headers

This change gets the Python codebase into a state where it conforms to
the conventions of this codebase. It's now possible to include headers
from Python, without worrying about ordering. Python has traditionally
solved that problem by "diamonding" everything in Python.h, but that's
problematic since it means any change to any Python header invalidates
all the build artifacts. Lastly it makes tooling not work. Since it is
hard to explain to Emacs when I press C-c C-h to add an import line it
shouldn't add the header that actually defines the symbol, and instead
do follow the nonstandard Python convention.

Progress has been made on letting Python load source code from the zip
executable structure via the standard C library APIs. System calss now
recognizes zip!FILENAME alternative URIs as equivalent to zip:FILENAME
since Python uses colon as its delimiter.

Some progress has been made on embedding the notice license terms into
the Python object code. This is easier said than done since Python has
an extremely complicated ownership story.

- Some termios APIs have been added
- Implement rewinddir() dirstream API
- GetCpuCount() API added to Cosmopolitan Libc
- More bugs in Cosmopolitan Libc have been fixed
- zipobj.com now has flags for mangling the path
- Fixed bug a priori with sendfile() on certain BSDs
- Polyfill F_DUPFD and F_DUPFD_CLOEXEC across platforms
- FIOCLEX / FIONCLEX now polyfilled for fast O_CLOEXEC changes
- APE now supports a hybrid solution to no-self-modify for builds
- Many BSD-only magnums added, e.g. O_SEARCH, O_SHLOCK, SF_NODISKIO
This commit is contained in:
Justine Tunney 2021-08-12 00:42:14 -07:00
parent 20bb8db9f8
commit b420ed8248
762 changed files with 18410 additions and 53772 deletions

View file

@ -31,6 +31,7 @@
#include "libc/nt/runtime.h"
#include "libc/nt/struct/win32finddata.h"
#include "libc/nt/synchronization.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/dt.h"
#include "libc/sysv/consts/o.h"
@ -70,6 +71,7 @@ struct dirstream {
};
struct {
bool isdone;
char16_t *name;
struct NtWin32FindData windata;
};
};
@ -110,7 +112,7 @@ struct dirent_netbsd {
char d_name[512];
};
static textwindows DIR *opendir_nt_impl(char16_t name[PATH_MAX], size_t len) {
static textwindows DIR *opendir_nt_impl(char16_t *name, size_t len) {
DIR *res;
if (len + 2 + 1 <= PATH_MAX) {
if (len > 1 && name[len - 1] != u'\\') {
@ -134,21 +136,33 @@ static textwindows DIR *opendir_nt_impl(char16_t name[PATH_MAX], size_t len) {
static textwindows noinline DIR *opendir_nt(const char *path) {
int len;
char16_t name[PATH_MAX];
if ((len = __mkntpath(path, name)) == -1) return NULL;
return opendir_nt_impl(name, len);
DIR *res;
char16_t *name;
if ((name = malloc(PATH_MAX * 2))) {
if ((len = __mkntpath(path, name)) != -1 &&
(res = opendir_nt_impl(name, len))) {
res->name = name;
return res;
}
free(name);
}
return NULL;
}
static textwindows noinline DIR *fdopendir_nt(int fd) {
DIR *res;
char16_t name[PATH_MAX];
char16_t *name;
if (__isfdkind(fd, kFdFile)) {
if ((res = opendir_nt_impl(
name, GetFinalPathNameByHandle(
g_fds.p[fd].handle, name, ARRAYLEN(name),
kNtFileNameNormalized | kNtVolumeNameDos)))) {
close(fd);
return res;
if ((name = malloc(PATH_MAX * 2))) {
if ((res = opendir_nt_impl(
name, GetFinalPathNameByHandle(
g_fds.p[fd].handle, name, PATH_MAX,
kNtFileNameNormalized | kNtVolumeNameDos)))) {
res->name = name;
close(fd);
return res;
}
free(name);
}
} else {
ebadf();
@ -217,6 +231,7 @@ DIR *opendir(const char *name) {
struct Zipos *zip;
struct ZiposUri zipname;
if (weaken(__zipos_get) && weaken(__zipos_parseuri)(name, &zipname) != -1) {
ZTRACE("__zipos_opendir(%`'s)", name);
zip = weaken(__zipos_get)();
res = calloc(1, sizeof(DIR));
res->iszip = true;
@ -369,6 +384,7 @@ int closedir(DIR *dir) {
} else if (!IsWindows()) {
rc = close(dir->fd);
} else {
free(dir->name);
rc = FindClose(dir->fd) ? 0 : __winerr();
}
free(dir);
@ -393,3 +409,26 @@ int dirfd(DIR *dir) {
if (IsWindows()) return eopnotsupp();
return dir->fd;
}
/**
* Seeks to beginning of directory stream.
*/
void rewinddir(DIR *dir) {
if (dir->iszip) {
dir->tell = 0;
dir->zip.offset = GetZipCdirOffset(weaken(__zipos_get)()->cdir);
} else if (!IsWindows()) {
if (!lseek(dir->fd, 0, SEEK_SET)) {
dir->buf_pos = dir->buf_end = 0;
dir->tell = 0;
}
} else {
FindClose(dir->fd);
if ((dir->fd = FindFirstFile(dir->name, &dir->windata)) != -1) {
dir->isdone = false;
dir->tell = 0;
} else {
dir->isdone = true;
}
}
}