Skip to content

Commit

Permalink
add ppp support and ensure returns can't error
Browse files Browse the repository at this point in the history
  • Loading branch information
lacraig2 committed May 9, 2024
1 parent 806fdaf commit 104caa5
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions panda/python/core/pandare/panda.py
Original file line number Diff line number Diff line change
Expand Up @@ -2737,7 +2737,6 @@ def _run_and_catch(*args, **kwargs): # Run function but if it raises an exceptio
try:
return self.ffi.cast(return_type, r)
except TypeError:
print("TypeError in _run_and_catch")
# consider throwing an exception
return self.ffi.cast(return_type, 0)
except Exception as e:
Expand All @@ -2748,18 +2747,14 @@ def _run_and_catch(*args, **kwargs): # Run function but if it raises an exceptio
self.end_analysis()
else:
raise e
return return_from_exception
if return_type is not None:
return self.ffi.cast(return_type, 0)

cast_rc = pandatype(_run_and_catch)
cast_rc_string = str(self.ffi.typeof(cast_rc))
return_type = self.ffi.typeof(cast_rc).result

if return_type.cname == "void":
return_type = None

return_from_exception = 0
if "void(*)(" in cast_rc_string:
return_from_exception = None

self.register_callback(pandatype, cast_rc, local_name, enabled=enabled, procname=procname)
def wrapper(*args, **kw):
Expand Down Expand Up @@ -2989,7 +2984,11 @@ def _run_and_catch(*args, **kwargs): # Run function but if it raises an exceptio
r = fun(*args, **kwargs)
#print(pandatype, type(r)) # XXX Can we use pandatype to determine requried return and assert if incorrect
#assert(isinstance(r, int)), "Invalid return type?"
return r
try:
return self.ffi.cast(return_type, r)
except TypeError:
# consider throwing an exception
return self.ffi.cast(return_type, 0)
except Exception as e:
# exceptions wont work in our thread. Therefore we print it here and then throw it after the
# machine exits.
Expand All @@ -2999,8 +2998,15 @@ def _run_and_catch(*args, **kwargs): # Run function but if it raises an exceptio
else:
raise e
# this works in all current callback cases. CFFI auto-converts to void, bool, int, and int32_t
if return_type is not None:
return self.ffi.cast(return_type, 0)

cast_rc = self.ffi.callback(attr+"_t")(_run_and_catch) # Wrap the python fn in a c-callback.
return_type = self.ffi.typeof(cast_rc).result

if return_type.cname == "void":
return_type = None

if local_name == "<lambda>":
local_name = f"<lambda_{self.lambda_cnt}>"
self.lambda_cnt += 1
Expand Down

0 comments on commit 104caa5

Please sign in to comment.