From 675e44e7e3b4fb6327b376323ee0db33d01a8b9c Mon Sep 17 00:00:00 2001 From: ahgamut <41098605+ahgamut@users.noreply.github.com> Date: Sat, 4 Sep 2021 05:05:12 +0530 Subject: [PATCH] added _cosmo builtin module The purpose of this module is to provide Cosmopolitan-Libc specific information to Python wherever required, and to also provide shortcut implementations of commonly used functions for possible speedups. --- third_party/python/Include/cosmo.h | 10 +++ third_party/python/Modules/config.c | 5 ++ third_party/python/Python/cosmomodule.c | 102 ++++++++++++++++++++++++ third_party/python/Python/pylifecycle.c | 14 ++++ third_party/python/python.mk | 4 +- 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 third_party/python/Include/cosmo.h create mode 100644 third_party/python/Python/cosmomodule.c diff --git a/third_party/python/Include/cosmo.h b/third_party/python/Include/cosmo.h new file mode 100644 index 000000000..97214976d --- /dev/null +++ b/third_party/python/Include/cosmo.h @@ -0,0 +1,10 @@ +#ifndef Py_COSMO_H +#define Py_COSMO_H +#include "third_party/python/Include/object.h" +COSMOPOLITAN_C_START_ +/* clang-format off */ + +PyMODINIT_FUNC PyInit_cosmo(void); + +COSMOPOLITAN_C_END_ +#endif /* !Py_IMPORT_H */ diff --git a/third_party/python/Modules/config.c b/third_party/python/Modules/config.c index 7d700708d..2e198af20 100644 --- a/third_party/python/Modules/config.c +++ b/third_party/python/Modules/config.c @@ -27,6 +27,7 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. #include "third_party/python/Include/pyport.h" #include "third_party/python/Include/pyport.h" #include "third_party/python/Include/Python.h" +#include "third_party/python/Include/cosmo.h" PyObject* PyInit__decimal(void); PyObject* PyInit_audioop(void); @@ -101,6 +102,7 @@ PyObject *PyInit__sqlite3(void); PyObject* PyMarshal_Init(void); PyObject* PyInit_imp(void); +PyObject* PyInit_cosmo(void); PyObject* PyInit_gc(void); PyObject* PyInit__ast(void); PyObject* _PyWarnings_Init(void); @@ -188,6 +190,9 @@ struct _inittab _PyImport_Inittab[] = { /* This lives in import.c */ {"_imp", PyInit_imp}, + /* This lives in cosmomodule.c */ + {"_cosmo", PyInit_cosmo}, + /* This lives in Python/Python-ast.c */ {"_ast", PyInit__ast}, diff --git a/third_party/python/Python/cosmomodule.c b/third_party/python/Python/cosmomodule.c new file mode 100644 index 000000000..31290e688 --- /dev/null +++ b/third_party/python/Python/cosmomodule.c @@ -0,0 +1,102 @@ +/*-*- 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│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ This is free and unencumbered software released into the public domain. │ +│ │ +│ Anyone is free to copy, modify, publish, use, compile, sell, or │ +│ distribute this software, either in source code form or as a compiled │ +│ binary, for any purpose, commercial or non-commercial, and by any │ +│ means. │ +│ │ +│ In jurisdictions that recognize copyright laws, the author or authors │ +│ of this software dedicate any and all copyright interest in the │ +│ software to the public domain. We make this dedication for the benefit │ +│ of the public at large and to the detriment of our heirs and │ +│ successors. We intend this dedication to be an overt act of │ +│ relinquishment in perpetuity of all present and future rights to this │ +│ software under copyright law. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ +│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ +│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ +│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ +│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ +│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ +│ OTHER DEALINGS IN THE SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "third_party/python/Include/Python-ast.h" +#include "third_party/python/Include/abstract.h" +#include "third_party/python/Include/boolobject.h" +#include "third_party/python/Include/ceval.h" +#include "third_party/python/Include/code.h" +#include "third_party/python/Include/cosmo.h" +#include "third_party/python/Include/dictobject.h" +#include "third_party/python/Include/errcode.h" +#include "third_party/python/Include/eval.h" +#include "third_party/python/Include/fileutils.h" +#include "third_party/python/Include/frameobject.h" +#include "third_party/python/Include/import.h" +#include "third_party/python/Include/listobject.h" +#include "third_party/python/Include/longobject.h" +#include "third_party/python/Include/marshal.h" +#include "third_party/python/Include/modsupport.h" +#include "third_party/python/Include/objimpl.h" +#include "third_party/python/Include/osdefs.h" +#include "third_party/python/Include/pgenheaders.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/pymacro.h" +#include "third_party/python/Include/pythonrun.h" +#include "third_party/python/Include/sysmodule.h" +#include "third_party/python/Include/traceback.h" +#include "third_party/python/Include/tupleobject.h" +#include "third_party/python/Include/warnings.h" +#include "third_party/python/Include/weakrefobject.h" +#include "third_party/python/Python/importdl.h" +/* clang-format off */ + +static int cosmo_constants(PyObject *m) +{ + if(PyModule_AddStringMacro(m, MODE)) return -1; + return 0; +} + + +PyDoc_STRVAR(doc_cosmo, +"additional information and special functions provided by Cosmopolitan Libc."); + +static PyMethodDef cosmo_methods[] = { + {NULL, NULL} /* sentinel */ +}; + + +static struct PyModuleDef cosmomodule = { + PyModuleDef_HEAD_INIT, + "_cosmo", + doc_cosmo, + -1, + cosmo_methods, + NULL, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC +PyInit_cosmo(void) +{ + PyObject *m, *d; + + m = PyModule_Create(&cosmomodule); + if (m == NULL) + goto failure; + + if(cosmo_constants(m)) + goto failure; + + return m; + failure: + Py_XDECREF(m); + return NULL; +} diff --git a/third_party/python/Python/pylifecycle.c b/third_party/python/Python/pylifecycle.c index d56282561..2614965ca 100644 --- a/third_party/python/Python/pylifecycle.c +++ b/third_party/python/Python/pylifecycle.c @@ -21,6 +21,7 @@ #include "third_party/python/Include/ast.h" #include "third_party/python/Include/boolobject.h" #include "third_party/python/Include/code.h" +#include "third_party/python/Include/cosmo.h" #include "third_party/python/Include/codecs.h" #include "third_party/python/Include/dictobject.h" #include "third_party/python/Include/errcode.h" @@ -258,6 +259,7 @@ import_init(PyInterpreterState *interp, PyObject *sysmod) { PyObject *importlib; PyObject *impmod; + PyObject *cosmomod; PyObject *sys_modules; PyObject *value; @@ -297,6 +299,18 @@ import_init(PyInterpreterState *interp, PyObject *sysmod) Py_FatalError("Py_Initialize: can't save _imp to sys.modules"); } + /* Import the _cosmo module */ + cosmomod = PyInit_cosmo(); + if (impmod == NULL) { + Py_FatalError("Py_Initialize: can't import _cosmo"); + } + else if (Py_VerboseFlag) { + PySys_FormatStderr("import _cosmo # for bonus Cosmopolitan Libc features\n"); + } + if (PyDict_SetItemString(sys_modules, "_cosmo", cosmomod) < 0) { + Py_FatalError("Py_Initialize: can't save _cosmo to sys.modules"); + } + /* Install importlib as the implementation of import */ value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod); if (value == NULL) { diff --git a/third_party/python/python.mk b/third_party/python/python.mk index 660b58245..f34aa696e 100644 --- a/third_party/python/python.mk +++ b/third_party/python/python.mk @@ -78,6 +78,7 @@ THIRD_PARTY_PYTHON_HDRS = \ third_party/python/Include/codecs.h \ third_party/python/Include/compile.h \ third_party/python/Include/complexobject.h \ + third_party/python/Include/cosmo.h \ third_party/python/Include/datetime.h \ third_party/python/Include/descrobject.h \ third_party/python/Include/dictobject.h \ @@ -315,7 +316,7 @@ THIRD_PARTY_PYTHON_INCS = \ THIRD_PARTY_PYTHON_STAGE1_A_SRCS = \ third_party/python/Modules/faulthandler.c \ - third_party/python/Objects/abstract.c \ + third_party/python/Objects/abstract.c \ third_party/python/Modules/fspath.c \ third_party/python/Modules/gcmodule.c \ third_party/python/Modules/getbuildinfo.c \ @@ -376,6 +377,7 @@ THIRD_PARTY_PYTHON_STAGE1_A_SRCS = \ third_party/python/Python/ceval.c \ third_party/python/Python/codecs.c \ third_party/python/Python/compile.c \ + third_party/python/Python/cosmomodule.c \ third_party/python/Python/dtoa.c \ third_party/python/Python/dynload_shlib.c \ third_party/python/Python/errors.c \