Skip to content

Handling TypeError when sys.stdout.encoding is None #558

New issue

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

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

Already on GitHub? # to your account

Closed
tcaiazzi opened this issue Jan 30, 2025 · 4 comments
Closed

Handling TypeError when sys.stdout.encoding is None #558

tcaiazzi opened this issue Jan 30, 2025 · 4 comments
Assignees

Comments

@tcaiazzi
Copy link

tcaiazzi commented Jan 30, 2025

Hello!

We've encountered an issue where sys.stdout.encoding can sometimes be None. This leads to a TypeError when calling the console_to_str method, as the console_encoding variable is also None.

    10 def console_to_str(s: bytes) -> str:                                                                                                                                                 
         │    11 │   """From pypa/pip project, pip.backwardwardcompat. License MIT."""                                                                                                                   
         │    12 │   try:                                                                                                                                                                             
         │ ❱  13 │   │   return s.decode(console_encoding, "ignore")                                                                                                                                 
         │    14 │   except UnicodeDecodeError:                                                                                                                                                       
         │    15 │   │   return s.decode("utf_8", "ignore")                                                                                                                                           
         │    16                                                                                                                                                                                      
         
         TypeError: decode() argument 'encoding' must be str, not None

Possible Cases Where sys.stdout.encoding is None

There are multiple scenarios where sys.stdout.encoding can be None, including:

  1. Running in a non-interactive environment

  2. Redirecting stdout to a file or a pipe

    • If standard output is redirected, it may not have an associated encoding.
    • Example:
    python script.py > output.txt
    
    • Similarly, when piping output:
    python script.py | cat
    
  3. Running in certain containerized environments

  4. Windows with specific configurations

  5. Using custom or overridden sys.stdout streams

    • If sys.stdout is reassigned (e.g., wrapped in a custom stream), the encoding may not be set explicitly.
    • Example:
     import sys
     sys.stdout = open('log.txt', 'w')
     print(sys.stdout.encoding)  # None
    

Context

This issue arises in [Kathará](https://github.com/KatharaFramework/Kathara), a container-based network emulator. More details can be found in [Kathará Issue #333](KatharaFramework/Kathara#333).

Possible Fix

A potential solution is the approach used [here](https://github.com/tcaiazzi/libtmux/blob/1bf73396ca579488a1f266a16e553f3ca735011e/src/libtmux/_compat.py#L7), which provides a fallback encoding:

console_encoding =  sys.stdout.encoding if sys.stdout.encoding else sys.getdefaultencoding()

However, I am unsure if this is the best approach. Would love to hear your thoughts on whether this is the right fix or if there's a better alternative.

Thanks!

@tcaiazzi tcaiazzi changed the title Handling TypeError when sys.stdout.encoding is None Handling TypeError when sys.stdout.encoding is None Jan 30, 2025
@tony tony self-assigned this Feb 1, 2025
@tony
Copy link
Member

tony commented Feb 2, 2025

@tony
Copy link
Member

tony commented Feb 2, 2025

@tcaiazzi If you try v0.42.0 - is anything different?

This was closed automatically - but want to confirm this did the trick! (We didn't have an exact recreation to work from, so want to be sure).

v0.42.0: tree, releases, changelog, PyPI

@tcaiazzi
Copy link
Author

tcaiazzi commented Feb 3, 2025

Dear @tony,

It works perfectly!

Thank you so much for your help and support! 🚀❤️

@tony
Copy link
Member

tony commented Feb 3, 2025

@tcaiazzi Superb!! Glad this solution worked!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants