Fix Python 3 issues in utils

* LogAdapter.exception(): on Python 3, socket.error is an alias to
  OSError, so group both exceptions to support Python 3.
* ThreadPool: call GreenPipe() with indexed parameter, don't pass the
  third parameter by its name, since the parameter name changed
  between Python 2 (bufsize) and Python 3 (buffering).
* strip_value() in test_utils: StringIO.getvalue() now requires
  seek(0), otherwise the buffer is filled with null characters.
* test_lock_file(): on Python 3, seek(0) is now required to go the
  beginning of a file opened in append mode. In append mode, Python
  goes to the end of the file.

Change-Id: I4e56a51690f016a0a2e1354380ce11cff1891f64
This commit is contained in:
Victor Stinner 2015-10-19 17:16:21 +02:00
parent c49f71585b
commit d877a42237
2 changed files with 7 additions and 6 deletions
swift/common
test/unit/common

@ -1334,13 +1334,10 @@ class LogAdapter(logging.LoggerAdapter, object):
_junk, exc, _junk = sys.exc_info()
call = self.error
emsg = ''
if isinstance(exc, OSError):
if isinstance(exc, (OSError, socket.error)):
if exc.errno in (errno.EIO, errno.ENOSPC):
emsg = str(exc)
else:
call = self._exception
elif isinstance(exc, socket.error):
if exc.errno == errno.ECONNREFUSED:
elif exc.errno == errno.ECONNREFUSED:
emsg = _('Connection refused')
elif exc.errno == errno.EHOSTUNREACH:
emsg = _('Host unreachable')
@ -3010,7 +3007,7 @@ class ThreadPool(object):
# multiple instances instantiated. Since the object server uses one
# pool per disk, we have to reimplement this stuff.
_raw_rpipe, self.wpipe = os.pipe()
self.rpipe = greenio.GreenPipe(_raw_rpipe, 'rb', bufsize=0)
self.rpipe = greenio.GreenPipe(_raw_rpipe, 'rb')
for _junk in range(nthreads):
thr = stdlib_threading.Thread(

@ -1308,6 +1308,7 @@ class TestUtils(unittest.TestCase):
logger.logger.addHandler(handler)
def strip_value(sio):
sio.seek(0)
v = sio.getvalue()
sio.truncate(0)
return v
@ -1401,6 +1402,7 @@ class TestUtils(unittest.TestCase):
logger.logger.addHandler(handler)
def strip_value(sio):
sio.seek(0)
v = sio.getvalue()
sio.truncate(0)
return v
@ -1455,6 +1457,7 @@ class TestUtils(unittest.TestCase):
logger.logger.addHandler(handler)
def strip_value(sio):
sio.seek(0)
v = sio.getvalue()
sio.truncate(0)
return v
@ -2537,6 +2540,7 @@ cluster_dfw1 = http://dfw1.host/v1/
fcntl.LOCK_EX | fcntl.LOCK_NB)
with utils.lock_file(nt.name, unlink=False, append=True) as f:
f.seek(0)
self.assertEqual(f.read(), "test string")
f.seek(0)
f.write("\nanother string")