/* Implementation of Color.escape_sequence (self, is_fg) -> str. */
static PyObject *
-colorpy_escape_sequence (PyObject *self, PyObject *is_fg_obj)
+colorpy_escape_sequence (PyObject *self, PyObject *args, PyObject *kwargs)
{
- if (!gdbpy_is_color (self))
- {
- PyErr_SetString (PyExc_RuntimeError,
- _("Object is not gdb.Color."));
- return nullptr;
- }
+ static const char *keywords[] = { "is_foreground", nullptr };
+ PyObject *is_fg_obj;
- if (!PyBool_Check (is_fg_obj))
- {
- PyErr_SetString (PyExc_RuntimeError,
- _("A boolean argument is required."));
- return nullptr;
- }
+ /* Parse method arguments. */
+ if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "O!", keywords,
+ &PyBool_Type, &is_fg_obj))
+ return nullptr;
+
+ /* Python ensures the type of SELF. */
+ gdb_assert (gdbpy_is_color (self));
+
+ /* The argument parsing ensures we have a bool. */
+ gdb_assert (PyBool_Check (is_fg_obj));
bool is_fg = is_fg_obj == Py_True;
std::string s = gdbpy_get_color (self).to_ansi (is_fg);
static PyMethodDef color_methods[] =
{
- { "escape_sequence", colorpy_escape_sequence, METH_O,
+ { "escape_sequence", (PyCFunction) colorpy_escape_sequence,
+ METH_VARARGS | METH_KEYWORDS,
"escape_sequence (is_foreground) -> str.\n\
Return the ANSI escape sequence for this color.\n\
IS_FOREGROUND indicates whether this is a foreground or background color."},
"c_none.escape_sequence (True))"] \
"\033\\\[31m\033\\\[42mred on green\033\\\[49m red on default\033\\\[39m" \
"escape sequences"
+gdb_test [concat "python print (c_red.escape_sequence (is_foreground = True) + " \
+ "c_green.escape_sequence (is_foreground = False) + 'red on green' + " \
+ "c_none.escape_sequence (is_foreground = False) + ' red on default' + " \
+ "c_none.escape_sequence (is_foreground = True))"] \
+ "\033\\\[31m\033\\\[42mred on green\033\\\[49m red on default\033\\\[39m" \
+ "escape sequences using keyword arguments"
gdb_test_multiline "Try to sub-class gdb.Color" \
"python" "" \
"Python Exception <class 'RuntimeError'>: color argument must be a gdb\\.Color object\\." \
"Error occurred in Python: color argument must be a gdb\\.Color object\\."] \
"set color parameter to a non-color type"
+
+gdb_test "python c_none.escape_sequence(c_red)" \
+ [multi_line \
+ "Python Exception <class 'TypeError'>: argument 1 must be bool, not gdb.Color" \
+ "Error occurred in Python: argument 1 must be bool, not gdb.Color"]