File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed
Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change 44
55from __future__ import annotations
66
7+ import linecache
78import re
9+ import secrets
810import textwrap
911from contextlib import contextmanager , nullcontext
1012from typing import (
@@ -216,7 +218,20 @@ def _make_function(
216218 """Wrap the current body contents in a function definition and turn it into a callable object"""
217219 source = f"def { fn_name } ({ source_value_name } ):\n { self ._body } return { return_value_name } "
218220 ctx : dict [str , Any ] = {** self ._fn_locals }
219- exec (source , ctx ) # noqa: S102
221+
222+ # add the function to linecache, to get better stacktraces when an error occurs
223+ # otherwise, the traceback within the generated code will just point
224+ # to '<string>'
225+ file_name = f"dto_transfer_function_{ secrets .token_hex (6 )} "
226+ linecache .cache [file_name ] = (
227+ len (source ),
228+ None , # mtime: not applicable
229+ [line + "\n " for line in source .splitlines ()],
230+ file_name ,
231+ )
232+ code = compile (source , file_name , "exec" )
233+ exec (code , ctx ) # noqa: S102
234+
220235 return ctx ["func" ] # type: ignore[no-any-return]
221236
222237 def _add_stmt (self , stmt : str ) -> None :
You can’t perform that action at this time.
0 commit comments