Skip to content

Commit

Permalink
raise ImportError if C and Python sizes of Simulation don't match.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannorein committed Sep 1, 2024
1 parent 5716b9a commit a548a27
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rebound/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
except:
# Might fail in some python3 setups, but not important
pass

# Exceptions
class GenericError(Exception):
"""The simulation exited with a generic error."""
Expand Down
9 changes: 8 additions & 1 deletion rebound/simulation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ctypes import Structure, c_double, POINTER, c_uint32, c_int, c_uint, c_int64, c_uint64, c_void_p, c_char_p, CFUNCTYPE, byref, create_string_buffer, addressof, c_char, c_size_t, string_at
from ctypes import Structure, c_double, POINTER, c_uint32, c_int, c_uint, c_int64, c_uint64, c_void_p, c_char_p, CFUNCTYPE, byref, create_string_buffer, addressof, c_char, c_size_t, string_at, sizeof
from . import clibrebound, Escape, NoParticles, Encounter, Collision, GenericError
from .citations import cite
from .units import units_convert_particle, check_units, convert_G, hash_to_unit
Expand Down Expand Up @@ -1578,6 +1578,13 @@ class ServerData(Structure):
COLRFF = CFUNCTYPE(c_int, POINTER(Simulation), CollisionS)
FPA = CFUNCTYPE(None, POINTER(Particle))

# Check if Simulation has same size upon import. Nothing will work if there is a mismatch.
clibrebound.reb_simulation_struct_size.res_type = c_size_t
simulation_size_c = clibrebound.reb_simulation_struct_size()

if simulation_size_c != sizeof(Simulation):
raise ImportError("Size of C struct `reb_simulation` differs from size of python class `Simulation`. This will lead to undefined behaviour. Make sure the C and Python versions of REBOUND are up-to-date and consistent. If you have installed REBOUND from source, you might need to recompile / reinstall the python package.")


# Import at the end to avoid circular dependence
from . import horizons
Expand Down

0 comments on commit a548a27

Please sign in to comment.