Changeset 260:cdf294440605

Show
Ignore:
Timestamp:
11/25/07 16:54:31 (12 months ago)
Author:
gh
Message:

Closes: #212
Implemented context managers.

Files:
2 added
4 modified

Legend:

Unmodified
Added
Removed
  • doc/usage-guide.txt

    r259 r260  
    2828|   `3.6 Setting an authorizer callback`_ 
    2929|   `3.7 Setting a progress handler`_ 
     30|   `3.8 Using the connection as a context manager`_ 
    3031| `4. SQLite and Python types`_ 
    3132|   `4.1 Introduction`_ 
     
    712713   :source-file: includes/sqlite3/progress.py 
    713714 
     7153.8 Using the connection as a context manager 
     716--------------------------------------------- 
     717 
     718With Python 2.5 or higher, pysqlite's connection objects can be used as context 
     719managers that automatically commit or rollback transactions.  In the event of 
     720an exception, the transaction is rolled back; otherwise, the transaction is 
     721committed: 
     722 
     723  .. code-block:: 
     724    :language: Python 
     725    :source-file: includes/sqlite3/ctx_manager.py 
     726 
    7147274. SQLite and Python types 
    715728========================== 
  • pysqlite2/test/__init__.py

    r243 r260  
    3838 
    3939def suite(): 
    40     return unittest.TestSuite( 
    41         (dbapi.suite(), types.suite(), userfunctions.suite(), factory.suite(),\ 
    42          transactions.suite(), hooks.suite(), regression.suite())) 
     40    tests = [dbapi.suite(), types.suite(), userfunctions.suite(), 
     41      factory.suite(), transactions.suite(), hooks.suite(), regression.suite()] 
     42    if sys.version_info >= (2, 5, 0): 
     43        from pysqlite2.test import py25tests 
     44        tests.append(py25tests.suite()) 
     45 
     46    return unittest.TestSuite(tuple(tests)) 
    4347 
    4448def test(): 
  • pysqlite2/test/transactions.py

    r243 r260  
    2222# 3. This notice may not be removed or altered from any source distribution. 
    2323 
     24import sys 
    2425import os, unittest 
    2526import pysqlite2.dbapi2 as sqlite 
  • src/connection.c

    r259 r260  
    12571257} 
    12581258 
     1259/* Called when the connection is used as a context manager. Returns itself as a 
     1260 * convenience to the caller. */ 
     1261static PyObject * 
     1262pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args) 
     1263{ 
     1264    Py_INCREF(self); 
     1265    return (PyObject*)self; 
     1266} 
     1267 
     1268/** Called when the connection is used as a context manager. If there was any 
     1269 * exception, a rollback takes place; otherwise we commit. */ 
     1270static PyObject * 
     1271pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args) 
     1272{ 
     1273    PyObject* exc_type, *exc_value, *exc_tb; 
     1274    char* method_name; 
     1275    PyObject* result; 
     1276 
     1277    if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) { 
     1278        return NULL; 
     1279    } 
     1280 
     1281    if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) { 
     1282        method_name = "commit"; 
     1283    } else { 
     1284        method_name = "rollback"; 
     1285    } 
     1286 
     1287    result = PyObject_CallMethod((PyObject*)self, method_name, ""); 
     1288    if (!result) { 
     1289        return NULL; 
     1290    } 
     1291    Py_DECREF(result); 
     1292 
     1293    Py_INCREF(Py_False); 
     1294    return Py_False; 
     1295} 
     1296 
    12591297static char connection_doc[] = 
    12601298PyDoc_STR("SQLite database connection object."); 
     
    12931331    {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS, 
    12941332        PyDoc_STR("Abort any pending database operation. Non-standard.")}, 
     1333    {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS, 
     1334        PyDoc_STR("For context manager. Non-standard.")}, 
     1335    {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS, 
     1336        PyDoc_STR("For context manager. Non-standard.")}, 
    12951337    {NULL, NULL} 
    12961338};