Changeset 391 for python/trunk/Modules/_sqlite
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Modules/_sqlite/cache.c
r2 r391 1 1 /* cache .c - a LRU cache 2 2 * 3 * Copyright (C) 2004-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 22 22 */ 23 23 24 #include "sqlitecompat.h" 24 25 #include "cache.h" 25 26 #include <limits.h> -
python/trunk/Modules/_sqlite/cache.h
r2 r391 1 1 /* cache.h - definitions for the LRU cache 2 2 * 3 * Copyright (C) 2004-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. -
python/trunk/Modules/_sqlite/connection.c
r2 r391 1 1 /* connection.c - the connection type 2 2 * 3 * Copyright (C) 2004-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 36 36 #define ACTION_RESET 2 37 37 38 #if SQLITE_VERSION_NUMBER >= 3003008 39 #ifndef SQLITE_OMIT_LOAD_EXTENSION 40 #define HAVE_LOAD_EXTENSION 41 #endif 42 #endif 43 38 44 static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level); 45 static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self); 39 46 40 47 … … 74 81 } 75 82 83 self->initialized = 1; 84 76 85 self->begin_statement = NULL; 77 86 78 87 self->statement_cache = NULL; 79 88 self->statements = NULL; 89 self->cursors = NULL; 80 90 81 91 Py_INCREF(Py_None); … … 151 161 } 152 162 163 self->created_statements = 0; 164 self->created_cursors = 0; 165 166 /* Create lists of weak references to statements/cursors */ 153 167 self->statements = PyList_New(0); 154 if (!self->statements) { 168 self->cursors = PyList_New(0); 169 if (!self->statements || !self->cursors) { 155 170 return -1; 156 171 } 157 self->created_statements = 0;158 172 159 173 /* By default, the Cache class INCREFs the factory in its initializer, and … … 219 233 220 234 /* action in (ACTION_RESET, ACTION_FINALIZE) */ 221 void pysqlite_do_all_statements(pysqlite_Connection* self, int action )235 void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors) 222 236 { 223 237 int i; 224 238 PyObject* weakref; 225 239 PyObject* statement; 240 pysqlite_Cursor* cursor; 226 241 227 242 for (i = 0; i < PyList_Size(self->statements); i++) { … … 236 251 } 237 252 } 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 } 238 263 } 239 264 … … 264 289 Py_XDECREF(self->collations); 265 290 Py_XDECREF(self->statements); 291 Py_XDECREF(self->cursors); 266 292 267 293 self->ob_type->tp_free((PyObject*)self); 294 } 295 296 /* 297 * Registers a cursor with the connection. 298 * 299 * 0 => error; 1 => ok 300 */ 301 int 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; 318 error: 319 return 0; 268 320 } 269 321 … … 274 326 PyObject* cursor; 275 327 276 277 328 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, 278 329 &factory)) { … … 289 340 290 341 cursor = PyObject_CallFunction(factory, "O", self); 342 343 _pysqlite_drop_unused_cursor_references(self); 291 344 292 345 if (cursor && self->row_factory != Py_None) { … … 308 361 } 309 362 310 pysqlite_do_all_statements(self, ACTION_FINALIZE );363 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1); 311 364 312 365 if (self->db) { … … 342 395 int pysqlite_check_connection(pysqlite_Connection* con) 343 396 { 397 if (!con->initialized) { 398 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called."); 399 return 0; 400 } 401 344 402 if (!con->db) { 345 403 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database."); … … 400 458 401 459 if (self->inTransaction) { 460 pysqlite_do_all_statements(self, ACTION_RESET, 0); 461 402 462 Py_BEGIN_ALLOW_THREADS 403 463 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail); … … 444 504 445 505 if (self->inTransaction) { 446 pysqlite_do_all_statements(self, ACTION_RESET );506 pysqlite_do_all_statements(self, ACTION_RESET, 1); 447 507 448 508 Py_BEGIN_ALLOW_THREADS … … 479 539 } 480 540 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) { 541 static int 542 _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) 543 { 544 if (py_val == Py_None) { 491 545 sqlite3_result_null(context); 492 546 } 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); 495 553 } else if (PyFloat_Check(py_val)) { 496 554 sqlite3_result_double(context, PyFloat_AsDouble(py_val)); 497 555 } else if (PyBuffer_Check(py_val)) { 556 const char* buffer; 557 Py_ssize_t buflen; 498 558 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) { 499 559 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); 503 563 } else if (PyString_Check(py_val)) { 504 564 sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT); 505 565 } 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; 514 575 } 515 576 … … 521 582 PyObject* cur_py_value; 522 583 const char* val_str; 523 PY_LONG_LONG val_int;524 584 Py_ssize_t buflen; 525 585 void* raw_buffer; … … 534 594 switch (sqlite3_value_type(argv[i])) { 535 595 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)); 538 597 break; 539 598 case SQLITE_FLOAT: … … 586 645 PyObject* py_func; 587 646 PyObject* py_retval = NULL; 647 int ok; 588 648 589 649 #ifdef WITH_THREAD … … 601 661 } 602 662 663 ok = 0; 603 664 if (py_retval) { 604 _pysqlite_set_result(context, py_retval);665 ok = _pysqlite_set_result(context, py_retval) == 0; 605 666 Py_DECREF(py_retval); 606 } else { 667 } 668 if (!ok) { 607 669 if (_enable_callback_tracebacks) { 608 670 PyErr_Print(); … … 684 746 void _pysqlite_final_callback(sqlite3_context* context) 685 747 { 686 PyObject* function_result = NULL;748 PyObject* function_result; 687 749 PyObject** aggregate_instance; 688 PyObject* aggregate_class;750 int ok; 689 751 690 752 #ifdef WITH_THREAD … … 693 755 threadstate = PyGILState_Ensure(); 694 756 #endif 695 696 aggregate_class = (PyObject*)sqlite3_user_data(context);697 757 698 758 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); … … 705 765 706 766 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) { 708 775 if (_enable_callback_tracebacks) { 709 776 PyErr_Print(); … … 712 779 } 713 780 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1); 714 } else {715 _pysqlite_set_result(context, function_result);716 781 } 717 782 718 783 error: 719 Py_XDECREF(*aggregate_instance);720 Py_XDECREF(function_result);721 722 784 #ifdef WITH_THREAD 723 785 PyGILState_Release(threadstate); … … 725 787 } 726 788 727 void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)789 static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self) 728 790 { 729 791 PyObject* new_list; … … 757 819 } 758 820 821 static 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 759 853 PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) 760 854 { … … 783 877 return NULL; 784 878 } else { 785 PyDict_SetItem(self->function_pinboard, func, Py_None); 879 if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1) 880 return NULL; 786 881 787 882 Py_INCREF(Py_None); … … 814 909 return NULL; 815 910 } 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; 817 913 818 914 Py_INCREF(Py_None); … … 842 938 } else { 843 939 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; 845 943 } else { 846 944 rc = SQLITE_DENY; … … 874 972 875 973 /* abort query if error occurred */ 876 rc = 1; 974 rc = 1; 877 975 } else { 878 976 rc = (int)PyObject_IsTrue(ret); … … 886 984 } 887 985 888 PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)986 static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) 889 987 { 890 988 PyObject* authorizer_cb; … … 908 1006 return NULL; 909 1007 } 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; 911 1010 912 1011 Py_INCREF(Py_None); … … 915 1014 } 916 1015 917 PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)1016 static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) 918 1017 { 919 1018 PyObject* progress_handler; … … 936 1035 } else { 937 1036 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; 939 1039 } 940 1040 … … 942 1042 return Py_None; 943 1043 } 1044 1045 #ifdef HAVE_LOAD_EXTENSION 1046 static 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 1070 static 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 944 1094 945 1095 int pysqlite_check_thread(pysqlite_Connection* self) … … 1052 1202 } 1053 1203 1204 statement->db = NULL; 1205 statement->st = NULL; 1206 statement->sql = NULL; 1207 statement->in_use = 0; 1208 statement->in_weakreflist = NULL; 1209 1054 1210 rc = pysqlite_statement_create(statement, self, sql); 1055 1211 … … 1186 1342 #endif 1187 1343 PyObject* retval = NULL; 1344 long longval; 1188 1345 int result = 0; 1189 1346 #ifdef WITH_THREAD … … 1209 1366 } 1210 1367 1211 result = PyInt_AsLong(retval); 1212 if (PyErr_Occurred()) { 1368 longval = PyLong_AsLongAndOverflow(retval, &result); 1369 if (longval == -1 && PyErr_Occurred()) { 1370 PyErr_Clear(); 1213 1371 result = 0; 1372 } 1373 else if (!result) { 1374 if (longval > 0) 1375 result = 1; 1376 else if (longval < 0) 1377 result = -1; 1214 1378 } 1215 1379 … … 1330 1494 1331 1495 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; 1335 1501 } 1336 1502 … … 1421 1587 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS, 1422 1588 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 1423 1595 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS, 1424 1596 PyDoc_STR("Sets progress handler callback. Non-standard.")}, -
python/trunk/Modules/_sqlite/connection.h
r2 r391 1 1 /* connection.h - definitions for the connection type 2 2 * 3 * Copyright (C) 2004-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 56 56 PyObject* isolation_level; 57 57 58 /* NULL for autocommit, otherwise a string with the BEGIN stat ment; will be58 /* NULL for autocommit, otherwise a string with the BEGIN statement; will be 59 59 * freed in connection destructor */ 60 60 char* begin_statement; … … 64 64 int check_same_thread; 65 65 66 int initialized; 67 66 68 /* thread identification of the thread the connection was created in */ 67 69 long thread_ident; … … 69 71 pysqlite_Cache* statement_cache; 70 72 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 */ 72 74 PyObject* statements; 75 PyObject* cursors; 73 76 74 /* a counter for how many statements were created in the connection. May be77 /* Counters for how many statements/cursors were created in the connection. May be 75 78 * reset to 0 at certain intervals */ 76 79 int created_statements; 80 int created_cursors; 77 81 78 82 PyObject* row_factory; … … 126 130 int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs); 127 131 132 int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor); 128 133 int pysqlite_check_thread(pysqlite_Connection* self); 129 134 int pysqlite_check_connection(pysqlite_Connection* con); -
python/trunk/Modules/_sqlite/cursor.c
r2 r391 1 1 /* cursor.c - the cursor type 2 2 * 3 * Copyright (C) 2004-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 27 27 #include "sqlitecompat.h" 28 28 29 /* used to decide wether to call PyInt_FromLong or PyLong_FromLongLong */30 #ifndef INT32_MIN31 #define INT32_MIN (-2147483647 - 1)32 #endif33 #ifndef INT32_MAX34 #define INT32_MAX 214748364735 #endif36 37 29 PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self); 30 31 static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from."; 38 32 39 33 static pysqlite_StatementKind detect_statement_type(char* statement) … … 54 48 dst = buf; 55 49 *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++); 58 52 } 59 53 … … 75 69 } 76 70 77 int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)71 static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) 78 72 { 79 73 pysqlite_Connection* connection; … … 88 82 self->statement = NULL; 89 83 self->next_row = NULL; 84 self->in_weakreflist = NULL; 90 85 91 86 self->row_cast_map = PyList_New(0); … … 101 96 102 97 self->arraysize = 1; 98 self->closed = 0; 99 self->reset = 0; 103 100 104 101 self->rowcount = -1L; … … 111 108 } 112 109 110 if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) { 111 return -1; 112 } 113 114 self->initialized = 1; 115 113 116 return 0; 114 117 } 115 118 116 void pysqlite_cursor_dealloc(pysqlite_Cursor* self)119 static void pysqlite_cursor_dealloc(pysqlite_Cursor* self) 117 120 { 118 121 int rc; … … 131 134 Py_XDECREF(self->next_row); 132 135 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); 134 141 } 135 142 … … 254 261 } 255 262 256 PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)263 PyObject* pysqlite_unicode_from_string(const char* val_str, Py_ssize_t size, int optimize) 257 264 { 258 265 const char* check; 266 Py_ssize_t pos; 259 267 int is_ascii = 0; 260 268 … … 263 271 264 272 check = val_str; 265 while (*check) {273 for (pos = 0; pos < size; pos++) { 266 274 if (*check & 0x80) { 267 275 is_ascii = 0; … … 274 282 275 283 if (is_ascii) { 276 return PyString_FromString (val_str);284 return PyString_FromStringAndSize(val_str, size); 277 285 } else { 278 return PyUnicode_DecodeUTF8(val_str, s trlen(val_str), NULL);286 return PyUnicode_DecodeUTF8(val_str, size, NULL); 279 287 } 280 288 } … … 292 300 PyObject* item = NULL; 293 301 int coltype; 294 PY_LONG_LONG intval;295 302 PyObject* converter; 296 303 PyObject* converted; … … 301 308 char buf[200]; 302 309 const char* colname; 310 311 if (self->reset) { 312 PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback); 313 return NULL; 314 } 303 315 304 316 Py_BEGIN_ALLOW_THREADS … … 346 358 converted = Py_None; 347 359 } 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)); 354 361 } else if (coltype == SQLITE_FLOAT) { 355 362 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i)); 356 363 } else if (coltype == SQLITE_TEXT) { 357 364 val_str = (const char*)sqlite3_column_text(self->statement->st, i); 365 nbytes = sqlite3_column_bytes(self->statement->st, i); 358 366 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type) 359 367 || (self->connection->text_factory == pysqlite_OptimizedUnicode)) { 360 368 361 converted = pysqlite_unicode_from_string(val_str, 369 converted = pysqlite_unicode_from_string(val_str, nbytes, 362 370 self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0); 363 371 … … 372 380 } 373 381 } else if (self->connection->text_factory == (PyObject*)&PyString_Type) { 374 converted = PyString_FromString (val_str);382 converted = PyString_FromStringAndSize(val_str, nbytes); 375 383 } else { 376 converted = PyObject_CallFunction(self->connection->text_factory, "s ", val_str);384 converted = PyObject_CallFunction(self->connection->text_factory, "s#", val_str, nbytes); 377 385 } 378 386 } else { … … 405 413 406 414 return row; 415 } 416 417 /* 418 * Checks if a cursor object is usable. 419 * 420 * 0 => error; 1 => ok 421 */ 422 static 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); 407 440 } 408 441 … … 420 453 PyObject* result; 421 454 int numcols; 422 PY_LONG_LONG lastrowid;423 455 int statement_type; 424 456 PyObject* descriptor; … … 426 458 int allow_8bit_chars; 427 459 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; 431 466 432 467 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */ 433 468 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)); 435 470 436 471 Py_XDECREF(self->next_row); … … 440 475 /* executemany() */ 441 476 if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) { 442 return NULL;477 goto error; 443 478 } 444 479 445 480 if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { 446 481 PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); 447 return NULL;482 goto error; 448 483 } 449 484 … … 456 491 parameters_iter = PyObject_GetIter(second_argument); 457 492 if (!parameters_iter) { 458 return NULL;493 goto error; 459 494 } 460 495 } … … 462 497 /* execute() */ 463 498 if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) { 464 return NULL;499 goto error; 465 500 } 466 501 467 502 if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { 468 503 PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); 469 return NULL;504 goto error; 470 505 } 471 506 472 507 parameters_list = PyList_New(0); 473 508 if (!parameters_list) { 474 return NULL;509 goto error; 475 510 } 476 511 … … 698 733 Py_DECREF(self->lastrowid); 699 734 if (!multiple && statement_type == STATEMENT_INSERT) { 735 sqlite_int64 lastrowid; 700 736 Py_BEGIN_ALLOW_THREADS 701 737 lastrowid = sqlite3_last_insert_rowid(self->connection->db); 702 738 Py_END_ALLOW_THREADS 703 self->lastrowid = PyInt_FromLong((long)lastrowid);739 self->lastrowid = _pysqlite_long_from_int64(lastrowid); 704 740 } else { 705 741 Py_INCREF(Py_None); … … 718 754 #ifdef SQLITE_VERSION_NUMBER 719 755 #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); 721 758 #endif 722 759 #endif … … 726 763 Py_XDECREF(parameters_iter); 727 764 Py_XDECREF(parameters_list); 765 766 self->locked = 0; 728 767 729 768 if (PyErr_Occurred()) { … … 754 793 int rc; 755 794 PyObject* result; 756 int statement_completed = 0;757 795 758 796 if (!PyArg_ParseTuple(args, "O", &script_obj)) { … … 760 798 } 761 799 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; 765 805 766 806 if (PyString_Check(script_obj)) { … … 786 826 787 827 while (1) { 788 if (!sqlite3_complete(script_cstr)) {789 break;790 }791 statement_completed = 1;792 793 828 Py_BEGIN_ALLOW_THREADS 794 829 rc = sqlite3_prepare(self->connection->db, … … 821 856 goto error; 822 857 } 858 859 if (*script_cstr == (char)0) { 860 break; 861 } 823 862 } 824 863 825 864 error: 826 865 Py_XDECREF(script_str); 827 828 if (!statement_completed) {829 PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement");830 }831 866 832 867 if (PyErr_Occurred()) { … … 850 885 int rc; 851 886 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); 853 893 return NULL; 854 894 } … … 992 1032 Py_CLEAR(self->statement); 993 1033 } 1034 1035 self->closed = 1; 994 1036 995 1037 Py_INCREF(Py_None); … … 1053 1095 0, /* tp_setattro */ 1054 1096 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 */ 1056 1098 cursor_doc, /* tp_doc */ 1057 1099 0, /* tp_traverse */ 1058 1100 0, /* tp_clear */ 1059 1101 0, /* tp_richcompare */ 1060 0,/* tp_weaklistoffset */1102 offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */ 1061 1103 (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */ 1062 1104 (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */ -
python/trunk/Modules/_sqlite/cursor.h
r2 r391 1 1 /* cursor.h - definitions for the cursor type 2 2 * 3 * Copyright (C) 2004-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 41 41 PyObject* row_factory; 42 42 pysqlite_Statement* statement; 43 int closed; 44 int reset; 45 int locked; 46 int initialized; 43 47 44 48 /* the next row to be returned, NULL if no next row available */ 45 49 PyObject* next_row; 50 51 PyObject* in_weakreflist; /* List of weak references */ 46 52 } pysqlite_Cursor; 47 53 … … 54 60 extern PyTypeObject pysqlite_CursorType; 55 61 56 int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs);57 void pysqlite_cursor_dealloc(pysqlite_Cursor* self);58 62 PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args); 59 63 PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args); -
python/trunk/Modules/_sqlite/module.c
r2 r391 1 1 /* module.c - the module itself 2 2 * 3 * Copyright (C) 2004-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. -
python/trunk/Modules/_sqlite/module.h
r2 r391 1 1 /* module.h - definitions for the module 2 2 * 3 * Copyright (C) 2004-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 26 26 #include "Python.h" 27 27 28 #define PYSQLITE_VERSION "2. 4.1"28 #define PYSQLITE_VERSION "2.6.0" 29 29 30 30 extern PyObject* pysqlite_Error; -
python/trunk/Modules/_sqlite/prepare_protocol.c
r2 r391 1 1 /* prepare_protocol.c - the protocol for preparing values for SQLite 2 2 * 3 * Copyright (C) 2005-20 06Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 22 22 */ 23 23 24 #include "sqlitecompat.h" 24 25 #include "prepare_protocol.h" 25 26 -
python/trunk/Modules/_sqlite/prepare_protocol.h
r2 r391 1 1 /* prepare_protocol.h - the protocol for preparing values for SQLite 2 2 * 3 * Copyright (C) 2005-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. -
python/trunk/Modules/_sqlite/row.c
r2 r391 1 1 /* row.c - an enhanced tuple for database rows 2 2 * 3 * Copyright (C) 2005-20 06Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 178 178 { 179 179 if (opid != Py_EQ && opid != Py_NE) { 180 181 180 Py_INCREF(Py_NotImplemented); 181 return Py_NotImplemented; 182 182 } 183 183 if (PyType_IsSubtype(Py_TYPE(_other), &pysqlite_RowType)) { 184 185 186 187 188 189 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 } 191 191 } 192 192 Py_INCREF(Py_NotImplemented); -
python/trunk/Modules/_sqlite/row.h
r2 r391 1 1 /* row.h - an enhanced tuple for database rows 2 2 * 3 * Copyright (C) 2005-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. -
python/trunk/Modules/_sqlite/sqlitecompat.h
r2 r391 1 1 /* sqlitecompat.h - compatibility macros 2 2 * 3 * Copyright (C) 2006 Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2006-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 22 22 */ 23 23 24 #include "Python.h" 25 24 26 #ifndef PYSQLITE_COMPAT_H 25 27 #define PYSQLITE_COMPAT_H … … 32 34 #endif 33 35 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) 34 40 #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 1 1 /* statement.c - the statement type 2 2 * 3 * Copyright (C) 2005-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 27 27 #include "microprotocols.h" 28 28 #include "prepare_protocol.h" 29 #include "util.h" 29 30 #include "sqlitecompat.h" 30 31 … … 102 103 { 103 104 int rc = SQLITE_OK; 104 long longval;105 PY_LONG_LONG longlongval;106 105 const char* buffer; 107 106 char* string; … … 154 153 155 154 switch (paramtype) { 156 case TYPE_INT: 157 long val = 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); 159 158 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); 164 166 break; 167 } 165 168 case TYPE_FLOAT: 166 169 rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter)); 167 170 break; 168 171 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); 171 174 break; 172 175 case TYPE_UNICODE: 173 176 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); 176 179 Py_DECREF(stringval); 177 180 break; … … 199 202 } 200 203 201 if (PyInt_CheckExact(obj) || PyLong_CheckExact(obj) 204 if (PyInt_CheckExact(obj) || PyLong_CheckExact(obj) 202 205 || PyFloat_CheckExact(obj) || PyString_CheckExact(obj) 203 206 || PyUnicode_CheckExact(obj) || PyBuffer_Check(obj)) { -
python/trunk/Modules/_sqlite/statement.h
r2 r391 1 1 /* statement.h - definitions for the statement type 2 2 * 3 * Copyright (C) 2005-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. -
python/trunk/Modules/_sqlite/util.c
r2 r391 1 1 /* util.c - various utility functions 2 2 * 3 * Copyright (C) 2005-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 105 105 } 106 106 107 #ifdef WORDS_BIGENDIAN 108 # define IS_LITTLE_ENDIAN 0 109 #else 110 # define IS_LITTLE_ENDIAN 1 111 #endif 112 113 PyObject * 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 138 sqlite_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 1 1 /* util.h - various utility functions 2 2 * 3 * Copyright (C) 2005-20 07Gerhard Häring <gh@ghaering.de>3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> 4 4 * 5 5 * This file is part of pysqlite. … … 36 36 */ 37 37 int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st); 38 39 PyObject * _pysqlite_long_from_int64(sqlite_int64 value); 40 sqlite_int64 _pysqlite_long_as_int64(PyObject * value); 41 38 42 #endif
Note:
See TracChangeset
for help on using the changeset viewer.