-
Notifications
You must be signed in to change notification settings - Fork 23.7k
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
Fix crypttab python3 compatibility issue #30457
Fix crypttab python3 compatibility issue #30457
Conversation
shipit |
We want to do the opposite. open the file in binary mode and explicitly change the data to bytes. This gives us greater control over the encoding and what to do on errors. from ansible.module_utils._text import to_bytes
[...]
f = open(path, 'wb')
f.write(to_bytes(crypttab, errors='surrogate_or_strict')) |
@jpiron Is changing your PR as I mentioned above something you'll have the time to do? |
In python2 str gives byte string. In Python3 it gives unicode string so it can't be written in a binary mode opened file. Use to_bytes helper function to ensure content being written will be properly encoded in both python2 and python3.
8c45322
to
eb82dea
Compare
Indeed much better than my naïve fix :) |
In python2 str gives byte string. In Python3 it gives unicode string so it can't be written in a binary mode opened file. Use to_bytes helper function to ensure content being written will be properly encoded in both python2 and python3. (cherry picked from commit 54859a2)
Thanks @jpiron Merged to devel and cherry-picked to stable-2.4 for the 2.4.1 release. |
In python2 str gives byte string. In Python3 it gives unicode string so it can't be written in a binary mode opened file. Use to_bytes helper function to ensure content being written will be properly encoded in both python2 and python3.
In python2 str gives byte string. In Python3 it gives unicode string so it can't be written in a binary mode opened file. Use to_bytes helper function to ensure content being written will be properly encoded in both python2 and python3.
SUMMARY
The crypttab module doesn't work with Python3.
ISSUE TYPE
COMPONENT NAME
crypttab
ANSIBLE VERSION
ADDITIONAL INFORMATION
Here is how to reproduce:
Python2 working case:
Python3 not working case: