diff options
author | Tom Tromey <tromey@redhat.com> | 2013-05-20 20:21:55 +0000 |
---|---|---|
committer | Tom Tromey <tromey@redhat.com> | 2013-05-20 20:21:55 +0000 |
commit | ba327838bac4a3a536fabaae0d151ff24f359495 (patch) | |
tree | 0e63302ae244d4a7a3c9a6f69906506a4b1f6d8d | |
parent | d81914324432f9b25708da13dce362428efa6b06 (diff) | |
download | binutils-ba327838bac4a3a536fabaae0d151ff24f359495.zip binutils-ba327838bac4a3a536fabaae0d151ff24f359495.tar.gz binutils-ba327838bac4a3a536fabaae0d151ff24f359495.tar.bz2 |
* python/py-cmd.c (cmdpy_completer): Use iterator protocol.
Correctly decref.
-rw-r--r-- | gdb/ChangeLog | 5 | ||||
-rw-r--r-- | gdb/python/py-cmd.c | 49 |
2 files changed, 33 insertions, 21 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 64bac86..84ca596 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,10 @@ 2013-05-20 Tom Tromey <tromey@redhat.com> + * python/py-cmd.c (cmdpy_completer): Use iterator protocol. + Correctly decref. + +2013-05-20 Tom Tromey <tromey@redhat.com> + * python/py-cmd.c (cmdpy_init): Decref 'ds_obj'. 2013-05-20 Tom Tromey <tromey@redhat.com> diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index 6516e1f..26823c7 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -247,26 +247,40 @@ cmdpy_completer (struct cmd_list_element *command, make_cleanup_py_decref (resultobj); result = NULL; - if (PySequence_Check (resultobj)) + if (PyInt_Check (resultobj)) { - Py_ssize_t i, len = PySequence_Size (resultobj); - Py_ssize_t out; + /* User code may also return one of the completion constants, + thus requesting that sort of completion. */ + long value; + + if (! gdb_py_int_as_long (resultobj, &value)) + { + /* Ignore. */ + PyErr_Clear (); + } + else if (value >= 0 && value < (long) N_COMPLETERS) + result = completers[value].completer (command, text, word); + } + else + { + PyObject *iter = PyObject_GetIter (resultobj); + PyObject *elt; - if (len < 0) + if (iter == NULL) goto done; - for (i = out = 0; i < len; ++i) + while ((elt = PyIter_Next (iter)) != NULL) { - PyObject *elt = PySequence_GetItem (resultobj, i); char *item; - if (elt == NULL || ! gdbpy_is_string (elt)) + if (! gdbpy_is_string (elt)) { /* Skip problem elements. */ - PyErr_Clear (); + Py_DECREF (elt); continue; } item = python_string_to_host_string (elt); + Py_DECREF (elt); if (item == NULL) { /* Skip problem elements. */ @@ -275,20 +289,13 @@ cmdpy_completer (struct cmd_list_element *command, } VEC_safe_push (char_ptr, result, item); } - } - else if (PyInt_Check (resultobj)) - { - /* User code may also return one of the completion constants, - thus requesting that sort of completion. */ - long value; - if (! gdb_py_int_as_long (resultobj, &value)) - { - /* Ignore. */ - PyErr_Clear (); - } - else if (value >= 0 && value < (long) N_COMPLETERS) - result = completers[value].completer (command, text, word); + Py_DECREF (iter); + + /* If we got some results, ignore problems. Otherwise, report + the problem. */ + if (result != NULL && PyErr_Occurred ()) + PyErr_Clear (); } done: |