Prevent encoding error in IPython extension in Windows#342
Merged
Conversation
When using the `%lprun` magic in Windows, and trying to store the profile results in a file, the following exception was raised if the functions profiled contain non-ASCII characters:
```python
UnicodeEncodeError Traceback (most recent call last)
Cell In[5], line 1
File ...\.conda\Lib\site-packages\IPython\core\interactiveshell.py:2482, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
2480 kwargs['local_ns'] = self.get_local_scope(stack_depth)
2481 with self.builtin_trap:
-> 2482 result = fn(*args, **kwargs)
2484 # The code below prevents the output from being displayed
2485 # when using magics with decorator @output_can_be_silenced
2486 # when the last Python token in the expression is a ';'.
2487 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):
File ...\.conda\Lib\site-packages\line_profiler\ipython_extension.py:161, in LineProfilerMagics.lprun(self, parameter_s)
159 if text_file:
160 pfile = open(text_file, "w")
--> 161 pfile.write(output)
162 pfile.close()
163 print(f"\n*** Profile printout saved to text file {text_file!r}. {message}")
File ...\.conda\Lib\encodings\cp1252.py:19, in IncrementalEncoder.encode(self, input, final)
18 def encode(self, input, final=False):
---> 19 return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u0394' in position 36719: character maps to <undefined>
```
This is because the default encoding in Windows does not support non-ASCII characters. Thus, the solution (as in pyutils#312) is to specify the UTF-8 encoding in the `open` call.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #342 +/- ##
=======================================
Coverage 64.84% 64.84%
=======================================
Files 13 13
Lines 1041 1041
Branches 228 228
=======================================
Hits 675 675
Misses 306 306
Partials 60 60
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
Member
|
LGTM. Thanks for the patch! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using the
%lprunmagic in Windows, and trying to store the profile results in a file, the following exception was raised if the functions profiled contain non-ASCII characters:This is because the default encoding in Windows does not support non-ASCII characters. Thus, the solution (as in #312) is to specify the UTF-8 encoding in the
opencall.