[oe] [PATCH 1/3] gitkpkgv: Ensure files are closed

Mike Looijmans mike.looijmans at topic.nl
Fri Jun 10 06:06:09 UTC 2016


On 07-06-16 15:49, Richard Purdie wrote:
> On Tue, 2016-06-07 at 11:02 +0200, Mike Looijmans wrote:
>> Looks like regression in Python itself?
>>
>> In both Python 2 and 3, the file is closed properly if the file
>> object is not
>> being stored:
>>
>>   >>> import os
>>   >>> os.listdir('/proc/self/fd')
>> ['0', '1', '2', '3']
>>   >>> l=open('/proc/self/stat').readline()
>>   >>> os.listdir('/proc/self/fd')
>> ['0', '1', '2', '3']
>>   >>> f=open('/proc/self/stat')
>>   >>> os.listdir('/proc/self/fd')
>> ['0', '1', '2', '3', '4']
>>   >>>
>>
>>
>> (file descriptor "3" is the one being used to read the /proc/self/fd
>> directory, "4" is the one used for reading the stat file)
>>
>> The "with" construction should not be needed here. Something else is
>> causing
>> this (e.g. nested function definition or exception handler?).
>
> $ python2 -Wdefault -c "open('/bin/bash')"
> $ python3 -Wdefault -c "open('/bin/bash')"
> -c:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='/bin/bash' mode='r' encoding='UTF-8'>
>
> Admittedly its not an out the box warning but it is one that seems to
> be enabled under bitbake. Details in:
>
> https://bugs.python.org/issue10093
>
> but the gist of the issue is that relying on the garbage collector to
> close files is a cpython'ism and other implementations of python may
> not do this.
>
> So whilst "with" might not be strictly required, it is recommended.

Oh dear, looks like's there's been a change of sorts in the Python community 
and they're now adopting the Java stupidity of "the garbage collector doesn't 
actually work so you'll have to do all your own resource management".

These are reasons why projects are so reluctant to move from Python 2 to 3. 
Python 3 is just a different language.

Ah well, guess we'll have to live with that. I'm already getting used to 
embedded devices running 30MB of software to enable the flashlight...


Kind regards,

Mike Looijmans
System Expert

TOPIC Embedded Products
Eindhovenseweg 32-C, NL-5683 KH Best
Postbus 440, NL-5680 AK Best
Telefoon: +31 (0) 499 33 69 79
E-mail: mike.looijmans at topicproducts.com
Website: www.topicproducts.com

Please consider the environment before printing this e-mail








More information about the Openembedded-devel mailing list