[Python-checkins] cpython (3.5): Issue #23758: Improve num_params docs of create_{function,aggregate} functions

berker.peksag python-checkins at python.org
Sun Mar 27 15:37:47 EDT 2016


https://hg.python.org/cpython/rev/02efd708c5c0
changeset:   100771:02efd708c5c0
branch:      3.5
parent:      100769:0d7d855a7878
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Sun Mar 27 22:39:14 2016 +0300
summary:
  Issue #23758: Improve num_params docs of create_{function,aggregate} functions

If you pass -1, the callable can take any number of arguments.

Added tests to verify the behavior.

Initial patch by Cédric Krier.

files:
  Doc/library/sqlite3.rst           |   8 +++-
  Lib/sqlite3/test/userfunctions.py |  31 +++++++++++++++++++
  2 files changed, 36 insertions(+), 3 deletions(-)


diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst
--- a/Doc/library/sqlite3.rst
+++ b/Doc/library/sqlite3.rst
@@ -324,8 +324,9 @@
 
       Creates a user-defined function that you can later use from within SQL
       statements under the function name *name*. *num_params* is the number of
-      parameters the function accepts, and *func* is a Python callable that is called
-      as the SQL function.
+      parameters the function accepts (if *num_params* is -1, the function may
+      take any number of arguments), and *func* is a Python callable that is
+      called as the SQL function.
 
       The function can return any of the types supported by SQLite: bytes, str, int,
       float and None.
@@ -340,7 +341,8 @@
       Creates a user-defined aggregate function.
 
       The aggregate class must implement a ``step`` method, which accepts the number
-      of parameters *num_params*, and a ``finalize`` method which will return the
+      of parameters *num_params* (if *num_params* is -1, the function may take
+      any number of arguments), and a ``finalize`` method which will return the
       final result of the aggregate.
 
       The ``finalize`` method can return any of the types supported by SQLite:
diff --git a/Lib/sqlite3/test/userfunctions.py b/Lib/sqlite3/test/userfunctions.py
--- a/Lib/sqlite3/test/userfunctions.py
+++ b/Lib/sqlite3/test/userfunctions.py
@@ -55,6 +55,9 @@
 def func_islonglong(v):
     return isinstance(v, int) and v >= 1<<31
 
+def func(*args):
+    return len(args)
+
 class AggrNoStep:
     def __init__(self):
         pass
@@ -111,6 +114,19 @@
     def finalize(self):
         return self.val
 
+class AggrCheckTypes:
+    def __init__(self):
+        self.val = 0
+
+    def step(self, whichType, *vals):
+        theType = {"str": str, "int": int, "float": float, "None": type(None),
+                   "blob": bytes}
+        for val in vals:
+            self.val += int(theType[whichType] is type(val))
+
+    def finalize(self):
+        return self.val
+
 class AggrSum:
     def __init__(self):
         self.val = 0.0
@@ -140,6 +156,7 @@
         self.con.create_function("isnone", 1, func_isnone)
         self.con.create_function("isblob", 1, func_isblob)
         self.con.create_function("islonglong", 1, func_islonglong)
+        self.con.create_function("spam", -1, func)
 
     def tearDown(self):
         self.con.close()
@@ -257,6 +274,13 @@
         val = cur.fetchone()[0]
         self.assertEqual(val, 1)
 
+    def CheckAnyArguments(self):
+        cur = self.con.cursor()
+        cur.execute("select spam(?, ?)", (1, 2))
+        val = cur.fetchone()[0]
+        self.assertEqual(val, 2)
+
+
 class AggregateTests(unittest.TestCase):
     def setUp(self):
         self.con = sqlite.connect(":memory:")
@@ -279,6 +303,7 @@
         self.con.create_aggregate("excStep", 1, AggrExceptionInStep)
         self.con.create_aggregate("excFinalize", 1, AggrExceptionInFinalize)
         self.con.create_aggregate("checkType", 2, AggrCheckType)
+        self.con.create_aggregate("checkTypes", -1, AggrCheckTypes)
         self.con.create_aggregate("mysum", 1, AggrSum)
 
     def tearDown(self):
@@ -349,6 +374,12 @@
         val = cur.fetchone()[0]
         self.assertEqual(val, 1)
 
+    def CheckAggrCheckParamsInt(self):
+        cur = self.con.cursor()
+        cur.execute("select checkTypes('int', ?, ?)", (42, 24))
+        val = cur.fetchone()[0]
+        self.assertEqual(val, 2)
+
     def CheckAggrCheckParamFloat(self):
         cur = self.con.cursor()
         cur.execute("select checkType('float', ?)", (3.14,))

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list