From 2cc4e8d066c4d252eb248fefaa07a50d83676501 Mon Sep 17 00:00:00 2001 From: Benoit Masson Date: Mon, 9 Jan 2017 02:37:56 +0100 Subject: [PATCH] Change to use the get_callback_args route function to return the right function to analyse for keywords and insert the sqlite connection. --- bottle.py | 4 ++++ bottle_sqlite.py | 19 ++++--------------- test.py | 7 +++++++ views/test_view.tpl | 1 + 4 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 views/test_view.tpl diff --git a/bottle.py b/bottle.py index ff74d5c..6c7280b 100644 --- a/bottle.py +++ b/bottle.py @@ -580,6 +580,7 @@ class Route(object): func = getattr(func, '__func__' if py3k else 'im_func', func) closure_attr = '__closure__' if py3k else 'func_closure' while hasattr(func, closure_attr) and getattr(func, closure_attr): + func_previous = func attributes = getattr(func, closure_attr) func = attributes[0].cell_contents @@ -588,6 +589,9 @@ class Route(object): # pick first FunctionType instance from multiple arguments func = filter(lambda x: isinstance(x, FunctionType), map(lambda x: x.cell_contents, attributes)) + if len(list(func))==0: + func = func_previous + break func = list(func)[0] # py3 support return func diff --git a/bottle_sqlite.py b/bottle_sqlite.py index a6e5388..944876a 100755 --- a/bottle_sqlite.py +++ b/bottle_sqlite.py @@ -83,9 +83,11 @@ class SQLitePlugin(object): if bottle.__version__.startswith('0.9'): config = route['config'] _callback = route['callback'] + argspec = inspect.getargspec(_callback).args else: config = route.config _callback = route.callback + argspec = route.get_callback_args() # Override global configuration with route-specific values. if "sqlite" in config: @@ -100,21 +102,8 @@ class SQLitePlugin(object): keyword = g('keyword', self.keyword) text_factory = g('keyword', self.text_factory) - # Test if the original callback accepts a 'db' keyword. - # Ignore it if it does not need a database handle. - argspec = inspect.getargspec(_callback) - 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 no_keyword_arg: - return callback + if keyword not in argspec: + return callback def wrapper(*args, **kwargs): # Connect to the database 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