Author: Philip Jenvey <[email protected]>
Branch: py3.5
Changeset: r89594:a2d0e3000c1f
Date: 2017-01-15 11:13 -0800
http://bitbucket.org/pypy/pypy/changeset/a2d0e3000c1f/
Log: add context management
diff --git a/lib-python/3/test/test_dbm_gnu.py
b/lib-python/3/test/test_dbm_gnu.py
--- a/lib-python/3/test/test_dbm_gnu.py
+++ b/lib-python/3/test/test_dbm_gnu.py
@@ -90,8 +90,7 @@
with self.assertRaises(gdbm.error) as cm:
db.keys()
- self.assertEqual(str(cm.exception),
- "GDBM object has already been closed")
+ self.assertIn("GDBM object has already been closed", str(cm.exception))
if __name__ == '__main__':
unittest.main()
diff --git a/lib_pypy/_dbm.py b/lib_pypy/_dbm.py
--- a/lib_pypy/_dbm.py
+++ b/lib_pypy/_dbm.py
@@ -112,6 +112,13 @@
if status < 0:
raise KeyError(key)
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *exc_info):
+ self.close()
+
+
### initialization: Berkeley DB versus normal DB
def _init_func(name, argtypes=None, restype=None):
diff --git a/lib_pypy/_gdbm.py b/lib_pypy/_gdbm.py
--- a/lib_pypy/_gdbm.py
+++ b/lib_pypy/_gdbm.py
@@ -150,6 +150,13 @@
self[key] = default
return default
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *exc_info):
+ self.close()
+
+
def open(filename, flags='r', mode=0o666):
if not isinstance(filename, str):
raise TypeError("must be str, not %s" % type(filename).__name__)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit