Skip to content

Commit 4e687ca

Browse files
fix: Fix for python2 support
1 parent a5e4df8 commit 4e687ca

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Pilot/proxyTools.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
try:
1414
IsADirectoryError # pylint: disable=used-before-assignment
1515
except NameError:
16-
IsADirectoryError = OSError
16+
IsADirectoryError = IOError
1717

1818
try:
1919
from urllib.parse import urlencode
2020
from urllib.request import Request, urlopen
2121
except ImportError:
2222
from urllib import urlencode
2323

24-
from urllib2 import urlopen
24+
from urllib2 import Request, urlopen
2525

2626

2727
VOMS_FQANS_OID = b"1.3.6.1.4.1.8005.100.100.4"
@@ -141,12 +141,22 @@ def executeRequest(self, raw_data, insecure=False):
141141
ctx.check_hostname = False
142142
ctx.verify_mode = ssl.CERT_NONE
143143

144-
with urlopen(request, context=ctx) as res:
145-
response_data = res.read().decode("utf-8") # Decode response bytes
144+
if sys.version_info[0] >= 3:
145+
# Python 3 code
146+
with urlopen(request, context=ctx) as res:
147+
response_data = res.read().decode("utf-8") # Decode response bytes
148+
else:
149+
# Python 2 code
150+
res = urlopen(request, context=ctx)
146151
try:
147-
return json.loads(response_data) # Parse JSON response
148-
except json.JSONDecodeError:
149-
raise Exception("Invalid JSON response: %s" % response_data)
152+
response_data = res.read().decode("utf-8") # Decode response bytes
153+
finally:
154+
res.close()
155+
156+
try:
157+
return json.loads(response_data) # Parse JSON response
158+
except ValueError: # In Python 2, json.JSONDecodeError is a subclass of ValueError
159+
raise Exception("Invalid JSON response: %s" % response_data)
150160

151161

152162
class TokenBasedRequest(BaseRequest):

0 commit comments

Comments
 (0)