Patchwork [hglib] client: connect to repo if necessary when using "with" statement

login
register
mail settings
Submitter Paul Tonelli
Date June 19, 2014, 8:41 a.m.
Message ID <3fb0aacdad91f7546c09.1403167287@crater2.logilab.fr>
Download mbox | patch
Permalink /patch/5015/
State Changes Requested
Headers show

Comments

Paul Tonelli - June 19, 2014, 8:41 a.m.
# HG changeset patch
# User Paul Tonelli <paul.tonelli@logilab.fr>
# Date 1402936146 -7200
#      Mon Jun 16 18:29:06 2014 +0200
# Node ID 3fb0aacdad91f7546c09384bc80158e64384ba9e
# Parent  861583ceca0bea2a4f692b1e247af7a21c176dcc
client: connect to repo if necessary when using "with" statement

While the '__exit__' closes the connection to the server, the __enter__ method
does not open it. Without this patch, a disconnected repo cannot be used with a
context managed unless you explicitely call the "open" method.
Augie Fackler - July 2, 2014, 8:26 p.m.
On Thu, Jun 19, 2014 at 10:41:27AM +0200, Paul Tonelli wrote:
> # HG changeset patch
> # User Paul Tonelli <paul.tonelli@logilab.fr>
> # Date 1402936146 -7200
> #      Mon Jun 16 18:29:06 2014 +0200
> # Node ID 3fb0aacdad91f7546c09384bc80158e64384ba9e
> # Parent  861583ceca0bea2a4f692b1e247af7a21c176dcc
> client: connect to repo if necessary when using "with" statement

patch LGTM, but I can't push to hglib. mpm?

>
> While the '__exit__' closes the connection to the server, the __enter__ method
> does not open it. Without this patch, a disconnected repo cannot be used with a
> context managed unless you explicitely call the "open" method.
>
> diff -r 861583ceca0b -r 3fb0aacdad91 hglib/client.py
> --- a/hglib/client.py	Wed May 21 15:19:19 2014 +0200
> +++ b/hglib/client.py	Mon Jun 16 18:29:06 2014 +0200
> @@ -61,6 +61,8 @@
>              self.open()
>
>      def __enter__(self):
> +        if self.server is None:
> +            self.open()
>          return self
>
>      def __exit__(self, exc_type, exc_val, exc_tb):
> diff -r 861583ceca0b -r 3fb0aacdad91 tests/test-hglib.py
> --- a/tests/test-hglib.py	Wed May 21 15:19:19 2014 +0200
> +++ b/tests/test-hglib.py	Mon Jun 16 18:29:06 2014 +0200
> @@ -12,3 +12,14 @@
>          common.basetest.setUp(self)
>          client2 = hglib.open()
>          self.client.close()
> +
> +    def test_context_manager_open(self):
> +        """
> +        make sure the "with" statement opens the connection when the client object
> +        is not already connected.
> +        """
> +        common.basetest.setUp(self)
> +        self.client.close()
> +        self.assertRaises(ValueError, self.client.log)
> +        with self.client:
> +            self.client.log()
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
Matt Mackall - July 2, 2014, 10:29 p.m.
On Wed, 2014-07-02 at 16:26 -0400, Augie Fackler wrote:
> On Thu, Jun 19, 2014 at 10:41:27AM +0200, Paul Tonelli wrote:
> > # HG changeset patch
> > # User Paul Tonelli <paul.tonelli@logilab.fr>
> > # Date 1402936146 -7200
> > #      Mon Jun 16 18:29:06 2014 +0200
> > # Node ID 3fb0aacdad91f7546c09384bc80158e64384ba9e
> > # Parent  861583ceca0bea2a4f692b1e247af7a21c176dcc
> > client: connect to repo if necessary when using "with" statement
> 
> patch LGTM, but I can't push to hglib. mpm?

Actually, 'with' is currently a problem as there's nothing to guard the
"with" tests from getting run with Py2.4, which broke the buildbot. I
guess this doesn't make it worse, but it's currently in an unreleasable
state.
Augie Fackler - July 3, 2014, 1:07 a.m.
On Jul 2, 2014, at 6:29 PM, Matt Mackall <mpm@selenic.com> wrote:

> On Wed, 2014-07-02 at 16:26 -0400, Augie Fackler wrote:
>> On Thu, Jun 19, 2014 at 10:41:27AM +0200, Paul Tonelli wrote:
>>> # HG changeset patch
>>> # User Paul Tonelli <paul.tonelli@logilab.fr>
>>> # Date 1402936146 -7200
>>> #      Mon Jun 16 18:29:06 2014 +0200
>>> # Node ID 3fb0aacdad91f7546c09384bc80158e64384ba9e
>>> # Parent  861583ceca0bea2a4f692b1e247af7a21c176dcc
>>> client: connect to repo if necessary when using "with" statement
>> 
>> patch LGTM, but I can't push to hglib. mpm?
> 
> Actually, 'with' is currently a problem as there's nothing to guard the
> "with" tests from getting run with Py2.4, which broke the buildbot. I
> guess this doesn't make it worse, but it's currently in an unreleasable
> state.

Oh, interesting. Feel encouraged to file an hglib bug and assign it to me, I know a workaround for this.

> 
> -- 
> Mathematics is the supreme nostalgia of our time.
> 
>

Patch

diff -r 861583ceca0b -r 3fb0aacdad91 hglib/client.py
--- a/hglib/client.py	Wed May 21 15:19:19 2014 +0200
+++ b/hglib/client.py	Mon Jun 16 18:29:06 2014 +0200
@@ -61,6 +61,8 @@ 
             self.open()
 
     def __enter__(self):
+        if self.server is None:
+            self.open()
         return self
 
     def __exit__(self, exc_type, exc_val, exc_tb):
diff -r 861583ceca0b -r 3fb0aacdad91 tests/test-hglib.py
--- a/tests/test-hglib.py	Wed May 21 15:19:19 2014 +0200
+++ b/tests/test-hglib.py	Mon Jun 16 18:29:06 2014 +0200
@@ -12,3 +12,14 @@ 
         common.basetest.setUp(self)
         client2 = hglib.open()
         self.client.close()
+
+    def test_context_manager_open(self):
+        """
+        make sure the "with" statement opens the connection when the client object
+        is not already connected.
+        """
+        common.basetest.setUp(self)
+        self.client.close()
+        self.assertRaises(ValueError, self.client.log)
+        with self.client:
+            self.client.log()