Mercurial > hg
comparison mercurial/url.py @ 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 | 1fe94103c6ee |
children | 5d22e631c365 |
comparison
equal
deleted
inserted
replaced
11879:4e804302d30c | 11880:e3526634d5a3 |
---|---|
6 # | 6 # |
7 # This software may be used and distributed according to the terms of the | 7 # This software may be used and distributed according to the terms of the |
8 # GNU General Public License version 2 or any later version. | 8 # GNU General Public License version 2 or any later version. |
9 | 9 |
10 import urllib, urllib2, urlparse, httplib, os, re, socket, cStringIO | 10 import urllib, urllib2, urlparse, httplib, os, re, socket, cStringIO |
11 import __builtin__ | |
11 from i18n import _ | 12 from i18n import _ |
12 import keepalive, util | 13 import keepalive, util |
13 | 14 |
14 def _urlunparse(scheme, netloc, path, params, query, fragment, url): | 15 def _urlunparse(scheme, netloc, path, params, query, fragment, url): |
15 '''Handle cases where urlunparse(urlparse(x://)) doesn't preserve the "//"''' | 16 '''Handle cases where urlunparse(urlparse(x://)) doesn't preserve the "//"''' |
248 return baseclass.add_header(self, key, val) | 249 return baseclass.add_header(self, key, val) |
249 req.__class__ = _request | 250 req.__class__ = _request |
250 | 251 |
251 return urllib2.ProxyHandler.proxy_open(self, req, proxy, type_) | 252 return urllib2.ProxyHandler.proxy_open(self, req, proxy, type_) |
252 | 253 |
253 class httpsendfile(file): | 254 class httpsendfile(object): |
255 """This is a wrapper around the objects returned by python's "open". | |
256 | |
257 Its purpose is to send file-like objects via HTTP and, to do so, it | |
258 defines a __len__ attribute to feed the Content-Length header. | |
259 """ | |
260 | |
261 def __init__(self, *args, **kwargs): | |
262 # We can't just "self._data = open(*args, **kwargs)" here because there | |
263 # is an "open" function defined in this module that shadows the global | |
264 # one | |
265 self._data = __builtin__.open(*args, **kwargs) | |
266 self.read = self._data.read | |
267 self.seek = self._data.seek | |
268 self.close = self._data.close | |
269 self.write = self._data.write | |
270 | |
254 def __len__(self): | 271 def __len__(self): |
255 return os.fstat(self.fileno()).st_size | 272 return os.fstat(self._data.fileno()).st_size |
256 | 273 |
257 def _gen_sendfile(connection): | 274 def _gen_sendfile(connection): |
258 def _sendfile(self, data): | 275 def _sendfile(self, data): |
259 # send a file | 276 # send a file |
260 if isinstance(data, httpsendfile): | 277 if isinstance(data, httpsendfile): |