Mercurial > hg
changeset 11880:e3526634d5a3
url.py: removed 'file' inheritance in the httpsendfile class
Since py3k doesn't have a "file" builtin and, consequently, doesn't support
inheriting from it, this patch refactors the httpsendfile class to wrap the
objects returned by the builtin "open" function while adding the necessary
methods (__len__ for constructing the Content-Length header and read, write,
close and seek for the file-like interface).
author | Renato Cunha <renatoc@gmail.com> |
---|---|
date | Sat, 14 Aug 2010 18:31:22 -0300 |
parents | 4e804302d30c |
children | 01e04df696e3 |
files | mercurial/url.py |
diffstat | 1 files changed, 19 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/url.py Sun Aug 15 11:05:04 2010 +0200 +++ b/mercurial/url.py Sat Aug 14 18:31:22 2010 -0300 @@ -8,6 +8,7 @@ # GNU General Public License version 2 or any later version. import urllib, urllib2, urlparse, httplib, os, re, socket, cStringIO +import __builtin__ from i18n import _ import keepalive, util @@ -250,9 +251,25 @@ return urllib2.ProxyHandler.proxy_open(self, req, proxy, type_) -class httpsendfile(file): +class httpsendfile(object): + """This is a wrapper around the objects returned by python's "open". + + Its purpose is to send file-like objects via HTTP and, to do so, it + defines a __len__ attribute to feed the Content-Length header. + """ + + def __init__(self, *args, **kwargs): + # We can't just "self._data = open(*args, **kwargs)" here because there + # is an "open" function defined in this module that shadows the global + # one + self._data = __builtin__.open(*args, **kwargs) + self.read = self._data.read + self.seek = self._data.seek + self.close = self._data.close + self.write = self._data.write + def __len__(self): - return os.fstat(self.fileno()).st_size + return os.fstat(self._data.fileno()).st_size def _gen_sendfile(connection): def _sendfile(self, data):