Changeset 391 for python/trunk/Modules/_sqlite/connection.c
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 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/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.")},
Note:
See TracChangeset
for help on using the changeset viewer.