Refactoring. (#415)

This commit is contained in:
Felix Fontein 2022-07-03 13:28:11 +02:00 committed by GitHub
parent 623786c659
commit 9e57f29b3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 5 deletions

View File

@ -0,0 +1,2 @@
minor_changes:
- "socker_handler and socket_helper module utils - improve Python forward compatibilty, create helper functions for file blocking/unblocking (https://github.com/ansible-collections/community.docker/pull/415)."

View File

@ -10,7 +10,7 @@ import os.path
import socket as pysocket
from ansible.module_utils.basic import missing_required_lib
from ansible.module_utils.six import PY3
from ansible.module_utils.six import PY2
try:
from docker.utils import socket as docker_socket
@ -89,7 +89,7 @@ class DockerSocketHandlerBase(object):
return
else:
raise
elif PY3 and isinstance(self._sock, getattr(pysocket, 'SocketIO')):
elif not PY2 and isinstance(self._sock, getattr(pysocket, 'SocketIO')):
data = self._sock.read()
else:
data = os.read(self._sock.fileno())

View File

@ -10,7 +10,15 @@ import os
import os.path
import socket as pysocket
from ansible.module_utils.six import PY3
from ansible.module_utils.six import PY2
def make_file_unblocking(file):
fcntl.fcntl(file.fileno(), fcntl.F_SETFL, fcntl.fcntl(file.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK)
def make_file_blocking(file):
fcntl.fcntl(file.fileno(), fcntl.F_SETFL, fcntl.fcntl(file.fileno(), fcntl.F_GETFL) & ~os.O_NONBLOCK)
def make_unblocking(sock):
@ -19,7 +27,7 @@ def make_unblocking(sock):
elif hasattr(sock, 'setblocking'):
sock.setblocking(0)
else:
fcntl.fcntl(sock.fileno(), fcntl.F_SETFL, fcntl.fcntl(sock.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK)
make_file_unblocking(sock)
def _empty_writer(msg):
@ -36,7 +44,7 @@ def shutdown_writing(sock, log=_empty_writer):
# probably: "TypeError: shutdown() takes 1 positional argument but 2 were given"
log('Shutting down for writing not possible; trying shutdown instead: {0}'.format(e))
sock.shutdown()
elif PY3 and isinstance(sock, getattr(pysocket, 'SocketIO')):
elif not PY2 and isinstance(sock, getattr(pysocket, 'SocketIO')):
sock._sock.shutdown(pysocket.SHUT_WR)
else:
log('No idea how to signal end of writing')