File tree 3 files changed +18
-7
lines changed
3 files changed +18
-7
lines changed Original file line number Diff line number Diff line change @@ -292,13 +292,12 @@ def _get_filepath_or_buffer(
292
292
293
293
# assuming storage_options is to be interpretted as headers
294
294
req_info = urllib .request .Request (filepath_or_buffer , headers = storage_options )
295
- req = urlopen (req_info )
296
- content_encoding = req .headers .get ("Content-Encoding" , None )
297
- if content_encoding == "gzip" :
298
- # Override compression based on Content-Encoding header
299
- compression = {"method" : "gzip" }
300
- reader = BytesIO (req .read ())
301
- req .close ()
295
+ with urlopen (req_info ) as req :
296
+ content_encoding = req .headers .get ("Content-Encoding" , None )
297
+ if content_encoding == "gzip" :
298
+ # Override compression based on Content-Encoding header
299
+ compression = {"method" : "gzip" }
300
+ reader = BytesIO (req .read ())
302
301
return IOArgs (
303
302
filepath_or_buffer = reader ,
304
303
encoding = encoding ,
Original file line number Diff line number Diff line change @@ -410,6 +410,9 @@ def load_workbook(self, filepath_or_buffer):
410
410
pass
411
411
412
412
def close (self ):
413
+ if hasattr (self .book , "close" ):
414
+ # pyxlsb opens a TemporaryFile
415
+ self .book .close ()
413
416
self .handles .close ()
414
417
415
418
@property
@@ -483,6 +486,9 @@ def parse(
483
486
sheet = self .get_sheet_by_index (asheetname )
484
487
485
488
data = self .get_sheet_data (sheet , convert_float )
489
+ if hasattr (sheet , "close" ):
490
+ # pyxlsb opens two TemporaryFiles
491
+ sheet .close ()
486
492
usecols = maybe_convert_usecols (usecols )
487
493
488
494
if not data :
Original file line number Diff line number Diff line change @@ -465,6 +465,12 @@ def __init__(self, path):
465
465
else :
466
466
self .headers = {"Content-Encoding" : None }
467
467
468
+ def __enter__ (self ):
469
+ return self
470
+
471
+ def __exit__ (self , * args ):
472
+ self .close ()
473
+
468
474
def read (self ):
469
475
return self .file .read ()
470
476
You can’t perform that action at this time.
0 commit comments