Skip to content

Commit

Permalink
python: Start fixing some Python 3 issues.
Browse files Browse the repository at this point in the history
This patch fixes just the Python 3 problems found by running:

  python3 setup.py install

There are still many other issues to be fixed, but this is a start.

Signed-off-by: Terry Wilson <[email protected]>
[[email protected] resolved conflicts with current master]
Signed-off-by: Russell Bryant <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
otherwiseguy authored and russellb committed Jan 12, 2016
1 parent ab2a315 commit 3ab76c5
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 46 deletions.
31 changes: 16 additions & 15 deletions python/ovs/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,38 +132,38 @@ def _make_pidfile():
global file_handle

file_handle = open(tmpfile, "w")
except IOError, e:
except IOError as e:
_fatal("%s: create failed (%s)" % (tmpfile, e.strerror))

try:
s = os.fstat(file_handle.fileno())
except IOError, e:
except IOError as e:
_fatal("%s: fstat failed (%s)" % (tmpfile, e.strerror))

try:
file_handle.write("%s\n" % pid)
file_handle.flush()
except OSError, e:
except OSError as e:
_fatal("%s: write failed: %s" % (tmpfile, e.strerror))

try:
fcntl.lockf(file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError, e:
except IOError as e:
_fatal("%s: fcntl failed: %s" % (tmpfile, e.strerror))

# Rename or link it to the correct name.
if _overwrite_pidfile:
try:
os.rename(tmpfile, _pidfile)
except OSError, e:
except OSError as e:
_fatal("failed to rename \"%s\" to \"%s\" (%s)"
% (tmpfile, _pidfile, e.strerror))
else:
while True:
try:
os.link(tmpfile, _pidfile)
error = 0
except OSError, e:
except OSError as e:
error = e.errno
if error == errno.EEXIST:
_check_already_running()
Expand Down Expand Up @@ -199,7 +199,7 @@ def _waitpid(pid, options):
while True:
try:
return os.waitpid(pid, options)
except OSError, e:
except OSError as e:
if e.errno == errno.EINTR:
pass
return -e.errno, 0
Expand All @@ -208,13 +208,13 @@ def _waitpid(pid, options):
def _fork_and_wait_for_startup():
try:
rfd, wfd = os.pipe()
except OSError, e:
except OSError as e:
sys.stderr.write("pipe failed: %s\n" % os.strerror(e.errno))
sys.exit(1)

try:
pid = os.fork()
except OSError, e:
except OSError as e:
sys.stderr.write("could not fork: %s\n" % os.strerror(e.errno))
sys.exit(1)

Expand All @@ -226,7 +226,7 @@ def _fork_and_wait_for_startup():
try:
s = os.read(rfd, 1)
error = 0
except OSError, e:
except OSError as e:
s = ""
error = e.errno
if error != errno.EINTR:
Expand Down Expand Up @@ -313,7 +313,8 @@ def _monitor_daemon(daemon_pid):
wakeup = last_restart + 10000
if now > wakeup:
break
print "sleep %f" % ((wakeup - now) / 1000.0)
sys.stdout.write("sleep %f\n" % (
(wakeup - now) / 1000.0))
time.sleep((wakeup - now) / 1000.0)
last_restart = ovs.timeval.msec()

Expand Down Expand Up @@ -404,7 +405,7 @@ def __read_pidfile(pidfile, delete_if_stale):

try:
file_handle = open(pidfile, "r+")
except IOError, e:
except IOError as e:
if e.errno == errno.ENOENT and delete_if_stale:
return 0
vlog.warn("%s: open: %s" % (pidfile, e.strerror))
Expand Down Expand Up @@ -437,15 +438,15 @@ def __read_pidfile(pidfile, delete_if_stale):
# We won the right to delete the stale pidfile.
try:
os.unlink(pidfile)
except IOError, e:
except IOError as e:
vlog.warn("%s: failed to delete stale pidfile (%s)"
% (pidfile, e.strerror))
return -e.errno
else:
vlog.dbg("%s: deleted stale pidfile" % pidfile)
file_handle.close()
return 0
except IOError, e:
except IOError as e:
if e.errno not in [errno.EACCES, errno.EAGAIN]:
vlog.warn("%s: fcntl: %s" % (pidfile, e.strerror))
return -e.errno
Expand All @@ -454,7 +455,7 @@ def __read_pidfile(pidfile, delete_if_stale):
try:
try:
error = int(file_handle.readline())
except IOError, e:
except IOError as e:
vlog.warn("%s: read: %s" % (pidfile, e.strerror))
error = -e.errno
except ValueError:
Expand Down
10 changes: 5 additions & 5 deletions python/ovs/db/idl.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def run(self):
self._monitor_request_id = None
self.__clear()
self.__parse_update(msg.result)
except error.Error, e:
except error.Error as e:
vlog.err("%s: parse error in received schema: %s"
% (self._session.get_name(), e))
self.__error()
Expand Down Expand Up @@ -354,7 +354,7 @@ def __send_monitor_request(self):
def __parse_update(self, update):
try:
self.__do_parse_update(update)
except error.Error, e:
except error.Error as e:
vlog.err("%s: error parsing update: %s"
% (self._session.get_name(), e))

Expand Down Expand Up @@ -451,7 +451,7 @@ def __row_update(self, table, row, row_json):

try:
datum = ovs.db.data.Datum.from_json(column.type, datum_json)
except error.Error, e:
except error.Error as e:
# XXX rate-limit
vlog.warn("error parsing column %s in table %s: %s"
% (column_name, table.name, e))
Expand Down Expand Up @@ -600,7 +600,7 @@ def __setattr__(self, column_name, value):
try:
datum = ovs.db.data.Datum.from_python(column.type, value,
_row_to_uuid)
except error.Error, e:
except error.Error as e:
# XXX rate-limit
vlog.err("attempting to write bad value to column %s (%s)"
% (column_name, e))
Expand All @@ -619,7 +619,7 @@ def from_json(cls, idl, table, uuid, row_json):
continue
try:
datum = ovs.db.data.Datum.from_json(column.type, datum_json)
except error.Error, e:
except error.Error as e:
# XXX rate-limit
vlog.warn("error parsing column %s in table %s: %s"
% (column_name, table.name, e))
Expand Down
2 changes: 1 addition & 1 deletion python/ovs/fatal_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _unlink(file_):
try:
os.unlink(file_)
return 0
except OSError, e:
except OSError as e:
return e.errno


Expand Down
2 changes: 1 addition & 1 deletion python/ovs/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def from_file(name):
def from_string(s):
try:
s = unicode(s, 'utf-8')
except UnicodeDecodeError, e:
except UnicodeDecodeError as e:
seq = ' '.join(["0x%2x" % ord(c)
for c in e.object[e.start:e.end] if ord(c) >= 0x80])
return ("not a valid UTF-8 string: invalid UTF-8 sequence %s" % seq)
Expand Down
2 changes: 1 addition & 1 deletion python/ovs/ovsuuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def from_json(json, symtab=None):
if not uuidRE.match(s):
raise error.Error("\"%s\" is not a valid UUID" % s, json)
return uuid.UUID(s)
except error.Error, e:
except error.Error as e:
if not symtab:
raise e
try:
Expand Down
2 changes: 1 addition & 1 deletion python/ovs/poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def block(self):
try:
events = self.poll.poll(self.timeout)
self.__log_wakeup(events)
except select.error, e:
except select.error as e:
# XXX rate-limit
error, msg = e
if error != errno.EINTR:
Expand Down
32 changes: 16 additions & 16 deletions python/ovs/socket_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def make_short_name(long_name):
os.symlink(long_dirname, link_name)
ovs.fatal_signal.add_file_to_unlink(link_name)
return os.path.join(link_name, os.path.basename(long_name))
except OSError, e:
except OSError as e:
if e.errno != errno.EEXIST:
break
raise Exception("Failed to create temporary symlink")
Expand All @@ -64,7 +64,7 @@ def make_unix_socket(style, nonblock, bind_path, connect_path, short=False):

try:
sock = socket.socket(socket.AF_UNIX, style)
except socket.error, e:
except socket.error as e:
return get_exception_errno(e), None

try:
Expand All @@ -74,7 +74,7 @@ def make_unix_socket(style, nonblock, bind_path, connect_path, short=False):
# Delete bind_path but ignore ENOENT.
try:
os.unlink(bind_path)
except OSError, e:
except OSError as e:
if e.errno != errno.ENOENT:
return e.errno, None

Expand All @@ -83,19 +83,19 @@ def make_unix_socket(style, nonblock, bind_path, connect_path, short=False):

try:
if sys.hexversion >= 0x02060000:
os.fchmod(sock.fileno(), 0700)
os.fchmod(sock.fileno(), 0o700)
else:
os.chmod("/dev/fd/%d" % sock.fileno(), 0700)
except OSError, e:
os.chmod("/dev/fd/%d" % sock.fileno(), 0o700)
except OSError as e:
pass
if connect_path is not None:
try:
sock.connect(connect_path)
except socket.error, e:
except socket.error as e:
if get_exception_errno(e) != errno.EINPROGRESS:
raise
return 0, sock
except socket.error, e:
except socket.error as e:
sock.close()
if (bind_path is not None and
os.path.exists(bind_path)):
Expand Down Expand Up @@ -124,7 +124,7 @@ def make_unix_socket(style, nonblock, bind_path, connect_path, short=False):
basename = os.path.basename(bind_path)
try:
bind_dirfd = os.open(dirname, os.O_DIRECTORY | os.O_RDONLY)
except OSError, err:
except OSError as err:
return get_exception_errno(err), None
short_bind_path = "/proc/self/fd/%d/%s" % (bind_dirfd,
basename)
Expand Down Expand Up @@ -172,7 +172,7 @@ def check_connection_completion(sock):
# XXX rate-limit
vlog.err("poll return POLLERR but send succeeded")
return errno.EPROTO
except socket.error, e:
except socket.error as e:
return get_exception_errno(e)
else:
return 0
Expand Down Expand Up @@ -220,19 +220,19 @@ def inet_open_active(style, target, default_port, dscp):
else:
sock = socket.socket(socket.AF_INET6, style, 0)
family = socket.AF_INET6
except socket.error, e:
except socket.error as e:
return get_exception_errno(e), None

try:
set_nonblocking(sock)
set_dscp(sock, family, dscp)
try:
sock.connect(address)
except socket.error, e:
except socket.error as e:
if get_exception_errno(e) != errno.EINPROGRESS:
raise
return 0, sock
except socket.error, e:
except socket.error as e:
sock.close()
return get_exception_errno(e), None

Expand All @@ -259,7 +259,7 @@ def get_null_fd():
if null_fd < 0:
try:
null_fd = os.open("/dev/null", os.O_RDWR)
except OSError, e:
except OSError as e:
vlog.err("could not open /dev/null: %s" % os.strerror(e.errno))
return -e.errno
return null_fd
Expand All @@ -285,14 +285,14 @@ def write_fully(fd, buf):
else:
bytes_written += retval
buf = buf[:retval]
except OSError, e:
except OSError as e:
return e.errno, bytes_written


def set_nonblocking(sock):
try:
sock.setblocking(0)
except socket.error, e:
except socket.error as e:
vlog.err("could not set nonblocking mode on socket: %s"
% os.strerror(get_exception_errno(e)))

Expand Down
12 changes: 7 additions & 5 deletions python/ovs/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,16 @@ def _open(suffix, dscp):
raise NotImplementedError("This method must be overrided by subclass")

@staticmethod
def open_block((error, stream)):
def open_block(error_stream):
"""Blocks until a Stream completes its connection attempt, either
succeeding or failing. (error, stream) should be the tuple returned by
Stream.open(). Returns a tuple of the same form.
Typical usage:
error, stream = Stream.open_block(Stream.open("unix:/tmp/socket"))"""

# Py3 doesn't support tuple parameter unpacking - PEP 3113
error, stream = error_stream
if not error:
while True:
error = stream.connect()
Expand Down Expand Up @@ -198,7 +200,7 @@ def recv(self, n):

try:
return (0, self.socket.recv(n))
except socket.error, e:
except socket.error as e:
return (ovs.socket_util.get_exception_errno(e), "")

def send(self, buf):
Expand All @@ -220,7 +222,7 @@ def send(self, buf):

try:
return self.socket.send(buf)
except socket.error, e:
except socket.error as e:
return -ovs.socket_util.get_exception_errno(e)

def run(self):
Expand Down Expand Up @@ -293,7 +295,7 @@ def open(name):

try:
sock.listen(10)
except socket.error, e:
except socket.error as e:
vlog.err("%s: listen: %s" % (name, os.strerror(e.error)))
sock.close()
return e.error, None
Expand Down Expand Up @@ -321,7 +323,7 @@ def accept(self):
sock, addr = self.socket.accept()
ovs.socket_util.set_nonblocking(sock)
return 0, Stream(sock, "unix:%s" % addr, 0)
except socket.error, e:
except socket.error as e:
error = ovs.socket_util.get_exception_errno(e)
if error != errno.EAGAIN:
# XXX rate-limit
Expand Down
2 changes: 1 addition & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

try:
# Try to set the version from the generated ovs/version.py
execfile("ovs/version.py")
exec(open("ovs/version.py").read())
except IOError:
print("Ensure version.py is created by running make python/ovs/version.py",
file=sys.stderr)
Expand Down

0 comments on commit 3ab76c5

Please sign in to comment.