Changeset 260:cdf294440605
- Timestamp:
- 11/25/07 16:54:31 (20 months ago)
- Author:
- gh
- Message:
-
Closes: #212
Implemented context managers.
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r259
|
r260
|
|
| 28 | 28 | | `3.6 Setting an authorizer callback`_ |
| 29 | 29 | | `3.7 Setting a progress handler`_ |
| | 30 | | `3.8 Using the connection as a context manager`_ |
| 30 | 31 | | `4. SQLite and Python types`_ |
| 31 | 32 | | `4.1 Introduction`_ |
| … |
… |
|
| 712 | 713 | :source-file: includes/sqlite3/progress.py |
| 713 | 714 | |
| | 715 | 3.8 Using the connection as a context manager |
| | 716 | --------------------------------------------- |
| | 717 | |
| | 718 | With Python 2.5 or higher, pysqlite's connection objects can be used as context |
| | 719 | managers that automatically commit or rollback transactions. In the event of |
| | 720 | an exception, the transaction is rolled back; otherwise, the transaction is |
| | 721 | committed: |
| | 722 | |
| | 723 | .. code-block:: |
| | 724 | :language: Python |
| | 725 | :source-file: includes/sqlite3/ctx_manager.py |
| | 726 | |
| 714 | 727 | 4. SQLite and Python types |
| 715 | 728 | ========================== |
-
|
r243
|
r260
|
|
| 38 | 38 | |
| 39 | 39 | def 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)) |
| 43 | 47 | |
| 44 | 48 | def test(): |
-
|
r243
|
r260
|
|
| 22 | 22 | # 3. This notice may not be removed or altered from any source distribution. |
| 23 | 23 | |
| | 24 | import sys |
| 24 | 25 | import os, unittest |
| 25 | 26 | import pysqlite2.dbapi2 as sqlite |
-
|
r259
|
r260
|
|
| 1257 | 1257 | } |
| 1258 | 1258 | |
| | 1259 | /* Called when the connection is used as a context manager. Returns itself as a |
| | 1260 | * convenience to the caller. */ |
| | 1261 | static PyObject * |
| | 1262 | pysqlite_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. */ |
| | 1270 | static PyObject * |
| | 1271 | pysqlite_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 | |
| 1259 | 1297 | static char connection_doc[] = |
| 1260 | 1298 | PyDoc_STR("SQLite database connection object."); |
| … |
… |
|
| 1293 | 1331 | {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS, |
| 1294 | 1332 | 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.")}, |
| 1295 | 1337 | {NULL, NULL} |
| 1296 | 1338 | }; |