[tor-commits] [bridgedb/develop] Fix context manager creation

phw at torproject.org phw at torproject.org
Wed Feb 19 18:27:17 UTC 2020


commit c1a48d1b568b00fab19a308e6497881f31d17680
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Jan 11 16:38:25 2020 -0800

    Fix context manager creation
    
    BridgeDB subclassed a GeneratorContextManager class which became private in
    python 3.x. Its constructor signature changed, so attempting to simply use
    the private version failed with...
    
      Traceback (most recent call last):
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_Storage.py", line 76, in test_insertBridgeAndGetRing_already_seen_bridge
          with Storage.getDB() as db:
        File "/home/atagar/Desktop/tor/bridgedb/bridgedb/Storage.py", line 400, in helper
          return DBGeneratorContextManager(func(*args, **kwds))
      builtins.TypeError: __init__() missing 2 required positional arguments: 'args' and 'kwds'
    
    Storage.py is pretty confusing, but they seem to simply want a context manager
    so simply implementing one.
    
    This changes the test results as follows...
    
      before: FAILED (skips=106, failures=16, errors=272, successes=410)
      after:  FAILED (skips=106, failures=17, errors=266, successes=415)
---
 bridgedb/Storage.py | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/bridgedb/Storage.py b/bridgedb/Storage.py
index 9e2be8e..1f81052 100644
--- a/bridgedb/Storage.py
+++ b/bridgedb/Storage.py
@@ -8,7 +8,6 @@ import binascii
 import sqlite3
 import time
 import hashlib
-from contextlib import _GeneratorContextManager
 from functools import wraps
 from ipaddr import IPAddress
 import sys
@@ -345,11 +344,18 @@ def openDatabase(sqlite_file):
     return conn
 
 
-class DBGeneratorContextManager(_GeneratorContextManager):
+class DBGeneratorContextManager(object):
     """Helper for @contextmanager decorator.
 
     Overload __exit__() so we can call the generator many times
     """
+
+    def __init__(self, gen):
+      self.gen = gen
+
+    def __enter__(self):
+      return next(self.gen)
+
     def __exit__(self, type, value, traceback):
         """Handle exiting a with statement block
 





More information about the tor-commits mailing list