Skip to content

Commit e67b53a

Browse files
author
spirit
committed
USE_URLOPEN_RESOURCE_WARNING_FIX
1 parent 1f9acbb commit e67b53a

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎language_tool/__init__.py‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import urllib.request
3030
import warnings
3131
from collections import namedtuple
32-
from contextlib import closing
3332
from functools import total_ordering
3433
from weakref import WeakValueDictionary
3534
try:
@@ -49,6 +48,8 @@
4948
FAILSAFE_LANGUAGE = "en"
5049
LANGUAGE_RE = re.compile(r"^([a-z]{2,3})(?:[_-]([a-z]{2}))?$", re.I)
5150

51+
# http://mail.python.org/pipermail/python-dev/2011-July/112551.html
52+
USE_URLOPEN_RESOURCE_WARNING_FIX = (3, 1) < sys.version_info < (3, 4)
5253

5354
if os.name == "nt":
5455
startupinfo = subprocess.STARTUPINFO()
@@ -217,7 +218,7 @@ def _start_server(cls):
217218
params = {"language": FAILSAFE_LANGUAGE, "text": ""}
218219
data = urllib.parse.urlencode(params).encode()
219220
try:
220-
with closing(urllib.request.urlopen(cls.url, data, 15)) as f:
221+
with urlopen(cls.url, data, 15) as f:
221222
tree = ElementTree.parse(f)
222223
except (IOError, http.client.HTTPException) as e:
223224
raise ServerError("{}: {}".format(cls.url, e))
@@ -280,9 +281,7 @@ def _get_languages(cls):
280281
def _get_root(cls, url, data=None, num_tries=2):
281282
for n in range(num_tries):
282283
try:
283-
with closing(
284-
urllib.request.urlopen(url, data, cls.TIMEOUT)
285-
) as f:
284+
with urlopen(url, data, cls.TIMEOUT) as f:
286285
return ElementTree.parse(f).getroot()
287286
except IOError as e:
288287
if n + 1 < num_tries:
@@ -498,3 +497,34 @@ def terminate_server():
498497
"""
499498
if LanguageTool._server_is_alive():
500499
LanguageTool._terminate_server()
500+
501+
502+
if USE_URLOPEN_RESOURCE_WARNING_FIX:
503+
class ClosingHTTPResponse(http.client.HTTPResponse):
504+
def __init__(self, sock, *args, **kwargs):
505+
super().__init__(sock, *args, **kwargs)
506+
self.sock = sock
507+
508+
def __exit__(self, exc_type, exc_value, traceback):
509+
self.sock.close()
510+
return super().__exit__(exc_type, exc_value, traceback)
511+
512+
class ClosingHTTPConnection(http.client.HTTPConnection):
513+
response_class = ClosingHTTPResponse
514+
515+
class ClosingHTTPHandler(urllib.request.HTTPHandler):
516+
def http_open(self, req):
517+
return self.do_open(ClosingHTTPConnection, req)
518+
519+
urlopen = urllib.request.build_opener(ClosingHTTPHandler).open
520+
521+
else:
522+
try:
523+
urllib.response.addinfourl.__exit__
524+
except AttributeError:
525+
from contextlib import closing
526+
527+
def urlopen(*args, **kwargs):
528+
return closing(urllib.request.urlopen(*args, **kwargs))
529+
else:
530+
urlopen = urllib.request.urlopen

‎test.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/usr/bin/env python3
22
"""Test suite for language_tool
33
"""
4+
45
import unittest
5-
from collections import namedtuple
66
import warnings
7+
from collections import namedtuple
78

89
import language_tool
910
from language_tool.country_codes import get_country_code

0 commit comments

Comments
 (0)