Make numerous improvements

- Python static hello world now 1.8mb
- Python static fully loaded now 10mb
- Python HTTPS client now uses MbedTLS
- Python REPL now completes import stmts
- Increase stack size for Python for now
- Begin synthesizing posixpath and ntpath
- Restore Python \N{UNICODE NAME} support
- Restore Python NFKD symbol normalization
- Add optimized code path for Intel SHA-NI
- Get more Python unit tests passing faster
- Get Python help() pagination working on NT
- Python hashlib now supports MbedTLS PBKDF2
- Make memcpy/memmove/memcmp/bcmp/etc. faster
- Add Mersenne Twister and Vigna to LIBC_RAND
- Provide privileged __printf() for error code
- Fix zipos opendir() so that it reports ENOTDIR
- Add basic chmod() implementation for Windows NT
- Add Cosmo's best functions to Python cosmo module
- Pin function trace indent depth to that of caller
- Show memory diagram on invalid access in MODE=dbg
- Differentiate stack overflow on crash in MODE=dbg
- Add stb_truetype and tools for analyzing font files
- Upgrade to UNICODE 13 and reduce its binary footprint
- COMPILE.COM now logs resource usage of build commands
- Start implementing basic poll() support on bare metal
- Set getauxval(AT_EXECFN) to GetModuleFileName() on NT
- Add descriptions to strerror() in non-TINY build modes
- Add COUNTBRANCH() macro to help with micro-optimizations
- Make error / backtrace / asan / memory code more unbreakable
- Add fast perfect C implementation of μ-Law and a-Law audio codecs
- Make strtol() functions consistent with other libc implementations
- Improve Linenoise implementation (see also github.com/jart/bestline)
- COMPILE.COM now suppresses stdout/stderr of successful build commands
This commit is contained in:
Justine Tunney 2021-09-27 22:58:51 -07:00
parent fa7b4f5bd1
commit 39bf41f4eb
806 changed files with 77494 additions and 63859 deletions

View file

@ -4,7 +4,9 @@
Python 3
https://docs.python.org/3/license.html │
*/
#define PY_SSIZE_T_CLEAN
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
@ -41,9 +43,12 @@
#include "third_party/python/Include/yoink.h"
/* clang-format off */
STATIC_YOINK("__die");
STATIC_YOINK("zip_uri_support");
PYTHON_YOINK("_bootlocale");
PYTHON_YOINK("cosmo");
PYTHON_YOINK("_locale");
PYTHON_YOINK("_bootlocale");
PYTHON_YOINK("encodings.aliases");
PYTHON_YOINK("encodings.latin_1");
PYTHON_YOINK("encodings.utf_8");
@ -51,19 +56,61 @@ PYTHON_YOINK("encodings.utf_8");
extern char kLaunchPythonModuleName[]; /* optionally generated by pyobj.com */
const struct _frozen *PyImport_FrozenModules = _PyImport_FrozenModules;
struct _inittab *PyImport_Inittab = _PyImport_Inittab;
static jmp_buf jbuf;
static int g_gotint;
static void
OnKeyboardInterrupt(int sig)
{
gclongjmp(jbuf, 1);
g_gotint = sig;
}
static void
AddCompletion(linenoiseCompletions *c, char *s)
{
c->cvec = realloc(c->cvec, ++c->len * sizeof(*c->cvec));
c->cvec[c->len - 1] = s;
char **p = c->cvec;
size_t n = c->len + 1;
if ((p = realloc(p, n * sizeof(*p)))) {
p[n - 1] = s;
c->cvec = p;
c->len = n;
}
}
static void
CompleteModule(const char *s, const char *p, linenoiseCompletions *c)
{
const char *name;
struct _inittab *it;
Py_ssize_t plen, namelen;
PyObject *m, *f, *g, *i, *v, *n;
plen = strlen(p);
for (it = PyImport_Inittab; it->name; ++it) {
if (startswith(it->name, p)) {
AddCompletion(c, xasprintf("%s%s", s, it->name + plen));
}
}
if ((m = PyImport_ImportModule("pkgutil"))) {
if ((f = PyObject_GetAttrString(m, "iter_modules"))) {
if ((g = PyObject_CallFunctionObjArgs(f, 0))) {
if ((i = PyObject_GetIter(g))) {
while ((v = PyIter_Next(i))) {
if ((n = PyObject_GetAttrString(v, "name"))) {
if (((name = PyUnicode_AsUTF8AndSize(n, &namelen)) &&
namelen >= plen && !bcmp(name, p, plen))) {
AddCompletion(c, xasprintf("%s%s", s, name + plen));
}
Py_DECREF(n);
}
Py_DECREF(v);
}
Py_DECREF(i);
}
Py_DECREF(g);
}
Py_DECREF(f);
}
Py_DECREF(m);
}
}
static void
@ -76,7 +123,7 @@ CompleteDict(const char *b, const char *q, const char *p,
for (i = 0; PyDict_Next(o, &i, &k, &v);) {
if ((v != Py_None && PyUnicode_Check(k) &&
(s = PyUnicode_AsUTF8AndSize(k, &m)) &&
m >= q - p && !memcmp(s, p, q - p))) {
m >= q - p && !bcmp(s, p, q - p))) {
AddCompletion(c, xasprintf("%.*s%.*s", p - b, b, m, s));
}
}
@ -89,25 +136,35 @@ CompleteDir(const char *b, const char *q, const char *p,
Py_ssize_t m;
const char *s;
PyObject *d, *i, *k;
if (!(d = PyObject_Dir(o))) return;
if ((i = PyObject_GetIter(d))) {
while ((k = PyIter_Next(i))) {
if (((s = PyUnicode_AsUTF8AndSize(k, &m)) &&
m >= q - p && !memcmp(s, p, q - p))) {
AddCompletion(c, xasprintf("%.*s%.*s", p - b, b, m, s));
if ((d = PyObject_Dir(o))) {
if ((i = PyObject_GetIter(d))) {
while ((k = PyIter_Next(i))) {
if (((s = PyUnicode_AsUTF8AndSize(k, &m)) &&
m >= q - p && !bcmp(s, p, q - p))) {
AddCompletion(c, xasprintf("%.*s%.*s", p - b, b, m, s));
}
Py_DECREF(k);
}
Py_DECREF(k);
Py_DECREF(i);
}
Py_DECREF(i);
Py_DECREF(d);
}
Py_DECREF(d);
}
static void
TerminalCompletion(const char *p, linenoiseCompletions *c)
Complete(const char *p, linenoiseCompletions *c)
{
PyObject *o, *t, *i;
const char *q, *s, *b;
if (startswith(p, "import ")) {
for (q = p + 7; *q; ++q) {
if (!isalnum(*q) && *q != '_') {
return;
}
}
CompleteModule(p, p + 7, c);
return;
}
for (b = p, p += strlen(p); p > b; --p) {
if (!isalnum(p[-1]) && p[-1] != '.' && p[-1] != '_') {
break;
@ -141,19 +198,38 @@ TerminalCompletion(const char *p, linenoiseCompletions *c)
}
}
static void
TerminalCompletion(const char *p, linenoiseCompletions *c)
{
Complete(p, c);
if (PyErr_Occurred()) {
PyErr_Clear();
}
}
static char *
TerminalHint(const char *p, const char **ansi1, const char **ansi2)
{
char *h = 0;
linenoiseCompletions c = {0};
TerminalCompletion(p, &c);
if (c.len == 1) {
h = strdup(c.cvec[0] + strlen(p));
}
if (c.len == 1) h = strdup(c.cvec[0] + strlen(p));
linenoiseFreeCompletions(&c);
return h;
}
static char *
ReinterpretCommand(const char *line)
{
size_t n;
n = strlen(line);
if (n && line[n - 1] == '?') {
return xstrcat("help(", gc(strndup(gc(line), n - 1)), ')');
} else {
return line;
}
}
static char *
TerminalReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
{
@ -161,23 +237,22 @@ TerminalReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
char *p, *q;
PyOS_sighandler_t saint;
saint = PyOS_setsig(SIGINT, OnKeyboardInterrupt);
if (setjmp(jbuf)) {
linenoiseDisableRawMode();
PyOS_setsig(SIGINT, saint);
return NULL;
}
p = ezlinenoise(prompt, "python");
p = linenoiseWithHistory(prompt, "python");
PyOS_setsig(SIGINT, saint);
if (p) {
if (g_gotint) {
PyOS_setsig(SIGINT, saint);
g_gotint = 0;
q = 0;
} else if (p) {
p = ReinterpretCommand(p);
n = strlen(p);
q = PyMem_RawMalloc(n + 2);
strcpy(mempcpy(q, p, n), "\n");
free(p);
clearerr(sys_stdin);
} else {
q = PyMem_RawMalloc(1);
if (q) *q = 0;
} else if ((q = PyMem_RawMalloc(1))) {
*q = 0;
}
free(p);
return q;
}
@ -191,10 +266,6 @@ RunPythonModule(int argc, char **argv)
int i, res;
char *oldloc;
/* if (FindDebugBinary()) { */
/* ShowCrashReports(); */
/* } */
if (argc == 1 && weaken(kLaunchPythonModuleName)) {
launchargs[0] = argv[0];
launchargs[1] = strdup("-m");