.. changelog::
: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
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):
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)
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")
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():
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):
return d
eq_(B.something, {'foo': 1, 'bazz': 2})
+
+