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/
39 #include <gromacs/utility/exceptions.h>
42 std::string detail = formatExceptionMessageToString(sipExceptionRef);
45 PyErr_SetString(sipException_gmx_GromacsException, detail.data());
50 // This MappedType makes sure that unique_ptr does not get destructed while wrapped pointer is still needed.
51 // A class called 'TYPEPointer' needs to be created for every use.
54 %MappedType std::unique_ptr < TYPE > /NoRelease/ {
57 PyObject *wrp = sipConvertFromType(sipCpp->get(), sipType_TYPE, NULL);
58 PyObject *ptr = sipConvertFromNewType(sipCpp, sipType_TYPEPointer, wrp);
59 // ptr is created only to bind wrp's ownership to it
60 GMX_UNUSED_VALUE(ptr);
66 // Make it impossible to convert into this type
72 GMX_ASSERT(true, "Converting something into unique_ptr. This should not happen!");
77 %VirtualErrorHandler vehandler
78 SIP_RELEASE_GIL(sipGILState);
81 GMX_THROW(gmx::InternalError("Python virtual overload raised an exception, see traceback"));
86 int argv_wrapper(PyObject *, const std::function < int(int, char**) > &);
87 template < typename T > class PyFactory;
90 // This is put into UnitPostIncludeCode so that sip* declarations are available.
93 #include <gromacs/utility/exceptions.h>
94 template < typename T > class PyFactory {
96 PyFactory(const sipTypeDef *sipType, PyObject *mod) : sipType_(sipType), mod(mod) {
98 std::unique_ptr < T > operator () () {
100 int can = sipCanConvertToType(mod, sipType_, 0);
104 GMX_THROW(gmx::APIError("Provided python object can not be converted to type required by runAsMain-like function"));
107 T *module = (T*) sipConvertToType(
108 mod, sipType_, NULL, 0, NULL, &iserr);
109 return std::unique_ptr < T > (module);
112 const sipTypeDef *sipType_;
118 int argv_wrapper(PyObject *py_argv, const std::function < int(int, char**) > &runner)
120 int argc = PyList_GET_SIZE(py_argv);
122 char **argv = new char *[argc + 1];
124 std::fill(to_free, to_free+argc, false);
127 // TODO: Use something better than AsLatin1String to avoid unicode errors
128 for (int a = 0; a < argc; ++a)
130 PyObject *arg_obj = PyList_GET_ITEM(py_argv, a);
131 const char *arg = sipString_AsLatin1String(&arg_obj);
144 argv[a] = const_cast < char * > (arg);
149 int result = runner(argc, argv);
151 for (int i = 0; i < argc; i++)