Skip to content

Commit c7fb24d

Browse files
committed
fixes #33
1 parent bc99b16 commit c7fb24d

File tree

2 files changed

+14
-42
lines changed

2 files changed

+14
-42
lines changed

01_funccall.ipynb

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,14 +1542,14 @@
15421542
"output_type": "stream",
15431543
"text": [
15441544
"Traceback (most recent call last):\n",
1545-
" File \"/var/folders/5c/jls7k26j1tq6l03cl_kvpnwh0000gn/T/ipykernel_97636/2963369439.py\", line 14, in python\n",
1545+
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_62370/2963369439.py\", line 14, in python\n",
15461546
" try: return _run(code, glb, loc)\n",
15471547
" ^^^^^^^^^^^^^^^^^^^^\n",
1548-
" File \"/var/folders/5c/jls7k26j1tq6l03cl_kvpnwh0000gn/T/ipykernel_97636/1858893181.py\", line 18, in _run\n",
1548+
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_62370/1858893181.py\", line 18, in _run\n",
15491549
" try: exec(compiled_code, glb, loc)\n",
15501550
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
15511551
" File \"<ast>\", line 1, in <module>\n",
1552-
" File \"/var/folders/5c/jls7k26j1tq6l03cl_kvpnwh0000gn/T/ipykernel_97636/2963369439.py\", line 9, in handler\n",
1552+
" File \"/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/ipykernel_62370/2963369439.py\", line 9, in handler\n",
15531553
" def handler(*args): raise TimeoutError()\n",
15541554
" ^^^^^^^^^^^^^^^^^^^^\n",
15551555
"TimeoutError\n",
@@ -1841,9 +1841,11 @@
18411841
" \"Call the function `fc_name` with the given `fc_inputs` using namespace `ns`.\"\n",
18421842
" if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)\n",
18431843
" func = ns[fc_name]\n",
1844+
" # Clean up bad param names\n",
1845+
" inps = {re.sub(r'\\W', '', k):v for k,v in fc_inputs.items()}\n",
18441846
" try: return func(**fc_inputs)\n",
18451847
" except Exception as e:\n",
1846-
" if raise_on_err: raise e\n",
1848+
" if raise_on_err: raise e from None\n",
18471849
" else: return traceback.format_exc()"
18481850
]
18491851
},
@@ -1917,39 +1919,6 @@
19171919
"test_fail(call_func, args=['subs', {'a': 1, 'b': '3'}], kwargs={'ns': mk_ns(d)})"
19181920
]
19191921
},
1920-
{
1921-
"cell_type": "code",
1922-
"execution_count": null,
1923-
"id": "b19298ac",
1924-
"metadata": {},
1925-
"outputs": [],
1926-
"source": [
1927-
"%%ai\n",
1928-
"How do I get the whole traceback of an error instead of just str(e) like above?"
1929-
]
1930-
},
1931-
{
1932-
"cell_type": "markdown",
1933-
"id": "6ec89b42",
1934-
"metadata": {},
1935-
"source": [
1936-
"To get the whole traceback of an error instead of just `str(e)`, you can use the `traceback` module, which you've already imported in your code. Modify the `call_func` function to capture and return the full traceback when an error occurs:\n",
1937-
"\n",
1938-
"```python\n",
1939-
"#| exports\n",
1940-
"def call_func(fc_name, fc_inputs, ns, raise_on_err=True):\n",
1941-
" \"Call the function `fc_name` with the given `fc_inputs` using namespace `ns`.\"\n",
1942-
" if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)\n",
1943-
" func = ns[fc_name]\n",
1944-
" try: return func(**fc_inputs)\n",
1945-
" except Exception as e:\n",
1946-
" if raise_on_err: raise e\n",
1947-
" else: return traceback.format_exc()\n",
1948-
"```\n",
1949-
"\n",
1950-
"This replaces `str(e)` with `traceback.format_exc()`, which returns the full traceback as a string, including the error type, message, and the call stack that led to the error. This gives you much more context about where and why the error occurred."
1951-
]
1952-
},
19531922
{
19541923
"cell_type": "markdown",
19551924
"id": "591574b8-6b53-4908-8159-b87be42133f7",
@@ -2002,7 +1971,7 @@
20021971
" if inspect.iscoroutine(res):\n",
20031972
" try: res = await res\n",
20041973
" except Exception as e:\n",
2005-
" if raise_on_err: raise e\n",
1974+
" if raise_on_err: raise e from None\n",
20061975
" else: return traceback.format_exc()\n",
20071976
" return res"
20081977
]
@@ -2035,7 +2004,8 @@
20352004
"metadata": {},
20362005
"outputs": [],
20372006
"source": [
2038-
"test_eq(await call_func_async('asums', {'a': 1, 'b': '2'}, ns=[asums], raise_on_err=False), \"unsupported operand type(s) for +: 'int' and 'str'\")"
2007+
"r = await call_func_async('asums', {'a': 1, 'b': '2'}, ns=[asums], raise_on_err=False)\n",
2008+
"assert \"unsupported operand type(s) for +: 'int' and 'str'\" in r"
20392009
]
20402010
},
20412011
{

toolslm/funccall.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,20 @@ def call_func(fc_name, fc_inputs, ns, raise_on_err=True):
195195
"Call the function `fc_name` with the given `fc_inputs` using namespace `ns`."
196196
if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)
197197
func = ns[fc_name]
198+
# Clean up bad param names
199+
inps = {re.sub(r'\W', '', k):v for k,v in fc_inputs.items()}
198200
try: return func(**fc_inputs)
199201
except Exception as e:
200-
if raise_on_err: raise e
202+
if raise_on_err: raise e from None
201203
else: return traceback.format_exc()
202204

203-
# %% ../01_funccall.ipynb 106
205+
# %% ../01_funccall.ipynb 104
204206
async def call_func_async(fc_name, fc_inputs, ns, raise_on_err=True):
205207
"Awaits the function `fc_name` with the given `fc_inputs` using namespace `ns`."
206208
res = call_func(fc_name, fc_inputs, ns, raise_on_err=raise_on_err)
207209
if inspect.iscoroutine(res):
208210
try: res = await res
209211
except Exception as e:
210-
if raise_on_err: raise e
212+
if raise_on_err: raise e from None
211213
else: return traceback.format_exc()
212214
return res

0 commit comments

Comments
 (0)