Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Modules/_sqlite/connection.c

    r2 r391  
    11/* connection.c - the connection type
    22 *
    3  * Copyright (C) 2004-2007 Gerhard Häring <gh@ghaering.de>
     3 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
    44 *
    55 * This file is part of pysqlite.
     
    3636#define ACTION_RESET 2
    3737
     38#if SQLITE_VERSION_NUMBER >= 3003008
     39#ifndef SQLITE_OMIT_LOAD_EXTENSION
     40#define HAVE_LOAD_EXTENSION
     41#endif
     42#endif
     43
    3844static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
     45static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
    3946
    4047
     
    7481    }
    7582
     83    self->initialized = 1;
     84
    7685    self->begin_statement = NULL;
    7786
    7887    self->statement_cache = NULL;
    7988    self->statements = NULL;
     89    self->cursors = NULL;
    8090
    8191    Py_INCREF(Py_None);
     
    151161    }
    152162
     163    self->created_statements = 0;
     164    self->created_cursors = 0;
     165
     166    /* Create lists of weak references to statements/cursors */
    153167    self->statements = PyList_New(0);
    154     if (!self->statements) {
     168    self->cursors = PyList_New(0);
     169    if (!self->statements || !self->cursors) {
    155170        return -1;
    156171    }
    157     self->created_statements = 0;
    158172
    159173    /* By default, the Cache class INCREFs the factory in its initializer, and
     
    219233
    220234/* action in (ACTION_RESET, ACTION_FINALIZE) */
    221 void pysqlite_do_all_statements(pysqlite_Connection* self, int action)
     235void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
    222236{
    223237    int i;
    224238    PyObject* weakref;
    225239    PyObject* statement;
     240    pysqlite_Cursor* cursor;
    226241
    227242    for (i = 0; i < PyList_Size(self->statements); i++) {
     
    236251        }
    237252    }
     253
     254    if (reset_cursors) {
     255        for (i = 0; i < PyList_Size(self->cursors); i++) {
     256            weakref = PyList_GetItem(self->cursors, i);
     257            cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
     258            if ((PyObject*)cursor != Py_None) {
     259                cursor->reset = 1;
     260            }
     261        }
     262    }
    238263}
    239264
     
    264289    Py_XDECREF(self->collations);
    265290    Py_XDECREF(self->statements);
     291    Py_XDECREF(self->cursors);
    266292
    267293    self->ob_type->tp_free((PyObject*)self);
     294}
     295
     296/*
     297 * Registers a cursor with the connection.
     298 *
     299 * 0 => error; 1 => ok
     300 */
     301int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
     302{
     303    PyObject* weakref;
     304
     305    weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
     306    if (!weakref) {
     307        goto error;
     308    }
     309
     310    if (PyList_Append(connection->cursors, weakref) != 0) {
     311        Py_CLEAR(weakref);
     312        goto error;
     313    }
     314
     315    Py_DECREF(weakref);
     316
     317    return 1;
     318error:
     319    return 0;
    268320}
    269321
     
    274326    PyObject* cursor;
    275327
    276 
    277328    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
    278329                                     &factory)) {
     
    289340
    290341    cursor = PyObject_CallFunction(factory, "O", self);
     342
     343    _pysqlite_drop_unused_cursor_references(self);
    291344
    292345    if (cursor && self->row_factory != Py_None) {
     
    308361    }
    309362
    310     pysqlite_do_all_statements(self, ACTION_FINALIZE);
     363    pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
    311364
    312365    if (self->db) {
     
    342395int pysqlite_check_connection(pysqlite_Connection* con)
    343396{
     397    if (!con->initialized) {
     398        PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
     399        return 0;
     400    }
     401
    344402    if (!con->db) {
    345403        PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
     
    400458
    401459    if (self->inTransaction) {
     460        pysqlite_do_all_statements(self, ACTION_RESET, 0);
     461
    402462        Py_BEGIN_ALLOW_THREADS
    403463        rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
     
    444504
    445505    if (self->inTransaction) {
    446         pysqlite_do_all_statements(self, ACTION_RESET);
     506        pysqlite_do_all_statements(self, ACTION_RESET, 1);
    447507
    448508        Py_BEGIN_ALLOW_THREADS
     
    479539}
    480540
    481 void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
    482 {
    483     long longval;
    484     const char* buffer;
    485     Py_ssize_t buflen;
    486     PyObject* stringval;
    487 
    488     if ((!py_val) || PyErr_Occurred()) {
    489         sqlite3_result_null(context);
    490     } else if (py_val == Py_None) {
     541static int
     542_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
     543{
     544    if (py_val == Py_None) {
    491545        sqlite3_result_null(context);
    492546    } else if (PyInt_Check(py_val)) {
    493         longval = PyInt_AsLong(py_val);
    494         sqlite3_result_int64(context, (PY_LONG_LONG)longval);
     547        sqlite3_result_int64(context, (sqlite_int64)PyInt_AsLong(py_val));
     548    } else if (PyLong_Check(py_val)) {
     549        sqlite_int64 value = _pysqlite_long_as_int64(py_val);
     550        if (value == -1 && PyErr_Occurred())
     551            return -1;
     552        sqlite3_result_int64(context, value);
    495553    } else if (PyFloat_Check(py_val)) {
    496554        sqlite3_result_double(context, PyFloat_AsDouble(py_val));
    497555    } else if (PyBuffer_Check(py_val)) {
     556        const char* buffer;
     557        Py_ssize_t buflen;
    498558        if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
    499559            PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
    500         } else {
    501             sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
    502         }
     560            return -1;
     561        }
     562        sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
    503563    } else if (PyString_Check(py_val)) {
    504564        sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
    505565    } else if (PyUnicode_Check(py_val)) {
    506         stringval = PyUnicode_AsUTF8String(py_val);
    507         if (stringval) {
    508             sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
    509             Py_DECREF(stringval);
    510         }
    511     } else {
    512         /* TODO: raise error */
    513     }
     566        PyObject * stringval = PyUnicode_AsUTF8String(py_val);
     567        if (!stringval)
     568            return -1;
     569        sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
     570        Py_DECREF(stringval);
     571    } else {
     572        return -1;
     573    }
     574    return 0;
    514575}
    515576
     
    521582    PyObject* cur_py_value;
    522583    const char* val_str;
    523     PY_LONG_LONG val_int;
    524584    Py_ssize_t buflen;
    525585    void* raw_buffer;
     
    534594        switch (sqlite3_value_type(argv[i])) {
    535595            case SQLITE_INTEGER:
    536                 val_int = sqlite3_value_int64(cur_value);
    537                 cur_py_value = PyInt_FromLong((long)val_int);
     596                cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
    538597                break;
    539598            case SQLITE_FLOAT:
     
    586645    PyObject* py_func;
    587646    PyObject* py_retval = NULL;
     647    int ok;
    588648
    589649#ifdef WITH_THREAD
     
    601661    }
    602662
     663    ok = 0;
    603664    if (py_retval) {
    604         _pysqlite_set_result(context, py_retval);
     665        ok = _pysqlite_set_result(context, py_retval) == 0;
    605666        Py_DECREF(py_retval);
    606     } else {
     667    }
     668    if (!ok) {
    607669        if (_enable_callback_tracebacks) {
    608670            PyErr_Print();
     
    684746void _pysqlite_final_callback(sqlite3_context* context)
    685747{
    686     PyObject* function_result = NULL;
     748    PyObject* function_result;
    687749    PyObject** aggregate_instance;
    688     PyObject* aggregate_class;
     750    int ok;
    689751
    690752#ifdef WITH_THREAD
     
    693755    threadstate = PyGILState_Ensure();
    694756#endif
    695 
    696     aggregate_class = (PyObject*)sqlite3_user_data(context);
    697757
    698758    aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
     
    705765
    706766    function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
    707     if (!function_result) {
     767    Py_DECREF(*aggregate_instance);
     768
     769    ok = 0;
     770    if (function_result) {
     771        ok = _pysqlite_set_result(context, function_result) == 0;
     772        Py_DECREF(function_result);
     773    }
     774    if (!ok) {
    708775        if (_enable_callback_tracebacks) {
    709776            PyErr_Print();
     
    712779        }
    713780        _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
    714     } else {
    715         _pysqlite_set_result(context, function_result);
    716781    }
    717782
    718783error:
    719     Py_XDECREF(*aggregate_instance);
    720     Py_XDECREF(function_result);
    721 
    722784#ifdef WITH_THREAD
    723785    PyGILState_Release(threadstate);
     
    725787}
    726788
    727 void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
     789static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
    728790{
    729791    PyObject* new_list;
     
    757819}
    758820
     821static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
     822{
     823    PyObject* new_list;
     824    PyObject* weakref;
     825    int i;
     826
     827    /* we only need to do this once in a while */
     828    if (self->created_cursors++ < 200) {
     829        return;
     830    }
     831
     832    self->created_cursors = 0;
     833
     834    new_list = PyList_New(0);
     835    if (!new_list) {
     836        return;
     837    }
     838
     839    for (i = 0; i < PyList_Size(self->cursors); i++) {
     840        weakref = PyList_GetItem(self->cursors, i);
     841        if (PyWeakref_GetObject(weakref) != Py_None) {
     842            if (PyList_Append(new_list, weakref) != 0) {
     843                Py_DECREF(new_list);
     844                return;
     845            }
     846        }
     847    }
     848
     849    Py_DECREF(self->cursors);
     850    self->cursors = new_list;
     851}
     852
    759853PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
    760854{
     
    783877        return NULL;
    784878    } else {
    785         PyDict_SetItem(self->function_pinboard, func, Py_None);
     879        if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
     880            return NULL;
    786881
    787882        Py_INCREF(Py_None);
     
    814909        return NULL;
    815910    } else {
    816         PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None);
     911        if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
     912            return NULL;
    817913
    818914        Py_INCREF(Py_None);
     
    842938    } else {
    843939        if (PyInt_Check(ret)) {
    844             rc = (int)PyInt_AsLong(ret);
     940            rc = _PyInt_AsInt(ret);
     941            if (rc == -1 && PyErr_Occurred())
     942                rc = SQLITE_DENY;
    845943        } else {
    846944            rc = SQLITE_DENY;
     
    874972
    875973        /* abort query if error occurred */
    876         rc = 1; 
     974        rc = 1;
    877975    } else {
    878976        rc = (int)PyObject_IsTrue(ret);
     
    886984}
    887985
    888 PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
     986static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
    889987{
    890988    PyObject* authorizer_cb;
     
    9081006        return NULL;
    9091007    } else {
    910         PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None);
     1008        if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
     1009            return NULL;
    9111010
    9121011        Py_INCREF(Py_None);
     
    9151014}
    9161015
    917 PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
     1016static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
    9181017{
    9191018    PyObject* progress_handler;
     
    9361035    } else {
    9371036        sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
    938         PyDict_SetItem(self->function_pinboard, progress_handler, Py_None);
     1037        if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
     1038            return NULL;
    9391039    }
    9401040
     
    9421042    return Py_None;
    9431043}
     1044
     1045#ifdef HAVE_LOAD_EXTENSION
     1046static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
     1047{
     1048    int rc;
     1049    int onoff;
     1050
     1051    if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
     1052        return NULL;
     1053    }
     1054
     1055    if (!PyArg_ParseTuple(args, "i", &onoff)) {
     1056        return NULL;
     1057    }
     1058
     1059    rc = sqlite3_enable_load_extension(self->db, onoff);
     1060
     1061    if (rc != SQLITE_OK) {
     1062        PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
     1063        return NULL;
     1064    } else {
     1065        Py_INCREF(Py_None);
     1066        return Py_None;
     1067    }
     1068}
     1069
     1070static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
     1071{
     1072    int rc;
     1073    char* extension_name;
     1074    char* errmsg;
     1075
     1076    if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
     1077        return NULL;
     1078    }
     1079
     1080    if (!PyArg_ParseTuple(args, "s", &extension_name)) {
     1081        return NULL;
     1082    }
     1083
     1084    rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
     1085    if (rc != 0) {
     1086        PyErr_SetString(pysqlite_OperationalError, errmsg);
     1087        return NULL;
     1088    } else {
     1089        Py_INCREF(Py_None);
     1090        return Py_None;
     1091    }
     1092}
     1093#endif
    9441094
    9451095int pysqlite_check_thread(pysqlite_Connection* self)
     
    10521202    }
    10531203
     1204    statement->db = NULL;
     1205    statement->st = NULL;
     1206    statement->sql = NULL;
     1207    statement->in_use = 0;
     1208    statement->in_weakreflist = NULL;
     1209
    10541210    rc = pysqlite_statement_create(statement, self, sql);
    10551211
     
    11861342#endif
    11871343    PyObject* retval = NULL;
     1344    long longval;
    11881345    int result = 0;
    11891346#ifdef WITH_THREAD
     
    12091366    }
    12101367
    1211     result = PyInt_AsLong(retval);
    1212     if (PyErr_Occurred()) {
     1368    longval = PyLong_AsLongAndOverflow(retval, &result);
     1369    if (longval == -1 && PyErr_Occurred()) {
     1370        PyErr_Clear();
    12131371        result = 0;
     1372    }
     1373    else if (!result) {
     1374        if (longval > 0)
     1375            result = 1;
     1376        else if (longval < 0)
     1377            result = -1;
    12141378    }
    12151379
     
    13301494
    13311495    if (callable != Py_None) {
    1332         PyDict_SetItem(self->collations, uppercase_name, callable);
    1333     } else {
    1334         PyDict_DelItem(self->collations, uppercase_name);
     1496        if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
     1497            goto finally;
     1498    } else {
     1499        if (PyDict_DelItem(self->collations, uppercase_name) == -1)
     1500            goto finally;
    13351501    }
    13361502
     
    14211587    {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
    14221588        PyDoc_STR("Sets authorizer callback. Non-standard.")},
     1589    #ifdef HAVE_LOAD_EXTENSION
     1590    {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
     1591        PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
     1592    {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
     1593        PyDoc_STR("Load SQLite extension module. Non-standard.")},
     1594    #endif
    14231595    {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
    14241596        PyDoc_STR("Sets progress handler callback. Non-standard.")},
Note: See TracChangeset for help on using the changeset viewer.