[issue11563] test_urllibnet is triggering a ResourceWarning

Jeff McNeil report at bugs.python.org
Thu Mar 17 06:48:06 CET 2011


Jeff McNeil <jeff at jmcneil.net> added the comment:

So, I've been meaning to get more into contributing back to Python and I found this one somewhat interesting.

As it turns out, even the following simple script raises the same warning:


[jeff at martian cpython]$ ./python -c 'import urllib.request; urllib.request.urlretrieve("http://www.python.org")'
/home/jeff/cpython/Lib/socket.py:340: ResourceWarning: unclosed <socket.socket object, fd=3, family=2, type=1, proto=6>
  self._sock = None
[64388 refs]
[jeff at martian cpython]$ 

The close method of Socket.SocketIO simply sets the underlying socket object to None, which causes that warning.  Explicitly calling the close method on the underlying socket clears that up (and it's protected by that reference counter).

The _decref_socketios just drops the internal ref count and never actually closes -- it won't unless self.__closed is True. 

So, when self._sock is set to None, that error bubbles up. As SocketIO is the foundation used in socket.makefile, I think just adding that close call ought to be correct.

I can do the simple patch if you agree.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11563>
_______________________________________


More information about the Python-bugs-list mailing list