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

Do not close given file #40

Merged
merged 4 commits into from
May 4, 2019

Conversation

christian-intra2net
Copy link
Contributor

Correct for excessive closing of file handles

When creating the last PR (#39), I closed too much. I did not take into account that closing an OleFileIO also closes the file handle it was given in the constructor. If the caller of OfficeFile wanted to continue working the the file handle (which is her right), she was presented with "ValueError: seek of closed file".

Correct this here and create unittest to prevent this from happening again.
And since we are looking at file handles: also seek to 0 before checking for isOleFile()

"file" is a open file handle, closing self.ole would close it. We should not
modify resource we are given in the constructor, caller might have different
plan with them.
@codecov-io
Copy link

codecov-io commented Apr 30, 2019

Codecov Report

Merging #40 into master will decrease coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #40      +/-   ##
==========================================
- Coverage   86.68%   86.64%   -0.04%     
==========================================
  Files          13       13              
  Lines        1179     1168      -11     
==========================================
- Hits         1022     1012      -10     
+ Misses        157      156       -1
Impacted Files Coverage Δ
msoffcrypto/format/ooxml.py 78.78% <ø> (-1.02%) ⬇️
msoffcrypto/__init__.py 79.16% <100%> (+0.9%) ⬆️
msoffcrypto/format/doc97.py 88.08% <100%> (+0.26%) ⬆️
msoffcrypto/format/xls97.py 87.8% <100%> (-0.15%) ⬇️
msoffcrypto/format/ppt97.py 98.88% <100%> (-0.01%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fa0cd48...e24d3ac. Read the comment docs.

"Thou shalst not close file handles that SHE, your caller, has given you!"

Ensure we comply with this dogma.
@nolze
Copy link
Owner

nolze commented May 4, 2019

Thanks for fixes and tests! Just merged.
By the way, your previous PR reminded me that there should have been OfficeFile.close().

Another possible way I'm considering as an option for the next major release is to redesign APIs boldly and make it more functional:

with open("sample.docx", "rb") as f:
    if msoffcrypto.is_encrypted(f):
        # msoffcrypto.test_key(f, password="1234")
        decrypted = msoffcrypto.decrypt(f, password="1234")

@nolze nolze removed the in progress label May 4, 2019
@christian-intra2net christian-intra2net deleted the do-not-close-given-file branch May 6, 2019 07:08
@christian-intra2net
Copy link
Contributor Author

christian-intra2net commented May 6, 2019

Thanks for the merge and sorry I implemented the excessive close in the first place.

For the OfficeFile.close() it would have to be a class, right?

Your api idea sounds good and feasible. However, I grew up with object oriented programming, so I would actually prefer if OfficeFile were a real class (not just a function that looks like a class). Then you could also easily make it a context manager that manages the file handle itself:

with OfficeFile("sample.docx") as ofile:
  if ofile.is_encrypted():
    ofile.test_key(password="1234")
    decrypted = ofile.decrypt(password="1234")

(However, it does not have to be a class to do that, a context manager could also be just another function)

@contextlib.contextmanager
def create_office_file(filename)
    with open(filename, 'rb') as file_handle:
       yield OfficeFile(file_handle)

with create_office_file("sample.docx") as ofile:
    ....

@nolze
Copy link
Owner

nolze commented May 27, 2019

A recent acknowledgement of the effort on Twitter. @christian-intra2net https://twitter.com/Malwageddon/status/1132006186008621060

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

3 participants