File: use-async-keyword.patch

package info (click to toggle)
python-ratelimiter 1.2.0.post0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: python: 201; makefile: 3
file content (65 lines) | stat: -rw-r--r-- 2,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Author: Diane Trout <diane@ghic.org>, Rebecca N. Palmer
Description: This is an upstream fix backported
 to version of ratelimiter as it exists in Debian.
Origin: backport https://github.com/RazerM/ratelimiter/commit/59a0827c434706d62b89e16a220e4ae12e618858

--- a/ratelimiter.py
+++ b/ratelimiter.py
@@ -30,8 +30,6 @@
 __license__ = 'Apache'
 __description__ = 'Simple python rate limiting object'
 
-PY35 = sys.version_info >= (3, 5)
-
 
 class RateLimiter(object):
     """Provides rate limiting for an operation with a configurable number of
@@ -101,30 +99,26 @@
             while self._timespan >= self.period:
                 self.calls.popleft()
 
-    if PY35:
-        # We have to exec this due to syntax errors on earlier versions.
-        aenter_code = dedent("""
-            async def __aenter__(self):
-                if self._alock is None:
-                    self._init_async_lock()
-                    
-                with await self._alock:
-                    # We want to ensure that no more than max_calls were run in the allowed
-                    # period. For this, we store the last timestamps of each call and run
-                    # the rate verification upon each __enter__ call.
-                    if len(self.calls) >= self.max_calls:
-                        until = time.time() + self.period - self._timespan
-                        if self.callback:
-                            asyncio.ensure_future(self.callback(until))
-                        sleeptime = until - time.time()
-                        if sleeptime > 0:
-                            await asyncio.sleep(sleeptime)
-                    return self
+    async def __aenter__(self):
+        if self._alock is None:
+            self._init_async_lock()
+
+        async with self._alock:
+            # We want to ensure that no more than max_calls were run in the allowed
+            # period. For this, we store the last timestamps of each call and run
+            # the rate verification upon each __enter__ call.
+            if len(self.calls) >= self.max_calls:
+                until = time.time() + self.period - self._timespan
+                if self.callback:
+                    asyncio.ensure_future(self.callback(until))
+                sleeptime = until - time.time()
+                if sleeptime > 0:
+                    await asyncio.sleep(sleeptime)
+            return self
 
-            """)
-        exec(aenter_code)
 
-        __aexit__ = asyncio.coroutine(__exit__)
+    async def __aexit__(self, exc_type, exc_value, traceback):
+         return self.__exit__(exc_type, exc_value, traceback)
 
     @property
     def _timespan(self):