mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-01 03:53:33 +00:00
8af197560e
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.
148 lines
5.8 KiB
C
148 lines
5.8 KiB
C
/*-*- 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│
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ │
|
|
│ Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> │
|
|
│ │
|
|
│ This file is part of pysqlite. │
|
|
│ │
|
|
│ This software is provided 'as-is', without any express or implied │
|
|
│ warranty. In no event will the authors be held liable for any damages │
|
|
│ arising from the use of this software. │
|
|
│ │
|
|
│ Permission is granted to anyone to use this software for any purpose, │
|
|
│ including commercial applications, and to alter it and redistribute it │
|
|
│ freely, subject to the following restrictions: │
|
|
│ │
|
|
│ 1. The origin of this software must not be misrepresented; you must not │
|
|
│ claim that you wrote the original software. If you use this software │
|
|
│ in a product, an acknowledgment in the product documentation would be │
|
|
│ appreciated but is not required. │
|
|
│ 2. Altered source versions must be plainly marked as such, and must not be │
|
|
│ misrepresented as being the original software. │
|
|
│ 3. This notice may not be removed or altered from any source distribution. │
|
|
│ │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "third_party/python/Modules/_sqlite/cursor.h"
|
|
#include "third_party/python/Modules/_sqlite/microprotocols.h"
|
|
#include "third_party/python/Modules/_sqlite/prepare_protocol.h"
|
|
|
|
asm(".ident\t\"\\n\\n\
|
|
pysqlite (zlib license)\\n\
|
|
Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>\"");
|
|
asm(".include \"libc/disclaimer.inc\"");
|
|
/* clang-format off */
|
|
|
|
/** the adapters registry **/
|
|
|
|
PyObject *psyco_adapters;
|
|
|
|
/* pysqlite_microprotocols_init - initialize the adapters dictionary */
|
|
|
|
int
|
|
pysqlite_microprotocols_init(PyObject *dict)
|
|
{
|
|
/* create adapters dictionary and put it in module namespace */
|
|
if ((psyco_adapters = PyDict_New()) == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
return PyDict_SetItemString(dict, "adapters", psyco_adapters);
|
|
}
|
|
|
|
|
|
/* pysqlite_microprotocols_add - add a reverse type-caster to the dictionary */
|
|
|
|
int
|
|
pysqlite_microprotocols_add(PyTypeObject *type, PyObject *proto, PyObject *cast)
|
|
{
|
|
PyObject* key;
|
|
int rc;
|
|
|
|
if (proto == NULL) proto = (PyObject*)&pysqlite_PrepareProtocolType;
|
|
|
|
key = Py_BuildValue("(OO)", (PyObject*)type, proto);
|
|
if (!key) {
|
|
return -1;
|
|
}
|
|
|
|
rc = PyDict_SetItem(psyco_adapters, key, cast);
|
|
Py_DECREF(key);
|
|
|
|
return rc;
|
|
}
|
|
|
|
/* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */
|
|
|
|
PyObject *
|
|
pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
|
|
{
|
|
PyObject *adapter, *key;
|
|
|
|
/* we don't check for exact type conformance as specified in PEP 246
|
|
because the pysqlite_PrepareProtocolType type is abstract and there is no
|
|
way to get a quotable object to be its instance */
|
|
|
|
/* look for an adapter in the registry */
|
|
key = Py_BuildValue("(OO)", (PyObject*)obj->ob_type, proto);
|
|
if (!key) {
|
|
return NULL;
|
|
}
|
|
adapter = PyDict_GetItem(psyco_adapters, key);
|
|
Py_DECREF(key);
|
|
if (adapter) {
|
|
PyObject *adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL);
|
|
return adapted;
|
|
}
|
|
|
|
/* try to have the protocol adapt this object*/
|
|
if (PyObject_HasAttrString(proto, "__adapt__")) {
|
|
_Py_IDENTIFIER(__adapt__);
|
|
PyObject *adapted = _PyObject_CallMethodId(proto, &PyId___adapt__, "O", obj);
|
|
|
|
if (adapted) {
|
|
if (adapted != Py_None) {
|
|
return adapted;
|
|
} else {
|
|
Py_DECREF(adapted);
|
|
}
|
|
}
|
|
|
|
if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError))
|
|
return NULL;
|
|
}
|
|
|
|
/* and finally try to have the object adapt itself */
|
|
if (PyObject_HasAttrString(obj, "__conform__")) {
|
|
_Py_IDENTIFIER(__conform__);
|
|
PyObject *adapted = _PyObject_CallMethodId(obj, &PyId___conform__,"O", proto);
|
|
|
|
if (adapted) {
|
|
if (adapted != Py_None) {
|
|
return adapted;
|
|
} else {
|
|
Py_DECREF(adapted);
|
|
}
|
|
}
|
|
|
|
if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_TypeError)) {
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
/* else set the right exception and return NULL */
|
|
PyErr_SetString(pysqlite_ProgrammingError, "can't adapt");
|
|
return NULL;
|
|
}
|
|
|
|
/** module-level functions **/
|
|
|
|
PyObject *
|
|
pysqlite_adapt(pysqlite_Cursor *self, PyObject *args)
|
|
{
|
|
PyObject *obj, *alt = NULL;
|
|
PyObject *proto = (PyObject*)&pysqlite_PrepareProtocolType;
|
|
|
|
if (!PyArg_ParseTuple(args, "O|OO", &obj, &proto, &alt)) return NULL;
|
|
return pysqlite_microprotocols_adapt(obj, proto, alt);
|
|
}
|