Ishan112 / pefile

Automatically exported from code.google.com/p/pefile
Other
0 stars 0 forks source link

file/mmap handler not closed on __parse__ success - query #33

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

What steps will reproduce the problem?
Run as follows (you will need notepad.exe in the same dir this is run from or 
any valid pe file (change filename appropriately)):

import pefile
import os
pe=pefile.PE('notepad.exe')
os.system("del notepad.exe")

What is the expected output? What do you see instead?
The following error is printed (by del) and the file is not deleted:
"The process cannot access the file because it is being used by another 
process."

What version of the product are you using? On what operating system?
pefile version: 1.2.10-114
Operating System: Windows XP Professional SP3

Please provide any additional information below.
This may not be an issue, it is similar the problem described in issue 26 but 
in this case it occurs when the pefile module does not except - i.e. 
__parse__() is successful - possibly this is as intended.

To avoid this issue I can call the pefile.PE close fn:
pe=pefile.PE('file.dll')
pe.close()
os.system("del file.dll")

Is this the expected way for pefile to function - i.e. should I be calling the 
pefile.PE close() fn in my script after calling pefile.PE to close the mmap 
handler?  I had a quick look at the pefile examples and don't see the close 
function being called.  Calling close() in my scripts seems a little misleading 
since it doesn't really close the pefile PE object as I'd expect since only the 
mmap handler is closed i.e. this works fine:

pe=pefile.PE('file.dll')
pe.close()
print pe.FileInfo

Alternatively I can add self.close() to the try block in pefile.PE init method:
        try:
            self.__parse__(name, data, fast_load)
            self.close()
        except:
            self.close()
            raise

I don't understand the impact of adding self.close() to the pefile.PE init 
method though i.e. will it affect the functionality of other pefile.PE 
functions that could normally be called after pefile.PE()?

I'd really appreciate your feedback on this before modifying our scripts.

Thank you very much

Original issue reported on code.google.com by robbinc...@gmail.com on 2 Dec 2011 at 4:29

GoogleCodeExporter commented 9 years ago
The problem with calling close() after the call to __parse__() is that it will 
close the mmap object if it exists. pefile uses mmap to be able to work with 
very large files when there's no need of loading it all in memory at once. 
Hence it will need the object to remain open.
I don't see an easy solution. Maybe wait until you've done all the processing 
you need to do with the PE file and then close it so you can delete it?

Original comment by ero.carr...@gmail.com on 8 Aug 2012 at 11:15