Skip to content

Python exception when using colorlog due to incomplete IO implementation in sys.stderr #2762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dylanmccall opened this issue Mar 3, 2023 · 1 comment · Fixed by #2763
Closed

Comments

@dylanmccall
Copy link
Contributor

I am attempting to run a program which uses TTYColoredFormatter from colorlog. This class formats log messages, adding ANSI escape codes only if the stream it is writing to returns True for stream.isatty().

Unfortunately, python-for-android's bootstrap code replaces sys.stderr and sys.stdout with a custom LogFile object:

PyRun_SimpleString(
"class LogFile(object):\n"
" def __init__(self):\n"
" self.__buffer = ''\n"
" def write(self, s):\n"
" s = self.__buffer + s\n"
" lines = s.split('\\n')\n"
" for l in lines[:-1]:\n"
" androidembed.log(l.replace('\\x00', ''))\n"
" self.__buffer = lines[-1]\n"
" def flush(self):\n"
" return\n"
"sys.stdout = sys.stderr = LogFile()\n"
"print('Android path', sys.path)\n"
"import os\n"
"print('os.environ is', os.environ)\n"
"print('Android kivy bootstrap done. __name__ is', __name__)");

This object doesn't implement isatty() (or much else, for that matter). As a result, the program raises an exception:

03-03 13:32:56.222  5806  5891 I python  : Traceback (most recent call last):
03-03 13:32:56.222  5806  5891 I python  :   File "/home/jenkins/workspace/kolibri-installer-android-pr/src/main.py", line 3, in <module>
03-03 13:32:56.222  5806  5891 I python  :   File "/home/jenkins/workspace/kolibri-installer-android-pr/src/kolibri_android/main_activity/__main__.py", line 7, in main
03-03 13:32:56.222  5806  5891 I python  :   File "/home/jenkins/workspace/kolibri-installer-android-pr/src/kolibri_android/main_activity/activity.py", line 19, in <module>
03-03 13:32:56.222  5806  5891 I python  :   File "/home/jenkins/workspace/kolibri-installer-android-pr/src/kolibri_android/kolibri_utils.py", line 13, in <module>
03-03 13:32:56.223  5806  5891 I python  :   File "/home/jenkins/workspace/kolibri-installer-android-pr/src/kolibri_android/android_whitenoise.py", line 11, in <module>
03-03 13:32:56.223  5806  5891 I python  :   File "/home/jenkins/workspace/kolibri-installer-android-pr/src/kolibri/__init__.py", line 10, in <module>
03-03 13:32:56.223  5806  5891 I python  :   File "/home/jenkins/workspace/kolibri-installer-android-pr/src/kolibri/utils/env.py", line 29, in <module>
03-03 13:32:56.223  5806  5891 I python  :   File "/home/jenkins/workspace/kolibri-installer-android-pr/src/kolibri/dist/colorlog/colorlog.py", line 203, in __init__
03-03 13:32:56.223  5806  5891 I python  : AttributeError: 'LogFile' object has no attribute 'isatty'

(For reference, we're using colorlog v3.2.0, so the code raising the exception looks like this: https://github.com/borntyping/python-colorlog/blob/v3.2.0/colorlog/colorlog.py#L191-L211).

@dylanmccall
Copy link
Contributor Author

I wonder if this LogFile class can inherit from IOBase instead: https://docs.python.org/3/library/io.html#io.IOBase. There are some pretty sane defaults there so we might just get the correct behaviour for free.

dylanmccall added a commit to dylanmccall/python-for-android that referenced this issue Mar 4, 2023
This provides reasonable default implementations for common IO class
methods such as isatty().

Closes kivy#2762.
misl6 pushed a commit that referenced this issue Mar 5, 2023
This provides reasonable default implementations for common IO class
methods such as isatty().

Closes #2762.
rtibbles pushed a commit to learningequality/python-for-android that referenced this issue Mar 7, 2023
This provides reasonable default implementations for common IO class
methods such as isatty().

Closes kivy#2762.
rtibbles pushed a commit to learningequality/python-for-android that referenced this issue Mar 22, 2023
This provides reasonable default implementations for common IO class
methods such as isatty().

Closes kivy#2762.
misl6 added a commit that referenced this issue May 22, 2023
* Add support for Python 3.10 and make it the default while building hostpython3 and python3 (#2577)

* Add support for Python 3.10 and make it the default while building hostpython3 and python3

* Update bugfix version

* Add PyAV recipe (#2750)

* pyav recipe

* flake8 fix

* Update pythonforandroid/recipes/av/__init__.py

Co-authored-by: Andre Miras <AndreMiras@users.noreply.github.com>

---------

Co-authored-by: Andre Miras <AndreMiras@users.noreply.github.com>

* Add debug loggings for identifying a matching dist (#2751)

* Add debug loggings for identifying a matching dist

* Fix lint failure on f-string.

* Adds flag to support home app (launcher) intent-filter on SDL2 bootstrap (#2761)

* Home app functionality

* Cleanup

* Added --home-app to the docs

* Fixed the if statements within the intent-filter

* Use io.IOBase for LogFile (#2763)

This provides reasonable default implementations for common IO class
methods such as isatty().

Closes #2762.

* android_api to integer (#2765)

* android_api to integer

* changes to android_api

* changes to android_api

* fix #2768 smallIconName null can t be compared to String (#2769)

* fix #2768 smallIconName null can t be compared to String

* fix: Remove Importance None of notification which is modification from my own fork

* fix: Readd Importance_NONE in the notification channel as it s also in upstream p4a

* fix tools/liblink: syntax error

regression from 22b7dfe

* Update pyjnius version in recipe (#2791)

* Update Kivy recipe for 2.2.0 (#2793)

* Update Kivy recipe for 2.2.0rc1

* Bump to 2.2.0

* Update CHANGELOG.md and bump version to 2023.05.21

---------

Co-authored-by: Dexer <73297572+DexerBR@users.noreply.github.com>
Co-authored-by: Andre Miras <AndreMiras@users.noreply.github.com>
Co-authored-by: BitcoinWukong <87334822+BitcoinWukong@users.noreply.github.com>
Co-authored-by: Mathias Lindström <kuzeyron@gmail.com>
Co-authored-by: Dylan McCall <dylan@dylanmccall.ca>
Co-authored-by: Benoît HERVIER <b@rvier.fr>
Co-authored-by: SomberNight <somber.night@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant