@@ -105,8 +105,12 @@ async def _loop(self, *args, **kwargs):
105105 def __get__ (self , obj , objtype ):
106106 if obj is None :
107107 return self
108- self ._injected = obj
109- return self
108+
109+ copy = Loop (self .coro , seconds = self .seconds , hours = self .hours , minutes = self .minutes ,
110+ count = self .count , reconnect = self .reconnect , loop = self .loop )
111+ copy ._injected = obj
112+ setattr (obj , self .coro .__name__ , copy )
113+ return copy
110114
111115 @property
112116 def current_loop (self ):
@@ -405,21 +409,6 @@ def change_interval(self, *, seconds=0, minutes=0, hours=0):
405409 self .hours = hours
406410 self .minutes = minutes
407411
408- class _LoopFactory :
409- def __init__ (self , func , ** kwargs ):
410- self .func = func
411- self .name = func .__name__
412- self .kwargs = kwargs
413-
414- def __get__ (self , obj , objtype ):
415- if obj is None :
416- return self
417-
418- loop = Loop (self .func , ** self .kwargs )
419- loop ._injected = obj
420- setattr (obj , self .name , loop )
421- return loop
422-
423412def loop (* , seconds = 0 , minutes = 0 , hours = 0 , count = None , reconnect = True , loop = None ):
424413 """A decorator that schedules a task in the background for you with
425414 optional reconnect logic. The decorator returns a :class:`Loop`.
@@ -451,23 +440,6 @@ def loop(*, seconds=0, minutes=0, hours=0, count=None, reconnect=True, loop=None
451440 The function was not a coroutine.
452441 """
453442 def decorator (func ):
454- defined_within_class = False
455- frames = inspect .stack ()
456- # Essentially, to detect whether we're using this decorator a class
457- # context we're walking the stack to see whether it's top level or
458- # within a class level. This code is pretty finicky and hacky but
459- # it's better than the alternative that requires maintaining a list
460- # of IDs. This code does however break if someone assigns a loop
461- # decorator using different ways, such as a dynamically created
462- # class or calling the decorator directly. However such uses should
463- # be niche and thus don't really impede functionality for 99.99% of users
464- for frame in frames [1 :]:
465- if frame [3 ] == '<module>' :
466- break
467- if '__module__' in frame [0 ].f_code .co_names :
468- defined_within_class = True
469- break
470-
471443 kwargs = {
472444 'seconds' : seconds ,
473445 'minutes' : minutes ,
@@ -476,8 +448,5 @@ def decorator(func):
476448 'reconnect' : reconnect ,
477449 'loop' : loop
478450 }
479-
480- if defined_within_class :
481- return _LoopFactory (func , ** kwargs )
482451 return Loop (func , ** kwargs )
483452 return decorator
0 commit comments