Patchwork url: make logginghttphandler compatible with Python 2.7.6

login
register
mail settings
Submitter Yuya Nishihara
Date March 24, 2018, 4:09 a.m.
Message ID <f40b6e7fc011715b2097.1521864546@mimosa>
Download mbox | patch
Permalink /patch/29808/
State Accepted
Headers show

Comments

Yuya Nishihara - March 24, 2018, 4:09 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521864147 -32400
#      Sat Mar 24 13:02:27 2018 +0900
# Node ID f40b6e7fc011715b209774c0d1fbc5b8a6694401
# Parent  db114320df7ee744047fe9a92a01afc40f9d0e87
url: make logginghttphandler compatible with Python 2.7.6

There wasn't a usable hook point in httplib, so we have to replace connect()
to wrap the socket before self._tunnel().
Pulkit Goyal - March 24, 2018, 12:04 p.m.
On Sat, Mar 24, 2018 at 9:39 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1521864147 -32400
> #      Sat Mar 24 13:02:27 2018 +0900
> # Node ID f40b6e7fc011715b209774c0d1fbc5b8a6694401
> # Parent  db114320df7ee744047fe9a92a01afc40f9d0e87
> url: make logginghttphandler compatible with Python 2.7.6
>
> There wasn't a usable hook point in httplib, so we have to replace connect()
> to wrap the socket before self._tunnel().

Queued this. Many thanks!

Patch

diff --git a/mercurial/url.py b/mercurial/url.py
--- a/mercurial/url.py
+++ b/mercurial/url.py
@@ -12,6 +12,7 @@  from __future__ import absolute_import
 import base64
 import os
 import socket
+import sys
 
 from .i18n import _
 from . import (
@@ -304,6 +305,16 @@  class logginghttpconnection(keepalive.HT
         keepalive.HTTPConnection.__init__(self, *args, **kwargs)
         self._create_connection = createconn
 
+    if sys.version_info < (2, 7, 7):
+        # copied from 2.7.14, since old implementations directly call
+        # socket.create_connection()
+        def connect(self):
+            self.sock = self._create_connection((self.host, self.port),
+                                                self.timeout,
+                                                self.source_address)
+            if self._tunnel_host:
+                self._tunnel()
+
 class logginghttphandler(httphandler):
     """HTTP handler that logs socket I/O."""
     def __init__(self, logfh, name, observeropts):