Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display ipc path in case of ENOENT exception #1301

Merged
merged 2 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions zmq/backend/cffi/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ def bind(self, address):
'characters (sizeof(sockaddr_un.sun_path)).'
.format(path, IPC_PATH_MAX_LEN))
raise ZMQError(C.zmq_errno(), msg=msg)
elif C.zmq_errno() == errno_mod.ENOENT:
# py3compat: address is bytes, but msg wants str
if str is unicode:
address = address.decode('utf-8', 'replace')
path = address.split('://', 1)[-1]
msg = ('No such file or directory for ipc path "{0}".'.format(
path))
raise ZMQError(C.zmq_errno(), msg=msg)
else:
_check_rc(rc)

Expand Down
10 changes: 9 additions & 1 deletion zmq/backend/cython/socket.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
cdef extern from "pyversion_compat.h":
pass

from libc.errno cimport ENAMETOOLONG, ENOTSOCK
from libc.errno cimport ENAMETOOLONG, ENOENT, ENOTSOCK
from libc.string cimport memcpy

from cpython cimport PyBytes_FromStringAndSize
Expand Down Expand Up @@ -542,6 +542,14 @@ cdef class Socket:
'to check addr length (if it is defined).'
.format(path, IPC_PATH_MAX_LEN))
raise ZMQError(msg=msg)
elif zmq_errno() == ENOENT:
# py3compat: address is bytes, but msg wants str
if str is unicode:
addr = addr.decode('utf-8', 'replace')
path = addr.split('://', 1)[-1]
msg = ('No such file or directory for ipc path "{0}".'.format(
path))
raise ZMQError(msg=msg)
while True:
try:
_check_rc(rc)
Expand Down
17 changes: 16 additions & 1 deletion zmq/tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Distributed under the terms of the Modified BSD License.

import copy
import errno
import json
import os
import platform
Expand All @@ -11,6 +12,7 @@
import socket
import sys

import pytest
from pytest import mark

import zmq
Expand Down Expand Up @@ -396,7 +398,20 @@ def test_ipc_path_max_length_msg(self):
s.bind('ipc://{0}'.format('a' * (zmq.IPC_PATH_MAX_LEN + 1)))
except zmq.ZMQError as e:
self.assertTrue(str(zmq.IPC_PATH_MAX_LEN) in e.strerror)


@mark.skipif(windows, reason="ipc not supported on Windows.")
def test_ipc_path_no_such_file_or_directory_message(self):
"""Display the ipc path in case of an ENOENT exception"""
s = self.context.socket(zmq.PUB)
self.sockets.append(s)
invalid_path = '/foo/bar'
with pytest.raises(zmq.ZMQError) as error:
s.bind('ipc://{0}'.format(invalid_path))
assert error.value.errno == errno.ENOENT
error_message = str(error.value)
assert invalid_path in error_message
assert "no such file or directory" in error_message.lower()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add one more assert that error.errno == ENOENT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

I pushed a fixup with that change.

Tell me if it is good for you and I will squash it. Or feel free to squash it yourself before merging. 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for that. Thanks!


def test_hwm(self):
zmq3 = zmq.zmq_version_info()[0] >= 3
for stype in (zmq.PUB, zmq.ROUTER, zmq.SUB, zmq.REQ, zmq.DEALER):
Expand Down