pythonfmu3 0.1.12__tar.gz → 0.1.13__tar.gz

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 (65) hide show
  1. {pythonfmu3-0.1.12/pythonfmu3.egg-info → pythonfmu3-0.1.13}/PKG-INFO +1 -1
  2. pythonfmu3-0.1.13/pythonfmu3/_version.py +1 -0
  3. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/fmi3slave.py +1 -1
  4. pythonfmu3-0.1.13/pythonfmu3/pythonfmu-export/src/pythonfmu/IPyState.hpp +18 -0
  5. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/src/pythonfmu/PySlaveInstance.cpp +60 -6
  6. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/src/pythonfmu/PySlaveInstance.hpp +3 -1
  7. pythonfmu3-0.1.13/pythonfmu3/pythonfmu-export/src/pythonfmu/PyState.hpp +85 -0
  8. pythonfmu3-0.1.13/pythonfmu3/resources/binaries/x86_64-linux/libpythonfmu-export.so +0 -0
  9. pythonfmu3-0.1.13/pythonfmu3/resources/binaries/x86_64-windows/pythonfmu-export.dll +0 -0
  10. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/variables.py +1 -1
  11. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13/pythonfmu3.egg-info}/PKG-INFO +1 -1
  12. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3.egg-info/SOURCES.txt +1 -0
  13. pythonfmu3-0.1.12/pythonfmu3/_version.py +0 -1
  14. pythonfmu3-0.1.12/pythonfmu3/pythonfmu-export/src/pythonfmu/PyState.hpp +0 -41
  15. pythonfmu3-0.1.12/pythonfmu3/resources/binaries/x86_64-linux/libpythonfmu-export.so +0 -0
  16. pythonfmu3-0.1.12/pythonfmu3/resources/binaries/x86_64-windows/pythonfmu-export.dll +0 -0
  17. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/LICENSE +0 -0
  18. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/MANIFEST.in +0 -0
  19. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/README.md +0 -0
  20. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/__init__.py +0 -0
  21. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/__main__.py +0 -0
  22. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/builder.py +0 -0
  23. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/default_experiment.py +0 -0
  24. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/deploy.py +0 -0
  25. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/enums.py +0 -0
  26. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/logmsg.py +0 -0
  27. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/osutil.py +0 -0
  28. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/.clang-format +0 -0
  29. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/CMakeLists.txt +0 -0
  30. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/build_unix.sh +0 -0
  31. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/build_win.sh +0 -0
  32. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/src/CMakeLists.txt +0 -0
  33. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/src/cppfmu/cppfmu_common.hpp +0 -0
  34. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/src/cppfmu/cppfmu_cs.cpp +0 -0
  35. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/src/cppfmu/cppfmu_cs.hpp +0 -0
  36. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/src/cppfmu/fmi_functions.cpp +0 -0
  37. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/src/fmi/fmi3FunctionTypes.h +0 -0
  38. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/src/fmi/fmi3Functions.h +0 -0
  39. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/pythonfmu-export/src/fmi/fmi3PlatformTypes.h +0 -0
  40. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/__init__.py +0 -0
  41. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/conftest.py +0 -0
  42. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/data/csvdemo.csv +0 -0
  43. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/data/hello.txt +0 -0
  44. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/slaves/PythonSlaveWithException.py +0 -0
  45. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/slaves/localmodule.py +0 -0
  46. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/slaves/pythonslave.py +0 -0
  47. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/slaves/pythonslave_read_file.py +0 -0
  48. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/slaves/slavewithdep.py +0 -0
  49. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/test_builder.py +0 -0
  50. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/test_deploy.py +0 -0
  51. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/test_fmi3slave.py +0 -0
  52. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/test_integration.py +0 -0
  53. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/test_logger.py +0 -0
  54. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/test_multiple_fmus.py +0 -0
  55. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/test_variables.py +0 -0
  56. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/tests/utils.py +0 -0
  57. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/unit.py +0 -0
  58. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3/variable_types.py +0 -0
  59. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3.egg-info/dependency_links.txt +0 -0
  60. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3.egg-info/entry_points.txt +0 -0
  61. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3.egg-info/not-zip-safe +0 -0
  62. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3.egg-info/requires.txt +0 -0
  63. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/pythonfmu3.egg-info/top_level.txt +0 -0
  64. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/setup.cfg +0 -0
  65. {pythonfmu3-0.1.12 → pythonfmu3-0.1.13}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pythonfmu3
3
- Version: 0.1.12
3
+ Version: 0.1.13
4
4
  Summary: A lightweight framework that enables the packaging of Python3.x code as co-simulation FMUs.
5
5
  Home-page: https://github.com/stephensmith25/PythonFMU3
6
6
  Author: Stephen Smith
@@ -0,0 +1 @@
1
+ __version__ = "0.1.13"
@@ -188,7 +188,7 @@ class Fmi3Slave(ABC):
188
188
  raise Exception(f"Unsupported type {type(var)}!")
189
189
  var.start = refs if len(getattr(var, "dimensions", [])) > 0 else refs[0]
190
190
 
191
- def register_variable(self, var: ModelVariable, nested: bool = True, units: str = "", var_type: Any = None):
191
+ def register_variable(self, var: ModelVariable, nested: bool = True, var_type: Any = None):
192
192
  """Register a variable as FMU interface.
193
193
 
194
194
  Args:
@@ -0,0 +1,18 @@
1
+ #ifndef PYTHONFMU_IPYTHONSTATE_HPP
2
+ #define PYTHONFMU_IPYTHONSTATE_HPP
3
+
4
+ namespace pythonfmu
5
+ {
6
+ class IPyState
7
+ {
8
+ public:
9
+ IPyState() = default;
10
+ IPyState(IPyState const& other) = delete;
11
+ IPyState(IPyState&& other) = delete;
12
+ IPyState& operator=(IPyState const& other) = delete;
13
+ IPyState& operator=(IPyState&& other) = delete;
14
+ virtual ~IPyState() = default;
15
+ };
16
+ }
17
+
18
+ #endif //PYTHONFMU_IPYTHONSTATE_HPP
@@ -1,4 +1,5 @@
1
1
 
2
+ #include "pythonfmu/IPyState.hpp"
2
3
  #include "pythonfmu/PySlaveInstance.hpp"
3
4
 
4
5
  #include "pythonfmu/PyState.hpp"
@@ -7,6 +8,7 @@
7
8
 
8
9
  #include <fstream>
9
10
  #include <functional>
11
+ #include <mutex>
10
12
  #include <regex>
11
13
  #include <sstream>
12
14
  #include <utility>
@@ -44,8 +46,9 @@ inline void py_safe_run(const std::function<void(PyGILState_STATE gilState)>& f)
44
46
  PyGILState_Release(gil_state);
45
47
  }
46
48
 
47
- PySlaveInstance::PySlaveInstance(std::string instanceName, std::string resources, const cppfmu::Logger& logger, const bool visible)
48
- : instanceName_(std::move(instanceName))
49
+ PySlaveInstance::PySlaveInstance(std::string instanceName, std::string resources, const cppfmu::Logger& logger, const bool visible, std::shared_ptr<IPyState> pyState)
50
+ : pyState_{ std::move(pyState) }
51
+ , instanceName_(std::move(instanceName))
49
52
  , resources_(std::move(resources))
50
53
  , logger_(logger)
51
54
  , visible_(visible)
@@ -625,7 +628,10 @@ PySlaveInstance::~PySlaveInstance()
625
628
 
626
629
  } // namespace pythonfmu
627
630
 
628
- std::unique_ptr<pythonfmu::PyState> pyState = nullptr;
631
+ namespace {
632
+ std::mutex pyStateMutex{};
633
+ std::shared_ptr<pythonfmu::PyState> pyState{};
634
+ }
629
635
 
630
636
  std::unique_ptr<cppfmu::SlaveInstance> CppfmuInstantiateSlave(
631
637
  cppfmu::FMIString instanceName,
@@ -650,9 +656,57 @@ std::unique_ptr<cppfmu::SlaveInstance> CppfmuInstantiateSlave(
650
656
  #endif
651
657
  }
652
658
 
653
- if (pyState == nullptr) {
654
- pyState = std::make_unique<pythonfmu::PyState>();
659
+ {
660
+ auto const ensurePyStateAlive = [&]() {
661
+ auto const lock = std::lock_guard{pyStateMutex};
662
+ if (nullptr == pyState) pyState = std::make_shared<pythonfmu::PyState>();
663
+ };
664
+
665
+ ensurePyStateAlive();
666
+ return std::make_unique<pythonfmu::PySlaveInstance>(
667
+ instanceName, resources, logger, visible, pyState);
655
668
  }
669
+ }
670
+
671
+ extern "C" {
672
+
673
+ // The PyState instance owns it's own thread for constructing and destroying the Py* from the same thread.
674
+ // Creation of an std::thread increments ref counter of a shared library. So, when the client unloads the library
675
+ // the library won't be freed, as std::thread is alive, and the std::thread itself waits for de-initialization request.
676
+ // Thus, use DllMain on Windows and __attribute__((destructor)) on Linux for signaling to the PyState about de-initialization.
677
+ void finalizePythonInterpreter()
678
+ {
679
+ pyState = nullptr;
680
+ }
681
+ }
656
682
 
657
- return std::make_unique<pythonfmu::PySlaveInstance>(instanceName, resources, logger, visible);
683
+ namespace
684
+ {
685
+ #ifdef _WIN32
686
+ #include <windows.h>
687
+
688
+ BOOL APIENTRY DllMain(HMODULE hModule,
689
+ DWORD ul_reason_for_call,
690
+ LPVOID lpReserved)
691
+ {
692
+ switch (ul_reason_for_call) {
693
+ case DLL_PROCESS_ATTACH:
694
+ break;
695
+ case DLL_THREAD_ATTACH:
696
+ case DLL_THREAD_DETACH:
697
+ break;
698
+ case DLL_PROCESS_DETACH:
699
+ finalizePythonInterpreter();
700
+ break;
701
+ }
702
+ return TRUE;
703
+ }
704
+ #elif defined(__linux__)
705
+ __attribute__((destructor)) void onLibraryUnload()
706
+ {
707
+ finalizePythonInterpreter();
708
+ }
709
+ #else
710
+ #error port the code
711
+ #endif
658
712
  }
@@ -3,6 +3,7 @@
3
3
  #define PYTHONFMU_SLAVEINSTANCE_HPP
4
4
 
5
5
  #include "cppfmu/cppfmu_cs.hpp"
6
+ #include "pythonfmu/IPyState.hpp"
6
7
 
7
8
  #include <Python.h>
8
9
  #include <string>
@@ -15,7 +16,7 @@ class PySlaveInstance : public cppfmu::SlaveInstance
15
16
  {
16
17
 
17
18
  public:
18
- PySlaveInstance(std::string instanceName, std::string resources, const cppfmu::Logger& logger, bool visible);
19
+ PySlaveInstance(std::string instanceName, std::string resources, const cppfmu::Logger& logger, bool visible, std::shared_ptr<IPyState> pyState);
19
20
 
20
21
  void initialize(PyGILState_STATE gilState);
21
22
 
@@ -53,6 +54,7 @@ public:
53
54
  ~PySlaveInstance() override;
54
55
 
55
56
  private:
57
+ std::shared_ptr<IPyState> pyState_;
56
58
  PyObject* pClass_;
57
59
  PyObject* pInstance_{};
58
60
  PyObject* pMessages_{};
@@ -0,0 +1,85 @@
1
+
2
+ #ifndef PYTHONFMU_PYTHONSTATE_HPP
3
+ #define PYTHONFMU_PYTHONSTATE_HPP
4
+
5
+ #include "IPyState.hpp"
6
+ #include <Python.h>
7
+ #include <condition_variable>
8
+ #include <iostream>
9
+ #include <mutex>
10
+ #include <thread>
11
+
12
+ namespace pythonfmu
13
+ {
14
+ class PyState : public IPyState
15
+ {
16
+ public:
17
+ PyState()
18
+ : _initDeinitPyThread_{ &PyState::Worker, this }
19
+ {
20
+ auto lock = std::unique_lock{ mutex_ };
21
+ conditionalVariable_.wait(lock, [&] { return consructed_; });
22
+ }
23
+
24
+ ~PyState() override
25
+ {
26
+ {
27
+ auto const lock = std::lock_guard{ mutex_ };
28
+ destroyRequested_ = true;
29
+ }
30
+ conditionalVariable_.notify_one();
31
+ if (_initDeinitPyThread_.joinable()) _initDeinitPyThread_.join();
32
+ }
33
+
34
+ private:
35
+
36
+ // In accordance to the documentation https://docs.python.org/3/c-api/init.html#c.Py_FinalizeEx
37
+ // the Py_Initialize/Py_Finalize should be called from the same
38
+ // thread. The FMI standard allows to call fmi functions from different threads, also different threads
39
+ // could be used for loading unloading the FMU libraries. There is a deadlock, when the simulation tool
40
+ // unloads the FMU library from the thread which is differs from the thread where the Py_Initialize was called.
41
+ // Create a new thread which is used for calling Py_Initialize and Py_Deinitialize. The thread waits for
42
+ // notification from destructor and then calls Py_Deinitialize.
43
+
44
+ void Worker()
45
+ {
46
+ // It will be nullptr in case when some other tool already called Py_IsInitialized.
47
+ // There is no need to call Py_Finalize thus, this thread can exit ASAP.
48
+ auto const mainPyThread = []() -> PyThreadState* {
49
+ auto const justInitialized = !Py_IsInitialized();
50
+ if (justInitialized) {
51
+ Py_SetProgramName(L"./PythonFMU");
52
+ Py_Initialize();
53
+ #if PY_VERSION_HEX < 0x03070000
54
+ PyEval_InitThreads();
55
+ #endif
56
+ return PyEval_SaveThread();
57
+ }
58
+ return nullptr;
59
+ }();
60
+
61
+ {
62
+ auto const lock = std::lock_guard{ mutex_ };
63
+ consructed_ = true;
64
+ }
65
+ conditionalVariable_.notify_one();
66
+
67
+ if (nullptr != mainPyThread) {
68
+ auto lock = std::unique_lock{ mutex_ };
69
+ conditionalVariable_.wait(lock, [&] { return destroyRequested_; });
70
+
71
+ PyEval_RestoreThread(mainPyThread);
72
+ Py_Finalize();
73
+ }
74
+ }
75
+
76
+ bool consructed_ = false;
77
+ bool destroyRequested_ = false;
78
+ std::condition_variable conditionalVariable_;
79
+ std::mutex mutex_;
80
+ std::thread _initDeinitPyThread_;
81
+ };
82
+
83
+ } // namespace pythonfmu
84
+
85
+ #endif //PYTHONFMU_PYTHONSTATE_HPP
@@ -144,7 +144,7 @@ class Dimension(object):
144
144
  if self.start:
145
145
  return self.start
146
146
  else:
147
- result = next((var for var in vars if var.value_reference == self.value_reference), None)
147
+ result = next((value for vr, value in vars.items() if vr == int(self.value_reference)), None)
148
148
  return result.getter()
149
149
 
150
150
  def to_xml(self) -> Element:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pythonfmu3
3
- Version: 0.1.12
3
+ Version: 0.1.13
4
4
  Summary: A lightweight framework that enables the packaging of Python3.x code as co-simulation FMUs.
5
5
  Home-page: https://github.com/stephensmith25/PythonFMU3
6
6
  Author: Stephen Smith
@@ -35,6 +35,7 @@ pythonfmu3/pythonfmu-export/src/cppfmu/fmi_functions.cpp
35
35
  pythonfmu3/pythonfmu-export/src/fmi/fmi3FunctionTypes.h
36
36
  pythonfmu3/pythonfmu-export/src/fmi/fmi3Functions.h
37
37
  pythonfmu3/pythonfmu-export/src/fmi/fmi3PlatformTypes.h
38
+ pythonfmu3/pythonfmu-export/src/pythonfmu/IPyState.hpp
38
39
  pythonfmu3/pythonfmu-export/src/pythonfmu/PySlaveInstance.cpp
39
40
  pythonfmu3/pythonfmu-export/src/pythonfmu/PySlaveInstance.hpp
40
41
  pythonfmu3/pythonfmu-export/src/pythonfmu/PyState.hpp
@@ -1 +0,0 @@
1
- __version__ = "0.1.12"
@@ -1,41 +0,0 @@
1
-
2
- #ifndef PYTHONFMU_PYTHONSTATE_HPP
3
- #define PYTHONFMU_PYTHONSTATE_HPP
4
-
5
- #include <Python.h>
6
- #include <iostream>
7
-
8
- namespace pythonfmu
9
- {
10
-
11
- class PyState
12
- {
13
- public:
14
- PyState()
15
- {
16
- was_initialized_ = Py_IsInitialized();
17
-
18
- if (!was_initialized_) {
19
- Py_SetProgramName(L"./PythonFMU");
20
- Py_Initialize();
21
- PyEval_InitThreads();
22
- _mainPyThread = PyEval_SaveThread();
23
- }
24
- }
25
-
26
- ~PyState()
27
- {
28
- if (!was_initialized_) {
29
- PyEval_RestoreThread(_mainPyThread);
30
- Py_Finalize();
31
- }
32
- }
33
-
34
- private:
35
- bool was_initialized_;
36
- PyThreadState* _mainPyThread;
37
- };
38
-
39
- } // namespace pythonfmu
40
-
41
- #endif //PYTHONFMU_PYTHONSTATE_HPP
File without changes
File without changes
File without changes
File without changes
File without changes