aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/python.c
diff options
context:
space:
mode:
authorTom Tromey <tromey@adacore.com>2023-04-28 09:11:23 -0600
committerTom Tromey <tromey@adacore.com>2023-05-23 14:17:04 -0600
commit125862f0f220f631a184599ea7c9ff7efbea9044 (patch)
treee89a0003d7086ab2e01319ddf1f66e74a125c156 /gdb/python/python.c
parent2c64cbb737e801c5c3b0e0d8b03901b65b2f84dc (diff)
downloadbinutils-125862f0f220f631a184599ea7c9ff7efbea9044.zip
binutils-125862f0f220f631a184599ea7c9ff7efbea9044.tar.gz
binutils-125862f0f220f631a184599ea7c9ff7efbea9044.tar.bz2
Add global_context parameter to gdb.parse_and_eval
This adds a 'global_context' parse_and_eval to gdb.parse_and_eval. This lets users request a parse that is done at "global scope". I considered letting callers pass in a block instead, with None meaning "global" -- but then there didn't seem to be a clean way to express the default for this parameter. Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Diffstat (limited to 'gdb/python/python.c')
-rw-r--r--gdb/python/python.c28
1 files changed, 22 insertions, 6 deletions
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 9703d6e..517cea7 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -969,19 +969,34 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
/* Parse a string and evaluate it as an expression. */
static PyObject *
-gdbpy_parse_and_eval (PyObject *self, PyObject *args)
+gdbpy_parse_and_eval (PyObject *self, PyObject *args, PyObject *kw)
{
+ static const char *keywords[] = { "expression", "global_context", nullptr };
+
const char *expr_str;
+ PyObject *global_context_obj = nullptr;
- if (!PyArg_ParseTuple (args, "s", &expr_str))
- return NULL;
+ if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords,
+ &expr_str,
+ &PyBool_Type, &global_context_obj))
+ return nullptr;
+
+ parser_flags flags = 0;
+ if (global_context_obj != NULL)
+ {
+ int cmp = PyObject_IsTrue (global_context_obj);
+ if (cmp < 0)
+ return nullptr;
+ if (cmp)
+ flags |= PARSER_LEAVE_BLOCK_ALONE;
+ }
PyObject *result = nullptr;
try
{
gdbpy_allow_threads allow_threads;
scoped_value_mark free_values;
- struct value *val = parse_and_eval (expr_str);
+ struct value *val = parse_and_eval (expr_str, flags);
result = value_to_value_object (val);
}
catch (const gdb_exception &except)
@@ -2563,8 +2578,9 @@ The first element contains any unparsed portion of the String parameter\n\
(or None if the string was fully parsed). The second element contains\n\
a tuple that contains all the locations that match, represented as\n\
gdb.Symtab_and_line objects (or None)."},
- { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
- "parse_and_eval (String) -> Value.\n\
+ { "parse_and_eval", (PyCFunction) gdbpy_parse_and_eval,
+ METH_VARARGS | METH_KEYWORDS,
+ "parse_and_eval (String, [Boolean]) -> Value.\n\
Parse String as an expression, evaluate it, and return the result as a Value."
},