Ignore:
Timestamp:
Apr 23, 2021, 8:31:24 PM (5 years ago)
Author:
Frederik Heber <frederik.heber@…>
Branches:
Candidate_v1.7.0, stable
Children:
3f5b0a
Parents:
ba49d8
git-author:
Frederik Heber <frederik.heber@…> (08/03/20 21:55:45)
git-committer:
Frederik Heber <frederik.heber@…> (04/23/21 20:31:24)
Message:

Making test code python2&3 compatible.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Python/PythonScripting.cpp

    rba49d8 rb0060a  
    5353bool executePythonScript(const std::string &python_string, const std::string &python_scripttitle)
    5454{
     55  std::stringstream input(python_string);
     56  std::stringstream buffer_import;
     57  std::stringstream buffer_else;
     58  // move all lines with "import" to the beginning
     59  std::string current_line;
     60  while(std::getline(input,current_line,'\n')){
     61    if (current_line.find("import ") != std::string::npos)
     62      buffer_import << current_line << std::endl;
     63    else
     64      buffer_else << current_line << std::endl;
     65  }
     66  std::string pythonscript_import( std::istreambuf_iterator<char>(buffer_import),
     67      (std::istreambuf_iterator<char>()) );
     68  std::string pythonscript_else( std::istreambuf_iterator<char>(buffer_else),
     69      (std::istreambuf_iterator<char>()) );
     70
    5571  try {
    5672#ifdef HAVE_PYTHON2
     
    6783    boost::python::object main_namespace = main_module.attr("__dict__");
    6884
     85    boost::python::handle<> imported(( PyRun_String( pythonscript_import.c_str(),
     86        Py_file_input,
     87        main_namespace.ptr(),
     88        main_namespace.ptr() ) ));
     89
    6990    boost::python::object molecuilder_module( (boost::python::handle<>(PyImport_ImportModule("pyMoleCuilder"))) );
    7091    main_namespace["pyMoleCuilder"] = molecuilder_module;
    7192
    7293    std::string enveloped_script("print(\"BEGIN of "+python_scripttitle+":\")\n");
    73     enveloped_script += python_string+std::string("\n");
     94    enveloped_script += pythonscript_else+std::string("\n");
    7495    enveloped_script += std::string("print(\"END of "+python_scripttitle+":\")\n");
    7596
Note: See TracChangeset for help on using the changeset viewer.