Show More
@@ -139,7 +139,22 b' def strip_encoding_cookie(filelike):' | |||||
139 | for line in it: |
|
139 | for line in it: | |
140 | yield line |
|
140 | yield line | |
141 |
|
141 | |||
142 |
def read_py_file(filename, |
|
142 | def read_py_file(filename, skip_encoding_cookie=True): | |
|
143 | """Read a Python file, using the encoding declared inside the file. | |||
|
144 | ||||
|
145 | Parameters | |||
|
146 | ---------- | |||
|
147 | filename : str | |||
|
148 | The path to the file to read. | |||
|
149 | skip_encoding_cookie : bool | |||
|
150 | If True (the default), and the encoding declaration is found in the first | |||
|
151 | two lines, that line will be excluded from the output - compiling a | |||
|
152 | unicode string with an encoding declaration is a SyntaxError in Python 2. | |||
|
153 | ||||
|
154 | Returns | |||
|
155 | ------- | |||
|
156 | A unicode string containing the contents of the file. | |||
|
157 | """ | |||
143 | with open(filename) as f: # the open function defined in this module. |
|
158 | with open(filename) as f: # the open function defined in this module. | |
144 | if skip_encoding_cookie: |
|
159 | if skip_encoding_cookie: | |
145 | return "".join(strip_encoding_cookie(f)) |
|
160 | return "".join(strip_encoding_cookie(f)) | |
@@ -147,8 +162,23 b" def read_py_file(filename, errors='replace', skip_encoding_cookie=True):" | |||||
147 | return f.read() |
|
162 | return f.read() | |
148 |
|
163 | |||
149 | def read_py_url(url, errors='replace', skip_encoding_cookie=True): |
|
164 | def read_py_url(url, errors='replace', skip_encoding_cookie=True): | |
150 |
""" |
|
165 | """Read a Python file from a URL, using the encoding declared inside the file. | |
151 | detect_encoding(). |
|
166 | ||
|
167 | Parameters | |||
|
168 | ---------- | |||
|
169 | url : str | |||
|
170 | The URL from which to fetch the file. | |||
|
171 | errors : str | |||
|
172 | How to handle decoding errors in the file. Options are the same as for | |||
|
173 | bytes.decode(), but here 'replace' is the default. | |||
|
174 | skip_encoding_cookie : bool | |||
|
175 | If True (the default), and the encoding declaration is found in the first | |||
|
176 | two lines, that line will be excluded from the output - compiling a | |||
|
177 | unicode string with an encoding declaration is a SyntaxError in Python 2. | |||
|
178 | ||||
|
179 | Returns | |||
|
180 | ------- | |||
|
181 | A unicode string containing the contents of the file. | |||
152 | """ |
|
182 | """ | |
153 | response = urllib.urlopen(url) |
|
183 | response = urllib.urlopen(url) | |
154 | buffer = io.BytesIO(response.read()) |
|
184 | buffer = io.BytesIO(response.read()) |
General Comments 0
You need to be logged in to leave comments.
Login now