Skip to content
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

Error if code includes unicode character #90

Open
rebeccamccabe opened this issue Dec 27, 2023 · 2 comments
Open

Error if code includes unicode character #90

rebeccamccabe opened this issue Dec 27, 2023 · 2 comments
Assignees
Labels
Milestone

Comments

@rebeccamccabe
Copy link

The comments of my code use the unicode character Σ.
When I go to run > run line profiler, I get the following error:

Traceback (most recent call last):
  File "C:\Users\rgm222\Anaconda3\envs\decider-10-7-23\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\rgm222\Anaconda3\envs\decider-10-7-23\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\rgm222\Anaconda3\envs\decider-10-7-23\lib\site-packages\kernprof.py", line 409, in <module>
    main(sys.argv[1:])
  File "C:\Users\rgm222\Anaconda3\envs\decider-10-7-23\lib\site-packages\kernprof.py", line 395, in main
    prof.print_stats(output_unit=options.unit,
  File "C:\Users\rgm222\Anaconda3\envs\decider-10-7-23\lib\site-packages\line_profiler\line_profiler.py", line 169, in print_stats
    show_text(lstats.timings, lstats.unit, output_unit=output_unit,
  File "C:\Users\rgm222\Anaconda3\envs\decider-10-7-23\lib\site-packages\line_profiler\line_profiler.py", line 460, in show_text
    show_func(fn, lineno, name, stats[fn, lineno, name], unit,
  File "C:\Users\rgm222\Anaconda3\envs\decider-10-7-23\lib\site-packages\line_profiler\line_profiler.py", line 433, in show_func
    stream.write(txt)
  File "C:\Users\rgm222\Anaconda3\envs\decider-10-7-23\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u03a3' in position 109: character maps to <undefined>

Removing the unicode character solves the issue.

Versions:
spyder 5.5.0
spyder-line-profiler 0.3.2

@jitseniesen
Copy link
Member

Hi @rebeccamccabe, sorry for not saying something when you filed your report and thanks for taking the time to write it up.

I tried to reproduce the error but everything works fine on my computer. My guess is that it depends on the operating system, your computer is Windows and mine is Linux, so I will have a go at it on Windows later.

Some initial thoughts, mainly written down so that I remember later: the error occurs in line_profiler and there is nothing about Spyder in the error message. When Spyder runs the line profiler, it does so in a separate Python process. Perhaps that process outputs to a pipe or something like that using the cp1252 encoding and the error occurs when it encounters the Σ character. There is a similar traceback at https://stackoverflow.com/questions/56936927/unicodeencodeerror-when-using-line-profiler so it's possible the real error is in the line profiler. Nevertheless, we should try to work around it. Maybe turn on UTF8 mode in Python?

@jitseniesen
Copy link
Member

I managed to reproduce the error in our test environment on Windows.

@jitseniesen jitseniesen added this to the v0.4.0 milestone Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants