1 // Copyright (C) 2006-2024 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef _PYTHONNODE_HXX_
21 #define _PYTHONNODE_HXX_
23 #include "YACSRuntimeSALOMEExport.hxx"
24 #include "InlineNode.hxx"
25 #include <SALOMEconfig.h>
26 #include CORBA_CLIENT_HEADER(SALOME_PyNode)
27 #include CORBA_CLIENT_HEADER(SALOME_Component)
37 class YACSRUNTIMESALOME_EXPORT PythonEntry
40 static bool IsProxy( PyObject *ob );
41 /*! return true only if ob is a proxy and destroy flag set to true*/
42 static bool GetDestroyStatus( PyObject *ob );
43 static void DoNotTouchFileIfProxy( PyObject *ob );
44 static void UnlinkOnDestructorIfProxy( PyObject *ob );
45 static void IfProxyDoSomething( PyObject *ob, const char *meth );
49 virtual void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp) = 0;
50 //! returns (if any) an object, you have to deal with (UnRegister)
51 virtual Engines::PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const = 0;
52 //! returns an object, you have to deal with (UnRegister)
53 virtual void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer) = 0;
54 virtual Engines::PyNodeBase_var getRemoteInterpreterHandle() = 0;
55 virtual const char *getSerializationScript() const = 0;
57 void commonRemoteLoad(InlineNode *reqNode);
58 void loadRemoteContainer(InlineNode *reqNode);
59 Engines::Container_var loadPythonAdapter(InlineNode *reqNode, bool& isInitializeRequested);
60 void loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested);
61 static std::string GetContainerLog(const std::string& mode, Container *container, const Task *askingTask);
62 virtual bool hasImposedResource()const;
66 PyObject *_pyfuncUnser;
67 PyObject *_pyfuncSimpleSer;
68 static PyObject *_pyClsBigObject;
69 std::string _imposedResource;
70 std::string _imposedContainer;
72 static const char SCRIPT_FOR_SIMPLE_SERIALIZATION[];
75 class YACSRUNTIMESALOME_EXPORT PythonNode : public InlineNode, public PythonEntry
78 Node *simpleClone(ComposedNode *father, bool editionOnly) const;
79 // overload part of PythonEntry
80 void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer);
81 Engines::PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const;
82 void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp);
83 Engines::PyNodeBase_var getRemoteInterpreterHandle();
84 const char *getSerializationScript() const { return SCRIPT_FOR_SERIALIZATION; }
85 // A kernel container may manage several python contexts identified by
86 // their name (PyNode and PyScript node). This function returns the name
87 // of the context used by this object. See SALOME_Component.idl in KERNEL.
88 std::string pythonEntryName()const;
90 PythonNode(const PythonNode& other, ComposedNode *father);
91 PythonNode(const std::string& name);
92 virtual ~PythonNode();
93 virtual void checkBasicConsistency() const ;
94 virtual void execute();
96 virtual void loadRemote();
97 virtual void loadLocal();
98 virtual void executeRemote();
99 virtual void executeLocal();
100 virtual void shutdown(int level);
101 void imposeResource(const std::string& resource_name,
102 const std::string& container_name) override;
103 bool canAcceptImposedResource()override;
104 bool hasImposedResource()const override;
105 bool isUsingPythonCache()const;
106 std::string getContainerLog();
107 PythonNode* cloneNode(const std::string& name);
108 virtual std::string typeName() { return "YACS__ENGINE__PythonNode"; }
109 void applyDPLScope(ComposedNode *gfn);
110 void setSqueezeStatusWithExceptions(bool sqStatus, const std::vector<std::string>& squeezeExceptions);
111 void setSqueezeStatus(bool sqStatus) { _autoSqueeze=sqStatus; }
112 bool getSqueezeStatus() const { return _autoSqueeze; }
113 void squeezeMemorySafe();
114 static void ExecuteLocalInternal(const std::string& codeStr, PyObject *context, std::string& errorDetails);
116 void executeLocalInternal(const std::string& codeStr);
118 void squeezeMemory();
119 void squeezeMemoryRemote();
120 void freeKernelPynode();
122 static const char KIND[];
123 static const char IMPL_NAME[];
124 static const char SCRIPT_FOR_SERIALIZATION[];
125 static const char REMOTE_NAME[];
126 static const char DPL_INFO_NAME[];
128 bool _autoSqueeze = false;
129 //! list on output port name excluded from the squeeze mecanism
130 std::set<std::string> _nonSqueezableOutputNodes;
131 Engines::PyScriptNode_var _pynode;
134 class YACSRUNTIMESALOME_EXPORT PyFuncNode : public InlineFuncNode, public PythonEntry
137 Node *simpleClone(ComposedNode *father, bool editionOnly) const;
138 // overload part of PythonEntry
139 void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer);Engines::
140 PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const;
141 void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp);
142 Engines::PyNodeBase_var getRemoteInterpreterHandle();
143 const char *getSerializationScript() const { return SCRIPT_FOR_SERIALIZATION; }
145 PyFuncNode(const PyFuncNode& other, ComposedNode *father);
146 PyFuncNode(const std::string& name);
147 virtual ~PyFuncNode();
148 virtual void init(bool start=true);
149 virtual void checkBasicConsistency() const ;
150 virtual void execute();
152 virtual void loadRemote();
153 virtual void loadLocal();
154 virtual void executeRemote();
155 virtual void executeLocal();
156 virtual void shutdown(int level);
157 void imposeResource(const std::string& resource_name,
158 const std::string& container_name) override;
159 bool canAcceptImposedResource()override;
160 bool hasImposedResource()const override;
161 std::string getContainerLog();
162 PyFuncNode* cloneNode(const std::string& name);
163 virtual std::string typeName() { return "YACS__ENGINE__PyFuncNode"; }
165 static const char SCRIPT_FOR_SERIALIZATION[];
168 Engines::PyNode_var _pynode;