Skip to content

Commit

Permalink
Py3 fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Sep 24, 2018
1 parent 094c9f4 commit 740e127
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/stampede/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@
from time import time, sleep

from .lock import FileLock
from .utils import IS_PY2

logger = getLogger(__name__)


def request(path, data, async=False):
logger.info("request(%r, %r, async=%s)", path, data, async)
if "\n" in data or "\r" in data:
if b"\n" in data or b"\r" in data:
raise RuntimeError("Request data must not have line endings!")
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
with closing(sock):
sock.settimeout(None)
sock.connect("%s.sock" % path)
fh = sock.makefile(bufsize=0)
fh.write("%s\n" % data)
if IS_PY2:
fh = sock.makefile(bufsize=0)
else:
fh = sock.makefile("rwb", buffering=0)
fh.write(b"%s\n" % data)
if async:
return
line = fh.readline()
if not line.startswith("done"):
if not line.startswith(b"done"):
raise RuntimeError("Request failed: %r. Check the logs !" % line)
logger.info("request(%r, %r, async=%s) - DONE.", path, data, async)
return line
Expand Down
8 changes: 5 additions & 3 deletions src/stampede/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
logger = getLogger(__name__)

ProcessExit = namedtuple("ProcessExit", ["pid", "status"])
IS_PY2 = sys.version_info[0] == 2


def close(*fds):
Expand Down Expand Up @@ -44,8 +45,8 @@ def collect_sigchld(sigfd, closeok=False):
logger.critical("Can't read any more events from signalfd (it's closed).")
elif exc.errno not in (errno.EAGAIN, errno.EINTR):
raise exc

sys.exc_clear()
if IS_PY2:
sys.exc_clear()
break
else:
assert si.ssi_signo == signal.SIGCHLD
Expand Down Expand Up @@ -74,7 +75,8 @@ def wait_pid(pid=0, mode=os.WNOHANG):
continue
if exc.errno != errno.ECHILD:
raise
sys.exc_clear()
if IS_PY2:
sys.exc_clear()
break
else:
if not exit_pid:
Expand Down

0 comments on commit 740e127

Please sign in to comment.