Use URLopener().open(file) instead of open(file, 'r').
(self, fullurl, data=None)
| 1757 | |
| 1758 | # External interface |
| 1759 | def open(self, fullurl, data=None): |
| 1760 | """Use URLopener().open(file) instead of open(file, 'r').""" |
| 1761 | fullurl = unwrap(_to_bytes(fullurl)) |
| 1762 | fullurl = quote(fullurl, safe="%/:=&?~#+!$,;'@()*[]|") |
| 1763 | if self.tempcache and fullurl in self.tempcache: |
| 1764 | filename, headers = self.tempcache[fullurl] |
| 1765 | fp = open(filename, 'rb') |
| 1766 | return addinfourl(fp, headers, fullurl) |
| 1767 | urltype, url = _splittype(fullurl) |
| 1768 | if not urltype: |
| 1769 | urltype = 'file' |
| 1770 | if urltype in self.proxies: |
| 1771 | proxy = self.proxies[urltype] |
| 1772 | urltype, proxyhost = _splittype(proxy) |
| 1773 | host, selector = _splithost(proxyhost) |
| 1774 | url = (host, fullurl) # Signal special case to open_*() |
| 1775 | else: |
| 1776 | proxy = None |
| 1777 | name = 'open_' + urltype |
| 1778 | self.type = urltype |
| 1779 | name = name.replace('-', '_') |
| 1780 | if not hasattr(self, name) or name == 'open_local_file': |
| 1781 | if proxy: |
| 1782 | return self.open_unknown_proxy(proxy, fullurl, data) |
| 1783 | else: |
| 1784 | return self.open_unknown(fullurl, data) |
| 1785 | try: |
| 1786 | if data is None: |
| 1787 | return getattr(self, name)(url) |
| 1788 | else: |
| 1789 | return getattr(self, name)(url, data) |
| 1790 | except (HTTPError, URLError): |
| 1791 | raise |
| 1792 | except OSError as msg: |
| 1793 | raise OSError('socket error', msg) from msg |
| 1794 | |
| 1795 | def open_unknown(self, fullurl, data=None): |
| 1796 | """Overridable interface to open unknown URL type.""" |
no test coverage detected