AVSLab / basilisk

Astrodynamics simulation framework
https://hanspeterschaub.info/basilisk
ISC License
138 stars 60 forks source link

BSK not yet compatible with python 3.12 #765

Open schaubh opened 2 months ago

schaubh commented 2 months ago

Describe your use case Python 3.12 is not yet supported. Need to work out updates to make Basilisk compatible with 3.12 and retain prior python compatibility.

sassy-asjp commented 6 days ago

I suspected the segfault under python3.12 was related to how SysModel is set up in SWIG separately for cSysModel and sysModel modules and hacked something together to test my suspicion. This diff fixes the segfault in python3.12, but isn't a proper thing that can be merged.

I'm not actively working on python3.12 support, but if anyone is, I'm sharing this snippet in case it is useful.

diff --git a/pyproject.toml b/pyproject.toml
index fe1a90f27..811e56264 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -14,7 +14,7 @@ requires = [
 [project]
 name        = 'Basilisk'
 dynamic     = ["version", "dependencies", "optional-dependencies"]
-requires-python = ">=3.8, <3.12"
+requires-python = ">=3.8, <3.13"

 readme      = "README.md"
 license     = {file = "LICENSE"}
diff --git a/src/architecture/_GeneralModuleFiles/py_sys_model.i b/src/architecture/_GeneralModuleFiles/py_sys_model.i
deleted file mode 100644
index 71b4a0b5e..000000000
--- a/src/architecture/_GeneralModuleFiles/py_sys_model.i
+++ /dev/null
@@ -1,57 +0,0 @@
-
-%module(directors="1") sysModel
-%{
-   #include "sys_model.h"
-%}
-
-%pythoncode %{
-import sys
-import traceback
-from Basilisk.architecture.swig_common_model import *
-%}
-%include "std_string.i"
-%include "swig_conly_data.i"
-%include "architecture/utilities/bskLogging.h"
-
-%feature("director") SysModel;
-%feature("pythonappend") SysModel::SysModel %{
-    self.__super_init_called__ = True%}
-%rename("_SysModel") SysModel;
-%include "sys_model.i"
-
-%pythoncode %{
-class SuperInitChecker(type):
-
-    def __call__(cls, *a, **kw):
-        rv = super(SuperInitChecker, cls).__call__(*a, **kw)
-        if not getattr(rv, "__super_init_called__", False):
-            error_msg = (
-               "Need to call parent __init__ in SysModel subclasses:\n"
-               f"class {cls.__name__}(sysModel.SysModel):\n"
-               "    def __init__(...):\n"
-               "        super().__init__()"
-            )
-            raise SyntaxError(error_msg)
-        return rv
-
-def logError(func):
-    """Decorator that prints any exceptions that happen when
-    the original function is called, and then raises them again."""
-    def inner(*arg, **kwargs):
-        try:
-            return func(*arg, **kwargs)
-        except Exception:
-            traceback.print_exc()
-            raise
-    return inner
-
-class SysModel(_SysModel, metaclass=SuperInitChecker):
-    bskLogger: BSKLogger = None
-
-    def __init_subclass__(cls):
-        # Make it so any exceptions in UpdateState and Reset
-        # print any exceptions before returning control to
-        # C++ (at which point exceptions will crash the program)
-        cls.UpdateState = logError(cls.UpdateState)
-        cls.Reset = logError(cls.Reset)
-%}
diff --git a/src/architecture/_GeneralModuleFiles/sys_model.i b/src/architecture/_GeneralModuleFiles/sys_model.i
index 63cf55ed7..639ea34cc 100644
--- a/src/architecture/_GeneralModuleFiles/sys_model.i
+++ b/src/architecture/_GeneralModuleFiles/sys_model.i
@@ -1,5 +1,5 @@

-%module cSysModel
+%module(directors="1") sysModel
 %{
    #include "sys_model.h"
 %}
@@ -18,6 +18,8 @@ from Basilisk.architecture.swig_common_model import *
 from typing import Union, Iterable
 %}

+%feature("director") SysModel;
+
 %extend SysModel
 {
     %pythoncode %{
schaubh commented 6 days ago

Thanks for sharing your insight @sassy-asjp .