Skip to content

Commit 8c632ba

Browse files
committed
fixes #444
1 parent cafd1d7 commit 8c632ba

File tree

5 files changed

+63
-91
lines changed

5 files changed

+63
-91
lines changed

fastcore/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.5.11"
1+
__version__ = "1.5.12"

fastcore/_modidx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
'status': '4',
2424
'title': 'fastcore',
2525
'user': 'fastai',
26-
'version': '1.5.11'},
26+
'version': '1.5.12'},
2727
'syms': { 'fastcore.all': {},
2828
'fastcore.basics': { 'fastcore.basics.AttrDict': 'https://fastcore.fast.ai/basics.html#attrdict',
2929
'fastcore.basics.Float': 'https://fastcore.fast.ai/basics.html#float',
@@ -309,6 +309,7 @@
309309
'fastcore.net.urldest': 'https://fastcore.fast.ai/net.html#urldest',
310310
'fastcore.net.urljson': 'https://fastcore.fast.ai/net.html#urljson',
311311
'fastcore.net.urlopen': 'https://fastcore.fast.ai/net.html#urlopen',
312+
'fastcore.net.urlopener': 'https://fastcore.fast.ai/net.html#urlopener',
312313
'fastcore.net.urlquote': 'https://fastcore.fast.ai/net.html#urlquote',
313314
'fastcore.net.urlread': 'https://fastcore.fast.ai/net.html#urlread',
314315
'fastcore.net.urlrequest': 'https://fastcore.fast.ai/net.html#urlrequest',

fastcore/net.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03b_net.ipynb.
22

33
# %% auto 0
4-
__all__ = ['url_default_headers', 'ExceptionsHTTP', 'urlquote', 'urlwrap', 'HTTP4xxClientError', 'HTTP5xxServerError', 'urlopen',
5-
'urlread', 'urljson', 'urlcheck', 'urlclean', 'urlretrieve', 'urldest', 'urlsave', 'urlvalid', 'urlrequest',
6-
'urlsend', 'do_request', 'start_server', 'start_client', 'HTTP400BadRequestError',
4+
__all__ = ['url_default_headers', 'ExceptionsHTTP', 'urlquote', 'urlwrap', 'HTTP4xxClientError', 'HTTP5xxServerError',
5+
'urlopener', 'urlopen', 'urlread', 'urljson', 'urlcheck', 'urlclean', 'urlretrieve', 'urldest', 'urlsave',
6+
'urlvalid', 'urlrequest', 'urlsend', 'do_request', 'start_server', 'start_client', 'HTTP400BadRequestError',
77
'HTTP401UnauthorizedError', 'HTTP402PaymentRequiredError', 'HTTP403ForbiddenError', 'HTTP404NotFoundError',
88
'HTTP405MethodNotAllowedError', 'HTTP406NotAcceptableError', 'HTTP407ProxyAuthRequiredError',
99
'HTTP408RequestTimeoutError', 'HTTP409ConflictError', 'HTTP410GoneError', 'HTTP411LengthRequiredError',
@@ -71,9 +71,13 @@ class HTTP5xxServerError(HTTPError):
7171
pass
7272

7373
# %% ../nbs/03b_net.ipynb 14
74-
_opener = urllib.request.build_opener()
75-
_opener.addheaders = list(url_default_headers.items())
76-
install_opener(_opener)
74+
def urlopener():
75+
_opener = urllib.request.build_opener()
76+
_opener.addheaders = list(url_default_headers.items())
77+
return _opener
78+
79+
# %% ../nbs/03b_net.ipynb 15
80+
# install_opener(_opener)
7781

7882
_httperrors = (
7983
(400,'Bad Request'),(401,'Unauthorized'),(402,'Payment Required'),(403,'Forbidden'),(404,'Not Found'),
@@ -90,19 +94,19 @@ class HTTP5xxServerError(HTTPError):
9094
cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code)
9195
globals()[nm] = ExceptionsHTTP[code] = cls
9296

93-
# %% ../nbs/03b_net.ipynb 15
97+
# %% ../nbs/03b_net.ipynb 16
9498
_all_ = ['HTTP400BadRequestError', 'HTTP401UnauthorizedError', 'HTTP402PaymentRequiredError', 'HTTP403ForbiddenError', 'HTTP404NotFoundError', 'HTTP405MethodNotAllowedError', 'HTTP406NotAcceptableError', 'HTTP407ProxyAuthRequiredError', 'HTTP408RequestTimeoutError', 'HTTP409ConflictError', 'HTTP410GoneError', 'HTTP411LengthRequiredError', 'HTTP412PreconditionFailedError', 'HTTP413PayloadTooLargeError', 'HTTP414URITooLongError', 'HTTP415UnsupportedMediaTypeError', 'HTTP416RangeNotSatisfiableError', 'HTTP417ExpectationFailedError', 'HTTP418AmAteapotError', 'HTTP421MisdirectedRequestError', 'HTTP422UnprocessableEntityError', 'HTTP423LockedError', 'HTTP424FailedDependencyError', 'HTTP425TooEarlyError', 'HTTP426UpgradeRequiredError', 'HTTP428PreconditionRequiredError', 'HTTP429TooManyRequestsError', 'HTTP431HeaderFieldsTooLargeError', 'HTTP451LegalReasonsError']
9599

96-
# %% ../nbs/03b_net.ipynb 16
100+
# %% ../nbs/03b_net.ipynb 17
97101
def urlopen(url, data=None, headers=None, timeout=None, **kwargs):
98102
"Like `urllib.request.urlopen`, but first `urlwrap` the `url`, and encode `data`"
99103
if kwargs and not data: data=kwargs
100104
if data is not None:
101105
if not isinstance(data, (str,bytes)): data = urlencode(data)
102106
if not isinstance(data, bytes): data = data.encode('ascii')
103-
return _opener.open(urlwrap(url, data=data, headers=headers), timeout=timeout)
107+
return urlopener().open(urlwrap(url, data=data, headers=headers), timeout=timeout)
104108

105-
# %% ../nbs/03b_net.ipynb 17
109+
# %% ../nbs/03b_net.ipynb 18
106110
def urlread(url, data=None, headers=None, decode=True, return_json=False, return_headers=False, timeout=None, **kwargs):
107111
"Retrieve `url`, using `data` dict or `kwargs` to `POST` if present"
108112
try:
@@ -115,13 +119,13 @@ def urlread(url, data=None, headers=None, decode=True, return_json=False, return
115119
if return_json: res = loads(res)
116120
return (res,dict(hdrs)) if return_headers else res
117121

118-
# %% ../nbs/03b_net.ipynb 18
122+
# %% ../nbs/03b_net.ipynb 19
119123
def urljson(url, data=None, timeout=None):
120124
"Retrieve `url` and decode json"
121125
res = urlread(url, data=data, timeout=timeout)
122126
return json.loads(res) if res else {}
123127

124-
# %% ../nbs/03b_net.ipynb 20
128+
# %% ../nbs/03b_net.ipynb 21
125129
def urlcheck(url, timeout=10):
126130
if not url: return True
127131
try:
@@ -130,12 +134,12 @@ def urlcheck(url, timeout=10):
130134
except socket.timeout: return False
131135
except InvalidURL: return False
132136

133-
# %% ../nbs/03b_net.ipynb 21
137+
# %% ../nbs/03b_net.ipynb 22
134138
def urlclean(url):
135139
"Remove fragment, params, and querystring from `url` if present"
136140
return urlunparse(urlparse(str(url))[:3]+('','',''))
137141

138-
# %% ../nbs/03b_net.ipynb 23
142+
# %% ../nbs/03b_net.ipynb 24
139143
def urlretrieve(url, filename=None, reporthook=None, data=None, timeout=None):
140144
"Same as `urllib.request.urlretrieve` but also works with `Request` objects"
141145
with contextlib.closing(urlopen(url, data, timeout=timeout)) as fp:
@@ -161,43 +165,43 @@ def urlretrieve(url, filename=None, reporthook=None, data=None, timeout=None):
161165
raise ContentTooShortError(f"retrieval incomplete: got only {read} out of {size} bytes", headers)
162166
return filename,headers
163167

164-
# %% ../nbs/03b_net.ipynb 24
168+
# %% ../nbs/03b_net.ipynb 25
165169
def urldest(url, dest=None):
166170
name = urlclean(Path(url).name)
167171
if dest is None: dest = name
168172
dest = Path(dest)
169173
return dest/name if dest.is_dir() else dest
170174

171-
# %% ../nbs/03b_net.ipynb 25
175+
# %% ../nbs/03b_net.ipynb 26
172176
def urlsave(url, dest=None, reporthook=None, timeout=None):
173177
"Retrieve `url` and save based on its name"
174178
dest = urldest(url, dest)
175179
dest.parent.mkdir(parents=True, exist_ok=True)
176180
nm,msg = urlretrieve(url, dest, reporthook, timeout=timeout)
177181
return nm
178182

179-
# %% ../nbs/03b_net.ipynb 27
183+
# %% ../nbs/03b_net.ipynb 28
180184
def urlvalid(x):
181185
"Test if `x` is a valid URL"
182186
return all (getattrs(urlparse(str(x)), 'scheme', 'netloc'))
183187

184-
# %% ../nbs/03b_net.ipynb 29
188+
# %% ../nbs/03b_net.ipynb 30
185189
def urlrequest(url, verb, headers=None, route=None, query=None, data=None, json_data=True):
186190
"`Request` for `url` with optional route params replaced by `route`, plus `query` string, and post `data`"
187191
if route: url = url.format(**route)
188192
if query: url += '?' + urlencode(query)
189193
if isinstance(data,dict): data = (json.dumps if json_data else urlencode)(data).encode('ascii')
190194
return Request(url, headers=headers or {}, data=data or None, method=verb.upper())
191195

192-
# %% ../nbs/03b_net.ipynb 32
196+
# %% ../nbs/03b_net.ipynb 33
193197
@patch
194198
def summary(self:Request, skip=None)->dict:
195199
"Summary containing full_url, headers, method, and data, removing `skip` from headers"
196200
res = L('full_url','method','data').map_dict(partial(getattr,self))
197201
res['headers'] = {k:v for k,v in self.headers.items() if k not in listify(skip)}
198202
return res
199203

200-
# %% ../nbs/03b_net.ipynb 34
204+
# %% ../nbs/03b_net.ipynb 35
201205
def urlsend(url, verb, headers=None, route=None, query=None, data=None, json_data=True,
202206
return_json=True, return_headers=False, debug=None):
203207
"Send request with `urlrequest`, converting result to json if `return_json`"
@@ -209,7 +213,7 @@ def urlsend(url, verb, headers=None, route=None, query=None, data=None, json_dat
209213

210214
return urlread(req, return_json=return_json, return_headers=return_headers)
211215

212-
# %% ../nbs/03b_net.ipynb 37
216+
# %% ../nbs/03b_net.ipynb 36
213217
def do_request(url, post=False, headers=None, **data):
214218
"Call GET or json-encoded POST on `url`, depending on `post`"
215219
if data:
@@ -219,13 +223,13 @@ def do_request(url, post=False, headers=None, **data):
219223
data = None
220224
return urljson(Request(url, headers=headers, data=data or None))
221225

222-
# %% ../nbs/03b_net.ipynb 38
226+
# %% ../nbs/03b_net.ipynb 37
223227
def _socket_det(port,host,dgram):
224228
if isinstance(port,int): family,addr = socket.AF_INET,(host or socket.gethostname(),port)
225229
else: family,addr = socket.AF_UNIX,port
226230
return family,addr,(socket.SOCK_STREAM,socket.SOCK_DGRAM)[dgram]
227231

228-
# %% ../nbs/03b_net.ipynb 39
232+
# %% ../nbs/03b_net.ipynb 38
229233
def start_server(port, host=None, dgram=False, reuse_addr=True, n_queue=None):
230234
"Create a `socket` server on `port`, with optional `host`, of type `dgram`"
231235
listen_args = [n_queue] if n_queue else []
@@ -239,7 +243,7 @@ def start_server(port, host=None, dgram=False, reuse_addr=True, n_queue=None):
239243
s.listen(*listen_args)
240244
return s
241245

242-
# %% ../nbs/03b_net.ipynb 41
246+
# %% ../nbs/03b_net.ipynb 40
243247
def start_client(port, host=None, dgram=False):
244248
"Create a `socket` client on `port`, with optional `host`, of type `dgram`"
245249
family,addr,typ = _socket_det(port,host,dgram)

nbs/03b_net.ipynb

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,21 @@
182182
{
183183
"data": {
184184
"text/markdown": [
185-
"<h4 id=\"HTTP4xxClientError\" class=\"doc_header\"><code>class</code> <code>HTTP4xxClientError</code><a href=\"\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
185+
"---\n",
186186
"\n",
187-
"> <code>HTTP4xxClientError</code>(**`url`**, **`code`**, **`msg`**, **`hdrs`**, **`fp`**) :: `HTTPError`\n",
187+
"### HTTP4xxClientError\n",
188+
"\n",
189+
"> HTTP4xxClientError (url, code, msg, hdrs, fp)\n",
188190
"\n",
189191
"Base class for client exceptions (code 4xx) from `url*` functions"
190192
],
191193
"text/plain": [
192-
"<IPython.core.display.Markdown object>"
194+
"<nbdev.showdoc.BasicMarkdownRenderer at 0x11baaacd0>"
193195
]
194196
},
197+
"execution_count": null,
195198
"metadata": {},
196-
"output_type": "display_data"
199+
"output_type": "execute_result"
197200
}
198201
],
199202
"source": [
@@ -208,18 +211,21 @@
208211
{
209212
"data": {
210213
"text/markdown": [
211-
"<h4 id=\"HTTP5xxServerError\" class=\"doc_header\"><code>class</code> <code>HTTP5xxServerError</code><a href=\"\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
214+
"---\n",
212215
"\n",
213-
"> <code>HTTP5xxServerError</code>(**`url`**, **`code`**, **`msg`**, **`hdrs`**, **`fp`**) :: `HTTPError`\n",
216+
"### HTTP5xxServerError\n",
217+
"\n",
218+
"> HTTP5xxServerError (url, code, msg, hdrs, fp)\n",
214219
"\n",
215220
"Base class for server exceptions (code 5xx) from `url*` functions"
216221
],
217222
"text/plain": [
218-
"<IPython.core.display.Markdown object>"
223+
"<nbdev.showdoc.BasicMarkdownRenderer at 0x11bac3400>"
219224
]
220225
},
226+
"execution_count": null,
221227
"metadata": {},
222-
"output_type": "display_data"
228+
"output_type": "execute_result"
223229
}
224230
],
225231
"source": [
@@ -233,9 +239,20 @@
233239
"outputs": [],
234240
"source": [
235241
"#|export\n",
236-
"_opener = urllib.request.build_opener()\n",
237-
"_opener.addheaders = list(url_default_headers.items())\n",
238-
"install_opener(_opener)\n",
242+
"def urlopener():\n",
243+
" _opener = urllib.request.build_opener()\n",
244+
" _opener.addheaders = list(url_default_headers.items())\n",
245+
" return _opener"
246+
]
247+
},
248+
{
249+
"cell_type": "code",
250+
"execution_count": null,
251+
"metadata": {},
252+
"outputs": [],
253+
"source": [
254+
"#|export\n",
255+
"# install_opener(_opener)\n",
239256
"\n",
240257
"_httperrors = (\n",
241258
" (400,'Bad Request'),(401,'Unauthorized'),(402,'Payment Required'),(403,'Forbidden'),(404,'Not Found'),\n",
@@ -276,7 +293,7 @@
276293
" if data is not None:\n",
277294
" if not isinstance(data, (str,bytes)): data = urlencode(data)\n",
278295
" if not isinstance(data, bytes): data = data.encode('ascii')\n",
279-
" return _opener.open(urlwrap(url, data=data, headers=headers), timeout=timeout)"
296+
" return urlopener().open(urlwrap(url, data=data, headers=headers), timeout=timeout)"
280297
]
281298
},
282299
{
@@ -551,36 +568,6 @@
551568
" return urlread(req, return_json=return_json, return_headers=return_headers)"
552569
]
553570
},
554-
{
555-
"cell_type": "markdown",
556-
"metadata": {},
557-
"source": [
558-
"Test [ghapi.actions.download_artifact](https://docs.github.com/rest/reference/actions#download-an-artifact)"
559-
]
560-
},
561-
{
562-
"cell_type": "code",
563-
"execution_count": null,
564-
"metadata": {},
565-
"outputs": [],
566-
"source": [
567-
"\n",
568-
"url = \"https://github.com/fastai/ghapi/archive/refs/tags/0.1.15.zip\"\n",
569-
"header={'Accept': 'application/vnd.github.v3+json'}\n",
570-
"route={'artifact_id': 1234, 'archive_format': 'zip'} \n",
571-
"\n",
572-
"res = urlsend(url, \"get\", headers=header, route=route)\n",
573-
"assert type(res) is bytes\n",
574-
"\n",
575-
"def _org_urlsend(url, verb, headers=None, route=None, query=None, data=None, json_data=True,\n",
576-
" return_json=True, return_headers=False, debug=None):\n",
577-
" req = urlrequest(url, verb, headers, route=route, query=query, data=data, json_data=json_data)\n",
578-
" if debug: debug(req)\n",
579-
" return urlread(req, return_json=return_json, return_headers=return_headers)\n",
580-
"\n",
581-
"test_fail(_org_urlsend, args=dict(url=url, verb=\"get\", headers=header, route=route), msg=\"'utf-8' codec can't decode\")"
582-
]
583-
},
584571
{
585572
"cell_type": "code",
586573
"execution_count": null,
@@ -665,30 +652,9 @@
665652
"cell_type": "code",
666653
"execution_count": null,
667654
"metadata": {},
668-
"outputs": [
669-
{
670-
"name": "stdout",
671-
"output_type": "stream",
672-
"text": [
673-
"Converted 00_test.ipynb.\n",
674-
"Converted 01_basics.ipynb.\n",
675-
"Converted 02_foundation.ipynb.\n",
676-
"Converted 03_xtras.ipynb.\n",
677-
"Converted 03a_parallel.ipynb.\n",
678-
"Converted 03b_net.ipynb.\n",
679-
"Converted 04_dispatch.ipynb.\n",
680-
"Converted 05_transform.ipynb.\n",
681-
"Converted 06_docments.ipynb.\n",
682-
"Converted 07_meta.ipynb.\n",
683-
"Converted 08_script.ipynb.\n",
684-
"Converted index.ipynb.\n",
685-
"Converted parallel_win.ipynb.\n"
686-
]
687-
}
688-
],
655+
"outputs": [],
689656
"source": [
690657
"#|hide\n",
691-
"#|eval: false\n",
692658
"from nbdev import nbdev_export\n",
693659
"nbdev_export()"
694660
]

settings.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author = Jeremy Howard and Sylvain Gugger
77
author_email = [email protected]
88
copyright = fast.ai
99
branch = master
10-
version = 1.5.11
10+
version = 1.5.12
1111
min_python = 3.7
1212
audience = Developers
1313
language = English
@@ -23,3 +23,4 @@ title = fastcore
2323
doc_host = https://fastcore.fast.ai
2424
doc_baseurl = /
2525
host = github
26+

0 commit comments

Comments
 (0)