mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 19:31:26 -07:00
Fix for persistent connection plugin on Python3 (#24431)
Fix for persistent connection plugin on Python3. Note that fixes are also needed to each terminal plugin. This PR only fixes the ios terminal (as proof that this approach is workable.) Future PRs can address the other terminal types. * On Python3, pickle needs to work with byte strings, not text strings. * Set the pickle protocol version to 0 because we're using a pty to feed data to the connection plugin. A pty can't have control characters. So we have to send ascii only. That means only using protocol=0 for pickling the data. * ansible-connection isn't being used with py3 in the bug but it needs several changes to work with python3. * In python3, closing the pty too early causes no data to be sent. So leave stdin open until after we finish with the ansible-connection process. * Fix typo using traceback.format_exc() * Cleanup unnecessary StringIO, BytesIO, and to_bytes calls * Modify the network_cli and terminal plugins for py3 compat. Lots of mixing of text and byte strings that needs to be straightened out to be compatible with python3 * Documentation for the bytes<=>text strategy for terminal plugins * Update unittests for more bytes-oriented internals Fixes #24355
This commit is contained in:
parent
e539726543
commit
d834412ead
6 changed files with 146 additions and 112 deletions
|
@ -24,7 +24,7 @@ import subprocess
|
|||
import sys
|
||||
|
||||
from ansible.module_utils._text import to_bytes
|
||||
from ansible.module_utils.six.moves import cPickle, StringIO
|
||||
from ansible.module_utils.six.moves import cPickle
|
||||
from ansible.plugins.connection import ConnectionBase
|
||||
|
||||
try:
|
||||
|
@ -52,16 +52,20 @@ class Connection(ConnectionBase):
|
|||
stdin = os.fdopen(master, 'wb', 0)
|
||||
os.close(slave)
|
||||
|
||||
src = StringIO()
|
||||
cPickle.dump(self._play_context.serialize(), src)
|
||||
stdin.write(src.getvalue())
|
||||
src.close()
|
||||
# Need to force a protocol that is compatible with both py2 and py3.
|
||||
# That would be protocol=2 or less.
|
||||
# Also need to force a protocol that excludes certain control chars as
|
||||
# stdin in this case is a pty and control chars will cause problems.
|
||||
# that means only protocol=0 will work.
|
||||
src = cPickle.dumps(self._play_context.serialize(), protocol=0)
|
||||
stdin.write(src)
|
||||
|
||||
stdin.write(b'\n#END_INIT#\n')
|
||||
stdin.write(to_bytes(action))
|
||||
stdin.write(b'\n\n')
|
||||
stdin.close()
|
||||
|
||||
(stdout, stderr) = p.communicate()
|
||||
stdin.close()
|
||||
|
||||
return (p.returncode, stdout, stderr)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue