Add context manager to subvertpy.wc.Adm.
authorJelmer Vernooij <jelmer@jelmer.uk>
Wed, 6 Jun 2018 00:12:10 +0000 (01:12 +0100)
committerJelmer Vernooij <jelmer@jelmer.uk>
Wed, 6 Jun 2018 00:12:10 +0000 (01:12 +0100)
subvertpy/tests/test_wc.py
subvertpy/wc_adm.c

index eac8933a7dbec12932b4211638906775c3f2c192..a501aee1610a51f102e56cc70b1e5ceb01038ae1 100644 (file)
@@ -100,6 +100,14 @@ class AdmObjTests(SubversionTestCase):
         self.assertFalse(adm.has_binary_prop("checkout/bar"))
         adm.close()
 
+    def test_with(self):
+        self.make_client("repos", "checkout")
+        self.build_tree({"checkout/bar": b"\x00 \x01"})
+        self.client_add('checkout/bar')
+        self.client_set_prop('checkout/bar', 'svn:mime-type', 'text/bar')
+        with wc.Adm(None, "checkout") as adm:
+            self.assertFalse(adm.has_binary_prop("checkout/bar"))
+
     def test_get_ancestry(self):
         repos_url = self.make_client("repos", "checkout")
         self.build_tree({"checkout/bar": b"\x00 \x01"})
index 7cdce98c4338a47ba9e727aaa5d4eab6d62a2649..07df1343a04611da64643b3520185f92a4d8c57b 100644 (file)
@@ -880,7 +880,7 @@ static PyObject *adm_close(PyObject *self)
             svn_wc_adm_close(admobj->adm);
 #endif
         Py_END_ALLOW_THREADS
-            admobj->adm = NULL;
+        admobj->adm = NULL;
     }
 
     Py_RETURN_NONE;
@@ -1782,6 +1782,27 @@ static PyObject *wc_add_lock(PyObject *self, PyObject *args)
     Py_RETURN_NONE;
 }
 
+static PyObject *wc_enter(PyObject *self)
+{
+    Py_INCREF(self);
+    return self;
+}
+
+static PyObject *wc_exit(PyObject *self, PyObject *args)
+{
+    PyObject *exc_type, *exc_value, *exc_tb, *ret;
+
+    if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb))
+        return NULL;
+
+    ret = adm_close(self);
+    if (ret == NULL) {
+        return NULL;
+    }
+
+    Py_RETURN_NONE;
+}
+
 static PyMethodDef adm_methods[] = {
     { "prop_set", adm_prop_set, METH_VARARGS, "S.prop_set(name, value, path, skip_checks=False)" },
     { "access_path", (PyCFunction)adm_access_path, METH_NOARGS,
@@ -1848,6 +1869,8 @@ static PyMethodDef adm_methods[] = {
         "S.resolved_conflict(path, resolve_text, resolve_props, resolve_tree, depth, conflict_choice, notify_func=None, cancel=None)" },
     { "status", (PyCFunction)wc_status, METH_VARARGS, "status(wc_path) -> Status" },
     { "add_lock", (PyCFunction)wc_add_lock, METH_VARARGS, "add_lock(path, lock)" },
+    { "__enter__", (PyCFunction)wc_enter, METH_NOARGS, "__enter__() -> self" },
+    { "__exit__", (PyCFunction)wc_exit, METH_VARARGS, "__exit__(exc_type, exc_value, exc_tb)" },
     { NULL, }
 };