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

Skip to content

File closing #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ def _is_writable_dir(p):
except TypeError: return False
try:
t = tempfile.TemporaryFile(dir=p)
t.write('1')
t.close()
try:
t.write('1')
finally:
t.close()
except OSError: return False
else: return True

Expand Down Expand Up @@ -698,21 +700,25 @@ def rc_params(fail_on_error=False):

cnt = 0
rc_temp = {}
for line in file(fname):
cnt += 1
strippedline = line.split('#',1)[0].strip()
if not strippedline: continue
tup = strippedline.split(':',1)
if len(tup) !=2:
warnings.warn('Illegal line #%d\n\t%s\n\tin file "%s"'%\
(cnt, line, fname))
continue
key, val = tup
key = key.strip()
val = val.strip()
if key in rc_temp:
warnings.warn('Duplicate key in file "%s", line #%d'%(fname,cnt))
rc_temp[key] = (val, line, cnt)
fd = open(fname)
try:
for line in fd:
cnt += 1
strippedline = line.split('#',1)[0].strip()
if not strippedline: continue
tup = strippedline.split(':',1)
if len(tup) !=2:
warnings.warn('Illegal line #%d\n\t%s\n\tin file "%s"'%\
(cnt, line, fname))
continue
key, val = tup
key = key.strip()
val = val.strip()
if key in rc_temp:
warnings.warn('Duplicate key in file "%s", line #%d'%(fname,cnt))
rc_temp[key] = (val, line, cnt)
finally:
fd.close()

ret = RcParams([ (key, default) for key, (default, converter) in \
defaultParams.iteritems() ])
Expand Down
7 changes: 5 additions & 2 deletions lib/matplotlib/afm.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,5 +500,8 @@ def get_vertical_stem_width(self):

for fname in os.listdir(pathname):
fh = file(os.path.join(pathname,fname))
afm = AFM(fh)
w,h = afm.string_width_height('John Hunter is the Man!')
try:
afm = AFM(fh)
w,h = afm.string_width_height('John Hunter is the Man!')
finally:
fh.close()
24 changes: 19 additions & 5 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def draw_markers(self, *kl, **kw):

def draw_path_collection(self, *kl, **kw):
return self._renderer.draw_path_collection(*kl, **kw)

def _update_methods(self):
#self.draw_path = self._renderer.draw_path # see below
#self.draw_markers = self._renderer.draw_markers
Expand Down Expand Up @@ -437,7 +437,14 @@ def print_raw(self, filename_or_obj, *args, **kwargs):
renderer.dpi = self.figure.dpi
if is_string_like(filename_or_obj):
filename_or_obj = file(filename_or_obj, 'wb')
renderer._renderer.write_rgba(filename_or_obj)
close = True
else:
close = False
try:
renderer._renderer.write_rgba(filename_or_obj)
finally:
if close:
filename_or_obj.close()
renderer.dpi = original_dpi
print_rgba = print_raw

Expand All @@ -448,9 +455,16 @@ def print_png(self, filename_or_obj, *args, **kwargs):
renderer.dpi = self.figure.dpi
if is_string_like(filename_or_obj):
filename_or_obj = file(filename_or_obj, 'wb')
_png.write_png(renderer._renderer.buffer_rgba(0, 0),
renderer.width, renderer.height,
filename_or_obj, self.figure.dpi)
close = True
else:
close = False
try:
_png.write_png(renderer._renderer.buffer_rgba(0, 0),
renderer.width, renderer.height,
filename_or_obj, self.figure.dpi)
finally:
if close:
filename_or_obj.close()
renderer.dpi = original_dpi

def print_to_buffer(self):
Expand Down
9 changes: 8 additions & 1 deletion lib/matplotlib/backends/backend_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,14 @@ def _save (self, fo, format, **kwargs):
filename = fo
if is_string_like(fo):
fo = open(fo, 'wb')
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
close = True
else:
close = False
try:
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
finally:
if close:
fo.close()
surface = cairo.SVGSurface (fo, width_in_points, height_in_points)
else:
warnings.warn ("unknown format: %s" % format)
Expand Down
53 changes: 29 additions & 24 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,10 @@ def writeFonts(self):

def _write_afm_font(self, filename):
fh = file(filename)
font = AFM(fh)
fh.close()
try:
font = AFM(fh)
finally:
fh.close()
fontname = font.get_fontname()
fontdict = { 'Type': Name('Font'),
'Subtype': Name('Type1'),
Expand Down Expand Up @@ -876,14 +878,13 @@ def embedTTFType42(font, characters, descriptor):
fontfileObject.id,
self.reserveObject('length of font stream'),
{'Length1': length1Object})
fontfile = open(filename, 'rb')
length1 = 0
while True:
data = fontfile.read(4096)
if not data: break
length1 += len(data)
self.currentstream.write(data)
fontfile.close()
with open(filename, 'rb') as fontfile:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Michael, this and every other "with" statement will fail in python2.4. Since 1.1.x is out last python2.4 compatible branch, we can't have any of these.

length1 = 0
while True:
data = fontfile.read(4096)
if not data: break
length1 += len(data)
self.currentstream.write(data)
self.endStream()
self.writeObject(length1Object, length1)

Expand Down Expand Up @@ -1831,9 +1832,11 @@ def _get_font_afm(self, prop):
font = self.afm_font_cache.get(filename)
if font is None:
fh = file(filename)
font = AFM(fh)
self.afm_font_cache[filename] = font
fh.close()
try:
font = AFM(fh)
self.afm_font_cache[filename] = font
finally:
fh.close()
self.afm_font_cache[key] = font
return font

Expand Down Expand Up @@ -2178,17 +2181,19 @@ def print_pdf(self, filename, **kwargs):
file = filename._file
else:
file = PdfFile(filename)
file.newPage(width, height)
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
renderer = MixedModeRenderer(self.figure,
width, height, image_dpi, RendererPdf(file, image_dpi),
bbox_inches_restore=_bbox_inches_restore)
self.figure.draw(renderer)
renderer.finalize()
if isinstance(filename, PdfPages): # finish off this page
file.endStream()
else: # we opened the file above; now finish it off
file.close()
try:
file.newPage(width, height)
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
renderer = MixedModeRenderer(self.figure,
width, height, image_dpi, RendererPdf(file, image_dpi),
bbox_inches_restore=_bbox_inches_restore)
self.figure.draw(renderer)
renderer.finalize()
finally:
if isinstance(filename, PdfPages): # finish off this page
file.endStream()
else: # we opened the file above; now finish it off
file.close()

class FigureManagerPdf(FigureManagerBase):
pass
Expand Down
Loading