pythonfmu3 0.3.4__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. pythonfmu3/_version.py +1 -1
  2. pythonfmu3/builder.py +96 -24
  3. pythonfmu3/fmi3slave.py +23 -19
  4. pythonfmu3/modelexchange.py +17 -2
  5. pythonfmu3/osutil.py +34 -9
  6. pythonfmu3/pythonfmu-export/CMakeLists.txt +36 -4
  7. pythonfmu3/pythonfmu-export/generate_shim.py +88 -0
  8. pythonfmu3/pythonfmu-export/src/CMakeLists.txt +41 -13
  9. pythonfmu3/pythonfmu-export/src/cppfmu/cppfmu_common.hpp +3 -0
  10. pythonfmu3/pythonfmu-export/src/cppfmu/fmi_functions.cpp +6 -4
  11. pythonfmu3/pythonfmu-export/src/pythonfmu/PySlaveInstance.cpp +560 -571
  12. pythonfmu3/pythonfmu-export/src/pythonfmu/PySlaveInstance.hpp +20 -7
  13. pythonfmu3/pythonfmu-export/src/shim/fmi3_forwarders.cpp +612 -0
  14. pythonfmu3/pythonfmu-export/src/shim/shim_loader.cpp +157 -0
  15. pythonfmu3/pythonfmu-export/src/shim/shim_loader.hpp +29 -0
  16. pythonfmu3/resources/binaries/x86_64-linux/libpythonfmu-backend.so +0 -0
  17. pythonfmu3/resources/binaries/x86_64-linux/libpythonfmu-export.so +0 -0
  18. pythonfmu3/resources/binaries/x86_64-windows/pythonfmu-export.dll +0 -0
  19. pythonfmu3/tests/data/fmi3_host.c +31 -0
  20. pythonfmu3/tests/fmi3_ctypes_driver.py +137 -0
  21. pythonfmu3/tests/test_builder.py +48 -2
  22. pythonfmu3/tests/test_docs.py +70 -61
  23. pythonfmu3/tests/test_non_python_host.py +75 -0
  24. pythonfmu3/tests/test_osutil.py +23 -0
  25. pythonfmu3/tests/test_variables.py +1 -1
  26. pythonfmu3/tests/test_wrapper_ctypes.py +61 -0
  27. pythonfmu3/variables.py +4 -3
  28. {pythonfmu3-0.3.4.dist-info → pythonfmu3-1.0.0.dist-info}/METADATA +61 -25
  29. {pythonfmu3-0.3.4.dist-info → pythonfmu3-1.0.0.dist-info}/RECORD +33 -23
  30. {pythonfmu3-0.3.4.dist-info → pythonfmu3-1.0.0.dist-info}/WHEEL +1 -1
  31. {pythonfmu3-0.3.4.dist-info → pythonfmu3-1.0.0.dist-info}/entry_points.txt +0 -0
  32. {pythonfmu3-0.3.4.dist-info → pythonfmu3-1.0.0.dist-info}/licenses/LICENSE +0 -0
  33. {pythonfmu3-0.3.4.dist-info → pythonfmu3-1.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,157 @@
1
+ // Linux/macOS loader shim.
2
+ //
3
+ // The importer loads this library (binaries/<platform>/<modelIdentifier>.so|.dylib). It
4
+ // deliberately has no link-time dependency on Python, so it can be loaded by any FMI
5
+ // importer (OMSimulator, fmusim, Simulink, ...), not just Python-based ones.
6
+ //
7
+ // On the first instantiation it:
8
+ // 1. makes the Python C API available process-wide: if the host process does not
9
+ // already provide it (Python-based importers such as FMPy do), libpython is opened
10
+ // with RTLD_GLOBAL, which also lets extension modules such as numpy resolve it;
11
+ // 2. loads the backend library (libpythonfmu-backend) from the same folder as this
12
+ // library, and resolves the FMI functions it implements.
13
+ //
14
+ // libpython is searched for in this order:
15
+ // - the PYTHONFMU_PYTHON_LIBRARY environment variable (a full path),
16
+ // - resources/pythonfmu_libpython.txt (written by the builder: the library of the
17
+ // Python that built the FMU, one candidate path per line),
18
+ // - common library names for Python 3.8 - 3.14.
19
+
20
+ #include "shim/shim_loader.hpp"
21
+
22
+ #include <dlfcn.h>
23
+
24
+ #include <cstdlib>
25
+ #include <fstream>
26
+ #include <mutex>
27
+ #include <sstream>
28
+ #include <string>
29
+ #include <vector>
30
+
31
+ namespace pythonfmu_shim
32
+ {
33
+ namespace
34
+ {
35
+
36
+ #ifdef __APPLE__
37
+ const char* const kBackendFile = "libpythonfmu-backend.dylib";
38
+ #else
39
+ const char* const kBackendFile = "libpythonfmu-backend.so";
40
+ #endif
41
+
42
+ std::mutex g_mutex;
43
+ bool g_loaded = false;
44
+ std::vector<void*> g_functions;
45
+
46
+ bool pythonAlreadyAvailable()
47
+ {
48
+ return dlsym(RTLD_DEFAULT, "Py_IsInitialized") != nullptr;
49
+ }
50
+
51
+ std::string directoryOfThisLibrary()
52
+ {
53
+ Dl_info info{};
54
+ if (dladdr(reinterpret_cast<void*>(&directoryOfThisLibrary), &info) == 0 || info.dli_fname == nullptr) {
55
+ return ".";
56
+ }
57
+ std::string path = info.dli_fname;
58
+ const auto slash = path.find_last_of('/');
59
+ return slash == std::string::npos ? "." : path.substr(0, slash);
60
+ }
61
+
62
+ std::string resourceDirectory(const char* resourcePath)
63
+ {
64
+ if (resourcePath == nullptr) return {};
65
+ std::string path = resourcePath;
66
+ const std::string scheme = "file://";
67
+ if (path.compare(0, scheme.size(), scheme) == 0) path.erase(0, scheme.size());
68
+ while (path.size() > 1 && path.back() == '/') path.pop_back();
69
+ return path;
70
+ }
71
+
72
+ std::vector<std::string> libpythonCandidates(const std::string& resources)
73
+ {
74
+ std::vector<std::string> candidates;
75
+ if (const char* env = std::getenv("PYTHONFMU_PYTHON_LIBRARY")) {
76
+ if (*env != '\0') candidates.emplace_back(env);
77
+ }
78
+ if (!resources.empty()) {
79
+ std::ifstream hints(resources + "/pythonfmu_libpython.txt");
80
+ std::string line;
81
+ while (std::getline(hints, line)) {
82
+ while (!line.empty() && (line.back() == '\r' || line.back() == ' ')) line.pop_back();
83
+ if (!line.empty()) candidates.push_back(line);
84
+ }
85
+ }
86
+ for (int minor = 14; minor >= 8; --minor) {
87
+ const std::string v = "3." + std::to_string(minor);
88
+ #ifdef __APPLE__
89
+ candidates.push_back("libpython" + v + ".dylib");
90
+ candidates.push_back("/opt/homebrew/opt/python@" + v + "/Frameworks/Python.framework/Versions/" + v + "/Python");
91
+ candidates.push_back("/usr/local/opt/python@" + v + "/Frameworks/Python.framework/Versions/" + v + "/Python");
92
+ candidates.push_back("/Library/Frameworks/Python.framework/Versions/" + v + "/Python");
93
+ #else
94
+ candidates.push_back("libpython" + v + ".so.1.0");
95
+ candidates.push_back("libpython" + v + ".so");
96
+ #endif
97
+ }
98
+ return candidates;
99
+ }
100
+
101
+ void report(fmi3InstanceEnvironment env, fmi3LogMessageCallback logMessage, const std::string& msg)
102
+ {
103
+ if (logMessage != nullptr) logMessage(env, fmi3Fatal, "logStatusFatal", msg.c_str());
104
+ }
105
+
106
+ } // namespace
107
+
108
+ bool ensureBackendLoaded(fmi3String resourcePath, fmi3InstanceEnvironment env, fmi3LogMessageCallback logMessage)
109
+ {
110
+ std::lock_guard<std::mutex> lock(g_mutex);
111
+ if (g_loaded) return true;
112
+
113
+ if (!pythonAlreadyAvailable()) {
114
+ std::ostringstream errors;
115
+ bool found = false;
116
+ for (const auto& candidate : libpythonCandidates(resourceDirectory(resourcePath))) {
117
+ if (dlopen(candidate.c_str(), RTLD_NOW | RTLD_GLOBAL) != nullptr && pythonAlreadyAvailable()) {
118
+ found = true;
119
+ break;
120
+ }
121
+ const char* err = dlerror();
122
+ errors << "\n " << candidate << ": " << (err ? err : "loaded, but Py_IsInitialized not found");
123
+ }
124
+ if (!found) {
125
+ report(env, logMessage,
126
+ "PythonFMU3: unable to load the Python shared library (libpython). Set the "
127
+ "PYTHONFMU_PYTHON_LIBRARY environment variable to its full path. Tried:" + errors.str());
128
+ return false;
129
+ }
130
+ }
131
+
132
+ const std::string backendPath = directoryOfThisLibrary() + "/" + kBackendFile;
133
+ // Never closed: the backend keeps the Python interpreter (and a thread) alive.
134
+ void* backend = dlopen(backendPath.c_str(), RTLD_NOW | RTLD_LOCAL);
135
+ if (backend == nullptr) {
136
+ const char* err = dlerror();
137
+ report(env, logMessage, "PythonFMU3: unable to load " + backendPath + ": " + (err ? err : "unknown error"));
138
+ return false;
139
+ }
140
+
141
+ std::vector<void*> functions(kFunctionCount, nullptr);
142
+ for (std::size_t i = 0; i < kFunctionCount; i++) {
143
+ functions[i] = dlsym(backend, (std::string(kBackendPrefix) + kFunctionNames[i]).c_str());
144
+ }
145
+ g_functions = std::move(functions);
146
+ g_loaded = true;
147
+ return true;
148
+ }
149
+
150
+ void* function(std::size_t index)
151
+ {
152
+ // g_functions is written once, under the mutex, before any instance exists; every
153
+ // later call is made on an instance and therefore happens after that write.
154
+ return index < g_functions.size() ? g_functions[index] : nullptr;
155
+ }
156
+
157
+ } // namespace pythonfmu_shim
@@ -0,0 +1,29 @@
1
+ #ifndef PYTHONFMU_SHIM_LOADER_HPP
2
+ #define PYTHONFMU_SHIM_LOADER_HPP
3
+
4
+ // Linux/macOS only. See shim_loader.cpp.
5
+
6
+ #include "fmi/fmi3Functions.h"
7
+
8
+ #include <cstddef>
9
+
10
+ namespace pythonfmu_shim
11
+ {
12
+
13
+ // Symbol prefix the backend library is compiled with (FMI3_FUNCTION_PREFIX).
14
+ constexpr const char* kBackendPrefix = "pythonfmu_backend_";
15
+
16
+ // Generated (fmi3_forwarders.cpp): FMI functions forwarded to the backend, by index.
17
+ extern const char* const kFunctionNames[];
18
+ extern const std::size_t kFunctionCount;
19
+
20
+ // Makes libpython available (if needed) and loads the backend library. Thread-safe and
21
+ // idempotent. On failure, reports through logMessage (if non-null) and returns false.
22
+ bool ensureBackendLoaded(fmi3String resourcePath, fmi3InstanceEnvironment env, fmi3LogMessageCallback logMessage);
23
+
24
+ // Backend implementation of kFunctionNames[index], or nullptr if not loaded/available.
25
+ void* function(std::size_t index);
26
+
27
+ } // namespace pythonfmu_shim
28
+
29
+ #endif
@@ -0,0 +1,31 @@
1
+ /* Minimal non-Python FMI 3 importer used by test_non_python_host.py.
2
+ usage: fmi3_host <fmu library> <resources dir> (runs pythonslave.py for 3 steps) */
3
+ #include <dlfcn.h>
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include "fmi3Functions.h"
7
+
8
+ static void logger(fmi3InstanceEnvironment env, fmi3Status s, fmi3String cat, fmi3String msg) {
9
+ printf("[log %d %s] %s\n", s, cat ? cat : "", msg);
10
+ }
11
+ #define LOAD(name) name##TYPE* name = (name##TYPE*)dlsym(h, #name); if (!name) { printf("missing %s\n", #name); return 1; }
12
+
13
+ int main(int argc, char** argv) {
14
+ void* h = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);
15
+ if (!h) { printf("dlopen failed: %s\n", dlerror()); return 1; }
16
+ LOAD(fmi3GetVersion) LOAD(fmi3InstantiateCoSimulation) LOAD(fmi3EnterInitializationMode)
17
+ LOAD(fmi3ExitInitializationMode) LOAD(fmi3DoStep) LOAD(fmi3GetFloat64) LOAD(fmi3FreeInstance)
18
+ printf("version %s\n", fmi3GetVersion());
19
+ fmi3Instance c = fmi3InstantiateCoSimulation("inst", "", argv[2], fmi3False, fmi3True, fmi3False, fmi3False,
20
+ NULL, 0, NULL, logger, NULL);
21
+ if (!c) { printf("instantiate failed\n"); return 2; }
22
+ fmi3EnterInitializationMode(c, fmi3False, 0, 0, fmi3False, 0);
23
+ fmi3ExitInitializationMode(c);
24
+ fmi3Boolean ehn, term, early; fmi3Float64 last;
25
+ for (int i = 0; i < 3; i++) fmi3DoStep(c, i * 0.5, 0.5, fmi3True, &ehn, &term, &early, &last);
26
+ fmi3ValueReference vr = 6; fmi3Float64 out = -1;
27
+ fmi3Status st = fmi3GetFloat64(c, &vr, 1, &out, 1);
28
+ printf("status %d realOut %g\n", st, out);
29
+ fmi3FreeInstance(c);
30
+ return (st == fmi3OK && out == 1.5) ? 0 : 3;
31
+ }
@@ -0,0 +1,137 @@
1
+ """Minimal ctypes FMI 3 co-simulation driver used by test_wrapper_ctypes.py.
2
+
3
+ usage: python fmi3_ctypes_driver.py <scenario> <extracted_fmu_dir>
4
+ """
5
+ import ctypes as C
6
+ import sys
7
+ from pathlib import Path
8
+
9
+ scenario, fmu_dir = sys.argv[1], Path(sys.argv[2])
10
+ lib_path = next(p for p in (fmu_dir / "binaries").rglob("*.so") if "pythonfmu-backend" not in p.name)
11
+ lib = C.CDLL(str(lib_path))
12
+
13
+ LOGCB = C.CFUNCTYPE(None, C.c_void_p, C.c_int, C.c_char_p, C.c_char_p)
14
+ logs = []
15
+ @LOGCB
16
+ def logger(env, status, category, message):
17
+ logs.append((status, (category or b"").decode(), message.decode()))
18
+
19
+ inst_fn = lib.fmi3InstantiateCoSimulation
20
+ inst_fn.restype = C.c_void_p
21
+ inst_fn.argtypes = [C.c_char_p, C.c_char_p, C.c_char_p, C.c_bool, C.c_bool, C.c_bool, C.c_bool,
22
+ C.c_void_p, C.c_size_t, C.c_void_p, LOGCB, C.c_void_p]
23
+ lib.fmi3FreeInstance.argtypes = [C.c_void_p]
24
+ for name in ["fmi3EnterInitializationMode"]:
25
+ getattr(lib, name).argtypes = [C.c_void_p, C.c_bool, C.c_double, C.c_double, C.c_bool, C.c_double]
26
+ for name in ["fmi3ExitInitializationMode", "fmi3Terminate", "fmi3Reset"]:
27
+ getattr(lib, name).argtypes = [C.c_void_p]
28
+ lib.fmi3DoStep.argtypes = [C.c_void_p, C.c_double, C.c_double, C.c_bool,
29
+ C.POINTER(C.c_bool), C.POINTER(C.c_bool), C.POINTER(C.c_bool), C.POINTER(C.c_double)]
30
+ for t, ct in [("Float64", C.c_double), ("Int32", C.c_int32), ("Boolean", C.c_bool), ("String", C.c_char_p)]:
31
+ for op in ("Get", "Set"):
32
+ getattr(lib, f"fmi3{op}{t}").argtypes = [C.c_void_p, C.POINTER(C.c_uint32), C.c_size_t, C.POINTER(ct), C.c_size_t]
33
+ lib.fmi3GetFMUState.argtypes = [C.c_void_p, C.POINTER(C.c_void_p)]
34
+ lib.fmi3SetFMUState.argtypes = [C.c_void_p, C.c_void_p]
35
+ lib.fmi3FreeFMUState.argtypes = [C.c_void_p, C.POINTER(C.c_void_p)]
36
+
37
+ def vrs(*v):
38
+ return (C.c_uint32 * len(v))(*v)
39
+
40
+ def instantiate(logcb=logger, resources=None):
41
+ res = str(fmu_dir / "resources") if resources is None else resources
42
+ return inst_fn(b"inst", b"", res.encode(), False, True, False, False, None, 0, None, logcb, None)
43
+
44
+ def step(c, t, dt):
45
+ ehn, term, early, last = C.c_bool(True), C.c_bool(True), C.c_bool(True), C.c_double(-1)
46
+ st = lib.fmi3DoStep(c, t, dt, True, C.byref(ehn), C.byref(term), C.byref(early), C.byref(last))
47
+ return st, ehn.value, term.value, early.value, last.value
48
+
49
+ # value references for tests/slaves/pythonslave.py
50
+ VR_INTPARAM, VR_REALIN, VR_BOOLPARAM, VR_STRPARAM, VR_REALOUT = 1, 2, 3, 4, 6
51
+
52
+ if scenario == "basic":
53
+ c = instantiate()
54
+ assert c
55
+ assert lib.fmi3EnterInitializationMode(c, False, 0, 0, False, 0) == 0
56
+ assert lib.fmi3ExitInitializationMode(c) == 0
57
+ st, ehn, term, early, last = step(c, 0.0, 0.5)
58
+ assert st == 0 and (ehn, term, early) == (False, False, False), (st, ehn, term, early)
59
+ assert abs(last - 0.5) < 1e-12, last
60
+ out = (C.c_double * 1)()
61
+ assert lib.fmi3GetFloat64(c, vrs(VR_REALOUT), 1, out, 1) == 0
62
+ assert out[0] == 0.5, out[0]
63
+ iv = (C.c_int32 * 1)(7)
64
+ assert lib.fmi3SetInt32(c, vrs(VR_INTPARAM), 1, iv, 1) == 0
65
+ iv2 = (C.c_int32 * 1)()
66
+ assert lib.fmi3GetInt32(c, vrs(VR_INTPARAM), 1, iv2, 1) == 0 and iv2[0] == 7
67
+ sv = (C.c_char_p * 1)(b"cat")
68
+ assert lib.fmi3SetString(c, vrs(VR_STRPARAM), 1, sv, 1) == 0
69
+ sv2 = (C.c_char_p * 1)()
70
+ assert lib.fmi3GetString(c, vrs(VR_STRPARAM), 1, sv2, 1) == 0 and sv2[0] == b"cat"
71
+ bv = (C.c_bool * 1)(True)
72
+ assert lib.fmi3SetBoolean(c, vrs(VR_BOOLPARAM), 1, bv, 1) == 0
73
+ bv2 = (C.c_bool * 1)()
74
+ assert lib.fmi3GetBoolean(c, vrs(VR_BOOLPARAM), 1, bv2, 1) == 0 and bv2[0]
75
+ # FMU state round-trip, including overwriting an existing state
76
+ state = C.c_void_p()
77
+ assert lib.fmi3GetFMUState(c, C.byref(state)) == 0 and state.value
78
+ step(c, 0.5, 0.5)
79
+ assert lib.fmi3GetFMUState(c, C.byref(state)) == 0 # overwrite (old state must be released)
80
+ assert lib.fmi3SetFMUState(c, state) == 0
81
+ assert lib.fmi3FreeFMUState(c, C.byref(state)) == 0
82
+ assert lib.fmi3Reset(c) == 0
83
+ assert lib.fmi3Terminate(c) == 0
84
+ lib.fmi3FreeInstance(c)
85
+ print("OK basic")
86
+
87
+ elif scenario == "bad_vr_type":
88
+ # Asking Float64 for an Int32 variable raises TypeError in Python -> error status, no crash
89
+ c = instantiate()
90
+ out = (C.c_double * 1)()
91
+ st = lib.fmi3GetFloat64(c, vrs(VR_INTPARAM), 1, out, 1)
92
+ assert st in (3, 4), st
93
+ assert any("TypeError" in m for _, _, m in logs), logs
94
+ lib.fmi3FreeInstance(c) # previously: double Py_DECREF after an exception
95
+ print("OK bad_vr_type", "traceback logged:", any("Traceback" in m for _, _, m in logs))
96
+
97
+ elif scenario == "too_few_values":
98
+ # nValues larger than what Python returns -> was an out-of-bounds read
99
+ c = instantiate()
100
+ out = (C.c_double * 5)()
101
+ st = lib.fmi3GetFloat64(c, vrs(VR_REALOUT), 1, out, 5)
102
+ assert st != 0, st
103
+ lib.fmi3FreeInstance(c)
104
+ print("OK too_few_values", logs[-1][2][:80])
105
+
106
+ elif scenario == "exception_in_step":
107
+ c = instantiate()
108
+ lib.fmi3EnterInitializationMode(c, False, 0, 0, False, 0)
109
+ lib.fmi3ExitInitializationMode(c)
110
+ st = step(c, 0.0, 0.1)[0]
111
+ assert st == 4, st # fatal
112
+ assert any("RuntimeError" in m for _, _, m in logs), logs
113
+ lib.fmi3FreeInstance(c)
114
+ print("OK exception_in_step")
115
+
116
+ elif scenario == "null_logger":
117
+ c = inst_fn(b"inst", b"", str(fmu_dir / "resources").encode(), False, True, False, False,
118
+ None, 0, None, C.cast(None, LOGCB), None)
119
+ assert c
120
+ out = (C.c_double * 5)()
121
+ lib.fmi3GetFloat64(c, vrs(VR_REALOUT), 1, out, 5) # error path logs -> must not call NULL
122
+ lib.fmi3FreeInstance(c)
123
+ print("OK null_logger")
124
+
125
+ elif scenario == "bad_resources":
126
+ c = instantiate(resources="/does/not/exist")
127
+ assert not c
128
+ print("OK bad_resources", logs[-1][2][:60] if logs else "")
129
+
130
+ elif scenario == "uri_resources":
131
+ c = instantiate(resources="file://" + str(fmu_dir / "resources").replace(" ", "%20") + "/")
132
+ assert c
133
+ lib.fmi3FreeInstance(c)
134
+ print("OK uri_resources")
135
+
136
+ else:
137
+ raise SystemExit(f"unknown scenario {scenario}")
@@ -62,13 +62,13 @@ def test_zip_content(tmp_path):
62
62
  pkg_folder = Path(pythonfmu3.__path__[0])
63
63
  for f in pkg_folder.rglob("*.py"):
64
64
  relative_f = f.relative_to(pkg_folder).as_posix()
65
- if "test" not in relative_f:
65
+ if "test" not in relative_f and not relative_f.startswith("pythonfmu-export/"):
66
66
  assert "/".join(("resources", "pythonfmu3", relative_f)) in names
67
67
 
68
68
  assert len(names) >= nfiles # Library + python script + XML + module name + sources
69
69
 
70
70
  with files.open(module_file) as myfile:
71
- assert myfile.read() == b"pythonslave"
71
+ assert myfile.read().decode().splitlines() == ["pythonslave", "PythonSlave"]
72
72
 
73
73
 
74
74
  @pytest.mark.parametrize("pfiles", PROJECT_TEST_CASES)
@@ -193,3 +193,49 @@ def test_terminals(tmp_path):
193
193
  names = files.namelist()
194
194
 
195
195
  assert "terminalsAndIcons/terminalsAndIcons.xml" in names
196
+
197
+
198
+ def test_terminals_file_is_embedded(tmp_path):
199
+ script_file = Path(__file__).parent / "slaves/pythonslave.py"
200
+ terminals = tmp_path / "terminalsAndIcons.xml"
201
+ terminals.write_text("<fmiTerminalsAndIcons fmiVersion=\"3.0\"/>")
202
+ doc = tmp_path / "doc"
203
+ doc.mkdir()
204
+ (doc / "index.html").write_text("<html/>")
205
+
206
+ fmu = FmuBuilder.build_FMU(
207
+ script_file, dest=tmp_path, documentation_folder=doc, terminals=terminals
208
+ )
209
+ with zipfile.ZipFile(fmu) as files:
210
+ assert files.read("terminalsAndIcons/terminalsAndIcons.xml") == terminals.read_bytes()
211
+
212
+
213
+ def test_default_cosim_options_in_model_description(tmp_path):
214
+ from xml.etree.ElementTree import parse
215
+ from pythonfmu3.fmi3slave import FMI3_MODEL_OPTIONS_COSIM
216
+
217
+ script_file = Path(__file__).parent / "slaves/pythonslave.py"
218
+ fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path)
219
+ with zipfile.ZipFile(fmu) as files:
220
+ cs = parse(files.open("modelDescription.xml")).getroot().find("CoSimulation")
221
+ for option in FMI3_MODEL_OPTIONS_COSIM:
222
+ assert cs.get(option.name) == str(option.value).lower()
223
+
224
+
225
+ def test_class_with_multiline_bases(tmp_path):
226
+ script = tmp_path / "multiline.py"
227
+ script.write_text(
228
+ "from pythonfmu3 import Fmi3Slave, Float64, Fmi3Causality\n"
229
+ "class Multi(\n"
230
+ " Fmi3Slave,\n"
231
+ "):\n"
232
+ " def __init__(self, **kwargs):\n"
233
+ " super().__init__(**kwargs)\n"
234
+ " self.x = 1.0\n"
235
+ " self.register_variable(Float64('x', causality=Fmi3Causality.output))\n"
236
+ " def do_step(self, t, dt):\n"
237
+ " return True\n"
238
+ )
239
+ fmu = FmuBuilder.build_FMU(script, dest=tmp_path)
240
+ with zipfile.ZipFile(fmu) as files:
241
+ assert files.read("resources/slavemodule.txt").decode().splitlines() == ["multiline", "Multi"]
@@ -3,6 +3,45 @@ import pathlib
3
3
  import re
4
4
  import tempfile
5
5
 
6
+ from pythonfmu3 import Fmi3Causality, Fmi3SlaveBase, Fmi3Variability
7
+
8
+
9
+ def check_model(cls):
10
+ """Instantiate a documented model and check it would work as an FMU.
11
+
12
+ Every variable must be readable, inputs and non-constant parameters must be
13
+ settable, and the model description must be generated without errors.
14
+ """
15
+ instance = cls(instance_name="doc_test")
16
+ for var in instance.vars.values():
17
+ var.getter()
18
+ settable = var.causality in (Fmi3Causality.input, Fmi3Causality.parameter, Fmi3Causality.structuralParameter)
19
+ if settable and var.variability != Fmi3Variability.constant:
20
+ assert var.setter is not None, f"{cls.__name__}.{var.name}: no setter (is the attribute created before register_variable?)"
21
+ instance.to_xml()
22
+ return instance
23
+
24
+
25
+ def models_in(namespace):
26
+ return [
27
+ obj for obj in namespace.values()
28
+ if isinstance(obj, type) and issubclass(obj, Fmi3SlaveBase) and obj.__module__ == "__main__"
29
+ and obj.__name__ not in ("Fmi3Slave", "Fmi3SlaveBase")
30
+ ]
31
+
32
+
33
+ def run_blocks(md_file):
34
+ namespace = {"__name__": "__main__"}
35
+ exec("from pythonfmu3 import *", namespace)
36
+ for i, code in enumerate(extract_python_code_from_md(md_file)):
37
+ try:
38
+ exec(code, namespace)
39
+ except Exception as e:
40
+ pytest.fail(f"{pathlib.Path(md_file).name}: code block {i+1} failed: {e}\nCode:\n{code}")
41
+ for model in models_in(namespace):
42
+ check_model(model)
43
+ return namespace
44
+
6
45
  def extract_python_code_from_md(md_file):
7
46
  """Extract Python code blocks from markdown, excluding skip blocks"""
8
47
  content = pathlib.Path(md_file).read_text()
@@ -21,78 +60,48 @@ def extract_python_code_from_md(md_file):
21
60
 
22
61
  return python_blocks
23
62
 
63
+ ROOT = pathlib.Path(__file__).parent.parent.parent
64
+
65
+
66
+ def _doc(*parts):
67
+ path = ROOT.joinpath(*parts)
68
+ if not path.exists():
69
+ pytest.skip(f"{path.name} is only available in a source checkout")
70
+ return path
71
+
72
+
24
73
  @pytest.mark.doc
25
74
  def test_readme():
26
- test_dir = pathlib.Path(__file__).parent
27
- fpath = test_dir.parent.parent / "README.md"
28
-
29
- # extract Python code
30
- code_blocks = extract_python_code_from_md(fpath)
75
+ namespace = run_blocks(_doc("README.md"))
76
+ assert "PythonSlave" in namespace and "PythonSlaveMX" in namespace
31
77
 
32
- namespace = {}
33
- exec("from pythonfmu3 import *", namespace)
34
-
35
- for i, code in enumerate(code_blocks):
36
- try:
37
- exec(code, namespace)
38
- print(f"✅ Code block {i+1} executed successfully")
39
- except Exception as e:
40
- pytest.fail(f"Code block {i+1} failed: {e}\nCode:\n{code}")
41
-
42
78
 
79
+ @pytest.mark.doc
80
+ def test_index():
81
+ namespace = run_blocks(_doc("docs", "index.md"))
82
+ assert "MyModel" in namespace
43
83
 
44
84
 
45
85
  @pytest.mark.doc
46
86
  def test_cosimulation_examples():
47
87
  """Test that all Python examples in usage.md actually work"""
48
- test_dir = pathlib.Path(__file__).parent
49
- usage_file = test_dir.parent.parent / "docs" / "usage.md"
50
-
51
- # Extract Python code
52
- code_blocks = extract_python_code_from_md(usage_file)
53
-
54
- # Create a test namespace
55
- namespace = {}
56
- exec("from pythonfmu3 import *", namespace)
57
-
58
- # Execute each code block
59
- for i, code in enumerate(code_blocks):
60
- try:
61
- exec(code, namespace)
62
- print(f"✅ Code block {i+1} executed successfully")
63
- except Exception as e:
64
- pytest.fail(f"Code block {i+1} failed: {e}\nCode:\n{code}")
65
-
66
- assert 'LinearTransform' in namespace, "LinearTransform class not found"
67
- assert 'PIDController' in namespace, "PIDController class not found"
68
-
69
- # Test instantiation
70
- lt = namespace['LinearTransform'](instance_name="test")
71
- pc = namespace['PIDController'](instance_name="test")
88
+ namespace = run_blocks(_doc("docs", "usage.md"))
89
+ assert "LinearTransform" in namespace, "LinearTransform class not found"
90
+ assert "PIDController" in namespace, "PIDController class not found"
72
91
 
73
92
 
74
93
  @pytest.mark.doc
75
94
  def test_model_exchange_examples():
76
95
  """Test Model Exchange examples"""
77
- test_dir = pathlib.Path(__file__).parent
78
- mx_file = test_dir.parent.parent / "docs" / "usageMX.md"
79
-
80
- code_blocks = extract_python_code_from_md(mx_file)
81
-
82
- namespace = {}
83
- exec("from pythonfmu3 import *", namespace)
84
-
85
- for i, code in enumerate(code_blocks):
86
- try:
87
- exec(code, namespace)
88
- except Exception as e:
89
- pytest.fail(f"MX Code block {i+1} failed: {e}")
90
-
91
- # Test that classes were actually created
92
- assert 'VanDerPol' in namespace, "VanDerPol class not found"
93
- assert 'BouncingBall' in namespace, "BouncingBall class not found"
94
-
95
- # Test instantiation
96
- vdp = namespace['VanDerPol'](instance_name="test")
97
-
98
- bb = namespace['BouncingBall'](instance_name="test")
96
+ namespace = run_blocks(_doc("docs", "usageMX.md"))
97
+ assert "VanDerPol" in namespace, "VanDerPol class not found"
98
+ assert "BouncingBall" in namespace, "BouncingBall class not found"
99
+
100
+
101
+ def test_update_discrete_states_result_aliases():
102
+ from pythonfmu3 import Fmi3UpdateDiscreteStatesResult
103
+
104
+ result = Fmi3UpdateDiscreteStatesResult()
105
+ result.discreteStateNeedsUpdate = True
106
+ result.nominalsOfContinuousStatesChanged = True
107
+ assert result.discreteStatesNeedUpdate and result.nominalContinuousStatesChanged
@@ -0,0 +1,75 @@
1
+ """Load a built FMU from a plain C program, i.e. without a Python interpreter in the host.
2
+
3
+ This is how most FMI importers (OMSimulator, fmusim, Simulink, ...) use an FMU. The
4
+ Linux/macOS shim must locate libpython itself in that case.
5
+ """
6
+ import os
7
+ import shutil
8
+ import subprocess
9
+ import sys
10
+ import zipfile
11
+ from pathlib import Path
12
+
13
+ import pytest
14
+
15
+ import pythonfmu3
16
+ from pythonfmu3.builder import FmuBuilder
17
+ from pythonfmu3.osutil import get_lib_extension, get_platform
18
+
19
+ HERE = Path(__file__).parent
20
+ CC = shutil.which("cc") or shutil.which("gcc") or shutil.which("clang")
21
+
22
+ pytestmark = [
23
+ pytest.mark.skipif(sys.platform == "win32", reason="the shim is only used on Linux/macOS"),
24
+ pytest.mark.skipif(not FmuBuilder.has_binary(), reason="needs the compiled wrapper"),
25
+ pytest.mark.skipif(CC is None, reason="needs a C compiler"),
26
+ ]
27
+
28
+
29
+ @pytest.fixture(scope="module")
30
+ def host(tmp_path_factory):
31
+ out = tmp_path_factory.mktemp("host") / "fmi3_host"
32
+ include = Path(pythonfmu3.__path__[0]) / "pythonfmu-export" / "src" / "fmi"
33
+ libs = ["-ldl"] if sys.platform.startswith("linux") else []
34
+ subprocess.run([CC, f"-I{include}", str(HERE / "data" / "fmi3_host.c"), "-o", str(out), *libs], check=True)
35
+ return out
36
+
37
+
38
+ @pytest.fixture(scope="module")
39
+ def fmu_dir(tmp_path_factory):
40
+ dest = tmp_path_factory.mktemp("fmu")
41
+ fmu = FmuBuilder.build_FMU(HERE / "slaves" / "pythonslave.py", dest=dest)
42
+ target = dest / "extracted"
43
+ with zipfile.ZipFile(fmu) as z:
44
+ z.extractall(target)
45
+ return target
46
+
47
+
48
+ def _run(host, fmu_dir, env):
49
+ library = fmu_dir / "binaries" / get_platform() / f"PythonSlave.{get_lib_extension()}"
50
+ # Minimal environment: nothing Python-related is inherited from the test process
51
+ base = {k: v for k, v in os.environ.items() if k in ("PATH", "HOME", "LANG", "TMPDIR")}
52
+ return subprocess.run(
53
+ [str(host), str(library), str(fmu_dir / "resources")],
54
+ capture_output=True, text=True, timeout=120, env={**base, **env},
55
+ )
56
+
57
+
58
+ def test_runs_without_python_host(host, fmu_dir):
59
+ proc = _run(host, fmu_dir, {})
60
+ assert proc.returncode == 0, proc.stdout + proc.stderr
61
+ assert "realOut 1.5" in proc.stdout
62
+
63
+
64
+ def test_library_path_from_environment(host, fmu_dir, tmp_path):
65
+ hints = fmu_dir / "resources" / "pythonfmu_libpython.txt"
66
+ if not hints.exists():
67
+ pytest.skip("building interpreter has no shared libpython")
68
+ library = hints.read_text().splitlines()[0]
69
+ backup = tmp_path / "hints.txt"
70
+ shutil.move(hints, backup)
71
+ try:
72
+ proc = _run(host, fmu_dir, {"PYTHONFMU_PYTHON_LIBRARY": library})
73
+ finally:
74
+ shutil.move(backup, hints)
75
+ assert proc.returncode == 0, proc.stdout + proc.stderr