@@ -27,21 +27,20 @@ void pydynd::init_w__type_callable_typeobject(PyObject *type)
27
27
void pydynd::add__type_names_to_dir_dict (const ndt::type &dt, PyObject *dict)
28
28
{
29
29
if (!dt.is_builtin ()) {
30
- const std::pair<std::string, nd::callable> *properties;
31
- size_t count;
32
30
// Add the type properties
33
- dt. extended ()-> get_dynamic_type_properties (&properties, &count) ;
34
- for ( size_t i = 0 ; i < count; ++i) {
35
- if ( PyDict_SetItemString (dict, properties[i]. first . c_str (), Py_None) <
36
- 0 ) {
31
+ std::map<std::string, nd::callable> properties ;
32
+ dt. extended ()-> get_dynamic_type_properties (properties);
33
+ for ( const auto &pair : properties) {
34
+ if ( PyDict_SetItemString (dict, pair. first . c_str (), Py_None) < 0 ) {
37
35
throw runtime_error (" " );
38
36
}
39
37
}
40
38
// Add the type functions
41
- dt.extended ()->get_dynamic_type_functions (&properties, &count);
39
+ const std::pair<std::string, nd::callable> *functions;
40
+ size_t count;
41
+ dt.extended ()->get_dynamic_type_functions (&functions, &count);
42
42
for (size_t i = 0 ; i < count; ++i) {
43
- if (PyDict_SetItemString (dict, properties[i].first .c_str (), Py_None) <
44
- 0 ) {
43
+ if (PyDict_SetItemString (dict, functions[i].first .c_str (), Py_None) < 0 ) {
45
44
throw runtime_error (" " );
46
45
}
47
46
}
@@ -52,30 +51,25 @@ PyObject *pydynd::get__type_dynamic_property(const dynd::ndt::type &dt,
52
51
PyObject *name)
53
52
{
54
53
if (!dt.is_builtin ()) {
55
- const std::pair<std::string, nd::callable> *properties;
56
- size_t count;
57
54
// Search for a property
58
- dt.extended ()->get_dynamic_type_properties (&properties, &count);
55
+ std::map<std::string, nd::callable> properties;
56
+ dt.extended ()->get_dynamic_type_properties (properties);
59
57
// TODO: We probably want to make some kind of acceleration structure for
60
58
// the name lookup
61
- if (count > 0 ) {
62
- std::string nstr = pystring_as_string (name);
63
- for (size_t i = 0 ; i < count; ++i) {
64
- if (properties[i].first == nstr) {
65
- return DyND_PyWrapper_New (const_cast <dynd::nd::callable &>(
66
- properties[i].second )(dynd::kwds (" self" , dt)));
67
- // return call_gfunc_callable(nstr, properties[i].second,
68
- // dt);
69
- }
70
- }
59
+ std::string nstr = pystring_as_string (name);
60
+ nd::callable p = properties[nstr];
61
+ if (!p.is_null ()) {
62
+ return DyND_PyWrapper_New (p (dt));
71
63
}
72
64
// Search for a function
73
- dt.extended ()->get_dynamic_type_functions (&properties, &count);
65
+ const std::pair<std::string, nd::callable> *functions;
66
+ size_t count;
67
+ dt.extended ()->get_dynamic_type_functions (&functions, &count);
74
68
if (count > 0 ) {
75
69
std::string nstr = pystring_as_string (name);
76
70
for (size_t i = 0 ; i < count; ++i) {
77
- if (properties [i].first == nstr) {
78
- return wrap__type_callable (nstr, properties [i].second , dt);
71
+ if (functions [i].first == nstr) {
72
+ return wrap__type_callable (nstr, functions [i].second , dt);
79
73
}
80
74
}
81
75
}
0 commit comments