Merge pull request #1641 from joakim-hove/python-enable-enum
Enable fine grained control of Python ON | OFF
This commit is contained in:
commit
eca0589fe1
@ -52,10 +52,45 @@ class EclipseState;
|
||||
else
|
||||
OpmLog::Error("This version of opmcommon has been built with support for embedded Python");
|
||||
|
||||
|
||||
The default constructor will enable the Python interpreter if the current
|
||||
version of opm-common has been built support for embedded Python, by using the
|
||||
alternative Python(Enable enable) constructor you can explicitly say if you
|
||||
want Python support or not; if that request can not be satisfied you will get
|
||||
std::logic_error().
|
||||
|
||||
Observe that the real underlying Python interpreter is essentially a singleton
|
||||
- i.e. only a one interpreter can be active at any time. The details of the
|
||||
interaction between build configuration, constructor arg and multiple
|
||||
instances is as follows:
|
||||
|
||||
|
||||
Build: | Constructor arg | Existing instance | Result
|
||||
---------|--------------------|---------------------|-------
|
||||
True | OFF | * | { }
|
||||
True | ON | False | { Python }
|
||||
True | ON | True | std::logic_error
|
||||
True | COND | True | { }
|
||||
True | COND | False | { Python }
|
||||
False | OFF | * | { }
|
||||
False | ON | * | std::logic_error
|
||||
False | COND | * | { }
|
||||
---------|--------------------|---------------------|-------
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
class Python {
|
||||
public:
|
||||
|
||||
enum class Enable {
|
||||
ON, /* Enable the Python extensions - throw std::logic_error() if it fails. */
|
||||
COND, /* Try to enable Python extensions*/
|
||||
OFF /* Do not enable Python */
|
||||
};
|
||||
|
||||
explicit Python(Enable enable);
|
||||
Python();
|
||||
bool exec(const std::string& python_code) const;
|
||||
bool exec(const std::string& python_code, const Parser& parser, Deck& deck) const;
|
||||
|
@ -24,8 +24,25 @@
|
||||
|
||||
namespace Opm {
|
||||
|
||||
Python::Python() :
|
||||
interp(std::make_shared<PythonInterp>())
|
||||
Python::Python(Enable enable) {
|
||||
if (enable == Enable::OFF)
|
||||
return;
|
||||
|
||||
#ifdef EMBEDDED_PYTHON
|
||||
if (!Py_IsInitialized())
|
||||
this->interp = std::make_shared<PythonInterp>();
|
||||
else if (enable == Enable::ON)
|
||||
throw std::logic_error("An instance of the Python interpreter is already running");
|
||||
|
||||
return;
|
||||
#endif
|
||||
|
||||
if (enable == Enable::ON)
|
||||
throw std::logic_error("The version has been built without Python support");
|
||||
}
|
||||
|
||||
|
||||
Python::Python() : Python(Enable::COND)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -41,6 +41,14 @@ BOOST_AUTO_TEST_CASE(INSTANTIATE) {
|
||||
BOOST_CHECK(!python);
|
||||
BOOST_CHECK_THROW(python.exec("print('Hello world')"), std::logic_error);
|
||||
BOOST_CHECK(! Python::enabled() );
|
||||
|
||||
|
||||
BOOST_CHECK_THROW( Python(Python::Enable::ON), std::logic_error );
|
||||
Python python_cond(Python::Enable::COND);
|
||||
BOOST_CHECK(!python_cond);
|
||||
|
||||
Python python_off(Python::Enable::OFF);
|
||||
BOOST_CHECK(!python_off);
|
||||
}
|
||||
|
||||
#else
|
||||
@ -129,6 +137,29 @@ BOOST_AUTO_TEST_CASE(PYACTION) {
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Python_Constructor) {
|
||||
Python python_off(Python::Enable::OFF);
|
||||
BOOST_CHECK(!python_off);
|
||||
|
||||
Python python_on(Python::Enable::ON);
|
||||
BOOST_CHECK(python_on);
|
||||
|
||||
// Can only have one Python interpreter active at any time
|
||||
BOOST_CHECK_THROW(Python(Python::Enable::ON), std::logic_error);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Python_Constructor2) {
|
||||
Python python_cond1(Python::Enable::COND);
|
||||
BOOST_CHECK(python_cond1);
|
||||
|
||||
Python python_cond2(Python::Enable::COND);
|
||||
BOOST_CHECK(!python_cond2);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user