2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 %Exception gmx::GromacsException(SIP_Exception) /Default/ {
38 #include <gromacs/utility/exceptions.h>
41 std::string detail = formatExceptionMessageToString(sipExceptionRef);
44 PyErr_SetString(sipException_gmx_GromacsException, detail.data());
49 // This MappedType makes sure that unique_ptr does not get destructed while wrapped pointer is still needed.
50 // A class called 'TYPEPointer' needs to be created for every use.
53 %MappedType std::unique_ptr<TYPE> /NoRelease/ {
56 PyObject *wrp = sipConvertFromType(sipCpp->get(), sipType_TYPE, NULL);
57 PyObject *ptr = sipConvertFromNewType(sipCpp, sipType_TYPEPointer, wrp);
58 // ptr is created only to bind wrp's ownership to it
59 GMX_UNUSED_VALUE(ptr);
65 // Make it impossible to convert into this type
69 GMX_ASSERT(true, "Converting something into unique_ptr. This should not happen!");
74 %VirtualErrorHandler vehandler
75 SIP_RELEASE_GIL(sipGILState);
78 GMX_THROW(gmx::InternalError("Python virtual overload raised an exception, see traceback"));
83 int argv_wrapper(PyObject *, const std::function<int(int, char**)> &);
84 template <typename T> class PyFactory;
87 // This is put into UnitPostIncludeCode so that sip* declarations are available.
90 #include <gromacs/utility/exceptions.h>
91 template <typename T> class PyFactory {
93 PyFactory(const sipTypeDef *sipType, PyObject *mod) : sipType_(sipType), mod(mod) {} ;
94 std::unique_ptr<T> operator () () {
96 int can = sipCanConvertToType(mod, sipType_, 0);
99 GMX_THROW(gmx::APIError("Provided python object can not be converted to type required by runAsMain-like function"));
101 T *module = (T*) sipConvertToType(
102 mod, sipType_, NULL, 0, NULL, &iserr);
103 return std::unique_ptr<T>(module);
106 const sipTypeDef *sipType_;
112 int argv_wrapper(PyObject *py_argv, const std::function<int(int, char**)> &runner) {
113 int argc = PyList_GET_SIZE(py_argv);
115 char **argv = new char *[argc + 1];
117 std::fill(to_free, to_free+argc, false);
120 // TODO: Use something better than AsLatin1String to avoid unicode errors
121 for (int a = 0; a < argc; ++a)
123 PyObject *arg_obj = PyList_GET_ITEM(py_argv, a);
124 const char *arg = sipString_AsLatin1String(&arg_obj);
137 argv[a] = const_cast<char *>(arg);
142 int result = runner(argc, argv);
144 for (int i = 0; i < argc; i++)