upip: url_open: Reworking error handling to guaranteedly close socket.

pull/68/merge
Paul Sokolovsky 2017-05-05 03:25:07 +03:00
rodzic 024d6bc2b1
commit 91d9c168b8
1 zmienionych plików z 28 dodań i 26 usunięć

Wyświetl plik

@ -117,6 +117,7 @@ def url_open(url):
addr = ai[0][4] addr = ai[0][4]
s = usocket.socket(ai[0][0]) s = usocket.socket(ai[0][0])
try:
#print("Connect address:", addr) #print("Connect address:", addr)
s.connect(addr) s.connect(addr)
@ -131,18 +132,18 @@ def url_open(url):
l = s.readline() l = s.readline()
protover, status, msg = l.split(None, 2) protover, status, msg = l.split(None, 2)
if status != b"200": if status != b"200":
s.close()
exc = ValueError(status)
if status == b"404" or status == b"301": if status == b"404" or status == b"301":
fatal("Package not found", exc) raise NotFoundError("Package not found")
fatal("Unexpected error querying for package", exc) raise ValueError(status)
while 1: while 1:
l = s.readline() l = s.readline()
if not l: if not l:
s.close() raise ValueError("Unexpected EOF in HTTP headers")
fatal("Unexpected EOF in HTTP headers", ValueError())
if l == b'\r\n': if l == b'\r\n':
break break
except Exception as e:
s.close()
raise e
return s return s
@ -215,9 +216,10 @@ def install(to_install, install_path=None):
if deps: if deps:
deps = deps.decode("utf-8").split("\n") deps = deps.decode("utf-8").split("\n")
to_install.extend(deps) to_install.extend(deps)
except NotFoundError: except Exception as e:
print("Error: cannot find '%s' package (or server error), packages may be partially installed" \ print("Error installing '{}': {}, packages may be partially installed".format(
% pkg_spec, file=sys.stderr) pkg_spec, e),
file=sys.stderr)
def get_install_path(): def get_install_path():
global install_path global install_path