-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsrreal_validators.cpp
More file actions
62 lines (51 loc) · 1.51 KB
/
srreal_validators.cpp
File metadata and controls
62 lines (51 loc) · 1.51 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
/*****************************************************************************
*
* diffpy.srreal Complex Modeling Initiative
* (c) 2013 Brookhaven Science Associates,
* Brookhaven National Laboratory.
* All rights reserved.
*
* File coded by: Pavol Juhas
*
* See AUTHORS.txt for a list of people who contributed.
* See LICENSE.txt for license information.
*
******************************************************************************
*
* Helper functions for argument checking.
*
*****************************************************************************/
#include <boost/python/errors.hpp>
#include <boost/python/import.hpp>
#include "srreal_validators.hpp"
namespace srrealmodule {
using namespace boost::python;
void ensure_index_bounds(int idx, int lo, int hi)
{
if (idx < lo || idx >= hi)
{
PyErr_SetString(PyExc_IndexError, "Index out of range.");
throw_error_already_set();
}
}
void ensure_non_negative(int value)
{
if (value < 0)
{
PyErr_SetString(PyExc_ValueError, "Value cannot be negative.");
throw_error_already_set();
}
}
bool isiterable(boost::python::object obj)
{
using boost::python::import;
#if PY_MAJOR_VERSION >= 3
object Iterable = import("collections.abc").attr("Iterable");
#else
object Iterable = import("collections").attr("Iterable");
#endif
bool rv = (1 == PyObject_IsInstance(obj.ptr(), Iterable.ptr()));
return rv;
}
} // namespace srrealmodule
// End of file