On 09/26/2016 02:39 PM, Iain Duncan wrote:
Hi folks, I'm having an issue with event listener registration. I
figured out that I need to deregister my listeners after each functional
test so they don't get registered twice in the same thread and execute
too many times. But I was using an anonymous callable to register them,
like this:

event.listen(resource_class, 'after_insert',
ModelVersioner(resource_class, 'create'))

So I can't unlisten, because I don't store the callable created by the
call to ModelVersioner, so I can't replicate the exact signature of the
the call to listen in my call to remove. I'd prefer not to make and
track a whole bunch of callables, and I wouldn't need to if I could
remove listeners without needing to pass in the exact callable. So I'm
hoping there is some other way to nuke listeners other than passing
target, event, callback to event.remove?

Like say
Perhaps event.remove(target, event, *everything*)
or altering the event registry itself? (this is just for tests, ok if
it's not stable API)

there is a call for that but it's not public API, because SQLAlchemy itself might someday want to also use a particular event hook and an across-the-board clear would step on those as well.

assuming standard unittest.TestCase this isn't that verbose:

class MyTest(unittest.TestCase):
   def _listener_for_test(self, target, evt, listener):
        event.listen(target, evt, listener)
        self.addCleanup(event.remove, target, evt, listener)

   def my_test(self):
self._listener_for_test(resource_class, "after_insert", ModelVersioniner(...))





thanks!
iain

--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to