Skip to content

Commit 856b2a3

Browse files
committed
Import and str/unicode fixes
1 parent 6404fd0 commit 856b2a3

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

osg-test

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/env python
22

33
from __future__ import print_function
44
from distutils.sysconfig import get_python_lib
55
from optparse import OptionParser
6-
import ConfigParser
6+
try:
7+
import ConfigParser
8+
except ImportError:
9+
import configparser as ConfigParser
710
import os
811
import re
912
import signal

osgtest/library/core.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,8 @@ def __run_command(command, use_test_user, a_input, a_stdout, a_stderr, log_outpu
555555
os.killpg(p.pid, timeout_signal)
556556
os._exit(0)
557557

558-
(stdout, stderr) = p.communicate(a_input)
558+
stdout, stderr = p.communicate(to_bytes(a_input))
559+
stdout, stderr = to_str(stdout), to_str(stderr)
559560

560561
if timeout is not None:
561562
if p.returncode >= 0:
@@ -783,11 +784,23 @@ def run_fn_if_el_release_ok(*args, **kwargs):
783784
return el_release_decorator
784785

785786

786-
try:
787-
unicode
788-
except NameError: # python 3
789-
unicode = str
787+
def to_str(strlike, encoding="latin-1", errors="backslashreplace"):
788+
"""Turns a bytes into a str (Python 3) or a unicode to a str (Python 2)"""
789+
if strlike is None:
790+
return
791+
if not isinstance(strlike, str):
792+
if str is bytes:
793+
return strlike.encode(encoding, errors)
794+
else:
795+
return strlike.decode(encoding, errors)
796+
else:
797+
return strlike
790798

791799

792-
def is_string(var):
793-
return isinstance(var, (str, unicode))
800+
def to_bytes(strlike, encoding="latin-1", errors="backslashreplace"):
801+
"""Turns a str into bytes (Python 3) or a unicode to a str (Python 2)"""
802+
if strlike is None:
803+
return
804+
if not isinstance(strlike, bytes):
805+
return strlike.encode(encoding, errors)
806+
return strlike

osgtest/tests/test_460_stashcache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_03_http_fetch_from_cache(self):
7979
f = urlopen(
8080
"http://localhost:%d/%s" % (getcfg("CacheHTTPPort"), path)
8181
)
82-
result = f.read()
82+
result = core.to_str(f.read())
8383
except IOError as e:
8484
self.fail("Unable to download from cache via http: %s" % e)
8585
self.assertEqualVerbose(result, contents, "downloaded file mismatch")

osgtest/tests/test_470_rsv.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import pwd
99
import shutil
1010
import socket
11-
import ConfigParser
11+
try:
12+
import ConfigParser
13+
except ImportError:
14+
import configparser as ConfigParser
1215

1316
"""
1417
Testing number conventions:

osgtest/tests/test_550_condorce.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
import os
55
import re
6-
import urllib2
6+
try:
7+
from urllib2 import urlopen
8+
except ImportError:
9+
from urllib.request import urlopen
710

811
import osgtest.library.condor as condor
912
import osgtest.library.core as core
@@ -126,9 +129,9 @@ def test_07_ceview(self):
126129
core.skip_ok_unless_installed('htcondor-ce-view')
127130
view_url = 'http://%s:%s' % (core.get_hostname(), int(core.config['condor-ce.view-port']))
128131
try:
129-
src = urllib2.urlopen(view_url).read()
130-
except urllib2.URLError:
131-
self.fail('Could not reach HTCondor-CE View at %s' % view_url)
132+
src = to_str(urlopen(view_url).read())
133+
except EnvironmentError as err:
134+
self.fail('Could not reach HTCondor-CE View at %s: %s' % (view_url, err))
132135
self.assert_(re.search(r'HTCondor-CE Overview', src), 'Failed to find expected CE View contents')
133136
core.config['condor-ce.view-listening'] = True
134137

0 commit comments

Comments
 (0)