wichert / lingua

Translation toolkit for Python
Other
45 stars 32 forks source link

Temp file can not be renamed on Windows #81

Closed kalbermattenm closed 7 years ago

kalbermattenm commented 7 years ago

I updated lingua from version 4.6 to 4.9 (Python 2.7 on Windows 7) and since then it is not working correctly anymore.

I always get this error:

WindowsError: [Error 32] The process cannot access the file beacause it is being used by another process.

Apparently, the tmpfileis not correctly closed and the system still thinks the file is being used, thus os.rename (https://github.com/wichert/lingua/blob/master/src/lingua/extract.py#L235) throws this error.

Moreover, just before os.rename I tried that:

f = open(tmpfile)
f.close()

And I get exactly the same error, but for the f = open(tmpfile) command...

wichert commented 7 years ago

Can you try doing os.close(fd) just before the rename? I think that will fix it. I don't have access to Windows myself, so I can't test that. The source should look like this:

      (fd, tmpfile) = tempfile.mkstemp(dir=os.path.dirname(filename), text=True)
      catalog.save(tmpfile)
      os.close(fd)
      os.rename(tmpfile, filename)
kalbermattenm commented 7 years ago

That did the trick, thanks ! I did not get that fd was the file... sorry....

Do you want me to submit a PR ?

wichert commented 7 years ago

I already have a commit ready here; so there is no need to submit a PR. Thanks for the offer though! I am currently traveling, but will try to get a new release out soon.

On 7 Oct 2016, at 18:19, Michael Kalbermatten notifications@github.com wrote:

That did the trick, thanks ! I did not get that fd was the file... sorry....

Do you want me to submit a PR ?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

kalbermattenm commented 7 years ago

OK thanks a lot for the fix and the future release ! Safe travels ;-)