Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e47172d

Browse filesBrowse files
wenghionelmc
authored andcommitted
Avoid repeated code compilation
1 parent 9f6f864 commit e47172d
Copy full SHA for e47172d

File tree

1 file changed

+24
-13
lines changed
Filter options

1 file changed

+24
-13
lines changed

‎src/tblib/__init__.py

Copy file name to clipboardExpand all lines: src/tblib/__init__.py
+24-13Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,27 +117,38 @@ def as_traceback(self):
117117
current = self
118118
top_tb = None
119119
tb = None
120+
stub = compile(
121+
'raise __traceback_maker',
122+
'<string>',
123+
'exec',
124+
)
120125
while current:
121126
f_code = current.tb_frame.f_code
122-
code = compile('\n' * (current.tb_lineno - 1) + 'raise __traceback_maker', current.tb_frame.f_code.co_filename, 'exec')
123-
if hasattr(code, 'replace'):
127+
if hasattr(stub, 'replace'):
124128
# Python 3.8 and newer
125-
code = code.replace(co_argcount=0, co_filename=f_code.co_filename, co_name=f_code.co_name, co_freevars=(), co_cellvars=())
129+
code = stub.replace(
130+
co_firstlineno=current.tb_lineno,
131+
co_argcount=0,
132+
co_filename=f_code.co_filename,
133+
co_name=f_code.co_name,
134+
co_freevars=(),
135+
co_cellvars=(),
136+
)
126137
else:
127138
code = CodeType(
128139
0,
129-
code.co_kwonlyargcount,
130-
code.co_nlocals,
131-
code.co_stacksize,
132-
code.co_flags,
133-
code.co_code,
134-
code.co_consts,
135-
code.co_names,
136-
code.co_varnames,
140+
stub.co_kwonlyargcount,
141+
stub.co_nlocals,
142+
stub.co_stacksize,
143+
stub.co_flags,
144+
stub.co_code,
145+
stub.co_consts,
146+
stub.co_names,
147+
stub.co_varnames,
137148
f_code.co_filename,
138149
f_code.co_name,
139-
code.co_firstlineno,
140-
code.co_lnotab,
150+
current.tb_lineno,
151+
stub.co_lnotab,
141152
(),
142153
(),
143154
)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.