-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpythonHelpers.cpp
More file actions
97 lines (81 loc) · 2.99 KB
/
pythonHelpers.cpp
File metadata and controls
97 lines (81 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Copyright (c) 2019, Lawrence Livermore National Security, LLC.
*
* Produced at the Lawrence Livermore National Laboratory
*
* LLNL-CODE-746361
*
* All rights reserved. See COPYRIGHT for details.
*
* This file is part of the GEOSX Simulation Framework.
*
* GEOSX is a free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License (as published by the
* Free Software Foundation) version 2.1 dated February 1999.
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
// Python must be the first include.
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// Source includes
#include "pythonHelpers.hpp"
#include "../limits.hpp"
namespace LvArray
{
namespace python
{
namespace internal
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void xincref( PyObject * const obj )
{ Py_XINCREF( obj ); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void xdecref( PyObject * const obj )
{ Py_XDECREF( obj ); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool isInstanceOf( PyObject * const obj, PyTypeObject * const type )
{
PYTHON_ERROR_IF( obj == nullptr, PyExc_RuntimeError, "Passed a nullptr as an argument.", false );
int isInstanceOfType = PyObject_IsInstance( obj, reinterpret_cast< PyObject * >( type ) );
if( isInstanceOfType < 0 )
{
return false;
}
PYTHON_ERROR_IF( !isInstanceOfType, PyExc_TypeError, "Expect an argument of type " << type->tp_name, false );
return true;
}
} // namespace internal
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool addTypeToModule( PyObject * const module,
PyTypeObject * const type,
char const * const typeName )
{
if( PyType_Ready( type ) < 0 )
{
return false;
}
Py_INCREF( type );
if( PyModule_AddObject( module, typeName, reinterpret_cast< PyObject * >( type ) ) < 0 )
{
Py_DECREF( type );
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PyObject * create( std::string const & value )
{ return PyUnicode_FromString( value.c_str() ); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PyObject * createPyListOfStrings( std::string const * const strptr, long long const size )
{
PyObject * pylist = PyList_New( size );
for( long long i = 0; i < size; ++i )
{
PyObject * pystr = PyUnicode_FromString( strptr[ i ].c_str() );
PyList_SET_ITEM( pylist, integerConversion< Py_ssize_t >( i ), pystr );
}
return pylist;
}
} // namespace python
} // namespace LvArray