From 3cc9c057e934080e8b13dc8b58b32efd9dcf141e Mon Sep 17 00:00:00 2001 From: Michael Ortmann Date: Fri, 31 May 2024 00:52:53 +0200 Subject: [PATCH] Add dir(eggdrop.tcl) --- src/mod/python.mod/pycmds.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/mod/python.mod/pycmds.c b/src/mod/python.mod/pycmds.c index 1e55088c4..0cf95506c 100644 --- a/src/mod/python.mod/pycmds.c +++ b/src/mod/python.mod/pycmds.c @@ -344,6 +344,34 @@ static PyObject *python_call_tcl(PyObject *self, PyObject *args, PyObject *kwarg return PyUnicode_DecodeUTF8(result, strlen(result), NULL); } + +static PyObject *py_dir(PyObject *self, PyObject *args) { + PyObject *py_list, *py_s; + Tcl_Obj *tcl_list, **objv; + int objc; + int i; + + py_list = PyList_New(0); + if (Tcl_VarEval(tclinterp, "info commands", NULL, NULL) == TCL_ERROR) + putlog(LOG_MISC, "*", "python error: Tcl_VarEval(info commands)"); + else { + tcl_list = Tcl_GetObjResult(tclinterp); + if (Tcl_ListObjGetElements(tclinterp, tcl_list, &objc, &objv) == TCL_ERROR) + putlog(LOG_MISC, "*", "python error: Tcl_VarEval(info commands)"); + else { + for (i = 0; i < objc; i++) { + const char *value = Tcl_GetString(objv[i]); + if (!strchr(value, ':')) { + py_s = PyUnicode_FromString(value); + PyList_Append(py_list, py_s); + Py_DECREF(py_s); + } + } + } + } + return py_list; +} + static PyObject *py_findtclfunc(PyObject *self, PyObject *args) { char *cmdname; TclFunc *result; @@ -372,7 +400,7 @@ static PyMethodDef MyPyMethods[] = { }; static PyMethodDef EggTclMethods[] = { - // TODO: __dict__ with all valid Tcl commands? + {"__dir__", py_dir, METH_VARARGS, ""}, {"__getattr__", py_findtclfunc, METH_VARARGS, "fallback to call Tcl functions transparently"}, {NULL, NULL, 0, NULL} };