response.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Response classes used by urllib.
  2. The base class, addbase, defines a minimal file-like interface,
  3. including read() and readline(). The typical response object is an
  4. addinfourl instance, which defines an info() method that returns
  5. headers and a geturl() method that returns the url.
  6. """
  7. from __future__ import absolute_import, division, unicode_literals
  8. from future.builtins import object
  9. class addbase(object):
  10. """Base class for addinfo and addclosehook."""
  11. # XXX Add a method to expose the timeout on the underlying socket?
  12. def __init__(self, fp):
  13. # TODO(jhylton): Is there a better way to delegate using io?
  14. self.fp = fp
  15. self.read = self.fp.read
  16. self.readline = self.fp.readline
  17. # TODO(jhylton): Make sure an object with readlines() is also iterable
  18. if hasattr(self.fp, "readlines"):
  19. self.readlines = self.fp.readlines
  20. if hasattr(self.fp, "fileno"):
  21. self.fileno = self.fp.fileno
  22. else:
  23. self.fileno = lambda: None
  24. def __iter__(self):
  25. # Assigning `__iter__` to the instance doesn't work as intended
  26. # because the iter builtin does something like `cls.__iter__(obj)`
  27. # and thus fails to find the _bound_ method `obj.__iter__`.
  28. # Returning just `self.fp` works for built-in file objects but
  29. # might not work for general file-like objects.
  30. return iter(self.fp)
  31. def __repr__(self):
  32. return '<%s at %r whose fp = %r>' % (self.__class__.__name__,
  33. id(self), self.fp)
  34. def close(self):
  35. if self.fp:
  36. self.fp.close()
  37. self.fp = None
  38. self.read = None
  39. self.readline = None
  40. self.readlines = None
  41. self.fileno = None
  42. self.__iter__ = None
  43. self.__next__ = None
  44. def __enter__(self):
  45. if self.fp is None:
  46. raise ValueError("I/O operation on closed file")
  47. return self
  48. def __exit__(self, type, value, traceback):
  49. self.close()
  50. class addclosehook(addbase):
  51. """Class to add a close hook to an open file."""
  52. def __init__(self, fp, closehook, *hookargs):
  53. addbase.__init__(self, fp)
  54. self.closehook = closehook
  55. self.hookargs = hookargs
  56. def close(self):
  57. if self.closehook:
  58. self.closehook(*self.hookargs)
  59. self.closehook = None
  60. self.hookargs = None
  61. addbase.close(self)
  62. class addinfo(addbase):
  63. """class to add an info() method to an open file."""
  64. def __init__(self, fp, headers):
  65. addbase.__init__(self, fp)
  66. self.headers = headers
  67. def info(self):
  68. return self.headers
  69. class addinfourl(addbase):
  70. """class to add info() and geturl() methods to an open file."""
  71. def __init__(self, fp, headers, url, code=None):
  72. addbase.__init__(self, fp)
  73. self.headers = headers
  74. self.url = url
  75. self.code = code
  76. def info(self):
  77. return self.headers
  78. def getcode(self):
  79. return self.code
  80. def geturl(self):
  81. return self.url
  82. del absolute_import, division, unicode_literals, object