Skip to content

Commit

Permalink
Fix AttributeError on certain methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Theelx committed Dec 5, 2022
1 parent 83e4332 commit d0330c4
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions line_profiler/_line_profiler.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ cpdef _code_replace(func, code, co_code):
"""
Implements CodeType.replace for Python < 3.8
"""
code = func.__code__
try:
code = func.__code__
except AttributeError:
code = func.__func__.__code__
if hasattr(code, 'replace'):
# python 3.8+
code = func.__code__.replace(co_code=co_code)
Expand Down Expand Up @@ -208,17 +211,23 @@ cdef class LineProfiler:
func = func.__func__
code = func.__code__
except AttributeError:
import warnings
warnings.warn("Could not extract a code object for the object %r" % (func,))
return
try:
code = func.__func__.__code__
except AttributeError:
import warnings
warnings.warn("Could not extract a code object for the object %r" % (func,))
return

if code.co_code in self.dupes_map:
self.dupes_map[code.co_code] += [code]
# code hash already exists, so there must be a duplicate function. add no-op
co_code = code.co_code + (9).to_bytes(1, byteorder=byteorder) * (len(self.dupes_map[code.co_code]))
CodeType = type(code)
code = _code_replace(func, code, co_code=co_code)
func.__code__ = code
try:
func.__code__ = code
except AttributeError as e:
func.__func__.__code__ = code
else:
self.dupes_map[code.co_code] = [code]
# TODO: Since each line can be many bytecodes, this is kinda inefficient
Expand Down

0 comments on commit d0330c4

Please sign in to comment.