marcolardera / chatgpt-cli

Simple yet effective command line client for chatting with ChatGPT using the official API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

default character encoding in some machines with Win OS causes crash!

orsnaro opened this issue · comments

PR#58

The bug is actually easy to fix but causes the whole code to collapse.

Details file i/o `open()` function have `encoding` argument that by default is set to the machine's default character encoding.
  • Most Linux machines uses utf-8 as default encoding which is ok with most languages characters, emojis and symbols.

  • In Windows machines, a big portion of them uses cp1252 as default encoding which is very limited:
    get win default encoding via python script


open() docs: "if encoding is not specified the encoding used is platform dependent" see

  • Reproducing the bug
    bug screenshot using Cmder shell & win terminal preview
    (as you see even if user didn't send any invalid characters, GPT sending one invalid character is enough to cause the bug)

  • Fix
    simply assigning open( , encoding= "utf-8") for all systems or detecting the system encoding first using:

import locale
sys_char_encoding = locale.getpreferredencoding()
#rest of the code

Thank you @orsnaro for catching this bug affecting Windows machines, I merged your fix...