Read a Python file from a URL, using the encoding declared inside the file. Parameters ---------- url : str The URL from which to fetch the file. errors : str How to handle decoding errors in the file. Options are the same as for bytes.decode(), but here 'repla
(url, errors='replace', skip_encoding_cookie=True)
| 79 | return f.read() |
| 80 | |
| 81 | def read_py_url(url, errors='replace', skip_encoding_cookie=True): |
| 82 | """Read a Python file from a URL, using the encoding declared inside the file. |
| 83 | |
| 84 | Parameters |
| 85 | ---------- |
| 86 | url : str |
| 87 | The URL from which to fetch the file. |
| 88 | errors : str |
| 89 | How to handle decoding errors in the file. Options are the same as for |
| 90 | bytes.decode(), but here 'replace' is the default. |
| 91 | skip_encoding_cookie : bool |
| 92 | If True (the default), and the encoding declaration is found in the first |
| 93 | two lines, that line will be excluded from the output. |
| 94 | |
| 95 | Returns |
| 96 | ------- |
| 97 | A unicode string containing the contents of the file. |
| 98 | """ |
| 99 | # Deferred import for faster start |
| 100 | from urllib.request import urlopen |
| 101 | response = urlopen(url) |
| 102 | buffer = io.BytesIO(response.read()) |
| 103 | return source_to_unicode(buffer, errors, skip_encoding_cookie) |
nothing calls this directly
no test coverage detected