diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-01-04 00:35:48 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-01-04 00:35:48 -0500 |
commit | c450cd6cb6d0c8fed110abcec5bc17ec4e0e8c5e (patch) | |
tree | 5123348a03368a518a08614bd195927cd7abf03a | |
parent | 31821011271bf2333b69954d53c3c922e39bf225 (diff) | |
download | sqlalchemy-c450cd6cb6d0c8fed110abcec5bc17ec4e0e8c5e.tar.gz |
- Fixed regression where using a ``functools.partial()`` with the event
system would cause a recursion overflow due to usage of inspect.getargspec()
on it in order to detect a legacy calling signature for certain events,
and apparently there's no way to do this with a partial object. Instead
we skip the legacy check and assume the modern style; the check itself
now only occurs for the SessionEvents.after_bulk_update and
SessionEvents.after_bulk_delete events. Those two events will require
the new signature style if assigned to a "partial" event listener.
[ticket:2905]
-rw-r--r-- | doc/build/changelog/changelog_09.rst | 13 | ||||
-rw-r--r-- | lib/sqlalchemy/event/attr.py | 9 | ||||
-rw-r--r-- | lib/sqlalchemy/util/langhelpers.py | 4 | ||||
-rw-r--r-- | test/base/test_events.py | 20 | ||||
-rw-r--r-- | test/base/test_utils.py | 31 |
5 files changed, 73 insertions, 4 deletions
diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst index c72d853d9..c35da3120 100644 --- a/doc/build/changelog/changelog_09.rst +++ b/doc/build/changelog/changelog_09.rst @@ -15,6 +15,19 @@ :version: 0.9.1 .. change:: + :tags: bug, orm, events + :tickets: 2905 + + Fixed regression where using a ``functools.partial()`` with the event + system would cause a recursion overflow due to usage of inspect.getargspec() + on it in order to detect a legacy calling signature for certain events, + and apparently there's no way to do this with a partial object. Instead + we skip the legacy check and assume the modern style; the check itself + now only occurs for the SessionEvents.after_bulk_update and + SessionEvents.after_bulk_delete events. Those two events will require + the new signature style if assigned to a "partial" event listener. + + .. change:: :tags: bug, orm, declarative Fixed an extremely unlikely memory issue where when using diff --git a/lib/sqlalchemy/event/attr.py b/lib/sqlalchemy/event/attr.py index 690ebce08..f0fff5ca4 100644 --- a/lib/sqlalchemy/event/attr.py +++ b/lib/sqlalchemy/event/attr.py @@ -62,10 +62,15 @@ class _DispatchDescriptor(RefCollection): self._empty_listeners = weakref.WeakKeyDictionary() def _adjust_fn_spec(self, fn, named): - argspec = util.get_callable_argspec(fn, no_self=True) if named: fn = self._wrap_fn_for_kw(fn) - fn = legacy._wrap_fn_for_legacy(self, fn, argspec) + if self.legacy_signatures: + try: + argspec = util.get_callable_argspec(fn, no_self=True) + except ValueError: + pass + else: + fn = legacy._wrap_fn_for_legacy(self, fn, argspec) return fn def _wrap_fn_for_kw(self, fn): diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 7e261e38f..105b64c6b 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -268,7 +268,9 @@ def get_callable_argspec(fn, no_self=False): return compat.ArgSpec(spec.args[1:], spec.varargs, spec.keywords, spec.defaults) elif hasattr(fn, '__func__'): return compat.inspect_getargspec(fn.__func__) - elif hasattr(fn, '__call__'): + elif hasattr(fn, '__call__') and \ + not hasattr(fn.__call__, '__call__'): # functools.partial does this; + # not much we can do return get_callable_argspec(fn.__call__) else: raise ValueError("Can't inspect function: %s" % fn) diff --git a/test/base/test_events.py b/test/base/test_events.py index 8673c9baf..e985f8d5b 100644 --- a/test/base/test_events.py +++ b/test/base/test_events.py @@ -311,6 +311,26 @@ class LegacySignatureTest(fixtures.TestBase): canary(x, y, kw) self._test_legacy_accept_kw(inst, canary) + def test_legacy_accept_partial(self): + canary = Mock() + def evt(a, x, y, **kw): + canary(a, x, y, **kw) + from functools import partial + evt_partial = partial(evt, 5) + target = self.TargetOne() + event.listen(target, "event_four", evt_partial) + # can't do legacy accept on a partial; we can't inspect it + assert_raises( + TypeError, + target.dispatch.event_four, 4, 5, 6, 7, foo="bar" + ) + target.dispatch.event_four(4, 5, foo="bar") + eq_( + canary.mock_calls, + [call(5, 4, 5, foo="bar")] + ) + + def _test_legacy_accept_kw(self, target, canary): target.dispatch.event_four(4, 5, 6, 7, foo="bar") diff --git a/test/base/test_utils.py b/test/base/test_utils.py index 1946bd704..86e4b190a 100644 --- a/test/base/test_utils.py +++ b/test/base/test_utils.py @@ -4,7 +4,7 @@ from sqlalchemy import util, sql, exc, testing from sqlalchemy.testing import assert_raises, assert_raises_message, fixtures from sqlalchemy.testing import eq_, is_, ne_, fails_if from sqlalchemy.testing.util import picklers, gc_collect -from sqlalchemy.util import classproperty, WeakSequence +from sqlalchemy.util import classproperty, WeakSequence, get_callable_argspec class KeyedTupleTest(): @@ -1184,6 +1184,33 @@ class ArgInspectionTest(fixtures.TestBase): test(f3) test(f4) + def test_callable_argspec_fn(self): + def foo(x, y, **kw): + pass + eq_( + get_callable_argspec(foo), + (['x', 'y'], None, 'kw', None) + ) + + def test_callable_argspec_method(self): + class Foo(object): + def foo(self, x, y, **kw): + pass + eq_( + get_callable_argspec(Foo.foo), + (['self', 'x', 'y'], None, 'kw', None) + ) + + def test_callable_argspec_partial(self): + from functools import partial + def foo(x, y, z, **kw): + pass + bar = partial(foo, 5) + + assert_raises( + ValueError, + get_callable_argspec, bar + ) class SymbolTest(fixtures.TestBase): @@ -1665,3 +1692,5 @@ class TestClassProperty(fixtures.TestBase): return d eq_(B.something, {'foo': 1, 'bazz': 2}) + + |