@@ -58,24 +58,38 @@ def decorator(func):
5858 @wraps (func )
5959 def wrapper (obj , * args , ** kwargs ):
6060
61+ # Get what locals() would return directly after calling
62+ # 'func' with the given args and kwargs
6163 future_locals = getcallargs (func , * ((obj ,) + args ), ** kwargs )
64+
65+ # Build the variable we'll inject
6266 url = "{url}{path}" .format (
6367 url = obj .url ,
6468 path = path .format (** future_locals ))
6569
70+ # Grab the global context for the passed function
6671 g = func .__globals__
67- sentinal = object ()
68- oldvalue = g .get ('endpoint' , sentinal )
72+
73+ # Create a unique default object so we can accurately determine
74+ # if we replaced a value
75+ sentinel = object ()
76+ oldvalue = g .get ('endpoint' , sentinel )
77+
78+ # Inject our variable into the global scope
6979 g ['endpoint' ] = url
7080
81+ # Logging and function call
7182 if oldvalue :
7283 logger .debug ("Value %s for 'endpoint' replaced in global scope "
7384 "for function %s" % (oldvalue , func .__name__ ))
7485 logger .debug ("%s.__globals__['endpoint'] = %s" % (func .__name__ , url ))
7586
7687 result = func (obj , * args , ** kwargs )
77- if oldvalue is not sentinal :
88+
89+ # Replace the previous value, if it existed
90+ if oldvalue is not sentinel :
7891 g ['endpoint' ] = oldvalue
92+
7993 return result
8094 return wrapper
8195 return decorator
0 commit comments