1 // Copyright (C) 2006-2023 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)
35 class YACSRUNTIMESALOME_EXPORT PythonEntry
38 static bool IsProxy( PyObject *ob );
39 /*! return true only if ob is a proxy and destroy flag set to true*/
40 static bool GetDestroyStatus( PyObject *ob );
41 static void DoNotTouchFileIfProxy( PyObject *ob );
42 static void UnlinkOnDestructorIfProxy( PyObject *ob );
43 static void IfProxyDoSomething( PyObject *ob, const char *meth );
47 virtual void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp) = 0;
48 //! returns (if any) an object, you have to deal with (UnRegister)
49 virtual Engines::PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const = 0;
50 //! returns an object, you have to deal with (UnRegister)
51 virtual void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer) = 0;
52 virtual Engines::PyNodeBase_var getRemoteInterpreterHandle() = 0;
53 virtual const char *getSerializationScript() const = 0;
55 void commonRemoteLoad(InlineNode *reqNode);
56 void loadRemoteContainer(InlineNode *reqNode);
57 Engines::Container_var loadPythonAdapter(InlineNode *reqNode, bool& isInitializeRequested);
58 void loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested);
59 static std::string GetContainerLog(const std::string& mode, Container *container, const Task *askingTask);
60 virtual bool hasImposedResource()const;
64 PyObject *_pyfuncUnser;
65 PyObject *_pyfuncSimpleSer;
66 static PyObject *_pyClsBigObject;
67 std::string _imposedResource;
68 std::string _imposedContainer;
70 static const char SCRIPT_FOR_SIMPLE_SERIALIZATION[];
73 class YACSRUNTIMESALOME_EXPORT PythonNode : public InlineNode, public PythonEntry
76 Node *simpleClone(ComposedNode *father, bool editionOnly) const;
77 // overload part of PythonEntry
78 void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer);
79 Engines::PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const;
80 void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp);
81 Engines::PyNodeBase_var getRemoteInterpreterHandle();
82 const char *getSerializationScript() const { return SCRIPT_FOR_SERIALIZATION; }
83 // A kernel container may manage several python contexts identified by
84 // their name (PyNode and PyScript node). This function returns the name
85 // of the context used by this object. See SALOME_Component.idl in KERNEL.
86 std::string pythonEntryName()const;
88 PythonNode(const PythonNode& other, ComposedNode *father);
89 PythonNode(const std::string& name);
90 virtual ~PythonNode();
91 virtual void checkBasicConsistency() const ;
92 virtual void execute();
94 virtual void loadRemote();
95 virtual void loadLocal();
96 virtual void executeRemote();
97 virtual void executeLocal();
98 virtual void shutdown(int level);
99 void imposeResource(const std::string& resource_name,
100 const std::string& container_name) override;
101 bool canAcceptImposedResource()override;
102 bool hasImposedResource()const override;
103 bool isUsingPythonCache()const;
104 std::string getContainerLog();
105 PythonNode* cloneNode(const std::string& name);
106 virtual std::string typeName() { return "YACS__ENGINE__PythonNode"; }
107 void applyDPLScope(ComposedNode *gfn);
108 void setSqueezeStatus(bool sqStatus) { _autoSqueeze=sqStatus; }
109 bool getSqueezeStatus() const { return _autoSqueeze; }
110 void squeezeMemorySafe();
111 static void ExecuteLocalInternal(const std::string& codeStr, PyObject *context, std::string& errorDetails);
113 void executeLocalInternal(const std::string& codeStr);
115 void squeezeMemory();
116 void squeezeMemoryRemote();
117 void freeKernelPynode();
119 static const char KIND[];
120 static const char IMPL_NAME[];
121 static const char SCRIPT_FOR_SERIALIZATION[];
122 static const char REMOTE_NAME[];
123 static const char DPL_INFO_NAME[];
125 bool _autoSqueeze = false;
126 Engines::PyScriptNode_var _pynode;
129 class YACSRUNTIMESALOME_EXPORT PyFuncNode : public InlineFuncNode, public PythonEntry
132 Node *simpleClone(ComposedNode *father, bool editionOnly) const;
133 // overload part of PythonEntry
134 void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer);Engines::
135 PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const;
136 void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp);
137 Engines::PyNodeBase_var getRemoteInterpreterHandle();
138 const char *getSerializationScript() const { return SCRIPT_FOR_SERIALIZATION; }
140 PyFuncNode(const PyFuncNode& other, ComposedNode *father);
141 PyFuncNode(const std::string& name);
142 virtual ~PyFuncNode();
143 virtual void init(bool start=true);
144 virtual void checkBasicConsistency() const ;
145 virtual void execute();
147 virtual void loadRemote();
148 virtual void loadLocal();
149 virtual void executeRemote();
150 virtual void executeLocal();
151 virtual void shutdown(int level);
152 void imposeResource(const std::string& resource_name,
153 const std::string& container_name) override;
154 bool canAcceptImposedResource()override;
155 bool hasImposedResource()const override;
156 std::string getContainerLog();
157 PyFuncNode* cloneNode(const std::string& name);
158 virtual std::string typeName() { return "YACS__ENGINE__PyFuncNode"; }
160 static const char SCRIPT_FOR_SERIALIZATION[];
163 Engines::PyNode_var _pynode;