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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "global.hpp"
#include "memory.hpp"
#include <sstream>
namespace orcus { namespace python {
void set_python_exception(PyObject* type, const std::exception& e)
{
std::ostringstream os;
os << "C++ exception caught: " << e.what();
PyErr_SetString(type, os.str().data());
}
PyObject* get_python_enum_value(const char* enum_class_name, const char* value_name)
{
py_scoped_ref orcus_mod = PyImport_ImportModule("orcus");
if (!orcus_mod)
{
PyErr_SetString(PyExc_RuntimeError, "failed to import orcus module.");
return nullptr;
}
py_scoped_ref cls = PyObject_GetAttrString(orcus_mod.get(), enum_class_name);
if (!cls)
{
std::ostringstream os;
os << "failed to find class orcus." << enum_class_name << ".";
PyErr_SetString(PyExc_RuntimeError, os.str().data());
return nullptr;
}
return PyObject_GetAttrString(cls.get(), value_name);
}
PyObject* create_object_from_type(PyTypeObject* type)
{
if (!type)
{
PyErr_SetString(PyExc_RuntimeError, "Type object is null.");
return nullptr;
}
PyObject* obj = type->tp_new(type, nullptr, nullptr);
if (!obj)
{
std::ostringstream os;
os << "Failed to instantiate an object of type " << type->tp_name << ".";
PyErr_SetString(PyExc_RuntimeError, os.str().data());
return nullptr;
}
return obj;
}
}}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|