"Closes the cursor.");
#define PYSQLITE_CURSOR_CLOSE_METHODDEF \
- {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, pysqlite_cursor_close__doc__},
+ {"close", (PyCFunction)(void(*)(void))pysqlite_cursor_close, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, pysqlite_cursor_close__doc__},
static PyObject *
-pysqlite_cursor_close_impl(pysqlite_Cursor *self);
+pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls);
static PyObject *
-pysqlite_cursor_close(pysqlite_Cursor *self, PyObject *Py_UNUSED(ignored))
+pysqlite_cursor_close(pysqlite_Cursor *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
- return pysqlite_cursor_close_impl(self);
+ PyObject *return_value = NULL;
+ static const char * const _keywords[] = { NULL};
+ static _PyArg_Parser _parser = {":close", _keywords, 0};
+
+ if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser
+ )) {
+ goto exit;
+ }
+ return_value = pysqlite_cursor_close_impl(self, cls);
+
+exit:
+ return return_value;
}
-/*[clinic end generated code: output=8f70eac5f8aa8d97 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=7b216aba2439f5cf input=a9049054013a1b77]*/
if (args[0] == NULL) {
return NULL;
}
- pysqlite_state *state = pysqlite_get_state(NULL);
- PyObject *inner = PyObject_Vectorcall(state->lru_cache, args, 1, NULL);
+ PyObject *lru_cache = self->state->lru_cache;
+ PyObject *inner = PyObject_Vectorcall(lru_cache, args, 1, NULL);
Py_DECREF(args[0]);
if (inner == NULL) {
return NULL;
return -1;
}
+ pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self));
+ self->state = state;
+
const char *database = PyBytes_AsString(database_obj);
self->initialized = 1;
Py_DECREF(database_obj); // needed bco. the AC FSConverter
if (rc != SQLITE_OK) {
- _pysqlite_seterror(self->db);
+ _pysqlite_seterror(state, self->db);
return -1;
}
self->function_pinboard_progress_handler = NULL;
self->function_pinboard_authorizer_cb = NULL;
- pysqlite_state *state = pysqlite_get_state(NULL);
self->Warning = state->Warning;
self->Error = state->Error;
self->InterfaceError = state->InterfaceError;
return NULL;
}
- pysqlite_state *state = pysqlite_get_state(NULL);
if (factory == NULL) {
- factory = (PyObject *)state->CursorType;
+ factory = (PyObject *)self->state->CursorType;
}
cursor = PyObject_CallOneArg(factory, (PyObject *)self);
if (cursor == NULL)
return NULL;
- if (!PyObject_TypeCheck(cursor, state->CursorType)) {
+ if (!PyObject_TypeCheck(cursor, self->state->CursorType)) {
PyErr_Format(PyExc_TypeError,
"factory must return a cursor, not %.100s",
Py_TYPE(cursor)->tp_name);
*/
int pysqlite_check_connection(pysqlite_Connection* con)
{
- pysqlite_state *state = pysqlite_get_state(NULL);
if (!con->initialized) {
+ pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(con));
PyErr_SetString(state->ProgrammingError,
"Base Connection.__init__ not called.");
return 0;
}
if (!con->db) {
- PyErr_SetString(state->ProgrammingError,
+ PyErr_SetString(con->state->ProgrammingError,
"Cannot operate on a closed database.");
return 0;
} else {
rc = sqlite3_prepare_v2(self->db, "COMMIT", 7, &statement, NULL);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
- _pysqlite_seterror(self->db);
+ _pysqlite_seterror(self->state, self->db);
goto error;
}
rc = pysqlite_step(statement);
if (rc != SQLITE_DONE) {
- _pysqlite_seterror(self->db);
+ _pysqlite_seterror(self->state, self->db);
}
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_finalize(statement);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK && !PyErr_Occurred()) {
- _pysqlite_seterror(self->db);
+ _pysqlite_seterror(self->state, self->db);
}
}
rc = sqlite3_prepare_v2(self->db, "ROLLBACK", 9, &statement, NULL);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
- _pysqlite_seterror(self->db);
+ _pysqlite_seterror(self->state, self->db);
goto error;
}
rc = pysqlite_step(statement);
if (rc != SQLITE_DONE) {
- _pysqlite_seterror(self->db);
+ _pysqlite_seterror(self->state, self->db);
}
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_finalize(statement);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK && !PyErr_Occurred()) {
- _pysqlite_seterror(self->db);
+ _pysqlite_seterror(self->state, self->db);
}
}
Py_END_ALLOW_THREADS
if (bck_handle == NULL) {
- _pysqlite_seterror(bck_conn);
+ _pysqlite_seterror(self->state, bck_conn);
return NULL;
}
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
- _pysqlite_seterror(bck_conn);
+ _pysqlite_seterror(self->state, bck_conn);
return NULL;
}
if (callable != Py_None) {
Py_DECREF(callable);
}
- _pysqlite_seterror(self->db);
+ _pysqlite_seterror(self->state, self->db);
return NULL;
}
{
PyObject_HEAD
sqlite3* db;
+ pysqlite_state *state;
/* the type detection mode. Only 0, PARSE_DECLTYPES, PARSE_COLNAMES or a
* bitwise combination thereof makes sense */
}
static PyObject *
-_pysqlite_get_converter(const char *keystr, Py_ssize_t keylen)
+_pysqlite_get_converter(pysqlite_state *state, const char *keystr,
+ Py_ssize_t keylen)
{
PyObject *key;
PyObject *upcase_key;
return NULL;
}
- pysqlite_state *state = pysqlite_get_state(NULL);
retval = PyDict_GetItemWithError(state->converters, upcase_key);
Py_DECREF(upcase_key);
type_start = pos + 1;
}
else if (*pos == ']' && type_start != NULL) {
- converter = _pysqlite_get_converter(type_start, pos - type_start);
+ pysqlite_state *state = self->connection->state;
+ converter = _pysqlite_get_converter(state, type_start,
+ pos - type_start);
if (!converter && PyErr_Occurred()) {
Py_CLEAR(self->row_cast_map);
return -1;
* 'NUMBER(10)' to be treated as 'NUMBER', for example.
* In other words, it will work as people expect it to work.*/
if (*pos == ' ' || *pos == '(' || *pos == 0) {
- converter = _pysqlite_get_converter(decltype, pos - decltype);
+ pysqlite_state *state = self->connection->state;
+ converter = _pysqlite_get_converter(state, decltype,
+ pos - decltype);
if (!converter && PyErr_Occurred()) {
Py_CLEAR(self->row_cast_map);
return -1;
*/
static int check_cursor(pysqlite_Cursor* cur)
{
- pysqlite_state *state = pysqlite_get_state(NULL);
-
if (!cur->initialized) {
+ pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(cur));
PyErr_SetString(state->ProgrammingError,
"Base Cursor.__init__ not called.");
return 0;
}
if (cur->closed) {
- PyErr_SetString(state->ProgrammingError,
+ PyErr_SetString(cur->connection->state->ProgrammingError,
"Cannot operate on a closed cursor.");
return 0;
}
if (cur->locked) {
- PyErr_SetString(state->ProgrammingError,
+ PyErr_SetString(cur->connection->state->ProgrammingError,
"Recursive use of cursors not allowed.");
return 0;
}
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
- _pysqlite_seterror(self->db);
+ _pysqlite_seterror(self->state, self->db);
goto error;
}
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK && !PyErr_Occurred()) {
- _pysqlite_seterror(self->db);
+ _pysqlite_seterror(self->state, self->db);
}
error:
static PyObject *
_pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation, PyObject* second_argument)
{
- pysqlite_state *state = pysqlite_get_state(NULL);
PyObject* parameters_list = NULL;
PyObject* parameters_iter = NULL;
PyObject* parameters = NULL;
}
}
+ pysqlite_state *state = self->connection->state;
while (1) {
parameters = PyIter_Next(parameters_iter);
if (!parameters) {
pysqlite_statement_mark_dirty(self->statement);
- pysqlite_statement_bind_parameters(self->statement, parameters);
+ pysqlite_statement_bind_parameters(state, self->statement, parameters);
if (PyErr_Occurred()) {
goto error;
}
}
}
(void)pysqlite_statement_reset(self->statement);
- _pysqlite_seterror(self->connection->db);
+ _pysqlite_seterror(state, self->connection->db);
goto error;
}
if (pysqlite_build_row_cast_map(self) != 0) {
- _PyErr_FormatFromCause(self->connection->OperationalError,
+ _PyErr_FormatFromCause(state->OperationalError,
"Error while building row_cast_map");
goto error;
}
if (rc == SQLITE_ROW) {
if (multiple) {
- PyErr_SetString(self->connection->ProgrammingError,
+ PyErr_SetString(state->ProgrammingError,
"executemany() can only execute DML "
"statements.");
goto error;
}
Py_DECREF(result);
+ pysqlite_state *state = self->connection->state;
while (1) {
const char *tail;
&tail);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
- _pysqlite_seterror(self->connection->db);
+ _pysqlite_seterror(state, self->connection->db);
goto error;
}
if (rc != SQLITE_DONE) {
(void)sqlite3_finalize(statement);
- _pysqlite_seterror(self->connection->db);
+ _pysqlite_seterror(state, self->connection->db);
goto error;
}
rc = sqlite3_finalize(statement);
if (rc != SQLITE_OK) {
- _pysqlite_seterror(self->connection->db);
+ _pysqlite_seterror(state, self->connection->db);
goto error;
}
if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
(void)pysqlite_statement_reset(self->statement);
Py_DECREF(next_row);
- _pysqlite_seterror(self->connection->db);
+ _pysqlite_seterror(self->connection->state, self->connection->db);
return NULL;
}
/*[clinic input]
_sqlite3.Cursor.close as pysqlite_cursor_close
+ cls: defining_class
+
Closes the cursor.
[clinic start generated code]*/
static PyObject *
-pysqlite_cursor_close_impl(pysqlite_Cursor *self)
-/*[clinic end generated code: output=b6055e4ec6fe63b6 input=08b36552dbb9a986]*/
+pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls)
+/*[clinic end generated code: output=a08ab3d772f45438 input=28ba9b532ab46ba0]*/
{
if (!self->connection) {
- pysqlite_state *state = pysqlite_get_state(NULL);
+ pysqlite_state *state = pysqlite_get_state_by_cls(cls);
PyErr_SetString(state->ProgrammingError,
"Base Cursor.__init__ not called.");
return NULL;
/* pysqlite_microprotocols_adapt - adapt an object to the built-in protocol */
PyObject *
-pysqlite_microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
+pysqlite_microprotocols_adapt(pysqlite_state *state, PyObject *obj,
+ PyObject *proto, PyObject *alt)
{
_Py_IDENTIFIER(__adapt__);
_Py_IDENTIFIER(__conform__);
if (!key) {
return NULL;
}
- pysqlite_state *state = pysqlite_get_state(NULL);
adapter = PyDict_GetItemWithError(state->psyco_adapters, key);
Py_DECREF(key);
if (adapter) {
extern int pysqlite_microprotocols_init(PyObject *module);
extern int pysqlite_microprotocols_add(
PyTypeObject *type, PyObject *proto, PyObject *cast);
-extern PyObject *pysqlite_microprotocols_adapt(
- PyObject *obj, PyObject *proto, PyObject *alt);
+extern PyObject *pysqlite_microprotocols_adapt(pysqlite_state *state,
+ PyObject *obj, PyObject *proto,
+ PyObject *alt);
#endif /* !defined(PSYCOPG_MICROPROTOCOLS_H) */
state->BaseTypeAdapted = 1;
}
- pysqlite_state *state = pysqlite_get_state(NULL);
+ pysqlite_state *state = pysqlite_get_state(module);
PyObject *protocol = (PyObject *)state->PrepareProtocolType;
rc = pysqlite_microprotocols_add(type, protocol, caster);
if (rc == -1) {
PyObject *alt)
/*[clinic end generated code: output=0c3927c5fcd23dd9 input=c8995aeb25d0e542]*/
{
- return pysqlite_microprotocols_adapt(obj, proto, alt);
+ pysqlite_state *state = pysqlite_get_state(module);
+ return pysqlite_microprotocols_adapt(state, obj, proto, alt);
}
static int converters_init(PyObject* module)
static inline pysqlite_state *
pysqlite_get_state(PyObject *Py_UNUSED(module))
{
+ return &pysqlite_global_state; // Replace with PyModule_GetState
+}
+
+static inline pysqlite_state *
+pysqlite_get_state_by_cls(PyTypeObject *Py_UNUSED(cls))
+{
+ return &pysqlite_global_state; // Replace with PyType_GetModuleState
+}
+
+static inline pysqlite_state *
+pysqlite_get_state_by_type(PyTypeObject *Py_UNUSED(tp))
+{
+ // Replace with _PyType_GetModuleByDef & PyModule_GetState
return &pysqlite_global_state;
}
if (opid != Py_EQ && opid != Py_NE)
Py_RETURN_NOTIMPLEMENTED;
- pysqlite_state *state = pysqlite_get_state(NULL);
+ pysqlite_state *state = pysqlite_get_state_by_cls(Py_TYPE(self));
if (PyObject_TypeCheck(_other, state->RowType)) {
pysqlite_Row *other = (pysqlite_Row *)_other;
int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ);
pysqlite_Statement *
pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql)
{
- pysqlite_state *state = pysqlite_get_state(NULL);
+ pysqlite_state *state = connection->state;
assert(PyUnicode_Check(sql));
Py_ssize_t size;
const char *sql_cstr = PyUnicode_AsUTF8AndSize(sql, &size);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
- _pysqlite_seterror(db);
+ _pysqlite_seterror(state, db);
return NULL;
}
}
/* returns 0 if the object is one of Python's internal ones that don't need to be adapted */
-static int _need_adapt(PyObject* obj)
+static int
+_need_adapt(pysqlite_state *state, PyObject *obj)
{
- pysqlite_state *state = pysqlite_get_state(NULL);
if (state->BaseTypeAdapted) {
return 1;
}
}
}
-void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
+void
+pysqlite_statement_bind_parameters(pysqlite_state *state,
+ pysqlite_Statement *self,
+ PyObject *parameters)
{
- pysqlite_state *state = pysqlite_get_state(NULL);
PyObject* current_param;
PyObject* adapted;
const char* binding_name;
return;
}
- if (!_need_adapt(current_param)) {
+ if (!_need_adapt(state, current_param)) {
adapted = current_param;
} else {
PyObject *protocol = (PyObject *)state->PrepareProtocolType;
- adapted = pysqlite_microprotocols_adapt(current_param,
+ adapted = pysqlite_microprotocols_adapt(state, current_param,
protocol,
current_param);
Py_DECREF(current_param);
return;
}
- if (!_need_adapt(current_param)) {
+ if (!_need_adapt(state, current_param)) {
adapted = current_param;
} else {
PyObject *protocol = (PyObject *)state->PrepareProtocolType;
- adapted = pysqlite_microprotocols_adapt(current_param,
+ adapted = pysqlite_microprotocols_adapt(state, current_param,
protocol,
current_param);
Py_DECREF(current_param);
pysqlite_Statement *pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql);
int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter);
-void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters);
+void pysqlite_statement_bind_parameters(pysqlite_state *state,
+ pysqlite_Statement *self,
+ PyObject *parameters);
int pysqlite_statement_finalize(pysqlite_Statement* self);
int pysqlite_statement_reset(pysqlite_Statement* self);
* Returns the error code (0 means no error occurred).
*/
int
-_pysqlite_seterror(sqlite3 *db)
+_pysqlite_seterror(pysqlite_state *state, sqlite3 *db)
{
- pysqlite_state *state = pysqlite_get_state(NULL);
int errorcode = sqlite3_errcode(db);
switch (errorcode)
* Checks the SQLite error code and sets the appropriate DB-API exception.
* Returns the error code (0 means no error occurred).
*/
-int _pysqlite_seterror(sqlite3 *db);
+int _pysqlite_seterror(pysqlite_state *state, sqlite3 *db);
sqlite_int64 _pysqlite_long_as_int64(PyObject * value);