@@ -168,6 +168,12 @@ class _closedsocket(object):
168168 __slots__ = []
169169 def _dummy (* args ):
170170 raise error (EBADF , 'Bad file descriptor' )
171+
172+ # Pyston change: socket close: add refcounting similar to pypy approach
173+ def _reuse (self ):
174+ pass
175+ def _drop (self ):
176+ pass
171177 # All _delegate_methods must also be initialized here.
172178 send = recv = recv_into = sendto = recvfrom = recvfrom_into = _dummy
173179 __getattr__ = _dummy
@@ -176,6 +182,9 @@ def _dummy(*args):
176182# a platform-independent dup() functionality. The
177183# implementation currently relies on reference counting
178184# to close the underlying socket object.
185+ # Pyston change: socket close: we workaround the socket closing problem similar to pypy
186+ # by manually keeping track of the ref count.
187+ # When we switch to ref counting we should remove this changes!
179188class _socketobject (object ):
180189
181190 __doc__ = _realsocket .__doc__
@@ -185,13 +194,22 @@ class _socketobject(object):
185194 def __init__ (self , family = AF_INET , type = SOCK_STREAM , proto = 0 , _sock = None ):
186195 if _sock is None :
187196 _sock = _realsocket (family , type , proto )
197+
198+ # Pyston change: socket close: add refcounting similar to pypys approach
199+ else :
200+ _sock ._reuse ()
201+
188202 self ._sock = _sock
189203 for method in _delegate_methods :
190204 setattr (self , method , getattr (_sock , method ))
191205
192206 def close (self , _closedsocket = _closedsocket ,
193207 _delegate_methods = _delegate_methods , setattr = setattr ):
194208 # This function should not reference any globals. See issue #808164.
209+
210+ # Pyston change: socket close: add refcounting similar to pypys approach
211+ self ._sock ._drop ()
212+
195213 self ._sock = _closedsocket ()
196214 dummy = self ._sock ._dummy
197215 for method in _delegate_methods :
@@ -200,7 +218,13 @@ def close(self, _closedsocket=_closedsocket,
200218
201219 def accept (self ):
202220 sock , addr = self ._sock .accept ()
203- return _socketobject (_sock = sock ), addr
221+
222+ # Pyston change: socket close: add refcounting similar to pypys approach
223+ # return _socketobject(_sock=sock), addr
224+ sockcopy = _socketobject (_sock = sock )
225+ sock ._drop ()
226+ return sockcopy , addr
227+
204228 accept .__doc__ = _realsocket .accept .__doc__
205229
206230 def dup (self ):
@@ -245,6 +269,10 @@ class _fileobject(object):
245269
246270 def __init__ (self , sock , mode = 'rb' , bufsize = - 1 , close = False ):
247271 self ._sock = sock
272+
273+ # Pyston change: socket close: add refcounting similar to pypys approach
274+ sock ._reuse ()
275+
248276 self .mode = mode # Not actually used in this version
249277 if bufsize < 0 :
250278 bufsize = self .default_bufsize
@@ -280,6 +308,11 @@ def close(self):
280308 finally :
281309 if self ._close :
282310 self ._sock .close ()
311+
312+ # Pyston change: socket close: add refcounting similar to pypys approach
313+ else :
314+ self ._sock ._drop ()
315+
283316 self ._sock = None
284317
285318 def __del__ (self ):
0 commit comments