Skip to content

Commit bdd3ef9

Browse files
committed
hal getattr
1 parent 82ee94b commit bdd3ef9

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

lib/python/hal.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,51 @@
2929

3030
import _hal
3131
from _hal import *
32+
import warnings
33+
import os
34+
35+
def __getattr__(name):
36+
if name == 'is_rt':
37+
warnings.simplefilter('always', DeprecationWarning)
38+
warnings.warn(f"{name} is deprecated, use get_realtime_type() instead", DeprecationWarning)
39+
warnings.simplefilter('default', DeprecationWarning)
40+
41+
#We the hal to be initialized to use get_realtime_type()
42+
#If it is not initialized, create a component which will inizialize it
43+
#It will be cleaned up imediately
44+
comp_name=f"halpy{os.getpid()}"
45+
if not is_initialized():
46+
c=component(comp_name);
47+
48+
t = get_realtime_type()
49+
if t == REALTIME_TYPE_UNINITIALIZED:
50+
warnings.warn("Unable to check realtime status, not initialized", Warning)
51+
return False
52+
elif t == REALTIME_TYPE_NONE:
53+
return False
54+
else:
55+
return True
56+
if name == 'is_sim':
57+
warnings.simplefilter('always', DeprecationWarning)
58+
warnings.warn(f"{name} is deprecated, use get_realtime_type() instead", DeprecationWarning)
59+
warnings.simplefilter('default', DeprecationWarning)
60+
61+
#We the hal to be initialized to use get_realtime_type()
62+
#If it is not initialized, create a component which will inizialize it
63+
#It will be cleaned up imediately
64+
comp_name=f"halpy{os.getpid()}"
65+
if not is_initialized():
66+
c=component(comp_name);
67+
68+
t = get_realtime_type()
69+
if t == REALTIME_TYPE_UNINITIALIZED:
70+
warnings.warn("Unable to check realtime status, not initialized", Warning)
71+
return True
72+
elif t == REALTIME_TYPE_NONE:
73+
return True
74+
else:
75+
return False
76+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
3277

3378
class _ItemWrap(object):
3479
def __new__(cls, item):

src/hal/halmodule.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,10 @@ static PyObject *pyhal_get_realtime_type(PyObject * /*self*/, PyObject * /*o*/)
18351835
return PyLong_FromLong(res);
18361836
}
18371837

1838+
static PyObject *pyhal_is_initialized(PyObject * /*self*/, PyObject * /*o*/) {
1839+
return PyBool_FromLong(hal_shmem_base != NULL);
1840+
}
1841+
18381842
struct shmobject {
18391843
PyObject_HEAD
18401844
halobject *comp;
@@ -2306,7 +2310,9 @@ static PyMethodDef module_methods[] = {
23062310
{"get_info_params", get_info_params, METH_VARARGS,
23072311
".get_info_params(): Get a list of dicts for all the parameters; {NAME:, VALUE:}"},
23082312
{"get_realtime_type", pyhal_get_realtime_type, METH_NOARGS,
2309-
"Call get_realtime_type"},
2313+
".get_realtime_type(): Return the type of the running realtime"},
2314+
{"is_initialized", pyhal_is_initialized, METH_NOARGS,
2315+
".is_initialized(): Return if hal is initialized"},
23102316
{},
23112317
};
23122318

@@ -2395,10 +2401,6 @@ PyMODINIT_FUNC PyInit__hal(void)
23952401
PyModule_AddIntConstant(m, "REALTIME_TYPE_XENOMAI", REALTIME_TYPE_XENOMAI);
23962402
PyModule_AddIntConstant(m, "REALTIME_TYPE_XENOMAI_EVL", REALTIME_TYPE_XENOMAI_EVL);
23972403

2398-
//ToDo: Search all references and eliminate them
2399-
//PyModule_AddIntConstant(m, "is_sim", !rtapi_is_realtime());
2400-
//PyModule_AddIntConstant(m, "is_rt", rtapi_is_realtime());
2401-
24022404
PyModule_AddIntConstant(m, "is_kernelspace", rtapi_is_kernelspace());
24032405
PyModule_AddIntConstant(m, "is_userspace", !rtapi_is_kernelspace());
24042406

0 commit comments

Comments
 (0)