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

Fix Python3 compatibility issues. #14

Merged
merged 1 commit into from May 8, 2017
Merged

Fix Python3 compatibility issues. #14

merged 1 commit into from May 8, 2017

Conversation

andy-slac
Copy link
Contributor

Fix for unicode/bytes when writing via low-level os.write call. Suppress
warning from ConfigParser which has deprecated one of the methods in
python3.

if not hasattr(script, 'read'):
script = open(script)
cleanup = script.close

# make tmpfile to store credentials and options
fd, fname = tempfile.mkstemp(text=True)
Copy link
Member

Choose a reason for hiding this comment

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

I am confused. If you are opening this file in text mode, why do you need to write bytes to it below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's os.write(), it only accepts bytes.

Copy link
Member

Choose a reason for hiding this comment

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

So why open it in text mode at all? Why is os.write being used rather than print?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it contains text? This should only matter for newline chars, I guess, not encoding. mkstemp() return OS-level file descriptor which cannot be used with print.

Copy link
Member

Choose a reason for hiding this comment

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

Ok. You win. I think the tail is wagging the dog a bit here. I'm not sure I understand why a special low level file descriptor is needed here rather than a boring Python-level file handle. There is no comment in the code explaining why that's necessary and it looks like the file is opened, text is written and the file is closed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I need both file (descriptor) and a name, and I don't care much if it is a descriptor or a true file object. I could switch to tempfile.NamedTemporaryFile if that makes things clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, should be better now

Fix for unicode/bytes when writing via low-level os.write call. Suppress
warning from ConfigParser which has deprecated one of the methods in
python3. Replace low-level file descriptor operations with file-like
object when writing options to a temporary file.
@andy-slac andy-slac merged commit 305c405 into master May 8, 2017
@ktlim ktlim deleted the tickets/DM-7237 branch August 25, 2018 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants