urllib2: redo response.readlines addition via class patching
authorJoerg Sonnenberger <joerg@bec.de>
Fri, 28 Jun 2024 16:26:06 +0200
changeset 51827 ace0da86edd0
parent 51826 0338fb200a30
child 51828 b08de326bee4
urllib2: redo response.readlines addition via class patching
mercurial/keepalive.py
mercurial/url.py
--- a/mercurial/keepalive.py	Wed Aug 21 22:15:05 2024 -0400
+++ b/mercurial/keepalive.py	Fri Jun 28 16:26:06 2024 +0200
@@ -573,19 +573,6 @@
         chunks[-1] = chunks[-1][:i]
         return b''.join(chunks)
 
-    def readlines(self, sizehint=0):
-        total = 0
-        list = []
-        while True:
-            line = self.readline()
-            if not line:
-                break
-            list.append(line)
-            total += len(line)
-            if sizehint and total >= sizehint:
-                break
-        return list
-
     def readinto(self, dest):
         if self._raw_readinto is None:
             res = self.read(len(dest))
--- a/mercurial/url.py	Wed Aug 21 22:15:05 2024 -0400
+++ b/mercurial/url.py	Fri Jun 28 16:26:06 2024 +0200
@@ -499,6 +499,28 @@
         return request
 
 
+class readlinehandler(urlreq.basehandler):
+    def http_response(self, request, response):
+        class readlineresponse(response.__class__):
+            def readlines(self, sizehint=0):
+                total = 0
+                list = []
+                while True:
+                    line = self.readline()
+                    if not line:
+                        break
+                    list.append(line)
+                    total += len(line)
+                    if sizehint and total >= sizehint:
+                        break
+                return list
+
+        response.__class__ = readlineresponse
+        return response
+
+    https_response = http_response
+
+
 handlerfuncs = []
 
 
@@ -564,6 +586,7 @@
     )
     handlers.extend([h(ui, passmgr) for h in handlerfuncs])
     handlers.append(cookiehandler(ui))
+    handlers.append(readlinehandler())
     opener = urlreq.buildopener(*handlers)
 
     # keepalive.py's handlers will populate these attributes if they exist.