mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +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
103
third_party/python/launch.c
vendored
Normal file
103
third_party/python/launch.c
vendored
Normal file
|
@ -0,0 +1,103 @@
|
|||
/*-*- 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/x/x.h"
|
||||
#include "third_party/python/Include/abstract.h"
|
||||
#include "third_party/python/Include/import.h"
|
||||
#include "third_party/python/Include/modsupport.h"
|
||||
#include "third_party/python/Include/object.h"
|
||||
#include "third_party/python/Include/pydebug.h"
|
||||
#include "third_party/python/Include/pylifecycle.h"
|
||||
#include "third_party/python/Include/pymem.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 */
|
||||
|
||||
STATIC_YOINK("zip_uri_support");
|
||||
PYTHON_YOINK("_bootlocale");
|
||||
PYTHON_YOINK("_locale");
|
||||
PYTHON_YOINK("encodings.aliases");
|
||||
PYTHON_YOINK("encodings.latin_1");
|
||||
PYTHON_YOINK("encodings.utf_8");
|
||||
PYTHON_YOINK("launchpy");
|
||||
|
||||
extern char kLaunchPythonModuleName[]; /* generated by pyobj.com */
|
||||
const struct _frozen *PyImport_FrozenModules = _PyImport_FrozenModules;
|
||||
struct _inittab *PyImport_Inittab = _PyImport_Inittab;
|
||||
|
||||
int
|
||||
LaunchPythonModule(const char *name)
|
||||
{
|
||||
PyObject *mod, *runpy, *runmodule, *runargs, *result;
|
||||
if (!(runpy = PyImport_ImportModule("launchpy"))) {
|
||||
PyErr_Print();
|
||||
return 123;
|
||||
}
|
||||
if (!(runmodule = PyObject_GetAttrString(runpy, "run_module_as_main"))) {
|
||||
PyErr_Print();
|
||||
Py_DECREF(runpy);
|
||||
return 122;
|
||||
}
|
||||
if (!(mod = PyUnicode_DecodeUTF8Stateful(name, strlen(name), 0, 0))) {
|
||||
PyErr_Print();
|
||||
Py_DECREF(runpy);
|
||||
Py_DECREF(runmodule);
|
||||
return 121;
|
||||
}
|
||||
if (!(runargs = Py_BuildValue("(O)", mod))) {
|
||||
PyErr_Print();
|
||||
Py_DECREF(runpy);
|
||||
Py_DECREF(runmodule);
|
||||
Py_DECREF(mod);
|
||||
return 119;
|
||||
}
|
||||
if (!(result = PyObject_Call(runmodule, runargs, NULL))) {
|
||||
PyErr_Print();
|
||||
}
|
||||
Py_DECREF(runpy);
|
||||
Py_DECREF(runmodule);
|
||||
Py_DECREF(mod);
|
||||
Py_DECREF(runargs);
|
||||
if (!result) return 118;
|
||||
Py_DECREF(result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
size_t n;
|
||||
int i, sts;
|
||||
wchar_t *w;
|
||||
PyObject *a, *s;
|
||||
Py_FrozenFlag++;
|
||||
Py_NoSiteFlag++;
|
||||
/* Py_VerboseFlag++; */
|
||||
Py_NoUserSiteDirectory++;
|
||||
Py_IgnoreEnvironmentFlag++;
|
||||
Py_DontWriteBytecodeFlag++;
|
||||
#if defined(Py_DEBUG) || defined(USE_TRACEMALLOC)
|
||||
_PyMem_SetupAllocators(Py_GETENV("PYTHONMALLOC"));
|
||||
#else
|
||||
_PyMem_SetupAllocators(0);
|
||||
#endif
|
||||
_PyRandom_Init();
|
||||
Py_Initialize();
|
||||
Py_LimitedPath();
|
||||
if (!(a = PyList_New(argc))) return 127;
|
||||
for (i = 0; i < argc; ++i) {
|
||||
if (!(w = utf8toutf32(argv[i], -1, &n))) return 126;
|
||||
if (!(s = PyUnicode_FromWideChar(w, n))) return 125;
|
||||
PyList_SetItem(a, i, s);
|
||||
free(w);
|
||||
}
|
||||
if (PySys_SetObject("argv", a)) return 124;
|
||||
sts = LaunchPythonModule(kLaunchPythonModuleName);
|
||||
if (Py_FinalizeEx() < 0) sts = 120;
|
||||
return sts;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue