From dfcf414111a5959df0b9438ab284d6087ac6f3f5 Mon Sep 17 00:00:00 2001 From: Harry Date: Fri, 29 Jun 2018 17:45:18 +0300 Subject: [PATCH] Fix hachoir don't close files by itself (#875) --- telethon/client/uploads.py | 44 ++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index cd38d024..17b361ef 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -414,32 +414,30 @@ class UploadMethods(MessageParseMethods, UserMethods): os.path.basename(file)) } if utils.is_audio(file) and hachoir: - m = hachoir.metadata.extractMetadata( - hachoir.parser.createParser(file) - ) - attr_dict[types.DocumentAttributeAudio] = \ - types.DocumentAttributeAudio( - voice=voice_note, - title=m.get('title') if m.has( - 'title') else None, - performer=m.get('author') if m.has( - 'author') else None, - duration=int(m.get('duration').seconds - if m.has('duration') else 0) - ) + with hachoir.parser.createParser(file) as parser: + m = hachoir.metadata.extractMetadata(parser) + attr_dict[types.DocumentAttributeAudio] = \ + types.DocumentAttributeAudio( + voice=voice_note, + title=m.get('title') if m.has( + 'title') else None, + performer=m.get('author') if m.has( + 'author') else None, + duration=int(m.get('duration').seconds + if m.has('duration') else 0) + ) if not force_document and utils.is_video(file): if hachoir: - m = hachoir.metadata.extractMetadata( - hachoir.parser.createParser(file) - ) - doc = types.DocumentAttributeVideo( - round_message=video_note, - w=m.get('width') if m.has('width') else 0, - h=m.get('height') if m.has('height') else 0, - duration=int(m.get('duration').seconds - if m.has('duration') else 0) - ) + with hachoir.parser.createParser(file) as parser: + m = hachoir.metadata.extractMetadata(parser) + doc = types.DocumentAttributeVideo( + round_message=video_note, + w=m.get('width') if m.has('width') else 0, + h=m.get('height') if m.has('height') else 0, + duration=int(m.get('duration').seconds + if m.has('duration') else 0) + ) else: doc = types.DocumentAttributeVideo( 0, 1, 1, round_message=video_note)