Salome HOME
Deal with PyUnicode_AsUTF8 returning NULL.
[tools/py2cpp.git] / src / TypeConversions.cxx
1 // Copyright (C) 2019  EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "TypeConversions.hxx"
20
21 namespace py2cpp
22 {
23 ConversionCheck fromPy(PyObject * po, int& result)
24 {
25   ConversionCheck check;
26   if(po && PyLong_Check(po))
27     result = PyLong_AsLong(po);
28   else
29     check.addError("int", po);
30   return check;
31 }
32
33 PyObject * toPy(int val)
34 {
35   return PyLong_FromLong(val);
36 }
37
38 ConversionCheck fromPy(PyObject * po, unsigned int& result)
39 {
40   ConversionCheck check;
41   if(po && PyLong_Check(po))
42     result = PyLong_AsUnsignedLong(po);
43   else
44     check.addError("int", po);
45   return check;
46 }
47
48 PyObject * toPy(unsigned int val)
49 {
50   return PyLong_FromUnsignedLong(val);
51 }
52
53 ConversionCheck fromPy(PyObject * po, bool& result)
54 {
55   ConversionCheck check;
56   if(po && PyBool_Check(po))
57     result = (Py_True == po);
58   else
59     check.addError("bool", po);
60   return check;
61 }
62
63 PyObject * toPy(bool val)
64 {
65   return PyBool_FromLong(val);
66 }
67
68 ConversionCheck fromPy(PyObject * po, double& result)
69 {
70   ConversionCheck check;
71   if(po && PyFloat_Check(po))
72     result = PyFloat_AsDouble(po);
73   else if(po && PyLong_Check(po))
74   {
75     int v = PyLong_AsLong(po);
76     result = v;
77   }
78   else 
79     check.addError("double", po);
80   return check;
81 }
82
83 PyObject * toPy(double val)
84 {
85   return PyFloat_FromDouble(val);
86 }
87
88 ConversionCheck fromPy(PyObject * po, std::string& result)
89 {
90   ConversionCheck check;
91   if(po && PyUnicode_Check(po))
92   {
93     const char* charstr = PyUnicode_AsUTF8(po);
94     if(charstr != nullptr)
95       result = charstr;
96     else
97       result = "";
98   }
99   else
100     check.addError("std::string", po);
101   return check;
102 }
103
104 PyObject * toPy(const std::string& val)
105 {
106   return PyUnicode_FromString(val.c_str());
107 }
108
109 PyObject * toPy(const char* val)
110 {
111   return PyUnicode_FromString(val);
112 }
113
114 ConversionCheck fromPy( PyObject *po, PyObject *& result)
115 {
116   result = po;
117   Py_XINCREF(result);
118   return ConversionCheck();
119 }
120
121 PyObject * toPy(PyObject * obj)
122 {
123   Py_XINCREF(obj);
124   return obj;
125 }
126
127 ConversionCheck fromPy( PyObject *po, PyPtr& result)
128 {
129   Py_XINCREF(po);
130   result.reset(po);
131   return ConversionCheck();
132 }
133
134 PyObject * toPy(const PyPtr& obj)
135 {
136   PyObject * result = obj.get();
137   Py_XINCREF(result);
138   return result;
139 }
140
141 template<>
142 PyPtr fromPy<PyPtr>( PyObject *po)
143 {
144   return PyPtr(po);
145 }
146
147 }