Skip to content

Commit

Permalink
python.mod: add dir(eggdrop.tcl) (#1596)
Browse files Browse the repository at this point in the history
* Add dir(eggdrop.tcl)

* Add "info procs" and filter by starting '*' instead of containing ':'

---------

Co-authored-by: Michael Ortmann <keine email>
  • Loading branch information
michaelortmann authored Aug 7, 2024
1 parent c61ae0d commit e9aead7
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/mod/python.mod/pycmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,38 @@ 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;
int i, j;
const char *info[] = {"info commands", "info procs"}, *s, *value;
Tcl_Obj *tcl_list, **objv;
int objc;

py_list = PyList_New(0);
for (i = 0; i < (sizeof info / sizeof info[0]); i++) {
s = info[i];
if (Tcl_VarEval(tclinterp, s, NULL, NULL) == TCL_ERROR)
putlog(LOG_MISC, "*", "python error: Tcl_VarEval(%s)", s);
else {
tcl_list = Tcl_GetObjResult(tclinterp);
if (Tcl_ListObjGetElements(tclinterp, tcl_list, &objc, &objv) == TCL_ERROR)
putlog(LOG_MISC, "*", "python error: Tcl_VarEval(%s)", s);
else {
for (j = 0; j < objc; j++) {
value = Tcl_GetString(objv[j]);
if (*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;
Expand Down Expand Up @@ -399,7 +431,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}
};
Expand Down

0 comments on commit e9aead7

Please sign in to comment.