iamDyeus / iamDyeus/tkreload

[BUG] UnicodeDecodeError when using non-ASCII characters in files

Open
#25 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
35
Forks
9
PR merge metrics
No merged PRs in 30d

Description

When using tkreload with files containing non-ASCII characters (such as Unicode emojis or special characters), the library throws a UnicodeDecodeError because it uses the default 'charmap' codec (cp1252 on Windows) instead of UTF-8 for file operations.

**Error Message**: `UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 1221: character maps to `

This error occurs in the file handling within `tkreload.app_event_handler`.

## Steps to Reproduce
1. Install tkreload (`pip install tkreload`)
2. Create a Python file using CustomTkinter or Tkinter that contains Unicode characters (e.g., emojis)
3. Run the application with auto-reload enabled
4. The application will crash with a UnicodeDecodeError when tkreload tries to read the file

## Example Code That Triggers the Error
```python
import customtkinter as ctk

app = ctk.CTk()
# Using an emoji or special character in the UI
label = ctk.CTkLabel(app, text="☀️") # This will trigger the error
app.mainloop()
```

**Current Behavior**

- tkreload uses the default system encoding (cp1252 on Windows)
- Files containing non-ASCII characters cause the application to crash
- No built-in handling for UTF-8 encoded files

**Expected Behavior**

- tkreload should handle UTF-8 encoded files correctly
- Support for Unicode characters in source files
- Graceful handling of different file encodings

**Workaround**
Currently, this can be worked around by monkey-patching the file opening operation in tkreload:
```python
import tkreload.app_event_handler

original_open = tkreload.app_event_handler.open

def utf8_open(file_path, mode='r', *args, **kwargs):
return original_open(file_path, mode, encoding='utf-8', *args, **kwargs)

tkreload.app_event_handler.open = utf8_open
```

**Proposed Fix**
The library should be updated to:
- Use UTF-8 as the default encoding for file operations
- Add an optional configuration parameter for specifying the desired encoding
- Implement proper error handling for encoding issues

**Environment**

- Python version: 3.11
- tkreload version: 1.0.4
- OS: Windows
- Related packages: CustomTkinter 5.2.2

**Additional Notes**
This issue particularly affects applications using Unicode characters, emojis, or international text. The current workaround requires additional setup code that should ideally be handled by the library itself.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.