From fc281224cba2a2f385a95952cc5fc350fe852cca Mon Sep 17 00:00:00 2001 From: Benoit Masson Date: Mon, 9 Jan 2017 02:37:56 +0100 Subject: [PATCH] with unit test and corrected function detection on the closure add a type impor tthough... --- bottle_sqlite.py | 16 +++++++++------- test.py | 7 +++++++ views/test_view.tpl | 1 + 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 views/test_view.tpl diff --git a/bottle_sqlite.py b/bottle_sqlite.py index a6e5388..52f9dcd 100755 --- a/bottle_sqlite.py +++ b/bottle_sqlite.py @@ -35,6 +35,7 @@ __license__ = 'MIT' import sqlite3 import inspect import bottle +import types # PluginError is defined to bottle >= 0.10 if not hasattr(bottle, 'PluginError'): @@ -106,13 +107,14 @@ class SQLitePlugin(object): if keyword not in argspec.args: #check for closure no_keyword_arg = True - for closure in _callback.func_closure: - contents = closure.cell_contents - if callable(contents): - argspec = inspect.getargspec(contents) - if keyword in argspec.args: - no_keyword_arg = False - break + if _callback.func_closure is not None: + for closure in _callback.func_closure: + contents = closure.cell_contents + if isinstance(contents, types.FunctionType): + argspec = inspect.getargspec(contents) + if keyword in argspec.args: + no_keyword_arg = False + break if no_keyword_arg: return callback diff --git a/test.py b/test.py index 681a79e..f91f4bc 100644 --- a/test.py +++ b/test.py @@ -28,6 +28,13 @@ class SQLiteTest(unittest.TestCase): def tearDown(self): os.unlink(self.plugin.dbfile) + def test_with_view(self): + @self.app.get('/') + @bottle.view('test_view') + def test(db): + self.assertEqual(type(db), type(sqlite3.connect(':memory:'))) + self._request('/') + def test_with_keyword(self): @self.app.get('/') def test(db): diff --git a/views/test_view.tpl b/views/test_view.tpl new file mode 100644 index 0000000..bbba0a0 --- /dev/null +++ b/views/test_view.tpl @@ -0,0 +1 @@ +test_view \ No newline at end of file