LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.77k stars 1.39k forks source link

send_file() with BytesIO file and defined file_size causes UnboundLocalError #1559

Closed zotho closed 4 years ago

zotho commented 4 years ago

Checklist

Code that causes the issue

import io
import os

from PIL import Image
from telethon.sync import TelegramClient

api_id: int = int(os.environ["API_ID"])
api_hash: str = os.environ["API_HASH"]
bot = TelegramClient("My bot", api_id, api_hash).start()

image = Image.new("L", (100, 100), 0)
result_buffer = io.BytesIO()
image.save(result_buffer, format="PNG")

bot.send_file("@user", result_buffer, file_size=result_buffer.tell())

Traceback

Traceback (most recent call last):
    File ".../lib/python3.8/site-packages/telethon/client/uploads.py", line 622, in upload_file
        part = await helpers._maybe_await(stream.read(part_size))
UnboundLocalError: local variable 'stream' referenced before assignment

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
    File "bot.py", line 14, in <module>
        bot.send_file("@zotho", result_buffer, file_size=result_buffer.tell())
    File ".../lib/python3.8/site-packages/telethon/sync.py", line 39, in syncified
        return loop.run_until_complete(coro)
    File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
        return future.result()
    File ".../lib/python3.8/site-packages/telethon/client/uploads.py", line 366, in send_file
        file_handle, media, image = await self._file_to_media(
    File ".../lib/python3.8/site-packages/telethon/client/uploads.py", line 724, in _file_to_media
        file_handle = await self.upload_file(
    File ".../lib/python3.8/site-packages/telethon/client/uploads.py", line 667, in upload_file
        if close_stream:
UnboundLocalError: local variable 'close_stream' referenced before assignment
zotho commented 4 years ago

Сorrectly working example:

result_buffer.seek(0)
bot.send_file("@user", result_buffer)

I think that in case I forget to call seek(0) UnboundLocalError is wrong exception

Lonami commented 4 years ago

The problem was the library was skipping a fair bit of code that is necessary when file_size was specified. The library won't seek(0) to send files. That's something the user has to decide (whether this is a good decision is another story, and changing it would be a breaking change).

Lonami commented 4 years ago

Forgot to mention in the commit, but this is closed by 219b4ecb77ae1b97259ed302c63285daa32aea08.