From 8c5a4dd36f3b0ec49473bb0026f1f0ae18d52884 Mon Sep 17 00:00:00 2001 From: joncrall Date: Mon, 23 Sep 2024 20:51:42 -0400 Subject: [PATCH] Attempt to work around windows issue --- line_profiler/_line_profiler.pyx | 2 ++ line_profiler/line_profiler.py | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/line_profiler/_line_profiler.pyx b/line_profiler/_line_profiler.pyx index acbae89..5438eb8 100644 --- a/line_profiler/_line_profiler.pyx +++ b/line_profiler/_line_profiler.pyx @@ -222,6 +222,8 @@ cdef class LineProfiler: self.code_hash_map = {} self.dupes_map = {} self.timer_unit = hpTimerUnit() + # Create a data store for thread-local objects + # https://docs.python.org/3/library/threading.html#thread-local-data self.threaddata = threading.local() for func in functions: diff --git a/line_profiler/line_profiler.py b/line_profiler/line_profiler.py index bb30198..84bb5e4 100755 --- a/line_profiler/line_profiler.py +++ b/line_profiler/line_profiler.py @@ -432,9 +432,17 @@ def show_func(filename, start_lineno, func_name, timings, unit, else: for lineno, line in zip(linenos, sublines): nhits, time, per_hit, percent = display.get(lineno, empty) - txt = template % (lineno, nhits, time, per_hit, percent, - line.rstrip('\n').rstrip('\r')) - stream.write(txt) + line_ = line.rstrip('\n').rstrip('\r') + txt = template % (lineno, nhits, time, per_hit, percent, line_) + try: + stream.write(txt) + except UnicodeEncodeError as ex: + # todo: better handling of windows encoding issue + # for now just work around it + line_ = f'{ex!r} - help wanted for a fix' + txt = template % (lineno, nhits, time, per_hit, percent, line_) + stream.write(txt) + stream.write('\n') stream.write('\n')