Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 914973a

Browse files
committed
Added __repr__ to addbase class; delete more objects
1 parent d5b9ea1 commit 914973a

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

Lib/urlopen.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class URLopener:
4848
# Constructor
4949
def __init__(self):
5050
self.addheaders = []
51-
self.ftpcache = ftpcache
5251
self.tempfiles = []
52+
self.ftpcache = ftpcache
5353
# Undocumented feature: you can use a different
5454
# ftp cache by assigning to the .ftpcache member;
5555
# in case you want logically independent URL openers
@@ -84,9 +84,8 @@ def open(self, url):
8484
name = regsub.gsub('-', '_', name)
8585
if not hasattr(self, name):
8686
raise IOError, ('url error', 'unknown url type', type)
87-
meth = getattr(self, name)
8887
try:
89-
return meth(url)
88+
return getattr(self, name)(url)
9089
except socket.error, msg:
9190
raise IOError, ('socket error', msg)
9291

@@ -98,6 +97,7 @@ def retrieve(self, url):
9897
if not type or type == 'file':
9998
try:
10099
fp = self.open_local_file(url1)
100+
del fp
101101
return splithost(url1)[1], None
102102
except IOError, msg:
103103
pass
@@ -112,8 +112,8 @@ def retrieve(self, url):
112112
tfp.write(block)
113113
block = fp.read(bs)
114114
headers = fp.info()
115-
fp.close()
116-
tfp.close()
115+
del fp
116+
del tfp
117117
return tfn, headers
118118

119119
# Each method named open_<type> knows how to open that type of URL
@@ -258,14 +258,16 @@ def __init__(self, fp):
258258
self.readline = self.fp.readline
259259
self.readlines = self.fp.readlines
260260
self.fileno = self.fp.fileno
261+
def __repr__(self):
262+
return '<%s at %s whose fp = %s>' % (
263+
self.__class__.__name__, `id(self)`, `self.fp`)
261264
def __del__(self):
262265
self.close()
263266
def close(self):
264267
self.read = None
265268
self.readline = None
266269
self.readlines = None
267270
self.fileno = None
268-
self.fp.close()
269271
self.fp = None
270272

271273
# Class to add a close hook to an open file
@@ -363,7 +365,9 @@ def test():
363365
print '======'
364366
fp = open(fn, 'r')
365367
data = fp.read()
368+
del fp
366369
print regsub.gsub('\r', '', data)
370+
fn, h = None, None
367371
print '-'*40
368372
finally:
369373
_urlopener.cleanup()

0 commit comments

Comments
 (0)