mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-10-15 06:06:12 +00:00
Improve Python and Linenoise
This change reinvents all the GNU Readline features I discovered that I couldn't live without, e.g. UTF-8, CTRL-R search and CTRL-Y yanking. It now feels just as good in terms of user interface from the subconscious workflow perspective. It's real nice to finally have an embeddable line reader that's actually good with a 30 kb footprint and a bsd-2 license. This change adds a directory to the examples folder, explaining how the new Python compiler may be used. Some of the bugs with Python binaries have been addressed but overall it's still a work in progress.
This commit is contained in:
parent
ad52387b74
commit
51904e2687
35 changed files with 3541 additions and 8587 deletions
2
third_party/python/Programs/hello.c
vendored
2
third_party/python/Programs/hello.c
vendored
|
@ -1,2 +0,0 @@
|
|||
#define LAUNCH "hello"
|
||||
#include "third_party/python/Programs/launch.c"
|
2
third_party/python/Programs/httpserver.c
vendored
2
third_party/python/Programs/httpserver.c
vendored
|
@ -1,2 +0,0 @@
|
|||
#define LAUNCH "http.server"
|
||||
#include "third_party/python/Programs/launch.c"
|
143
third_party/python/Programs/launch.c
vendored
143
third_party/python/Programs/launch.c
vendored
|
@ -1,143 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Python 3 │
|
||||
│ https://docs.python.org/3/license.html │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/bits/safemacros.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/rdtsc.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/fileno.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/unicode/locale.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "libc/zip.h"
|
||||
#include "third_party/linenoise/linenoise.h"
|
||||
#include "third_party/python/Include/abstract.h"
|
||||
#include "third_party/python/Include/ceval.h"
|
||||
#include "third_party/python/Include/dictobject.h"
|
||||
#include "third_party/python/Include/fileutils.h"
|
||||
#include "third_party/python/Include/funcobject.h"
|
||||
#include "third_party/python/Include/import.h"
|
||||
#include "third_party/python/Include/listobject.h"
|
||||
#include "third_party/python/Include/modsupport.h"
|
||||
#include "third_party/python/Include/moduleobject.h"
|
||||
#include "third_party/python/Include/object.h"
|
||||
#include "third_party/python/Include/pydebug.h"
|
||||
#include "third_party/python/Include/pyerrors.h"
|
||||
#include "third_party/python/Include/pylifecycle.h"
|
||||
#include "third_party/python/Include/pymem.h"
|
||||
#include "third_party/python/Include/pyport.h"
|
||||
#include "third_party/python/Include/pythonrun.h"
|
||||
#include "third_party/python/Include/sysmodule.h"
|
||||
#include "third_party/python/Include/unicodeobject.h"
|
||||
#include "third_party/python/Include/yoink.h"
|
||||
/* clang-format off */
|
||||
|
||||
#define _L(x) L##x
|
||||
#define L(x) _L(x)
|
||||
|
||||
PYTHON_YOINK(LAUNCH);
|
||||
|
||||
PYTHON_YOINK("_bootlocale");
|
||||
PYTHON_YOINK("_locale");
|
||||
PYTHON_YOINK("encodings.aliases");
|
||||
PYTHON_YOINK("encodings.latin_1");
|
||||
PYTHON_YOINK("encodings.utf_8");
|
||||
PYTHON_YOINK("launchpy");
|
||||
|
||||
const struct _frozen *PyImport_FrozenModules = _PyImport_FrozenModules;
|
||||
struct _inittab *PyImport_Inittab = _PyImport_Inittab;
|
||||
|
||||
static int LaunchModule(wchar_t *modname)
|
||||
{
|
||||
PyObject *module, *runpy, *runmodule, *runargs, *result;
|
||||
runpy = PyImport_ImportModule("launchpy");
|
||||
if (runpy == NULL) {
|
||||
fprintf(stderr, "Could not import launchpy module\n");
|
||||
PyErr_Print();
|
||||
return -1;
|
||||
}
|
||||
runmodule = PyObject_GetAttrString(runpy, "run_module_as_main");
|
||||
if (runmodule == NULL) {
|
||||
fprintf(stderr, "Could not access launchpy.run_module_as_main\n");
|
||||
PyErr_Print();
|
||||
Py_DECREF(runpy);
|
||||
return -1;
|
||||
}
|
||||
module = PyUnicode_FromWideChar(modname, wcslen(modname));
|
||||
if (module == NULL) {
|
||||
fprintf(stderr, "Could not convert module name to unicode\n");
|
||||
PyErr_Print();
|
||||
Py_DECREF(runpy);
|
||||
Py_DECREF(runmodule);
|
||||
return -1;
|
||||
}
|
||||
runargs = Py_BuildValue("(O)", module);
|
||||
if (runargs == NULL) {
|
||||
fprintf(stderr,
|
||||
"Could not create arguments for runpy._run_module_as_main\n");
|
||||
PyErr_Print();
|
||||
Py_DECREF(runpy);
|
||||
Py_DECREF(runmodule);
|
||||
Py_DECREF(module);
|
||||
return -1;
|
||||
}
|
||||
result = PyObject_Call(runmodule, runargs, NULL);
|
||||
if (result == NULL) {
|
||||
PyErr_Print();
|
||||
}
|
||||
Py_DECREF(runpy);
|
||||
Py_DECREF(runmodule);
|
||||
Py_DECREF(module);
|
||||
Py_DECREF(runargs);
|
||||
if (result == NULL) {
|
||||
return -1;
|
||||
}
|
||||
Py_DECREF(result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int i, res;
|
||||
char *oldloc;
|
||||
wchar_t **argv_copy;
|
||||
wchar_t **argv_copy2;
|
||||
_PyMem_SetupAllocators("malloc");
|
||||
argv_copy = gc(malloc(sizeof(wchar_t*) * (argc+1)));
|
||||
argv_copy2 = gc(malloc(sizeof(wchar_t*) * (argc+1)));
|
||||
oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
|
||||
setlocale(LC_ALL, "");
|
||||
for (i = 0; i < argc; i++) {
|
||||
argv_copy2[i] = argv_copy[i] = gc(utf8toutf32(argv[i], -1, 0));
|
||||
}
|
||||
argv_copy2[argc] = argv_copy[argc] = NULL;
|
||||
setlocale(LC_ALL, oldloc);
|
||||
PyMem_RawFree(oldloc);
|
||||
_PyRandom_Init();
|
||||
Py_FrozenFlag++;
|
||||
Py_NoSiteFlag++;
|
||||
/* Py_VerboseFlag++; */
|
||||
Py_NoUserSiteDirectory++;
|
||||
Py_IgnoreEnvironmentFlag++;
|
||||
Py_DontWriteBytecodeFlag++;
|
||||
Py_Initialize();
|
||||
Py_LimitedPath();
|
||||
PySys_SetArgvEx(argc, argv_copy, 0);
|
||||
res = LaunchModule(L(LAUNCH)) != 0;
|
||||
_PyMem_SetupAllocators("malloc");
|
||||
return res;
|
||||
}
|
2
third_party/python/Programs/repl.c
vendored
2
third_party/python/Programs/repl.c
vendored
|
@ -153,7 +153,7 @@ TerminalReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
|
|||
PyOS_sighandler_t saint;
|
||||
saint = PyOS_setsig(SIGINT, OnKeyboardInterrupt);
|
||||
if (setjmp(jbuf)) {
|
||||
linenoiseDisableRawMode(STDIN_FILENO);
|
||||
linenoiseDisableRawMode();
|
||||
PyOS_setsig(SIGINT, saint);
|
||||
return NULL;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue