CYCLUS
pyhooks.cc
Go to the documentation of this file.
1 #include "pyhooks.h"
2 
3 #ifdef CYCLUS_WITH_PYTHON
4 #include <stdlib.h>
5 
6 #include "Python.h"
7 
8 extern "C" {
9 #include "eventhooks.h"
10 #include "pyinfile.h"
11 #include "pymodule.h"
12 }
13 
14 namespace cyclus {
15 int PY_INTERP_COUNT = 0;
16 bool PY_INTERP_INIT = false;
17 
18 
19 void PyAppendInitTab(void) {
20 #if PY_MAJOR_VERSION < 3
21  // Not used before Python 3
22 #else
23  PyImport_AppendInittab("_cyclus_eventhooks", PyInit_eventhooks);
24  PyImport_AppendInittab("_cyclus_pyinfile", PyInit_pyinfile);
25  PyImport_AppendInittab("_cyclus_pymodule", PyInit_pymodule);
26 #endif
27 }
28 
29 void PyImportInit(void) {
30 #if PY_MAJOR_VERSION < 3
32  initpyinfile();
33  initpymodule();
34 #else
35  PyImport_ImportModule("_cyclus_eventhooks");
36  PyImport_ImportModule("_cyclus_pyinfile");
37  PyImport_ImportModule("_cyclus_pymodule");
38 #endif
39 };
40 
41 
42 void PyImportCallInit(void) {
43 #if PY_MAJOR_VERSION < 3
45  initpyinfile();
46  initpymodule();
47 #else
48  PyInit_eventhooks();
49  PyInit_pyinfile();
50  PyInit_pymodule();
51 #endif
52 };
53 
54 
55 void PyStart(void) {
56  if (!PY_INTERP_INIT) {
58  Py_Initialize();
59  PyImportInit();
60  atexit(PyStop);
61  PY_INTERP_INIT = true;
62  };
63  PY_INTERP_COUNT++;
64 };
65 
66 void PyStop(void) {
67  PY_INTERP_COUNT--;
68 
69  // PY_INTERP_COUNT should only be negative when called atexit()
70  if (PY_INTERP_INIT && PY_INTERP_COUNT < 0) {
71  Py_Finalize();
72  };
73 };
74 
75 void EventLoop(void) { CyclusEventLoopHook(); };
76 
78 
79 Agent* MakePyAgent(std::string lib, std::string agent, void* ctx) {
80  return CyclusMakePyAgent(lib, agent, ctx);
81 };
82 
83 void InitFromPyAgent(Agent* src, Agent* dst, void* ctx) {
84  CyclusInitFromPyAgent(src, dst, ctx);
85 };
86 
88 
89 void PyDelAgent(int i) { CyclusPyDelAgent(i); };
90 
91 namespace toolkit {
92 std::string PyToJson(std::string infile) { return CyclusPyToJson(infile); };
93 
94 std::string JsonToPy(std::string infile) { return CyclusJsonToPy(infile); };
95 
96 void PyCallListeners(std::string tstype, Agent* agent, void* cpp_ctx, int time, boost::spirit::hold_any value){
97  CyclusPyCallListeners(tstype, agent, cpp_ctx, time, value);
98 };
99 
100 } // namespace toolkit
101 } // namespace cyclus
102 #else // else CYCLUS_WITH_PYTHON
103 #include "error.h"
104 
105 namespace cyclus {
106 int PY_INTERP_COUNT = 0;
107 bool PY_INTERP_INIT = false;
108 
109 void PyAppendInitTab(void) {};
110 
111 void PyImportInit(void) {};
112 
113 void PyImportCallInit(void) {};
114 
115 void PyStart(void) {};
116 
117 void PyStop(void) {};
118 
119 void EventLoop(void) {};
120 
122 
123 Agent* MakePyAgent(std::string lib, std::string agent, void* ctx) { return NULL; };
124 
125 void InitFromPyAgent(Agent* src, Agent* dst, void* ctx) {};
126 
127 void ClearPyAgentRefs(void) {};
128 
129 void PyDelAgent(int i) {};
130 
131 namespace toolkit {
133  throw cyclus::ValidationError("Cannot convert from Python input files since "
134  "Cyclus was not built with Python bindings.");
135  return "";
136 };
137 
139  throw cyclus::ValidationError("Cannot convert to Python input files since "
140  "Cyclus was not built with Python bindings.");
141  return "";
142 };
143 
144 void PyCallListeners(std::string tsname, Agent* agent, void* cpp_ctx, int time, boost::spirit::hold_any value) {};
145 
146 } // namespace toolkit
147 } // namespace cyclus
148 #endif // ends CYCLUS_WITH_PYTHON
void EventLoop(void)
Definition: pyhooks.cc:119
PyMODINIT_FUNC initpymodule(void)
__PYX_EXTERN_C std::string CyclusPyToJson(std::string)
void PyImportInit(void)
Convience function for import initialization.
Definition: pyhooks.cc:111
For validating files received via IO.
Definition: error.h:71
std::string PyFindModule(std::string lib)
Finds a Python module and returns its filename.
Definition: pyhooks.cc:121
void ClearPyAgentRefs(void)
Removes all Python agents from the internal cache.
Definition: pyhooks.cc:127
void PyImportCallInit(void)
Convience function for imports when Python has already been started.
Definition: pyhooks.cc:113
Agent * MakePyAgent(std::string lib, std::string agent, void *ctx)
Finds a Python module and returns an agent pointer from it.
Definition: pyhooks.cc:123
void PyAppendInitTab(void)
Convience function for appending to import table for initialization.
Definition: pyhooks.cc:109
std::string JsonToPy(std::string infile)
Convert JSON string to Python simulation string.
Definition: pyhooks.cc:138
__PYX_EXTERN_C void CyclusPyDelAgent(int)
std::string PyToJson(std::string infile)
Convert Python simulation string to JSON.
Definition: pyhooks.cc:132
__PYX_EXTERN_C void CyclusEventLoopHook(void)
__PYX_EXTERN_C void CyclusInitFromPyAgent(cyclus::Agent *, cyclus::Agent *, void *)
void PyStop(void)
Closes the current Python session.
Definition: pyhooks.cc:117
__PYX_EXTERN_C void CyclusPyCallListeners(std::string, cyclus::Agent *, void *, int, boost::spirit::hold_any)
PyMODINIT_FUNC initpyinfile(void)
__PYX_EXTERN_C cyclus::Agent * CyclusMakePyAgent(std::string, std::string, void *)
PyMODINIT_FUNC initeventhooks(void)
void PyStart(void)
Initialize Python functionality, this is a no-op if Python was not installed along with Cyclus...
Definition: pyhooks.cc:115
The abstract base class used by all types of agents that live and interact in a simulation.
Definition: agent.h:51
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
__PYX_EXTERN_C std::string CyclusJsonToPy(std::string)
__PYX_EXTERN_C void CyclusClearPyAgentRefs(void)
void PyDelAgent(int i)
Removes a single Python agent from the reference cache.
Definition: pyhooks.cc:129
__PYX_EXTERN_C std::string CyclusPyFindModule(std::string)
void InitFromPyAgent(Agent *src, Agent *dst, void *ctx)
Initializes a Python agent fron another agent.
Definition: pyhooks.cc:125
void PyCallListeners(std::string tsname, Agent *agent, void *cpp_ctx, int time, boost::spirit::hold_any value)
Calls the Python listeners.
Definition: pyhooks.cc:144
bool PY_INTERP_INIT
Whether or not the Python interpreter has been initilized.
Definition: pyhooks.cc:107
int PY_INTERP_COUNT
Because of NumPy #7595, we can only initialize & finalize the Python interpreter once.
Definition: pyhooks.cc:106