[pypy-svn] pypy default: Implement sqlite progress handlers

amauryfa commits-noreply at bitbucket.org
Wed Feb 9 19:13:26 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r41747:1efaa4da25c9
Date: 2011-02-09 16:41 +0100
http://bitbucket.org/pypy/pypy/changeset/1efaa4da25c9/

Log:	Implement sqlite progress handlers

diff --git a/lib_pypy/_sqlite3.py b/lib_pypy/_sqlite3.py
--- a/lib_pypy/_sqlite3.py
+++ b/lib_pypy/_sqlite3.py
@@ -468,9 +468,33 @@
                                               SQLITE_UTF8,
                                               None,
                                               c_collation_callback)
+        if ret != SQLITE_OK:
+            raise self._get_exception(ret)
 
     def set_progress_handler(self, callable, nsteps):
-        raise NotImplementedError
+        self._check_thread()
+        self._check_closed()
+        if callable is None:
+            c_progress_handler = cast(None, PROGRESS)
+        else:
+            try:
+                c_progress_handler, _ = self.func_cache[callable]
+            except KeyError:
+                def progress_handler(userdata):
+                    try:
+                        ret = callable()
+                        return bool(ret)
+                    except Exception:
+                        # abort query if error occurred
+                        return 1
+                c_progress_handler = PROGRESS(progress_handler)
+
+                self.func_cache[callable] = c_progress_handler, progress_handler
+        ret = sqlite.sqlite3_progress_handler(self.db, nsteps,
+                                              c_progress_handler,
+                                              None)
+        if ret != SQLITE_OK:
+            raise self._get_exception(ret)
 
     def set_authorizer(self, callback):
         raise NotImplementedError
@@ -1052,6 +1076,10 @@
 sqlite.sqlite3_create_collation.argtypes = [c_void_p, c_char_p, c_int, c_void_p, COLLATION]
 sqlite.sqlite3_create_collation.restype = c_int
 
+PROGRESS = CFUNCTYPE(c_int, c_void_p)
+sqlite.sqlite3_progress_handler.argtypes = [c_void_p, c_int, PROGRESS, c_void_p]
+sqlite.sqlite3_progress_handler.restype = c_int
+
 converters = {}
 adapters = {}
 


More information about the Pypy-commit mailing list