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

Automatus: close hanging tempfiles descriptors #9199

Merged
merged 1 commit into from
Jul 27, 2022

Conversation

matusmarhefka
Copy link
Member

We don't need those descriptors only their filenames,
similar as in ace8f69.

We don't need those descriptors only their filenames,
similar as in ace8f69.
@matusmarhefka matusmarhefka added the Test Suite Update in Test Suite. label Jul 21, 2022
@github-actions
Copy link

Start a new ephemeral environment with changes proposed in this pull request:

Fedora Environment
Open in Gitpod

Oracle Linux 8 Environment
Open in Gitpod

@matusmarhefka matusmarhefka added this to the 0.1.63 milestone Jul 21, 2022
@codeclimate
Copy link

codeclimate bot commented Jul 21, 2022

Code Climate has analyzed commit 14d46cd and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 42.7% (0.0% change).

View more on Code Climate.

@yuumasato yuumasato self-assigned this Jul 25, 2022
@@ -493,7 +493,8 @@ def copy_of_datastream(self, new_filename=None):
os.unlink(new_filename)

def _change_variable_value(self, varname, value):
_, xslt_filename = tempfile.mkstemp(prefix="xslt-change-value", dir="/tmp")
descriptor, xslt_filename = tempfile.mkstemp(prefix="xslt-change-value", dir="/tmp")
os.close(descriptor)
Copy link
Member

Choose a reason for hiding this comment

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

How about using the already open descriptor to write the template into it, instead of opening the file again in lines 499~500.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can, but mkstemp returns a low-level file descriptor (not file object which is expected by write() function), so we will need to use os.write and convert the string which we will be writing into the file to bytes.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh, okay, overlooked that detail.

Copy link
Member

@yuumasato yuumasato Jul 27, 2022

Choose a reason for hiding this comment

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

@matusmarhefka So, the file is opened with open() and in line 500 we do fp.write().
And the docs say that the descriptor should be the same as if an open() was done:
https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp

I have not tried, but I think it should work, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

mkstemp() returns a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order.

So not open, but os.open, which means only write from the os module will work (the last in example), but we would need to convert string to bytes:

>>> import os
>>> from tempfile import mkstemp
>>> fd, name = mkstemp()
>>> str = """test"""
>>> fd.write(str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'write'
>>> os.write(fd, str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
>>> os.write(fd, str.encode())
4

I don't have strong opinion, but I would rather stick to the original code as it doesn't require conversion to bytes.

Copy link
Member

Choose a reason for hiding this comment

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

I wasn't paying attention to difference between open() and os.open()

Copy link
Member Author

Choose a reason for hiding this comment

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

No problem, thanks for the review.

@yuumasato yuumasato merged commit d8a9429 into ComplianceAsCode:master Jul 27, 2022
@matusmarhefka matusmarhefka deleted the close_tempfiles branch July 27, 2022 16:02
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Test Suite Update in Test Suite.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants