Improve Libc by making Python work even better

Actually Portable Python is now outperforming the Python binaries
that come bundled with Linux distros, at things like HTTP serving.
You can now have a fully featured Python install in just one .com
file that runs on six operating systems and is about 10mb in size.
With tuning, the tiniest is ~1mb. We've got most of the libraries
working, including pysqlite, and the repl now feels very pleasant.
The things you can't do quite yet are: threads and shared objects
but that can happen in the future, if the community falls in love
with this project and wants to see it developed further. Changes:

- Add siginterrupt()
- Add sqlite3 to Python
- Add issymlink() helper
- Make GetZipCdir() faster
- Add tgamma() and finite()
- Add legacy function lutimes()
- Add readlink() and realpath()
- Use heap allocations when appropriate
- Reorganize Python into two-stage build
- Save Lua / Python shell history to dotfile
- Integrate Python Lib embedding into linkage
- Make isregularfile() and isdirectory() go faster
- Make Python shell auto-completion work perfectly
- Make crash reports work better if changed directory
- Fix Python+NT open() / access() flag overflow error
- Disable Python tests relating to \N{LONG NAME} syntax
- Have Python REPL copyright() show all notice embeddings

The biggest technical challenge at the moment is working around
when Python tries to be too clever about filenames.
This commit is contained in:
Justine Tunney 2021-08-18 14:21:30 -07:00
parent 98ccbf44b1
commit 8af197560e
179 changed files with 6728 additions and 10430 deletions

View file

@ -228,8 +228,6 @@ static int pending_async_exc = 0;
#ifdef WITH_THREAD
#ifdef HAVE_ERRNO_H
#endif
#include "third_party/python/Include/pythread.h"
static PyThread_type_lock pending_lock = 0; /* for pending calls */

View file

@ -33,7 +33,7 @@ static unsigned char M___hello__[] = {
#define SIZE (int)sizeof(M___hello__)
static const struct _frozen _PyImport_FrozenModules[] = {
const struct _frozen _PyImport_FrozenModules[] = {
/* importlib */
{"_frozen_importlib", _Py_M__importlib, (int)sizeof(_Py_M__importlib)},
{"_frozen_importlib_external", _Py_M__importlib_external,
@ -45,8 +45,3 @@ static const struct _frozen _PyImport_FrozenModules[] = {
{"__phello__.spam", M___hello__, SIZE},
{0, 0, 0} /* sentinel */
};
/* Embedding apps may change this pointer to point to their favorite
collection of frozen modules: */
const struct _frozen *PyImport_FrozenModules = _PyImport_FrozenModules;

View file

@ -0,0 +1,22 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "third_party/python/Include/import.h"
static const struct _frozen _PyImport_FrozenModules_Empty[1];
const struct _frozen *PyImport_FrozenModules = _PyImport_FrozenModules_Empty;

View file

@ -4,27 +4,37 @@
Python 3
https://docs.python.org/3/license.html │
*/
#include "libc/bits/bits.h"
#include "libc/bits/weaken.h"
#include "libc/stdio/append.internal.h"
#include "libc/str/str.h"
#include "third_party/python/Include/pylifecycle.h"
/* clang-format off */
/* Return the copyright string. This is updated manually. */
asm(".ident\t\"\\n\\n\
Python 3.6 (https://docs.python.org/3/license.html)\\n\
Copyright (c) 2001-2021 Python Software Foundation.\\n\
All Rights Reserved.\\n\
Copyright (c) 2000 BeOpen.com.\\n\
All Rights Reserved.\\n\
Copyright (c) 1995-2001 Corporation for National Research Initiatives.\\n\
All Rights Reserved.\\n\
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\\n\
All Rights Reserved.\"");
static const char cprt[] =
"\
Copyright (c) 2001-2021 Python Software Foundation.\n\
All Rights Reserved.\n\
\n\
Copyright (c) 2000 BeOpen.com.\n\
All Rights Reserved.\n\
\n\
Copyright (c) 1995-2001 Corporation for National Research Initiatives.\n\
All Rights Reserved.\n\
\n\
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\n\
All Rights Reserved.";
extern const char kLegalNotices[];
const char *
Py_GetCopyright(void)
{
return cprt;
const char *p;
static bool once;
static char *res;
if (cmpxchg(&once, 0, 1)) {
appends(&res, "");
for (p = *weaken(kLegalNotices); *p; p += strlen(p) + 1) {
appends(&res, p);
}
}
return res;
}

22
third_party/python/Python/inittab.c vendored Normal file
View file

@ -0,0 +1,22 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "third_party/python/Include/import.h"
static struct _inittab _PyImport_Inittab_Empty[1];
struct _inittab *PyImport_Inittab = _PyImport_Inittab_Empty;

View file

@ -8,6 +8,8 @@
#include "dsp/mpeg/video.h"
#include "libc/calls/calls.h"
#include "libc/calls/weirdtypes.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
#include "third_party/python/Include/abstract.h"
#include "third_party/python/Include/boolobject.h"
#include "third_party/python/Include/code.h"
@ -648,12 +650,12 @@ PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
void
PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
{
char buf[BUFSIZ];
WFILE wf;
char *buf = gc(malloc(BUFSIZ));
bzero(&wf, sizeof(wf));
wf.fp = fp;
wf.ptr = wf.buf = buf;
wf.end = wf.ptr + sizeof(buf);
wf.end = wf.ptr + BUFSIZ;
wf.error = WFERR_OK;
wf.version = version;
if (w_init_refs(&wf, version))

View file

@ -6,7 +6,7 @@
╚─────────────────────────────────────────────────────────────────────────────*/
/* clang-format off */
static void *opcode_targets[256] = {
static void *const opcode_targets[256] = {
&&_unknown_opcode,
&&TARGET_POP_TOP,
&&TARGET_ROT_TWO,

View file

@ -6,6 +6,8 @@
*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/exit.h"
@ -2175,14 +2177,14 @@ sys_update_path(int argc, wchar_t **argv)
PyObject *a;
PyObject *path;
#ifdef HAVE_READLINK
wchar_t link[MAXPATHLEN+1];
wchar_t argv0copy[2*MAXPATHLEN+1];
wchar_t *link = gc(calloc(MAXPATHLEN+1, sizeof(wchar_t)));
wchar_t *argv0copy = gc(calloc(2*MAXPATHLEN+1, sizeof(wchar_t)));
int nr = 0;
#endif
#if defined(HAVE_REALPATH)
wchar_t fullpath[MAXPATHLEN];
wchar_t *fullpath = gc(calloc(MAXPATHLEN, sizeof(wchar_t)));
#elif defined(MS_WINDOWS)
wchar_t fullpath[MAX_PATH];
wchar_t *fullpath = gc(calloc(MAX_PATH, sizeof(wchar_t)));
#endif
path = _PySys_GetObjectId(&PyId_path);
@ -2224,10 +2226,7 @@ sys_update_path(int argc, wchar_t **argv)
#if defined(MS_WINDOWS)
/* Replace the first element in argv with the full path. */
wchar_t *ptemp;
if (GetFullPathNameW(argv0,
Py_ARRAY_LENGTH(fullpath),
fullpath,
&ptemp)) {
if (GetFullPathNameW(argv0, MAX_PATH, fullpath, &ptemp)) {
argv0 = fullpath;
}
#endif
@ -2245,7 +2244,7 @@ sys_update_path(int argc, wchar_t **argv)
#else /* All other filename syntaxes */
if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
#if defined(HAVE_REALPATH)
if (_Py_wrealpath(argv0, fullpath, Py_ARRAY_LENGTH(fullpath))) {
if (_Py_wrealpath(argv0, fullpath, MAXPATHLEN)) {
argv0 = fullpath;
}
#endif