[MERGE][Win32] explicit close of file in textfile

Andrew Bennetts andrew at canonical.com
Thu May 4 05:37:14 BST 2006


On Wed, May 03, 2006 at 08:55:20PM -0400, Aaron Bentley wrote:
> This patch fixes the binary file detection by explicitly closing the 
> file, which is needed on win32.
> 
> Aaron

> === modified file 'a/bzrlib/textfile.py'
> --- a/bzrlib/textfile.py	
> +++ b/bzrlib/textfile.py	
> @@ -46,4 +46,8 @@
>      """Check whether the supplied path is a text, not binary file.
>      Raise BinaryFile if a NUL occurs in the first 1024 bytes.
>      """
> -    text_file(open(path, 'rb'))
> +    try:
> +        f = open(path, 'rb')
> +        text_file(f)
> +    finally:
> +        f.close()
> 

I believe a better style is this:

    f = open(path, 'rb')
    try:
        text_file(f)
    finally:
        f.close()

Otherwise an exception raised during the "open(path, 'rb')" (perhaps a
permission error, or even a KeyboardInterrupt) will try to execute "f.close()"
when f is undefined.  This will cause an unnecessary NameError, and prevent the
original exception from propagating.

-Andrew.





More information about the bazaar mailing list