[PATCH 04 of 13] compat: class for subclassing the 'file' datatype on Py3k

Alejandro Santos alejolp at alejolp.com
Wed Aug 5 15:55:38 CDT 2009


# HG changeset patch
# User Alejandro Santos <alejolp at alejolp.com>
# Date 1249505493 10800
# Node ID 5190ff297043f89a3108d4a8ed10f4ea71358f4d
# Parent  34f746c2c5229c19a3adf7a475b34f9ddafa1772
compat: class for subclassing the 'file' datatype on Py3k

diff -r 34f746c2c522 -r 5190ff297043 mercurial/py2compat.py
--- a/mercurial/py2compat.py	Wed Aug 05 12:39:49 2009 -0300
+++ b/mercurial/py2compat.py	Wed Aug 05 17:51:33 2009 -0300
@@ -12,3 +12,4 @@
 import __builtin__
 
 buffer = __builtin__.buffer
+posixfiletype = file
diff -r 34f746c2c522 -r 5190ff297043 mercurial/py3compat.py
--- a/mercurial/py3compat.py	Wed Aug 05 12:39:49 2009 -0300
+++ b/mercurial/py3compat.py	Wed Aug 05 17:51:33 2009 -0300
@@ -9,5 +9,27 @@
 This file contains helper functions for Python 3 (see py2compat.py)
 """
 
+class posixfiletype:
+    """
+    Wrapper for the io class to replicate the classic 'file' type. This class
+    can be subclassed without the need to know the specific type the 'open'
+    function returns.
+    """
+
+    __slots__ = ('_fd',)
+
+    def __init__(self, file, mode='r', buffering=None, encoding=None, \
+        errors=None, newline=None, closefd=True):
+        fd = open(file, mode=mode, buffering=buffering,
+            encoding=encoding, errors=errors, newline=newline,
+            closefd=closefd)
+        object.__setattr__(self, '_fd', fd)
+
+    def __getattr__(self, name):
+        return getattr(self._fd, name)
+
+    def __setattr__(self, name, value):
+        return setattr(self._fd, name, value)
+
 def buffer(data, offset=None, size=None):
     return memoryview(data)[offset:size and offset + size]
diff -r 34f746c2c522 -r 5190ff297043 mercurial/url.py
--- a/mercurial/url.py	Wed Aug 05 12:39:49 2009 -0300
+++ b/mercurial/url.py	Wed Aug 05 17:51:33 2009 -0300
@@ -231,7 +231,7 @@
 
         return urllib2.ProxyHandler.proxy_open(self, req, proxy, type_)
 
-class httpsendfile(file):
+class httpsendfile(util.posixfiletype):
     def __len__(self):
         return os.fstat(self.fileno()).st_size
 


More information about the Mercurial-devel mailing list