Skip to content

bpo-40956: Convert _sqlite3 module level functions to Argument Clinic #22484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Convert _sqlite3 module level functions, except connect, to Argument …
…Clinic

Note: Moves sqlite3.adapt from microprotocols.c to module.c
  • Loading branch information
Erlend E. Aasland committed Oct 1, 2020
commit 39df100d593363becd62da5a1c25cacbd9063d2c
208 changes: 208 additions & 0 deletions Modules/_sqlite/clinic/module.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/*[clinic input]
preserve
[clinic start generated code]*/

PyDoc_STRVAR(pysqlite_complete_statement__doc__,
"complete_statement($module, statement, /)\n"
"--\n"
"\n"
"Checks if a string contains a complete SQL statement. Non-standard.");

#define PYSQLITE_COMPLETE_STATEMENT_METHODDEF \
{"complete_statement", (PyCFunction)pysqlite_complete_statement, METH_O, pysqlite_complete_statement__doc__},

static PyObject *
pysqlite_complete_statement_impl(PyObject *module, const char *statement);

static PyObject *
pysqlite_complete_statement(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
const char *statement;

if (!PyUnicode_Check(arg)) {
_PyArg_BadArgument("complete_statement", "argument", "str", arg);
goto exit;
}
Py_ssize_t statement_length;
statement = PyUnicode_AsUTF8AndSize(arg, &statement_length);
if (statement == NULL) {
goto exit;
}
if (strlen(statement) != (size_t)statement_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
return_value = pysqlite_complete_statement_impl(module, statement);

exit:
return return_value;
}

PyDoc_STRVAR(pysqlite_enable_shared_cache__doc__,
"enable_shared_cache($module, enable, /)\n"
"--\n"
"\n"
"Enable or disable shared cache mode for the calling thread.\n"
"\n"
"Experimental/Non-standard.");

#define PYSQLITE_ENABLE_SHARED_CACHE_METHODDEF \
{"enable_shared_cache", (PyCFunction)pysqlite_enable_shared_cache, METH_O, pysqlite_enable_shared_cache__doc__},

static PyObject *
pysqlite_enable_shared_cache_impl(PyObject *module, int do_enable);

static PyObject *
pysqlite_enable_shared_cache(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
int do_enable;

do_enable = _PyLong_AsInt(arg);
if (do_enable == -1 && PyErr_Occurred()) {
goto exit;
}
return_value = pysqlite_enable_shared_cache_impl(module, do_enable);

exit:
return return_value;
}

PyDoc_STRVAR(pysqlite_register_adapter__doc__,
"register_adapter($module, type, caster, /)\n"
"--\n"
"\n"
"Registers an adapter with pysqlite\'s adapter registry. Non-standard.");

#define PYSQLITE_REGISTER_ADAPTER_METHODDEF \
{"register_adapter", (PyCFunction)(void(*)(void))pysqlite_register_adapter, METH_FASTCALL, pysqlite_register_adapter__doc__},

static PyObject *
pysqlite_register_adapter_impl(PyObject *module, PyTypeObject *type,
PyObject *caster);

static PyObject *
pysqlite_register_adapter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
PyTypeObject *type;
PyObject *caster;

if (!_PyArg_CheckPositional("register_adapter", nargs, 2, 2)) {
goto exit;
}
type = (PyTypeObject *)args[0];
caster = args[1];
return_value = pysqlite_register_adapter_impl(module, type, caster);

exit:
return return_value;
}

PyDoc_STRVAR(pysqlite_register_converter__doc__,
"register_converter($module, name, converter, /)\n"
"--\n"
"\n"
"Registers a converter with pysqlite. Non-standard.");

#define PYSQLITE_REGISTER_CONVERTER_METHODDEF \
{"register_converter", (PyCFunction)(void(*)(void))pysqlite_register_converter, METH_FASTCALL, pysqlite_register_converter__doc__},

static PyObject *
pysqlite_register_converter_impl(PyObject *module, PyObject *orig_name,
PyObject *callable);

static PyObject *
pysqlite_register_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
PyObject *orig_name;
PyObject *callable;

if (!_PyArg_CheckPositional("register_converter", nargs, 2, 2)) {
goto exit;
}
if (!PyUnicode_Check(args[0])) {
_PyArg_BadArgument("register_converter", "argument 1", "str", args[0]);
goto exit;
}
if (PyUnicode_READY(args[0]) == -1) {
goto exit;
}
orig_name = args[0];
callable = args[1];
return_value = pysqlite_register_converter_impl(module, orig_name, callable);

exit:
return return_value;
}

PyDoc_STRVAR(pysqlite_enable_callback_trace__doc__,
"enable_callback_tracebacks($module, enable, /)\n"
"--\n"
"\n"
"Enable or disable callback functions throwing errors to stderr.");

#define PYSQLITE_ENABLE_CALLBACK_TRACE_METHODDEF \
{"enable_callback_tracebacks", (PyCFunction)pysqlite_enable_callback_trace, METH_O, pysqlite_enable_callback_trace__doc__},

static PyObject *
pysqlite_enable_callback_trace_impl(PyObject *module, int enable);

static PyObject *
pysqlite_enable_callback_trace(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
int enable;

enable = _PyLong_AsInt(arg);
if (enable == -1 && PyErr_Occurred()) {
goto exit;
}
return_value = pysqlite_enable_callback_trace_impl(module, enable);

exit:
return return_value;
}

PyDoc_STRVAR(pysqlite_adapt__doc__,
"adapt($module, obj, proto=PrepareProtocolType, alt=<unrepresentable>, /)\n"
"--\n"
"\n"
"Adapt given object to given protocol. Non-standard.");

#define PYSQLITE_ADAPT_METHODDEF \
{"adapt", (PyCFunction)(void(*)(void))pysqlite_adapt, METH_FASTCALL, pysqlite_adapt__doc__},

static PyObject *
pysqlite_adapt_impl(PyObject *module, PyObject *obj, PyObject *proto,
PyObject *alt);

static PyObject *
pysqlite_adapt(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
PyObject *obj;
PyObject *proto = (PyObject*)pysqlite_PrepareProtocolType;
PyObject *alt = NULL;

if (!_PyArg_CheckPositional("adapt", nargs, 1, 3)) {
goto exit;
}
obj = args[0];
if (nargs < 2) {
goto skip_optional;
}
proto = args[1];
if (nargs < 3) {
goto skip_optional;
}
alt = args[2];
skip_optional:
return_value = pysqlite_adapt_impl(module, obj, proto, alt);

exit:
return return_value;
}
/*[clinic end generated code: output=f56e6c44a21b22f8 input=a9049054013a1b77]*/
13 changes: 0 additions & 13 deletions Modules/_sqlite/microprotocols.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "microprotocols.h"
#include "prepare_protocol.h"


/** the adapters registry **/

static PyObject *psyco_adapters = NULL;
Expand Down Expand Up @@ -145,15 +144,3 @@ pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
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);
}
5 changes: 0 additions & 5 deletions Modules/_sqlite/microprotocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,4 @@ extern int pysqlite_microprotocols_add(
extern PyObject *pysqlite_microprotocols_adapt(
PyObject *obj, PyObject *proto, PyObject *alt);

extern PyObject *
pysqlite_adapt(pysqlite_Cursor* self, PyObject *args);
#define pysqlite_adapt_doc \
"adapt(obj, protocol, alternate) -> adapt obj to given protocol. Non-standard."

#endif /* !defined(PSYCOPG_MICROPROTOCOLS_H) */
Loading