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

Skip to content

Commit 3d400b7

Browse files
committed
Merged revisions 85497 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r85497 | antoine.pitrou | 2010-10-14 23:15:17 +0200 (jeu., 14 oct. 2010) | 3 lines Explicitly close some files (from issue #10093) ........
1 parent 51be0f4 commit 3d400b7

3 files changed

Lines changed: 11 additions & 5 deletions

File tree

Lib/base64.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ def main():
383383
if o == '-u': func = decode
384384
if o == '-t': test(); return
385385
if args and args[0] != '-':
386-
func(open(args[0], 'rb'), sys.stdout.buffer)
386+
with open(args[0], 'rb') as f:
387+
func(f, sys.stdout.buffer)
387388
else:
388389
func(sys.stdin.buffer, sys.stdout.buffer)
389390

Lib/mimetypes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,8 @@ def read(self, filename, strict=True):
193193
list of standard types, else to the list of non-standard
194194
types.
195195
"""
196-
fp = open(filename)
197-
self.readfp(fp, strict)
198-
fp.close()
196+
with open(filename) as fp:
197+
self.readfp(fp, strict)
199198

200199
def readfp(self, fp, strict=True):
201200
"""
@@ -302,7 +301,7 @@ def init(files=None):
302301
files = knownfiles
303302
for file in files:
304303
if os.path.isfile(file):
305-
db.readfp(open(file))
304+
db.read(file)
306305
encodings_map = db.encodings_map
307306
suffix_map = db.suffix_map
308307
types_map = db.types_map[True]

Python/traceback.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
209209
PyObject *binary;
210210
PyObject *fob = NULL;
211211
PyObject *lineobj = NULL;
212+
PyObject *res;
212213
char buf[MAXPATHLEN+1];
213214
Py_UNICODE *u, *p;
214215
Py_ssize_t len;
@@ -254,6 +255,11 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
254255
break;
255256
}
256257
}
258+
res = PyObject_CallMethod(fob, "close", "");
259+
if (res)
260+
Py_DECREF(res);
261+
else
262+
PyErr_Clear();
257263
Py_DECREF(fob);
258264
if (!lineobj || !PyUnicode_Check(lineobj)) {
259265
Py_XDECREF(lineobj);

0 commit comments

Comments
 (0)