Skip to content

Commit

Permalink
Add dir(eggdrop.tcl)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ortmann committed May 30, 2024
1 parent 39a582c commit 3cc9c05
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/mod/python.mod/pycmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}
};
Expand Down

0 comments on commit 3cc9c05

Please sign in to comment.