We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[BUG] Passing non-bytes file input leads to error
https://t.me/pythontelegrambotgroup/396541

TL;DR:

`send_document(open('text_file', 'rb'))` works but `send_document(open('text_file', 'r'))` raises is error.
This is, because we try to guess if the file is an image using `imghdr.what(None, stream)` in `InputFile.is_image`, which only works if `stream` is a bytes stream.
If I comment the `is_image` out, the file is sent without issue, so I guess we should just check if the input is bytes before calling `is_image`

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `telegram/files/inputfile.py`
Content:
```
1 #!/usr/bin/env python
2 # pylint: disable=W0622,E0611
3 #
4 # A library that provides a Python interface to the Telegram Bot API
5 # Copyright (C) 2015-2020
6 # Leandro Toledo de Souza <devs@python-telegram-bot.org>
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Lesser Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Lesser Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser Public License
19 # along with this program.  If not, see [http://www.gnu.org/licenses/].
20 """This module contains an object that represents a Telegram InputFile."""
21 
22 import imghdr
23 import mimetypes
24 import os
25 from typing import IO, Optional, Tuple
26 from uuid import uuid4
27 
28 from telegram import TelegramError
29 
30 DEFAULT_MIME_TYPE = 'application/octet-stream'
31 
32 
33 class InputFile:
34     """This object represents a Telegram InputFile.
35 
36     Attributes:
37         input_file_content (:obj:`bytes`): The binary content of the file to send.
38         filename (:obj:`str`): Optional. Filename for the file to be sent.
39         attach (:obj:`str`): Optional. Attach id for sending multiple files.
40 
41     Args:
42         obj (:obj:`File handler`): An open file descriptor.
43         filename (:obj:`str`, optional): Filename for this InputFile.
44         attach (:obj:`bool`, optional): Whether this should be send as one file or is part of a
45             collection of files.
46 
47     Raises:
48         TelegramError
49 
50     """
51 
52     def __init__(self, obj: IO, filename: str = None, attach: bool = None):
53         self.filename = None
54         self.input_file_content = obj.read()
55         self.attach = 'attached' + uuid4().hex if attach else None
56 
57         if filename:
58             self.filename = filename
59         elif hasattr(obj, 'name') and not isinstance(obj.name, int):
60             self.filename = os.path.basename(obj.name)
61 
62         try:
63             self.mimetype = self.is_image(self.input_file_content)
64         except TelegramError:
65             if self.filename:
66                 self.mimetype = mimetypes.guess_type(self.filename)[0] or DEFAULT_MIME_TYPE
67             else:
68                 self.mimetype = DEFAULT_MIME_TYPE
69         if not self.filename:
70             self.filename = self.mimetype.replace('/', '.')
71 
72     @property
73     def field_tuple(self) -> Tuple[str, bytes, str]:
74         return self.filename, self.input_file_content, self.mimetype
75 
76     @staticmethod
77     def is_image(stream: bytes) -> str:
78         """Check if the content file is an image by analyzing its headers.
79 
80         Args:
81             stream (:obj:`bytes`): A byte stream representing the content of a file.
82 
83         Returns:
84             :obj:`str`: The str mime-type of an image.
85 
86         """
87         image = imghdr.what(None, stream)
88         if image:
89             return 'image/%s' % image
90 
91         raise TelegramError('Could not parse file content')
92 
93     @staticmethod
94     def is_file(obj: object) -> bool:
95         return hasattr(obj, 'read')
96 
97     def to_dict(self) -> Optional[str]:
98         if self.attach:
99             return 'attach://' + self.attach
100         return None
101
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/telegram/files/inputfile.py b/telegram/files/inputfile.py
--- a/telegram/files/inputfile.py
+++ b/telegram/files/inputfile.py
@@ -20,14 +20,14 @@
 """This module contains an object that represents a Telegram InputFile."""
 
 import imghdr
+import logging
 import mimetypes
 import os
 from typing import IO, Optional, Tuple
 from uuid import uuid4
 
-from telegram import TelegramError
-
 DEFAULT_MIME_TYPE = 'application/octet-stream'
+logger = logging.getLogger(__name__)
 
 
 class InputFile:
@@ -59,13 +59,14 @@
         elif hasattr(obj, 'name') and not isinstance(obj.name, int):
             self.filename = os.path.basename(obj.name)
 
-        try:
-            self.mimetype = self.is_image(self.input_file_content)
-        except TelegramError:
-            if self.filename:
-                self.mimetype = mimetypes.guess_type(self.filename)[0] or DEFAULT_MIME_TYPE
-            else:
-                self.mimetype = DEFAULT_MIME_TYPE
+        image_mime_type = self.is_image(self.input_file_content)
+        if image_mime_type:
+            self.mimetype = image_mime_type
+        elif self.filename:
+            self.mimetype = mimetypes.guess_type(self.filename)[0] or DEFAULT_MIME_TYPE
+        else:
+            self.mimetype = DEFAULT_MIME_TYPE
+
         if not self.filename:
             self.filename = self.mimetype.replace('/', '.')
 
@@ -74,21 +75,27 @@
         return self.filename, self.input_file_content, self.mimetype
 
     @staticmethod
-    def is_image(stream: bytes) -> str:
+    def is_image(stream: bytes) -> Optional[str]:
         """Check if the content file is an image by analyzing its headers.
 
         Args:
             stream (:obj:`bytes`): A byte stream representing the content of a file.
 
         Returns:
-            :obj:`str`: The str mime-type of an image.
+            :obj:`str` | :obj:`None`: The mime-type of an image, if the input is an image, or
+            :obj:`None` else.
 
         """
-        image = imghdr.what(None, stream)
-        if image:
-            return 'image/%s' % image
-
-        raise TelegramError('Could not parse file content')
+        try:
+            image = imghdr.what(None, stream)
+            if image:
+                return f'image/{image}'
+            return None
+        except Exception:
+            logger.debug(
+                "Could not parse file content. Assuming that file is not an image.", exc_info=True
+            )
+            return None
 
     @staticmethod
     def is_file(obj: object) -> bool:

{"golden_diff": "diff --git a/telegram/files/inputfile.py b/telegram/files/inputfile.py\n--- a/telegram/files/inputfile.py\n+++ b/telegram/files/inputfile.py\n@@ -20,14 +20,14 @@\n \"\"\"This module contains an object that represents a Telegram InputFile.\"\"\"\n \n import imghdr\n+import logging\n import mimetypes\n import os\n from typing import IO, Optional, Tuple\n from uuid import uuid4\n \n-from telegram import TelegramError\n-\n DEFAULT_MIME_TYPE = 'application/octet-stream'\n+logger = logging.getLogger(__name__)\n \n \n class InputFile:\n@@ -59,13 +59,14 @@\n         elif hasattr(obj, 'name') and not isinstance(obj.name, int):\n             self.filename = os.path.basename(obj.name)\n \n-        try:\n-            self.mimetype = self.is_image(self.input_file_content)\n-        except TelegramError:\n-            if self.filename:\n-                self.mimetype = mimetypes.guess_type(self.filename)[0] or DEFAULT_MIME_TYPE\n-            else:\n-                self.mimetype = DEFAULT_MIME_TYPE\n+        image_mime_type = self.is_image(self.input_file_content)\n+        if image_mime_type:\n+            self.mimetype = image_mime_type\n+        elif self.filename:\n+            self.mimetype = mimetypes.guess_type(self.filename)[0] or DEFAULT_MIME_TYPE\n+        else:\n+            self.mimetype = DEFAULT_MIME_TYPE\n+\n         if not self.filename:\n             self.filename = self.mimetype.replace('/', '.')\n \n@@ -74,21 +75,27 @@\n         return self.filename, self.input_file_content, self.mimetype\n \n     @staticmethod\n-    def is_image(stream: bytes) -> str:\n+    def is_image(stream: bytes) -> Optional[str]:\n         \"\"\"Check if the content file is an image by analyzing its headers.\n \n         Args:\n             stream (:obj:`bytes`): A byte stream representing the content of a file.\n \n         Returns:\n-            :obj:`str`: The str mime-type of an image.\n+            :obj:`str` | :obj:`None`: The mime-type of an image, if the input is an image, or\n+            :obj:`None` else.\n \n         \"\"\"\n-        image = imghdr.what(None, stream)\n-        if image:\n-            return 'image/%s' % image\n-\n-        raise TelegramError('Could not parse file content')\n+        try:\n+            image = imghdr.what(None, stream)\n+            if image:\n+                return f'image/{image}'\n+            return None\n+        except Exception:\n+            logger.debug(\n+                \"Could not parse file content. Assuming that file is not an image.\", exc_info=True\n+            )\n+            return None\n \n     @staticmethod\n     def is_file(obj: object) -> bool:\n", "issue": "[BUG] Passing non-bytes file input leads to error\nhttps://t.me/pythontelegrambotgroup/396541\r\n\r\nTL;DR:\r\n\r\n`send_document(open('text_file', 'rb'))` works but `send_document(open('text_file', 'r'))` raises is error.\r\nThis is, because we try to guess if the file is an image using `imghdr.what(None, stream)` in `InputFile.is_image`, which only works if `stream` is a bytes stream.\r\nIf I comment the `is_image` out, the file is sent without issue, so I guess we should just check if the input is bytes before calling `is_image`\n", "before_files": [{"content": "#!/usr/bin/env python\n# pylint: disable=W0622,E0611\n#\n# A library that provides a Python interface to the Telegram Bot API\n# Copyright (C) 2015-2020\n# Leandro Toledo de Souza <devs@python-telegram-bot.org>\n#\n# This program is free software: you can redistribute it and/or modify\n# it under the terms of the GNU Lesser Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU Lesser Public License for more details.\n#\n# You should have received a copy of the GNU Lesser Public License\n# along with this program.  If not, see [http://www.gnu.org/licenses/].\n\"\"\"This module contains an object that represents a Telegram InputFile.\"\"\"\n\nimport imghdr\nimport mimetypes\nimport os\nfrom typing import IO, Optional, Tuple\nfrom uuid import uuid4\n\nfrom telegram import TelegramError\n\nDEFAULT_MIME_TYPE = 'application/octet-stream'\n\n\nclass InputFile:\n    \"\"\"This object represents a Telegram InputFile.\n\n    Attributes:\n        input_file_content (:obj:`bytes`): The binary content of the file to send.\n        filename (:obj:`str`): Optional. Filename for the file to be sent.\n        attach (:obj:`str`): Optional. Attach id for sending multiple files.\n\n    Args:\n        obj (:obj:`File handler`): An open file descriptor.\n        filename (:obj:`str`, optional): Filename for this InputFile.\n        attach (:obj:`bool`, optional): Whether this should be send as one file or is part of a\n            collection of files.\n\n    Raises:\n        TelegramError\n\n    \"\"\"\n\n    def __init__(self, obj: IO, filename: str = None, attach: bool = None):\n        self.filename = None\n        self.input_file_content = obj.read()\n        self.attach = 'attached' + uuid4().hex if attach else None\n\n        if filename:\n            self.filename = filename\n        elif hasattr(obj, 'name') and not isinstance(obj.name, int):\n            self.filename = os.path.basename(obj.name)\n\n        try:\n            self.mimetype = self.is_image(self.input_file_content)\n        except TelegramError:\n            if self.filename:\n                self.mimetype = mimetypes.guess_type(self.filename)[0] or DEFAULT_MIME_TYPE\n            else:\n                self.mimetype = DEFAULT_MIME_TYPE\n        if not self.filename:\n            self.filename = self.mimetype.replace('/', '.')\n\n    @property\n    def field_tuple(self) -> Tuple[str, bytes, str]:\n        return self.filename, self.input_file_content, self.mimetype\n\n    @staticmethod\n    def is_image(stream: bytes) -> str:\n        \"\"\"Check if the content file is an image by analyzing its headers.\n\n        Args:\n            stream (:obj:`bytes`): A byte stream representing the content of a file.\n\n        Returns:\n            :obj:`str`: The str mime-type of an image.\n\n        \"\"\"\n        image = imghdr.what(None, stream)\n        if image:\n            return 'image/%s' % image\n\n        raise TelegramError('Could not parse file content')\n\n    @staticmethod\n    def is_file(obj: object) -> bool:\n        return hasattr(obj, 'read')\n\n    def to_dict(self) -> Optional[str]:\n        if self.attach:\n            return 'attach://' + self.attach\n        return None\n", "path": "telegram/files/inputfile.py"}], "after_files": [{"content": "#!/usr/bin/env python\n# pylint: disable=W0622,E0611\n#\n# A library that provides a Python interface to the Telegram Bot API\n# Copyright (C) 2015-2020\n# Leandro Toledo de Souza <devs@python-telegram-bot.org>\n#\n# This program is free software: you can redistribute it and/or modify\n# it under the terms of the GNU Lesser Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU Lesser Public License for more details.\n#\n# You should have received a copy of the GNU Lesser Public License\n# along with this program.  If not, see [http://www.gnu.org/licenses/].\n\"\"\"This module contains an object that represents a Telegram InputFile.\"\"\"\n\nimport imghdr\nimport logging\nimport mimetypes\nimport os\nfrom typing import IO, Optional, Tuple\nfrom uuid import uuid4\n\nDEFAULT_MIME_TYPE = 'application/octet-stream'\nlogger = logging.getLogger(__name__)\n\n\nclass InputFile:\n    \"\"\"This object represents a Telegram InputFile.\n\n    Attributes:\n        input_file_content (:obj:`bytes`): The binary content of the file to send.\n        filename (:obj:`str`): Optional. Filename for the file to be sent.\n        attach (:obj:`str`): Optional. Attach id for sending multiple files.\n\n    Args:\n        obj (:obj:`File handler`): An open file descriptor.\n        filename (:obj:`str`, optional): Filename for this InputFile.\n        attach (:obj:`bool`, optional): Whether this should be send as one file or is part of a\n            collection of files.\n\n    Raises:\n        TelegramError\n\n    \"\"\"\n\n    def __init__(self, obj: IO, filename: str = None, attach: bool = None):\n        self.filename = None\n        self.input_file_content = obj.read()\n        self.attach = 'attached' + uuid4().hex if attach else None\n\n        if filename:\n            self.filename = filename\n        elif hasattr(obj, 'name') and not isinstance(obj.name, int):\n            self.filename = os.path.basename(obj.name)\n\n        image_mime_type = self.is_image(self.input_file_content)\n        if image_mime_type:\n            self.mimetype = image_mime_type\n        elif self.filename:\n            self.mimetype = mimetypes.guess_type(self.filename)[0] or DEFAULT_MIME_TYPE\n        else:\n            self.mimetype = DEFAULT_MIME_TYPE\n\n        if not self.filename:\n            self.filename = self.mimetype.replace('/', '.')\n\n    @property\n    def field_tuple(self) -> Tuple[str, bytes, str]:\n        return self.filename, self.input_file_content, self.mimetype\n\n    @staticmethod\n    def is_image(stream: bytes) -> Optional[str]:\n        \"\"\"Check if the content file is an image by analyzing its headers.\n\n        Args:\n            stream (:obj:`bytes`): A byte stream representing the content of a file.\n\n        Returns:\n            :obj:`str` | :obj:`None`: The mime-type of an image, if the input is an image, or\n            :obj:`None` else.\n\n        \"\"\"\n        try:\n            image = imghdr.what(None, stream)\n            if image:\n                return f'image/{image}'\n            return None\n        except Exception:\n            logger.debug(\n                \"Could not parse file content. Assuming that file is not an image.\", exc_info=True\n            )\n            return None\n\n    @staticmethod\n    def is_file(obj: object) -> bool:\n        return hasattr(obj, 'read')\n\n    def to_dict(self) -> Optional[str]:\n        if self.attach:\n            return 'attach://' + self.attach\n        return None\n", "path": "telegram/files/inputfile.py"}]}