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

Fix bug where stacked decorators don't update #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 22 additions & 3 deletions gradescope_utils/autograder_utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
from functools import wraps
from functools import wraps, update_wrapper
import signal


class _update_wrapper_after_call(object):
"""Context manager to update a wrapper function after the wrapped function is called. Thus,
if the wrapped function modifies the wrapper state (as in @partial_credit, for example), any
changes to the wrapper will be preserved.
"""
def __init__(self, wrapper, func):
self.wrapper = wrapper
self.func = func

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
update_wrapper(self.wrapper, self.func)


class weight(object):
Expand Down Expand Up @@ -112,7 +129,8 @@ def set_leaderboard_value(x):
@wraps(func)
def wrapper(*args, **kwargs):
kwargs['set_leaderboard_value'] = set_leaderboard_value
return func(*args, **kwargs)
with _update_wrapper_after_call(wrapper, func):
return func(*args, **kwargs)

return wrapper

Expand Down Expand Up @@ -146,6 +164,7 @@ def set_score(x):
@wraps(func)
def wrapper(*args, **kwargs):
kwargs['set_score'] = set_score
return func(*args, **kwargs)
with _update_wrapper_after_call(wrapper, func):
return func(*args, **kwargs)

return wrapper