Salome HOME
[EDF27816] : management of proxy from/to Foreach
[modules/yacs.git] / src / runtime / TypeConversions.cxx
index a3da28270ecb5ca9f49a795bb4fe77622a6cd8b7..23701e50eb7c7a7e8352d3c1968d178071f8593a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2006-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2022  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -33,6 +33,7 @@
 #include "TypeCode.hxx"
 #include "Cstr2d.hxx"
 #include "SALOME_GenericObj.hh"
+#include "PythonNode.hxx"
 
 #include <iostream>
 #include <iomanip>
@@ -59,22 +60,6 @@ namespace YACS
 {
   namespace ENGINE
   {
-    void printbin(const std::string& bin)
-      {
-        register char c;
-        for(int i=0;i<bin.length();i++)
-          {
-            c=bin[i];
-            if (c < ' ' || c >= 0x7f) 
-              {
-                fprintf(stderr,"\\x%02x",c & 0xff);
-              }
-            else
-              fprintf(stderr,"%c",c);
-          }
-        fprintf(stderr,"\n");
-      }
-
     std::string getImplName(ImplType impl)
       {
          switch(impl)
@@ -742,7 +727,7 @@ namespace YACS
           if (PyUnicode_Check(o))
             {
               Py_ssize_t size;
-              char *ptr = PyUnicode_AsUTF8AndSize(o, &size);
+              const char *ptr = PyUnicode_AsUTF8AndSize(o, &size);
               if (!ptr)
                 throw YACS::ENGINE::ConversionException("Conversion from PyUnicode to string failed");
               s.assign(ptr, size);
@@ -793,7 +778,7 @@ namespace YACS
               // the objref is used by Python as a string (prefix:value) keep it as a string
               Py_ssize_t size;
               std::string s;
-              char *ptr = PyUnicode_AsUTF8AndSize(o, &size);
+              const char *ptr = PyUnicode_AsUTF8AndSize(o, &size);
               if (!ptr)
                 throw YACS::ENGINE::ConversionException("Conversion from PyUnicode to string failed");
               s.assign(ptr, size);
@@ -801,9 +786,14 @@ namespace YACS
             }
           if(strncmp(t->id(),"python",6)==0)
             {
+              bool somthingToDo = YACS::ENGINE::PythonEntry::GetDestroyStatus(o);
+              if( somthingToDo )
+                YACS::ENGINE::PythonEntry::DoNotTouchFileIfProxy(o);
               // It's a native Python object pickle it
               PyObject* mod=PyImport_ImportModule("pickle");
               PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",o,protocol);
+              if( somthingToDo )
+                YACS::ENGINE::PythonEntry::UnlinkOnDestructorIfProxy(o);
               DEBTRACE(PyObject_Repr(pickled) );
               Py_DECREF(mod);
               if(pickled==NULL)
@@ -846,7 +836,7 @@ namespace YACS
                 }
               Py_ssize_t size;
               std::string mystr;
-              char *ptr = PyUnicode_AsUTF8AndSize(pystring, &size);
+              const char *ptr = PyUnicode_AsUTF8AndSize(pystring, &size);
               if (!ptr)
                 throw YACS::ENGINE::ConversionException("Conversion from PyUnicode to string failed");
               mystr.assign(ptr, size);
@@ -1328,7 +1318,10 @@ namespace YACS
                     {
                       DEBTRACE("############### workaround to improve...");
                     }
-                  return mystr;
+                  if(strncmp(t->id(),"python",6)==0 )
+                    return FromBase64Safe(mystr);
+                  else
+                    return mystr;
                 }
               else if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))// <- here case where pyobj value has been stored in XML. pyobj has kind==ObjRef. And the stored format is String ! EDF11027
                 {
@@ -1498,7 +1491,7 @@ namespace YACS
       static inline std::string convert(const TypeCode *t,std::string& o)
         {
           if(strncmp(t->id(),"python",6)==0 )
-            return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
+            return "<value><objref><![CDATA[" + ToBase64(o) + "]]></objref></value>\n";
           else if(strncmp(t->id(),"json",4)==0)
             return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
           else
@@ -1613,7 +1606,7 @@ namespace YACS
     {
       static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,int protocol)
         {
-          if(o->getType()->kind()==String)
+          if(o->getType()->kind()==String || o->getType()->kind()==Objref)
             return o->getStringValue();
           stringstream msg;
           msg << "Problem in conversion: a objref(string) is expected " ;
@@ -1699,7 +1692,7 @@ namespace YACS
         {
           //Check if objref is a GenericObj and register it if it is the case (workaround for bad management of GenericObj)
           if(o=="" || (t->isA(Runtime::_tc_file)) || (strncmp(t->id(),"python",6)==0) || (strncmp(t->id(),"json",4)==0))
-            return YACS::ENGINE::AtomAny::New(o);
+            return YACS::ENGINE::AtomAny::New(o, const_cast<TypeCode *>(t));
 
           //Objref CORBA. prefix=IOR,corbaname,corbaloc
           CORBA::Object_var obref;
@@ -1733,7 +1726,7 @@ namespace YACS
           else
               DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
 
-          return YACS::ENGINE::AtomAny::New(o);
+          return YACS::ENGINE::AtomAny::New(o, const_cast<TypeCode *>(t));
         }
     };
 
@@ -2312,7 +2305,7 @@ namespace YACS
        s=PyObject_ASCII(ob); // escape non ASCII characters and like repr(), which is not the same as str()...
       }
       Py_ssize_t size;
-      char* characters=PyUnicode_AsUTF8AndSize(s, &size);
+      const char* characters =  PyUnicode_AsUTF8AndSize(s, &size);
       std::string ss( characters, size);
       Py_DECREF(s);
       PyGILState_Release(gstate);