From 7bcdd924e983b43e075f142b6decd871049b4d69 Mon Sep 17 00:00:00 2001 From: seem Date: Sat, 10 Sep 2022 16:24:25 +1000 Subject: [PATCH 1/2] fix: error while handling http exceptions --- fastcore/_modidx.py | 1 + fastcore/net.py | 9 +++++++-- nbs/03b_net.ipynb | 9 +++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/fastcore/_modidx.py b/fastcore/_modidx.py index 64b25f9e..b976fe93 100644 --- a/fastcore/_modidx.py +++ b/fastcore/_modidx.py @@ -333,6 +333,7 @@ 'fastcore.net': { 'fastcore.net.HTTP4xxClientError': ('net.html#http4xxclienterror', 'fastcore/net.py'), 'fastcore.net.HTTP5xxServerError': ('net.html#http5xxservererror', 'fastcore/net.py'), 'fastcore.net.Request.summary': ('net.html#request.summary', 'fastcore/net.py'), + 'fastcore.net._mk_error': ('net.html#_mk_error', 'fastcore/net.py'), 'fastcore.net._socket_det': ('net.html#_socket_det', 'fastcore/net.py'), 'fastcore.net.do_request': ('net.html#do_request', 'fastcore/net.py'), 'fastcore.net.start_client': ('net.html#start_client', 'fastcore/net.py'), diff --git a/fastcore/net.py b/fastcore/net.py index 2b56ab19..afb36579 100644 --- a/fastcore/net.py +++ b/fastcore/net.py @@ -89,10 +89,15 @@ def urlopener(): (431,'Header Fields Too Large'),(451,'Legal Reasons') ) +def _mk_error(nm, code, msg): + cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code) + def _init(self, url, hdrs, fp, msg=msg, code=code): super(cls, self).__init__(url, code, msg, hdrs, fp) + cls.__init__ = _init + return cls + for code,msg in _httperrors: nm = f'HTTP{code}{msg.replace(" ","")}Error' - cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code) - globals()[nm] = ExceptionsHTTP[code] = cls + globals()[nm] = ExceptionsHTTP[code] = _mk_error(nm, code, msg) # %% ../nbs/03b_net.ipynb 16 _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'] diff --git a/nbs/03b_net.ipynb b/nbs/03b_net.ipynb index abdb40de..6d6f8202 100644 --- a/nbs/03b_net.ipynb +++ b/nbs/03b_net.ipynb @@ -284,10 +284,15 @@ " (431,'Header Fields Too Large'),(451,'Legal Reasons')\n", ")\n", "\n", + "def _mk_error(nm, code, msg):\n", + " cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code)\n", + " def _init(self, url, hdrs, fp, msg=msg, code=code): super(cls, self).__init__(url, code, msg, hdrs, fp)\n", + " cls.__init__ = _init\n", + " return cls\n", + "\n", "for code,msg in _httperrors:\n", " nm = f'HTTP{code}{msg.replace(\" \",\"\")}Error'\n", - " cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code)\n", - " globals()[nm] = ExceptionsHTTP[code] = cls" + " globals()[nm] = ExceptionsHTTP[code] = _mk_error(nm, code, msg)" ] }, { From 8fdfd817c34a9711e68c3faeccf7bd1192036def Mon Sep 17 00:00:00 2001 From: seem Date: Mon, 12 Sep 2022 08:49:13 +1000 Subject: [PATCH 2/2] simplify `HTTP4xxClientError` subclasses --- fastcore/_modidx.py | 1 - fastcore/net.py | 10 +++------- nbs/03b_net.ipynb | 18 +++++++----------- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/fastcore/_modidx.py b/fastcore/_modidx.py index b976fe93..64b25f9e 100644 --- a/fastcore/_modidx.py +++ b/fastcore/_modidx.py @@ -333,7 +333,6 @@ 'fastcore.net': { 'fastcore.net.HTTP4xxClientError': ('net.html#http4xxclienterror', 'fastcore/net.py'), 'fastcore.net.HTTP5xxServerError': ('net.html#http5xxservererror', 'fastcore/net.py'), 'fastcore.net.Request.summary': ('net.html#request.summary', 'fastcore/net.py'), - 'fastcore.net._mk_error': ('net.html#_mk_error', 'fastcore/net.py'), 'fastcore.net._socket_det': ('net.html#_socket_det', 'fastcore/net.py'), 'fastcore.net.do_request': ('net.html#do_request', 'fastcore/net.py'), 'fastcore.net.start_client': ('net.html#start_client', 'fastcore/net.py'), diff --git a/fastcore/net.py b/fastcore/net.py index afb36579..f0c0edcd 100644 --- a/fastcore/net.py +++ b/fastcore/net.py @@ -89,15 +89,11 @@ def urlopener(): (431,'Header Fields Too Large'),(451,'Legal Reasons') ) -def _mk_error(nm, code, msg): - cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code) - def _init(self, url, hdrs, fp, msg=msg, code=code): super(cls, self).__init__(url, code, msg, hdrs, fp) - cls.__init__ = _init - return cls - for code,msg in _httperrors: nm = f'HTTP{code}{msg.replace(" ","")}Error' - globals()[nm] = ExceptionsHTTP[code] = _mk_error(nm, code, msg) + def _init(self, url, hdrs, fp, msg=msg, code=code): HTTP4xxClientError.__init__(self, url, code, msg, hdrs, fp) + cls = type(nm, (HTTP4xxClientError,), {'__init__':_init}) + globals()[nm] = ExceptionsHTTP[code] = cls # %% ../nbs/03b_net.ipynb 16 _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'] diff --git a/nbs/03b_net.ipynb b/nbs/03b_net.ipynb index 6d6f8202..8696723a 100644 --- a/nbs/03b_net.ipynb +++ b/nbs/03b_net.ipynb @@ -184,7 +184,7 @@ "text/markdown": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/net.py#L63){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/net.py#L64){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### HTTP4xxClientError\n", "\n", @@ -195,7 +195,7 @@ "text/plain": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/net.py#L63){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/net.py#L64){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### HTTP4xxClientError\n", "\n", @@ -223,7 +223,7 @@ "text/markdown": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/net.py#L68){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/net.py#L69){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### HTTP5xxServerError\n", "\n", @@ -234,7 +234,7 @@ "text/plain": [ "---\n", "\n", - "[source](https://github.com/fastai/fastcore/blob/master/fastcore/net.py#L68){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", + "[source](https://github.com/fastai/fastcore/blob/master/fastcore/net.py#L69){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "#### HTTP5xxServerError\n", "\n", @@ -284,15 +284,11 @@ " (431,'Header Fields Too Large'),(451,'Legal Reasons')\n", ")\n", "\n", - "def _mk_error(nm, code, msg):\n", - " cls = get_class(nm, 'url', 'hdrs', 'fp', sup=HTTP4xxClientError, msg=msg, code=code)\n", - " def _init(self, url, hdrs, fp, msg=msg, code=code): super(cls, self).__init__(url, code, msg, hdrs, fp)\n", - " cls.__init__ = _init\n", - " return cls\n", - "\n", "for code,msg in _httperrors:\n", " nm = f'HTTP{code}{msg.replace(\" \",\"\")}Error'\n", - " globals()[nm] = ExceptionsHTTP[code] = _mk_error(nm, code, msg)" + " def _init(self, url, hdrs, fp, msg=msg, code=code): HTTP4xxClientError.__init__(self, url, code, msg, hdrs, fp)\n", + " cls = type(nm, (HTTP4xxClientError,), {'__init__':_init})\n", + " globals()[nm] = ExceptionsHTTP[code] = cls" ] }, {