Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a decorator to avoid circular calling of class methods #1207

Merged
merged 3 commits into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ v0.10.0 (unreleased)
Finally, ``ComponentID`` objects now hold a reference to the first parent data
they are used in. [#1189]

- Added a decorator that can be used to avoid circular calling of methods (can
occur when dealing with callbacks). [#1207]

v0.9.2 (unreleased)
-------------------

Expand Down
13 changes: 12 additions & 1 deletion glue/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import traceback

__all__ = ['die_on_error']
__all__ = ['die_on_error', 'avoid_circular']


def die_on_error(msg):
Expand All @@ -23,3 +23,14 @@ def wrapper(*args, **kwargs):
print('=' * 72)
return wrapper
return decorator


def avoid_circular(meth):
def wrapper(self, *args, **kwargs):
if not hasattr(self, '_in_avoid_circular') or not self._in_avoid_circular:
self._in_avoid_circular = True
try:
return meth(self, *args, **kwargs)
finally:
self._in_avoid_circular = False
return wrapper
19 changes: 19 additions & 0 deletions glue/utils/tests/test_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from ..decorators import avoid_circular


def test_avoid_circular():

class CircularCall(object):

@avoid_circular
def a(self):
self.b()

@avoid_circular
def b(self):
self.a()

c = CircularCall()

# Without avoid_circular, the following causes a recursion error
c.a()