Skip to content
Snippets Groups Projects
Commit 493e90c2 authored by AlQuemist's avatar AlQuemist
Browse files

rm uneeded PyCore/Sample/ImportSample

parent e76d7342
No related branches found
No related tags found
1 merge request!2755Improve the construction of a 'Sample' from a Python script
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file PyCore/Sample/ImportSample.cpp
//! @brief Implements importing sample from Python.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#ifdef BORNAGAIN_PYTHON
#include "PyCore/Sample/ImportSample.h"
#include "PyCore/Embed/PyCore.h"
#include "PyCore/Embed/PyInterpreter.h" // BornAgain::importScript
#include "auto/Wrap/swig_runtime.h" // for creating a core object from Python
#include <stdexcept>
void* PyInterpreter::pyscript2object(const std::string& script, const std::string& functionName,
const std::string& typeName, const std::string& path)
{
PyObjectPtr tmpModule{PyInterpreter::BornAgain::importScript(script, path)};
// locate the `get_simulation` function (it is an attribute of the module)
PyObject* pAddFn = PyObject_GetAttrString(tmpModule.get(), functionName.c_str());
if (!pAddFn)
throw std::runtime_error(errorDescription("PyInterpreter::BornAgain: "
"Cannot locate the compiled function '"
+ functionName + "'"));
// create a `Sample` Python object via calling the function
PyObject* ret1 = PyObject_CallFunctionObjArgs(pAddFn, NULL);
// clean up
Py_DecRef(pAddFn);
if (!ret1)
throw std::runtime_error(
errorDescription("Failed executing Python function '" + functionName + "'"));
// Construct a C++ object from the Python object.
// This conversion requires a SWIG-produced Python API.
void* argp1 = 0;
swig_type_info* pTypeInfo = SWIG_TypeQuery((typeName + "*").c_str());
const int res = SWIG_ConvertPtr(ret1, &argp1, pTypeInfo, 0);
if (!SWIG_IsOK(res)) {
Py_DecRef(ret1);
throw std::runtime_error(
errorDescription("Function " + functionName + " did not yield Sample instance"));
}
return reinterpret_cast<void*>(argp1);
}
#endif // BORNAGAIN_PYTHON
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file PyCore/Sample/ImportSample.h
//! @brief Declares importing sample from Python.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#ifndef BORNAGAIN_PYTHON
#error this header requires Python support
#endif
#ifdef SWIG
#error no need to expose this header to Swig
#endif
#ifndef BORNAGAIN_PYCORE_SAMPLE_IMPORTSAMPLE_H
#define BORNAGAIN_PYCORE_SAMPLE_IMPORTSAMPLE_H
#include "PyCore/Embed/PyObjectPtr.h"
#include <string>
namespace PyInterpreter {
//! Creates a core object by running python code in embedded interpreter.
void* pyscript2object(const std::string& script, const std::string& functionName,
const std::string& typeName, const std::string& path = "");
} // namespace PyInterpreter
#endif // BORNAGAIN_PYCORE_SAMPLE_IMPORTSAMPLE_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment