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:
18 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

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

    r2 r391  
    11/* cache .c - a LRU cache
    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.
     
    2222 */
    2323
     24#include "sqlitecompat.h"
    2425#include "cache.h"
    2526#include <limits.h>
  • python/trunk/Modules/_sqlite/cache.h

    r2 r391  
    11/* cache.h - definitions for the LRU cache
    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.
  • 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.")},
  • python/trunk/Modules/_sqlite/connection.h

    r2 r391  
    11/* connection.h - definitions for 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.
     
    5656    PyObject* isolation_level;
    5757
    58     /* NULL for autocommit, otherwise a string with the BEGIN statment; will be
     58    /* NULL for autocommit, otherwise a string with the BEGIN statement; will be
    5959     * freed in connection destructor */
    6060    char* begin_statement;
     
    6464    int check_same_thread;
    6565
     66    int initialized;
     67
    6668    /* thread identification of the thread the connection was created in */
    6769    long thread_ident;
     
    6971    pysqlite_Cache* statement_cache;
    7072
    71     /* A list of weak references to statements used within this connection */
     73    /* Lists of weak references to statements and cursors used within this connection */
    7274    PyObject* statements;
     75    PyObject* cursors;
    7376
    74     /* a counter for how many statements were created in the connection. May be
     77    /* Counters for how many statements/cursors were created in the connection. May be
    7578     * reset to 0 at certain intervals */
    7679    int created_statements;
     80    int created_cursors;
    7781
    7882    PyObject* row_factory;
     
    126130int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs);
    127131
     132int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor);
    128133int pysqlite_check_thread(pysqlite_Connection* self);
    129134int pysqlite_check_connection(pysqlite_Connection* con);
  • python/trunk/Modules/_sqlite/cursor.c

    r2 r391  
    11/* cursor.c - the cursor 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.
     
    2727#include "sqlitecompat.h"
    2828
    29 /* used to decide wether to call PyInt_FromLong or PyLong_FromLongLong */
    30 #ifndef INT32_MIN
    31 #define INT32_MIN (-2147483647 - 1)
    32 #endif
    33 #ifndef INT32_MAX
    34 #define INT32_MAX 2147483647
    35 #endif
    36 
    3729PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
     30
     31static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
    3832
    3933static pysqlite_StatementKind detect_statement_type(char* statement)
     
    5448    dst = buf;
    5549    *dst = 0;
    56     while (isalpha(*src) && dst - buf < sizeof(buf) - 2) {
    57         *dst++ = tolower(*src++);
     50    while (Py_ISALPHA(*src) && dst - buf < sizeof(buf) - 2) {
     51        *dst++ = Py_TOLOWER(*src++);
    5852    }
    5953
     
    7569}
    7670
    77 int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
     71static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
    7872{
    7973    pysqlite_Connection* connection;
     
    8882    self->statement = NULL;
    8983    self->next_row = NULL;
     84    self->in_weakreflist = NULL;
    9085
    9186    self->row_cast_map = PyList_New(0);
     
    10196
    10297    self->arraysize = 1;
     98    self->closed = 0;
     99    self->reset = 0;
    103100
    104101    self->rowcount = -1L;
     
    111108    }
    112109
     110    if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) {
     111        return -1;
     112    }
     113
     114    self->initialized = 1;
     115
    113116    return 0;
    114117}
    115118
    116 void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
     119static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
    117120{
    118121    int rc;
     
    131134    Py_XDECREF(self->next_row);
    132135
    133     Py_TYPE(self)->tp_free((PyObject*)self);
     136    if (self->in_weakreflist != NULL) {
     137        PyObject_ClearWeakRefs((PyObject*)self);
     138    }
     139
     140    self->ob_type->tp_free((PyObject*)self);
    134141}
    135142
     
    254261}
    255262
    256 PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
     263PyObject* pysqlite_unicode_from_string(const char* val_str, Py_ssize_t size, int optimize)
    257264{
    258265    const char* check;
     266    Py_ssize_t pos;
    259267    int is_ascii = 0;
    260268
     
    263271
    264272        check = val_str;
    265         while (*check) {
     273        for (pos = 0; pos < size; pos++) {
    266274            if (*check & 0x80) {
    267275                is_ascii = 0;
     
    274282
    275283    if (is_ascii) {
    276         return PyString_FromString(val_str);
     284        return PyString_FromStringAndSize(val_str, size);
    277285    } else {
    278         return PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
     286        return PyUnicode_DecodeUTF8(val_str, size, NULL);
    279287    }
    280288}
     
    292300    PyObject* item = NULL;
    293301    int coltype;
    294     PY_LONG_LONG intval;
    295302    PyObject* converter;
    296303    PyObject* converted;
     
    301308    char buf[200];
    302309    const char* colname;
     310
     311    if (self->reset) {
     312        PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
     313        return NULL;
     314    }
    303315
    304316    Py_BEGIN_ALLOW_THREADS
     
    346358                converted = Py_None;
    347359            } else if (coltype == SQLITE_INTEGER) {
    348                 intval = sqlite3_column_int64(self->statement->st, i);
    349                 if (intval < INT32_MIN || intval > INT32_MAX) {
    350                     converted = PyLong_FromLongLong(intval);
    351                 } else {
    352                     converted = PyInt_FromLong((long)intval);
    353                 }
     360                converted = _pysqlite_long_from_int64(sqlite3_column_int64(self->statement->st, i));
    354361            } else if (coltype == SQLITE_FLOAT) {
    355362                converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
    356363            } else if (coltype == SQLITE_TEXT) {
    357364                val_str = (const char*)sqlite3_column_text(self->statement->st, i);
     365                nbytes = sqlite3_column_bytes(self->statement->st, i);
    358366                if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
    359367                    || (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
    360368
    361                     converted = pysqlite_unicode_from_string(val_str,
     369                    converted = pysqlite_unicode_from_string(val_str, nbytes,
    362370                        self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
    363371
     
    372380                    }
    373381                } else if (self->connection->text_factory == (PyObject*)&PyString_Type) {
    374                     converted = PyString_FromString(val_str);
     382                    converted = PyString_FromStringAndSize(val_str, nbytes);
    375383                } else {
    376                     converted = PyObject_CallFunction(self->connection->text_factory, "s", val_str);
     384                    converted = PyObject_CallFunction(self->connection->text_factory, "s#", val_str, nbytes);
    377385                }
    378386            } else {
     
    405413
    406414    return row;
     415}
     416
     417/*
     418 * Checks if a cursor object is usable.
     419 *
     420 * 0 => error; 1 => ok
     421 */
     422static int check_cursor(pysqlite_Cursor* cur)
     423{
     424    if (!cur->initialized) {
     425        PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
     426        return 0;
     427    }
     428
     429    if (cur->closed) {
     430        PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
     431        return 0;
     432    }
     433
     434    if (cur->locked) {
     435        PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
     436        return 0;
     437    }
     438
     439    return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
    407440}
    408441
     
    420453    PyObject* result;
    421454    int numcols;
    422     PY_LONG_LONG lastrowid;
    423455    int statement_type;
    424456    PyObject* descriptor;
     
    426458    int allow_8bit_chars;
    427459
    428     if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
    429         return NULL;
    430     }
     460    if (!check_cursor(self)) {
     461        goto error;
     462    }
     463
     464    self->locked = 1;
     465    self->reset = 0;
    431466
    432467    /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
    433468    allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) &&
    434         (self->connection->text_factory != (PyObject*)&PyUnicode_Type && pysqlite_OptimizedUnicode));
     469        (self->connection->text_factory != pysqlite_OptimizedUnicode));
    435470
    436471    Py_XDECREF(self->next_row);
     
    440475        /* executemany() */
    441476        if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
    442             return NULL;
     477            goto error;
    443478        }
    444479
    445480        if (!PyString_Check(operation) && !PyUnicode_Check(operation)) {
    446481            PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode");
    447             return NULL;
     482            goto error;
    448483        }
    449484
     
    456491            parameters_iter = PyObject_GetIter(second_argument);
    457492            if (!parameters_iter) {
    458                 return NULL;
     493                goto error;
    459494            }
    460495        }
     
    462497        /* execute() */
    463498        if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
    464             return NULL;
     499            goto error;
    465500        }
    466501
    467502        if (!PyString_Check(operation) && !PyUnicode_Check(operation)) {
    468503            PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode");
    469             return NULL;
     504            goto error;
    470505        }
    471506
    472507        parameters_list = PyList_New(0);
    473508        if (!parameters_list) {
    474             return NULL;
     509            goto error;
    475510        }
    476511
     
    698733        Py_DECREF(self->lastrowid);
    699734        if (!multiple && statement_type == STATEMENT_INSERT) {
     735            sqlite_int64 lastrowid;
    700736            Py_BEGIN_ALLOW_THREADS
    701737            lastrowid = sqlite3_last_insert_rowid(self->connection->db);
    702738            Py_END_ALLOW_THREADS
    703             self->lastrowid = PyInt_FromLong((long)lastrowid);
     739            self->lastrowid = _pysqlite_long_from_int64(lastrowid);
    704740        } else {
    705741            Py_INCREF(Py_None);
     
    718754    #ifdef SQLITE_VERSION_NUMBER
    719755    #if SQLITE_VERSION_NUMBER >= 3002002
    720     self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
     756    if (self->connection && self->connection->db)
     757        self->connection->inTransaction = !sqlite3_get_autocommit(self->connection->db);
    721758    #endif
    722759    #endif
     
    726763    Py_XDECREF(parameters_iter);
    727764    Py_XDECREF(parameters_list);
     765
     766    self->locked = 0;
    728767
    729768    if (PyErr_Occurred()) {
     
    754793    int rc;
    755794    PyObject* result;
    756     int statement_completed = 0;
    757795
    758796    if (!PyArg_ParseTuple(args, "O", &script_obj)) {
     
    760798    }
    761799
    762     if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
    763         return NULL;
    764     }
     800    if (!check_cursor(self)) {
     801        return NULL;
     802    }
     803
     804    self->reset = 0;
    765805
    766806    if (PyString_Check(script_obj)) {
     
    786826
    787827    while (1) {
    788         if (!sqlite3_complete(script_cstr)) {
    789             break;
    790         }
    791         statement_completed = 1;
    792 
    793828        Py_BEGIN_ALLOW_THREADS
    794829        rc = sqlite3_prepare(self->connection->db,
     
    821856            goto error;
    822857        }
     858
     859        if (*script_cstr == (char)0) {
     860            break;
     861        }
    823862    }
    824863
    825864error:
    826865    Py_XDECREF(script_str);
    827 
    828     if (!statement_completed) {
    829         PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement");
    830     }
    831866
    832867    if (PyErr_Occurred()) {
     
    850885    int rc;
    851886
    852     if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
     887    if (!check_cursor(self)) {
     888        return NULL;
     889    }
     890
     891    if (self->reset) {
     892        PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
    853893        return NULL;
    854894    }
     
    9921032        Py_CLEAR(self->statement);
    9931033    }
     1034
     1035    self->closed = 1;
    9941036
    9951037    Py_INCREF(Py_None);
     
    10531095        0,                                              /* tp_setattro */
    10541096        0,                                              /* tp_as_buffer */
    1055         Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_ITER|Py_TPFLAGS_BASETYPE, /* tp_flags */
     1097        Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_ITER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
    10561098        cursor_doc,                                     /* tp_doc */
    10571099        0,                                              /* tp_traverse */
    10581100        0,                                              /* tp_clear */
    10591101        0,                                              /* tp_richcompare */
    1060         0,                                              /* tp_weaklistoffset */
     1102        offsetof(pysqlite_Cursor, in_weakreflist),      /* tp_weaklistoffset */
    10611103        (getiterfunc)pysqlite_cursor_getiter,           /* tp_iter */
    10621104        (iternextfunc)pysqlite_cursor_iternext,         /* tp_iternext */
  • python/trunk/Modules/_sqlite/cursor.h

    r2 r391  
    11/* cursor.h - definitions for the cursor 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.
     
    4141    PyObject* row_factory;
    4242    pysqlite_Statement* statement;
     43    int closed;
     44    int reset;
     45    int locked;
     46    int initialized;
    4347
    4448    /* the next row to be returned, NULL if no next row available */
    4549    PyObject* next_row;
     50
     51    PyObject* in_weakreflist; /* List of weak references */
    4652} pysqlite_Cursor;
    4753
     
    5460extern PyTypeObject pysqlite_CursorType;
    5561
    56 int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs);
    57 void pysqlite_cursor_dealloc(pysqlite_Cursor* self);
    5862PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args);
    5963PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args);
  • python/trunk/Modules/_sqlite/module.c

    r2 r391  
    11/* module.c - the module itself
    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.
  • python/trunk/Modules/_sqlite/module.h

    r2 r391  
    11/* module.h - definitions for the module
    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.
     
    2626#include "Python.h"
    2727
    28 #define PYSQLITE_VERSION "2.4.1"
     28#define PYSQLITE_VERSION "2.6.0"
    2929
    3030extern PyObject* pysqlite_Error;
  • python/trunk/Modules/_sqlite/prepare_protocol.c

    r2 r391  
    11/* prepare_protocol.c - the protocol for preparing values for SQLite
    22 *
    3  * Copyright (C) 2005-2006 Gerhard Häring <gh@ghaering.de>
     3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
    44 *
    55 * This file is part of pysqlite.
     
    2222 */
    2323
     24#include "sqlitecompat.h"
    2425#include "prepare_protocol.h"
    2526
  • python/trunk/Modules/_sqlite/prepare_protocol.h

    r2 r391  
    11/* prepare_protocol.h - the protocol for preparing values for SQLite
    22 *
    3  * Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
     3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
    44 *
    55 * This file is part of pysqlite.
  • python/trunk/Modules/_sqlite/row.c

    r2 r391  
    11/* row.c - an enhanced tuple for database rows
    22 *
    3  * Copyright (C) 2005-2006 Gerhard Häring <gh@ghaering.de>
     3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
    44 *
    55 * This file is part of pysqlite.
     
    178178{
    179179    if (opid != Py_EQ && opid != Py_NE) {
    180         Py_INCREF(Py_NotImplemented);
    181         return Py_NotImplemented;
     180        Py_INCREF(Py_NotImplemented);
     181        return Py_NotImplemented;
    182182    }
    183183    if (PyType_IsSubtype(Py_TYPE(_other), &pysqlite_RowType)) {
    184         pysqlite_Row *other = (pysqlite_Row *)_other;
    185         PyObject *res = PyObject_RichCompare(self->description, other->description, opid);
    186         if ((opid == Py_EQ && res == Py_True)
    187             || (opid == Py_NE && res == Py_False)) {
    188             Py_DECREF(res);
    189             return PyObject_RichCompare(self->data, other->data, opid);
    190         }
     184        pysqlite_Row *other = (pysqlite_Row *)_other;
     185        PyObject *res = PyObject_RichCompare(self->description, other->description, opid);
     186        if ((opid == Py_EQ && res == Py_True)
     187            || (opid == Py_NE && res == Py_False)) {
     188            Py_DECREF(res);
     189            return PyObject_RichCompare(self->data, other->data, opid);
     190        }
    191191    }
    192192    Py_INCREF(Py_NotImplemented);
  • python/trunk/Modules/_sqlite/row.h

    r2 r391  
    11/* row.h - an enhanced tuple for database rows
    22 *
    3  * Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
     3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
    44 *
    55 * This file is part of pysqlite.
  • python/trunk/Modules/_sqlite/sqlitecompat.h

    r2 r391  
    11/* sqlitecompat.h - compatibility macros
    22 *
    3  * Copyright (C) 2006 Gerhard Häring <gh@ghaering.de>
     3 * Copyright (C) 2006-2010 Gerhard Häring <gh@ghaering.de>
    44 *
    55 * This file is part of pysqlite.
     
    2222 */
    2323
     24#include "Python.h"
     25
    2426#ifndef PYSQLITE_COMPAT_H
    2527#define PYSQLITE_COMPAT_H
     
    3234#endif
    3335
     36
     37/* define PyDict_CheckExact for pre-2.4 versions of Python */
     38#ifndef PyDict_CheckExact
     39#define PyDict_CheckExact(op) ((op)->ob_type == &PyDict_Type)
    3440#endif
     41
     42/* define Py_CLEAR for pre-2.4 versions of Python */
     43#ifndef Py_CLEAR
     44#define Py_CLEAR(op)                            \
     45        do {                                    \
     46                if (op) {                       \
     47                        PyObject *tmp = (PyObject *)(op);       \
     48                        (op) = NULL;            \
     49                        Py_DECREF(tmp);         \
     50                }                               \
     51        } while (0)
     52#endif
     53
     54#ifndef PyVarObject_HEAD_INIT
     55#define PyVarObject_HEAD_INIT(type, size) \
     56    PyObject_HEAD_INIT(type) size,
     57#endif
     58
     59#ifndef Py_TYPE
     60#define Py_TYPE(ob) ((ob)->ob_type)
     61#endif
     62
     63#endif
  • python/trunk/Modules/_sqlite/statement.c

    r2 r391  
    11/* statement.c - the statement type
    22 *
    3  * Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
     3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
    44 *
    55 * This file is part of pysqlite.
     
    2727#include "microprotocols.h"
    2828#include "prepare_protocol.h"
     29#include "util.h"
    2930#include "sqlitecompat.h"
    3031
     
    102103{
    103104    int rc = SQLITE_OK;
    104     long longval;
    105     PY_LONG_LONG longlongval;
    106105    const char* buffer;
    107106    char* string;
     
    154153
    155154    switch (paramtype) {
    156         case TYPE_INT:
    157             longval = PyInt_AsLong(parameter);
    158             rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longval);
     155        case TYPE_INT: {
     156            long longval = PyInt_AsLong(parameter);
     157            rc = sqlite3_bind_int64(self->st, pos, longval);
    159158            break;
    160         case TYPE_LONG:
    161             longlongval = PyLong_AsLongLong(parameter);
    162             /* in the overflow error case, longlongval is -1, and an exception is set */
    163             rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
     159        }
     160        case TYPE_LONG: {
     161            sqlite_int64 value = _pysqlite_long_as_int64(parameter);
     162            if (value == -1 && PyErr_Occurred())
     163                rc = -1;
     164            else
     165                rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)value);
    164166            break;
     167        }
    165168        case TYPE_FLOAT:
    166169            rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
    167170            break;
    168171        case TYPE_STRING:
    169             string = PyString_AS_STRING(parameter);
    170             rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
     172            PyString_AsStringAndSize(parameter, &string, &buflen);
     173            rc = sqlite3_bind_text(self->st, pos, string, buflen, SQLITE_TRANSIENT);
    171174            break;
    172175        case TYPE_UNICODE:
    173176            stringval = PyUnicode_AsUTF8String(parameter);
    174             string = PyString_AsString(stringval);
    175             rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
     177            PyString_AsStringAndSize(stringval, &string, &buflen);
     178            rc = sqlite3_bind_text(self->st, pos, string, buflen, SQLITE_TRANSIENT);
    176179            Py_DECREF(stringval);
    177180            break;
     
    199202    }
    200203
    201     if (PyInt_CheckExact(obj) || PyLong_CheckExact(obj) 
     204    if (PyInt_CheckExact(obj) || PyLong_CheckExact(obj)
    202205            || PyFloat_CheckExact(obj) || PyString_CheckExact(obj)
    203206            || PyUnicode_CheckExact(obj) || PyBuffer_Check(obj)) {
  • python/trunk/Modules/_sqlite/statement.h

    r2 r391  
    11/* statement.h - definitions for the statement type
    22 *
    3  * Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
     3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
    44 *
    55 * This file is part of pysqlite.
  • python/trunk/Modules/_sqlite/util.c

    r2 r391  
    11/* util.c - various utility functions
    22 *
    3  * Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
     3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
    44 *
    55 * This file is part of pysqlite.
     
    105105}
    106106
     107#ifdef WORDS_BIGENDIAN
     108# define IS_LITTLE_ENDIAN 0
     109#else
     110# define IS_LITTLE_ENDIAN 1
     111#endif
     112
     113PyObject *
     114_pysqlite_long_from_int64(sqlite_int64 value)
     115{
     116#ifdef HAVE_LONG_LONG
     117# if SIZEOF_LONG_LONG < 8
     118    if (value > PY_LLONG_MAX || value < PY_LLONG_MIN) {
     119        return _PyLong_FromByteArray(&value, sizeof(value),
     120                                     IS_LITTLE_ENDIAN, 1 /* signed */);
     121    }
     122# endif
     123# if SIZEOF_LONG < SIZEOF_LONG_LONG
     124    if (value > LONG_MAX || value < LONG_MIN)
     125        return PyLong_FromLongLong(value);
     126# endif
     127#else
     128# if SIZEOF_LONG < 8
     129    if (value > LONG_MAX || value < LONG_MIN) {
     130        return _PyLong_FromByteArray(&value, sizeof(value),
     131                                     IS_LITTLE_ENDIAN, 1 /* signed */);
     132    }
     133# endif
     134#endif
     135    return PyInt_FromLong(value);
     136}
     137
     138sqlite_int64
     139_pysqlite_long_as_int64(PyObject * py_val)
     140{
     141    int overflow;
     142#ifdef HAVE_LONG_LONG
     143    PY_LONG_LONG value = PyLong_AsLongLongAndOverflow(py_val, &overflow);
     144#else
     145    long value = PyLong_AsLongAndOverflow(py_val, &overflow);
     146#endif
     147    if (value == -1 && PyErr_Occurred())
     148        return -1;
     149    if (!overflow) {
     150#ifdef HAVE_LONG_LONG
     151# if SIZEOF_LONG_LONG > 8
     152        if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL)
     153# endif
     154#else
     155# if SIZEOF_LONG > 8
     156        if (-0x8000000000000000L <= value && value <= 0x7FFFFFFFFFFFFFFFL)
     157# endif
     158#endif
     159            return value;
     160    }
     161    else if (sizeof(value) < sizeof(sqlite_int64)) {
     162        sqlite_int64 int64val;
     163        if (_PyLong_AsByteArray((PyLongObject *)py_val,
     164                                (unsigned char *)&int64val, sizeof(int64val),
     165                                IS_LITTLE_ENDIAN, 1 /* signed */) >= 0) {
     166            return int64val;
     167        }
     168    }
     169    PyErr_SetString(PyExc_OverflowError,
     170                    "Python int too large to convert to SQLite INTEGER");
     171    return -1;
     172}
  • python/trunk/Modules/_sqlite/util.h

    r2 r391  
    11/* util.h - various utility functions
    22 *
    3  * Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
     3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
    44 *
    55 * This file is part of pysqlite.
     
    3636 */
    3737int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st);
     38
     39PyObject * _pysqlite_long_from_int64(sqlite_int64 value);
     40sqlite_int64 _pysqlite_long_as_int64(PyObject * value);
     41
    3842#endif
Note: See TracChangeset for help on using the changeset viewer.