Blame SOURCES/gdb-python-completer-1of2.patch

f9426a
http://sourceware.org/ml/gdb-patches/2014-07/msg00002.html
f9426a
Subject: Re: [PATCH] PR python/16699: GDB Python command completion with overriden complete vs. completer class
f9426a
https://bugzilla.redhat.com/show_bug.cgi?id=1075199
f9426a
f9426a
On Wednesday, May 21 2014, Doug Evans wrote:
f9426a
f9426a
> Sergio Durigan Junior <sergiodj@redhat.com> writes:
f9426a
>> [...]
f9426a
>> Thanks.  WDYT of the following patch?
f9426a
>
f9426a
> Hi.
f9426a
>
f9426a
> fwiw it's too bad the ability to plug in different completers isn't more,
f9426a
> I dunno, parameterized (couldn't pick a better term, apologies -
f9426a
> I thought of "object oriented" but that carries its own baggage).
f9426a
> Performing completion obviously involves specifying more than a just
f9426a
> single function (witness the comparison of the completer with specific
f9426a
> functions).
f9426a
f9426a
Thanks for the reply, and sorry for the long delay in answering.
f9426a
f9426a
> Plus it's more than specifying brkchars.
f9426a
> Witness code like this:
f9426a
>
f9426a
> 		      /* Many commands which want to complete on
f9426a
> 			 file names accept several file names, as
f9426a
> 			 in "run foo bar >>baz".  So we don't want
f9426a
> 			 to complete the entire text after the
f9426a
> 			 command, just the last word.  To this
f9426a
> 			 end, we need to find the beginning of the
f9426a
> 			 file name by starting at `word' and going
f9426a
> 			 backwards.  */
f9426a
> 		      for (p = word;
f9426a
> 			   p > tmp_command
f9426a
> 			     && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
f9426a
> 			   p--)
f9426a
> 			;
f9426a
>
f9426a
> IWBN if a "completer" object described how to do all these three things.
f9426a
> Then the special case code for filename_completer (and location_completer)
f9426a
> in completer.c could disappear.  But maybe that's a patch for another day.
f9426a
f9426a
While I agree with you that the completion mechanism could be better (I
f9426a
myself had a lot of trouble with it), I unfortunately don't have enough
f9426a
time to tackle this problem now.  So yeah, I think it will be a patch
f9426a
for another day...
f9426a
f9426a
> Regarding the hack of using a static local to pass data from
f9426a
> handle_brkchars to handle_completion, I know it's a hacky pragmatic
f9426a
> choice.  To get the reference counting right the code assumes that
f9426a
> if the handle_brkchars phase is done then the handle_completion
f9426a
> phase will be done too, right?
f9426a
f9426a
Yeah.  This is true for the current code (well, except maybe for the
f9426a
case you describe below...).
f9426a
f9426a
> I wonder if a SIGINT could sneak in
f9426a
> there between the two passes (either today or tomorrow).
f9426a
> Maybe the code in cmdpy_completer_helper for handle_brkchars_p could
f9426a
> first check whether resultobj is already non-NULL, and decrement its
f9426a
> reference count before setting it to NULL?
f9426a
f9426a
Yes, done (I think).  Thanks for raising this issue.
f9426a
f9426a
> And cmdpy_completer_helper
f9426a
> could be defined to return a borrowed reference to resultobj?
f9426a
> Dunno, just thinking out loud.
f9426a
f9426a
Done, I guess.
f9426a
f9426a
> Something puzzles me though: If it's ok to cache the completion result from the
f9426a
> handle_brkchars pass to the handle_completion pass, why have two passes?
f9426a
> It feels like there must be a case where this caching of the result
f9426a
> in a static local from one pass to the next won't work.
f9426a
f9426a
I'm not sure I follow.
f9426a
f9426a
It's OK to cache the result because handle_brkchars and
f9426a
handle_completion work on the same set of data.  In fact, we wouldn't
f9426a
need to have two passes here; my previous patch didn't have two passes,
f9426a
and worked fine.  I just implemented it this way because Tom (correctly)
f9426a
raised the point that the completion itself might be time-consuming, and
f9426a
thus we could reuse its result in the two phases.
f9426a
f9426a
> Another question:
f9426a
> I noticed complete_command doesn't do this two-phase dance
f9426a
> of handle_brkchars followed by handle_completions.  Should it?
f9426a
> It just goes straight to handle_completions.
f9426a
f9426a
I don't think it should, because for complete_command (and
f9426a
complete_filename et al) the handle_brkchars is already defined
f9426a
internally by GDB.  See:
f9426a
f9426a
    ...
f9426a
  /* Choose the default set of word break characters to break
f9426a
     completions.  If we later find out that we are doing completions
f9426a
     on command strings (as opposed to strings supplied by the
f9426a
     individual command completer functions, which can be any string)
f9426a
     then we will switch to the special word break set for command
f9426a
     strings, which leaves out the '-' character used in some
f9426a
     commands.  */
f9426a
  rl_completer_word_break_characters =
f9426a
    current_language->la_word_break_characters();
f9426a
    ...
f9426a
f9426a
    /* It is a normal command; what comes after it is
f9426a
       completed by the command's completer function.  */
f9426a
    if (c->completer == filename_completer)
f9426a
      {
f9426a
        /* Many commands which want to complete on
f9426a
           file names accept several file names, as
f9426a
           in "run foo bar >>baz".  So we don't want
f9426a
           to complete the entire text after the
f9426a
           command, just the last word.  To this
f9426a
           end, we need to find the beginning of the
f9426a
           file name by starting at `word' and going
f9426a
           backwards.  */
f9426a
        for (p = word;
f9426a
             p > tmp_command
f9426a
               && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
f9426a
             p--)
f9426a
          ;
f9426a
        rl_completer_word_break_characters =
f9426a
          gdb_completer_file_name_break_characters;
f9426a
      }
f9426a
    else if (c->completer == location_completer)
f9426a
      {
f9426a
        /* Commands which complete on locations want to
f9426a
           see the entire argument.  */
f9426a
        for (p = word;
f9426a
             p > tmp_command
f9426a
               && p[-1] != ' ' && p[-1] != '\t';
f9426a
             p--)
f9426a
          ;
f9426a
      }
f9426a
    if (reason == handle_brkchars
f9426a
        && c->completer_handle_brkchars != NULL)
f9426a
      (*c->completer_handle_brkchars) (c, p, word);
f9426a
    if (reason != handle_brkchars && c->completer != NULL)
f9426a
      list = (*c->completer) (c, p, word);
f9426a
f9426a
f9426a
The complete_command function will only be called by the last "if"
f9426a
clause, when reason != handle_brkchars.  Otherwise,
f9426a
complete_line_internal will just deal with handle_brkchars.  And for
f9426a
complete_command, the right value for rl_completer_word_break_characters
f9426a
is what is attributed at the beginning of the function.
f9426a
f9426a
My patch implements this "two-phase" dance for Python because in this
f9426a
specific case (i.e., a completion method defined in the Python script)
f9426a
there is no way to know the value of handle_brkchars before we actually
f9426a
do the completion itself.
f9426a
f9426a
In fact, my patch could probably be "simplified" and be made to call
f9426a
cmdpy_completer directly (without any cmdpy_completer_handle_brkchars),
f9426a
assuming that this function took care of filling handle_brkchars
f9426a
correctly.  What I mean is: when dealing with the handle_brkchars case,
f9426a
the completer command can do anything it wants.
f9426a
f9426a
> [Maybe that explains the difference from using TAB.  Dunno off hand.]
f9426a
> It seems like complete_command is trying to hand-code its own
f9426a
> handle_brkchars handling:
f9426a
>
f9426a
> static void
f9426a
> complete_command (char *arg, int from_tty)
f9426a
> {
f9426a
>   int argpoint;
f9426a
>   char *point, *arg_prefix;
f9426a
>   VEC (char_ptr) *completions;
f9426a
>
f9426a
>   dont_repeat ();
f9426a
>
f9426a
>   if (arg == NULL)
f9426a
>     arg = "";
f9426a
>   argpoint = strlen (arg);
f9426a
>
f9426a
>   /* complete_line assumes that its first argument is somewhere
f9426a
>      within, and except for filenames at the beginning of, the word to
f9426a
>      be completed.  The following crude imitation of readline's
f9426a
>      word-breaking tries to accomodate this.  */
f9426a
>   point = arg + argpoint;
f9426a
>   while (point > arg)
f9426a
>     {
f9426a
>       if (strchr (rl_completer_word_break_characters, point[-1]) != 0)
f9426a
>         break;
f9426a
>       point--;
f9426a
>     }
f9426a
>
f9426a
>   arg_prefix = alloca (point - arg + 1);
f9426a
>   memcpy (arg_prefix, arg, point - arg);
f9426a
>   arg_prefix[point - arg] = 0;
f9426a
>
f9426a
>   completions = complete_line (point, arg, argpoint);
f9426a
>
f9426a
>   ...
f9426a
> }
f9426a
f9426a
Yes, it seems so, but I did not check further.
f9426a
f9426a
> TAB and the complete command should work identically of course,
f9426a
> but for your testcase, maybe you should test both just to verify
f9426a
> both work reasonably well (even if not identically).
f9426a
> Given that complete_command doesn't do the two phase dance,
f9426a
> does it work with your patch?
f9426a
> Maybe it does, but IWBN to confirm that.
f9426a
f9426a
The 'complete' command does not work as it should with my patch:
f9426a
f9426a
  (gdb) complete completefileinit /hom
f9426a
  completefileinit /home
f9426a
  (gdb) complete completefilemethod /hom
f9426a
  completefilemethod /home
f9426a
f9426a
But then, it also does not work without my patch either:
f9426a
f9426a
  (gdb) complete file /hom
f9426a
  file /home
f9426a
f9426a
So I am not extending the testcase for now, because this bug needs to be
f9426a
fixed first (and it is unrelated to the patch I am proposing).
f9426a
f9426a
WDYT of this version?
f9426a
f9426a
Thanks,
f9426a
f9426a
-- 
f9426a
Sergio
f9426a
GPG key ID: 65FC5E36
f9426a
Please send encrypted e-mail if possible
f9426a
http://blog.sergiodj.net/
f9426a
f9426a
gdb/
f9426a
2014-06-30  Sergio Durigan Junior  <sergiodj@redhat.com>
f9426a
f9426a
	PR python/16699
f9426a
	* cli/cli-decode.c (set_cmd_completer_handle_brkchars): New
f9426a
	function.
f9426a
	(add_cmd): Set "completer_handle_brkchars" to NULL.
f9426a
	* cli/cli-decode.h (struct cmd_list_element)
f9426a
	<completer_handle_brkchars>: New field.
f9426a
	* command.h (completer_ftype_void): New typedef.
f9426a
	(set_cmd_completer_handle_brkchars): New prototype.
f9426a
	* completer.c (set_gdb_completion_word_break_characters): New
f9426a
	function.
f9426a
	(complete_line_internal): Call "completer_handle_brkchars"
f9426a
	callback from command.
f9426a
	* completer.h: Include "command.h".
f9426a
	(set_gdb_completion_word_break_characters): New prototype.
f9426a
	* python/py-cmd.c (cmdpy_completer_helper): New function.
f9426a
	(cmdpy_completer_handle_brkchars): New function.
f9426a
	(cmdpy_completer): Adjust to use cmdpy_completer_helper.
f9426a
	(cmdpy_init): Set completer_handle_brkchars to
f9426a
	cmdpy_completer_handle_brkchars.
f9426a
f9426a
gdb/testsuite/
f9426a
2014-06-30  Sergio Durigan Junior  <sergiodj@redhat.com>
f9426a
f9426a
	PR python/16699
f9426a
	* gdb.python/py-completion.exp: New file.
f9426a
	* gdb.python/py-completion.py: Likewise.
f9426a
f9426a
f9426a
Index: gdb-7.7.90.20140627/gdb/cli/cli-decode.c
f9426a
===================================================================
f9426a
--- gdb-7.7.90.20140627.orig/gdb/cli/cli-decode.c	2014-07-07 20:53:52.635106914 +0200
f9426a
+++ gdb-7.7.90.20140627/gdb/cli/cli-decode.c	2014-07-07 20:53:55.429110207 +0200
f9426a
@@ -164,6 +164,15 @@ set_cmd_completer (struct cmd_list_eleme
f9426a
   cmd->completer = completer; /* Ok.  */
f9426a
 }
f9426a
 
f9426a
+/* See definition in commands.h.  */
f9426a
+
f9426a
+void
f9426a
+set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd,
f9426a
+			       completer_ftype_void *completer_handle_brkchars)
f9426a
+{
f9426a
+  cmd->completer_handle_brkchars = completer_handle_brkchars;
f9426a
+}
f9426a
+
f9426a
 /* Add element named NAME.
f9426a
    Space for NAME and DOC must be allocated by the caller.
f9426a
    CLASS is the top level category into which commands are broken down
f9426a
@@ -239,6 +248,7 @@ add_cmd (const char *name, enum command_
f9426a
   c->prefix = NULL;
f9426a
   c->abbrev_flag = 0;
f9426a
   set_cmd_completer (c, make_symbol_completion_list_fn);
f9426a
+  c->completer_handle_brkchars = NULL;
f9426a
   c->destroyer = NULL;
f9426a
   c->type = not_set_cmd;
f9426a
   c->var = NULL;
f9426a
Index: gdb-7.7.90.20140627/gdb/cli/cli-decode.h
f9426a
===================================================================
f9426a
--- gdb-7.7.90.20140627.orig/gdb/cli/cli-decode.h	2014-07-07 20:53:52.636106915 +0200
f9426a
+++ gdb-7.7.90.20140627/gdb/cli/cli-decode.h	2014-07-07 20:53:55.429110207 +0200
f9426a
@@ -176,6 +176,15 @@ struct cmd_list_element
f9426a
        "baz/foo", return "baz/foobar".  */
f9426a
     completer_ftype *completer;
f9426a
 
f9426a
+    /* Handle the word break characters for this completer.  Usually
f9426a
+       this function need not be defined, but for some types of
f9426a
+       completers (e.g., Python completers declared as methods inside
f9426a
+       a class) the word break chars may need to be redefined
f9426a
+       depending on the completer type (e.g., for filename
f9426a
+       completers).  */
f9426a
+
f9426a
+    completer_ftype_void *completer_handle_brkchars;
f9426a
+
f9426a
     /* Destruction routine for this command.  If non-NULL, this is
f9426a
        called when this command instance is destroyed.  This may be
f9426a
        used to finalize the CONTEXT field, if needed.  */
f9426a
Index: gdb-7.7.90.20140627/gdb/command.h
f9426a
===================================================================
f9426a
--- gdb-7.7.90.20140627.orig/gdb/command.h	2014-07-07 20:53:52.636106915 +0200
f9426a
+++ gdb-7.7.90.20140627/gdb/command.h	2014-07-07 20:53:55.430110208 +0200
f9426a
@@ -158,8 +158,16 @@ extern void set_cmd_sfunc (struct cmd_li
f9426a
 typedef VEC (char_ptr) *completer_ftype (struct cmd_list_element *,
f9426a
 					 const char *, const char *);
f9426a
 
f9426a
+typedef void completer_ftype_void (struct cmd_list_element *,
f9426a
+				   const char *, const char *);
f9426a
+
f9426a
 extern void set_cmd_completer (struct cmd_list_element *, completer_ftype *);
f9426a
 
f9426a
+/* Set the completer_handle_brkchars callback.  */
f9426a
+
f9426a
+extern void set_cmd_completer_handle_brkchars (struct cmd_list_element *,
f9426a
+					       completer_ftype_void *);
f9426a
+
f9426a
 /* HACK: cagney/2002-02-23: Code, mostly in tracepoints.c, grubs
f9426a
    around in cmd objects to test the value of the commands sfunc().  */
f9426a
 extern int cmd_cfunc_eq (struct cmd_list_element *cmd,
f9426a
Index: gdb-7.7.90.20140627/gdb/completer.c
f9426a
===================================================================
f9426a
--- gdb-7.7.90.20140627.orig/gdb/completer.c	2014-07-07 20:53:52.637106916 +0200
f9426a
+++ gdb-7.7.90.20140627/gdb/completer.c	2014-07-07 20:53:55.430110208 +0200
f9426a
@@ -450,6 +450,21 @@ expression_completer (struct cmd_list_el
f9426a
   return location_completer (ignore, p, word);
f9426a
 }
f9426a
 
f9426a
+/* See definition in completer.h.  */
f9426a
+
f9426a
+void
f9426a
+set_gdb_completion_word_break_characters (completer_ftype *fn)
f9426a
+{
f9426a
+  /* So far we are only interested in differentiating filename
f9426a
+     completers from everything else.  */
f9426a
+  if (fn == filename_completer)
f9426a
+    rl_completer_word_break_characters
f9426a
+      = gdb_completer_file_name_break_characters;
f9426a
+  else
f9426a
+    rl_completer_word_break_characters
f9426a
+      = gdb_completer_command_word_break_characters;
f9426a
+}
f9426a
+
f9426a
 /* Here are some useful test cases for completion.  FIXME: These
f9426a
    should be put in the test suite.  They should be tested with both
f9426a
    M-? and TAB.
f9426a
@@ -678,6 +693,9 @@ complete_line_internal (const char *text
f9426a
 			   p--)
f9426a
 			;
f9426a
 		    }
f9426a
+		  if (reason == handle_brkchars
f9426a
+		      && c->completer_handle_brkchars != NULL)
f9426a
+		    (*c->completer_handle_brkchars) (c, p, word);
f9426a
 		  if (reason != handle_brkchars && c->completer != NULL)
f9426a
 		    list = (*c->completer) (c, p, word);
f9426a
 		}
f9426a
@@ -751,6 +769,9 @@ complete_line_internal (const char *text
f9426a
 		       p--)
f9426a
 		    ;
f9426a
 		}
f9426a
+	      if (reason == handle_brkchars
f9426a
+		  && c->completer_handle_brkchars != NULL)
f9426a
+		(*c->completer_handle_brkchars) (c, p, word);
f9426a
 	      if (reason != handle_brkchars && c->completer != NULL)
f9426a
 		list = (*c->completer) (c, p, word);
f9426a
 	    }
f9426a
Index: gdb-7.7.90.20140627/gdb/completer.h
f9426a
===================================================================
f9426a
--- gdb-7.7.90.20140627.orig/gdb/completer.h	2014-07-07 20:53:52.637106916 +0200
f9426a
+++ gdb-7.7.90.20140627/gdb/completer.h	2014-07-07 20:54:13.297131831 +0200
f9426a
@@ -18,6 +18,7 @@
f9426a
 #define COMPLETER_H 1
f9426a
 
f9426a
 #include "gdb_vecs.h"
f9426a
+#include "command.h"
f9426a
 
f9426a
 extern VEC (char_ptr) *complete_line (const char *text,
f9426a
 				      char *line_buffer,
f9426a
@@ -48,6 +49,13 @@ extern char *get_gdb_completer_quote_cha
f9426a
 
f9426a
 extern char *gdb_completion_word_break_characters (void);
f9426a
 
f9426a
+/* Set the word break characters array to the corresponding set of
f9426a
+   chars, based on FN.  This function is useful for cases when the
f9426a
+   completer doesn't know the type of the completion until some
f9426a
+   calculation is done (e.g., for Python functions).  */
f9426a
+
f9426a
+extern void set_gdb_completion_word_break_characters (completer_ftype *fn);
f9426a
+
f9426a
 /* Exported to linespec.c */
f9426a
 
f9426a
 extern const char *skip_quoted_chars (const char *, const char *,
f9426a
Index: gdb-7.7.90.20140627/gdb/python/py-cmd.c
f9426a
===================================================================
f9426a
--- gdb-7.7.90.20140627.orig/gdb/python/py-cmd.c	2014-07-07 20:53:52.637106916 +0200
f9426a
+++ gdb-7.7.90.20140627/gdb/python/py-cmd.c	2014-07-07 20:53:55.431110209 +0200
f9426a
@@ -208,45 +208,155 @@ cmdpy_function (struct cmd_list_element
f9426a
   do_cleanups (cleanup);
f9426a
 }
f9426a
 
f9426a
+/* Helper function for the Python command completers (both "pure"
f9426a
+   completer and brkchar handler).  This function takes COMMAND, TEXT
f9426a
+   and WORD and tries to call the Python method for completion with
f9426a
+   these arguments.  It also takes HANDLE_BRKCHARS_P, an argument to
f9426a
+   identify whether it is being called from the brkchar handler or
f9426a
+   from the "pure" completer.  In the first case, it effectively calls
f9426a
+   the Python method for completion, and records the PyObject in a
f9426a
+   static variable (used as a "cache").  In the second case, it just
f9426a
+   returns that variable, without actually calling the Python method
f9426a
+   again.  This saves us one Python method call.
f9426a
+
f9426a
+   It is important to mention that this function is built on the
f9426a
+   assumption that the calls to cmdpy_completer_handle_brkchars and
f9426a
+   cmdpy_completer will be subsequent with nothing intervening.  This
f9426a
+   is true for our completer mechanism.
f9426a
+
f9426a
+   This function returns the PyObject representing the Python method
f9426a
+   call.  */
f9426a
+
f9426a
+static PyObject *
f9426a
+cmdpy_completer_helper (struct cmd_list_element *command,
f9426a
+			const char *text, const char *word,
f9426a
+			int handle_brkchars_p)
f9426a
+{
f9426a
+  cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
f9426a
+  PyObject *textobj, *wordobj;
f9426a
+  /* This static variable will server as a "cache" for us, in order to
f9426a
+     store the PyObject that results from calling the Python
f9426a
+     function.  */
f9426a
+  static PyObject *resultobj = NULL;
f9426a
+
f9426a
+  if (handle_brkchars_p)
f9426a
+    {
f9426a
+      /* If we were called to handle brkchars, it means this is the
f9426a
+	 first function call of two that will happen in a row.
f9426a
+	 Therefore, we need to call the completer ourselves, and cache
f9426a
+	 the return value in the static variable RESULTOBJ.  Then, in
f9426a
+	 the second call, we can just use the value of RESULTOBJ to do
f9426a
+	 our job.  */
f9426a
+      if (resultobj != NULL)
f9426a
+	Py_DECREF (resultobj);
f9426a
+
f9426a
+      resultobj = NULL;
f9426a
+      if (!obj)
f9426a
+	error (_("Invalid invocation of Python command object."));
f9426a
+      if (!PyObject_HasAttr ((PyObject *) obj, complete_cst))
f9426a
+	{
f9426a
+	  /* If there is no complete method, don't error.  */
f9426a
+	  return NULL;
f9426a
+	}
f9426a
+
f9426a
+      textobj = PyUnicode_Decode (text, strlen (text), host_charset (), NULL);
f9426a
+      if (!textobj)
f9426a
+	error (_("Could not convert argument to Python string."));
f9426a
+      wordobj = PyUnicode_Decode (word, sizeof (word), host_charset (), NULL);
f9426a
+      if (!wordobj)
f9426a
+	{
f9426a
+	  Py_DECREF (textobj);
f9426a
+	  error (_("Could not convert argument to Python string."));
f9426a
+	}
f9426a
+
f9426a
+      resultobj = PyObject_CallMethodObjArgs ((PyObject *) obj, complete_cst,
f9426a
+					      textobj, wordobj, NULL);
f9426a
+      Py_DECREF (textobj);
f9426a
+      Py_DECREF (wordobj);
f9426a
+      if (!resultobj)
f9426a
+	{
f9426a
+	  /* Just swallow errors here.  */
f9426a
+	  PyErr_Clear ();
f9426a
+	}
f9426a
+
f9426a
+      Py_XINCREF (resultobj);
f9426a
+    }
f9426a
+
f9426a
+  return resultobj;
f9426a
+}
f9426a
+
f9426a
+/* Python function called to determine the break characters of a
f9426a
+   certain completer.  We are only interested in knowing if the
f9426a
+   completer registered by the user will return one of the integer
f9426a
+   codes (see COMPLETER_* symbols).  */
f9426a
+
f9426a
+static void
f9426a
+cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
f9426a
+				 const char *text, const char *word)
f9426a
+{
f9426a
+  PyObject *resultobj = NULL;
f9426a
+  struct cleanup *cleanup;
f9426a
+
f9426a
+  cleanup = ensure_python_env (get_current_arch (), current_language);
f9426a
+
f9426a
+  /* Calling our helper to obtain the PyObject of the Python
f9426a
+     function.  */
f9426a
+  resultobj = cmdpy_completer_helper (command, text, word, 1);
f9426a
+
f9426a
+  /* Check if there was an error.  */
f9426a
+  if (resultobj == NULL)
f9426a
+    goto done;
f9426a
+
f9426a
+  if (PyInt_Check (resultobj))
f9426a
+    {
f9426a
+      /* User code may also return one of the completion constants,
f9426a
+	 thus requesting that sort of completion.  We are only
f9426a
+	 interested in this kind of return.  */
f9426a
+      long value;
f9426a
+
f9426a
+      if (!gdb_py_int_as_long (resultobj, &value))
f9426a
+	{
f9426a
+	  /* Ignore.  */
f9426a
+	  PyErr_Clear ();
f9426a
+	}
f9426a
+      else if (value >= 0 && value < (long) N_COMPLETERS)
f9426a
+	{
f9426a
+	  /* This is the core of this function.  Depending on which
f9426a
+	     completer type the Python function returns, we have to
f9426a
+	     adjust the break characters accordingly.  */
f9426a
+	  set_gdb_completion_word_break_characters
f9426a
+	    (completers[value].completer);
f9426a
+	}
f9426a
+    }
f9426a
+
f9426a
+ done:
f9426a
+
f9426a
+  /* We do not call Py_XDECREF here because RESULTOBJ will be used in
f9426a
+     the subsequent call to cmdpy_completer function.  */
f9426a
+  do_cleanups (cleanup);
f9426a
+}
f9426a
+
f9426a
 /* Called by gdb for command completion.  */
f9426a
 
f9426a
 static VEC (char_ptr) *
f9426a
 cmdpy_completer (struct cmd_list_element *command,
f9426a
 		 const char *text, const char *word)
f9426a
 {
f9426a
-  cmdpy_object *obj = (cmdpy_object *) get_cmd_context (command);
f9426a
-  PyObject *textobj, *wordobj, *resultobj = NULL;
f9426a
+  PyObject *resultobj = NULL;
f9426a
   VEC (char_ptr) *result = NULL;
f9426a
   struct cleanup *cleanup;
f9426a
 
f9426a
   cleanup = ensure_python_env (get_current_arch (), current_language);
f9426a
 
f9426a
-  if (! obj)
f9426a
-    error (_("Invalid invocation of Python command object."));
f9426a
-  if (! PyObject_HasAttr ((PyObject *) obj, complete_cst))
f9426a
-    {
f9426a
-      /* If there is no complete method, don't error -- instead, just
f9426a
-	 say that there are no completions.  */
f9426a
-      goto done;
f9426a
-    }
f9426a
-
f9426a
-  textobj = PyUnicode_Decode (text, strlen (text), host_charset (), NULL);
f9426a
-  if (! textobj)
f9426a
-    error (_("Could not convert argument to Python string."));
f9426a
-  wordobj = PyUnicode_Decode (word, strlen (word), host_charset (), NULL);
f9426a
-  if (! wordobj)
f9426a
-    error (_("Could not convert argument to Python string."));
f9426a
-
f9426a
-  resultobj = PyObject_CallMethodObjArgs ((PyObject *) obj, complete_cst,
f9426a
-					  textobj, wordobj, NULL);
f9426a
-  Py_DECREF (textobj);
f9426a
-  Py_DECREF (wordobj);
f9426a
-  if (! resultobj)
f9426a
-    {
f9426a
-      /* Just swallow errors here.  */
f9426a
-      PyErr_Clear ();
f9426a
-      goto done;
f9426a
-    }
f9426a
+  /* Calling our helper to obtain the PyObject of the Python
f9426a
+     function.  */
f9426a
+  resultobj = cmdpy_completer_helper (command, text, word, 0);
f9426a
+
f9426a
+  /* If the result object of calling the Python function is NULL, it
f9426a
+     means that there was an error.  In this case, just give up and
f9426a
+     return NULL.  */
f9426a
+  if (resultobj == NULL)
f9426a
+    goto done;
f9426a
 
f9426a
   result = NULL;
f9426a
   if (PyInt_Check (resultobj))
f9426a
@@ -302,7 +412,6 @@ cmdpy_completer (struct cmd_list_element
f9426a
 
f9426a
  done:
f9426a
 
f9426a
-  Py_XDECREF (resultobj);
f9426a
   do_cleanups (cleanup);
f9426a
 
f9426a
   return result;
f9426a
@@ -548,6 +657,9 @@ cmdpy_init (PyObject *self, PyObject *ar
f9426a
       set_cmd_context (cmd, self);
f9426a
       set_cmd_completer (cmd, ((completetype == -1) ? cmdpy_completer
f9426a
 			       : completers[completetype].completer));
f9426a
+      if (completetype == -1)
f9426a
+	set_cmd_completer_handle_brkchars (cmd,
f9426a
+					   cmdpy_completer_handle_brkchars);
f9426a
     }
f9426a
   if (except.reason < 0)
f9426a
     {
f9426a
Index: gdb-7.7.90.20140627/gdb/testsuite/gdb.python/py-completion.exp
f9426a
===================================================================
f9426a
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
f9426a
+++ gdb-7.7.90.20140627/gdb/testsuite/gdb.python/py-completion.exp	2014-07-07 20:53:55.431110209 +0200
f9426a
@@ -0,0 +1,70 @@
f9426a
+# Copyright (C) 2014 Free Software Foundation, Inc.
f9426a
+
f9426a
+# This program is free software; you can redistribute it and/or modify
f9426a
+# it under the terms of the GNU General Public License as published by
f9426a
+# the Free Software Foundation; either version 3 of the License, or
f9426a
+# (at your option) any later version.
f9426a
+#
f9426a
+# This program is distributed in the hope that it will be useful,
f9426a
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
f9426a
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f9426a
+# GNU General Public License for more details.
f9426a
+#
f9426a
+# You should have received a copy of the GNU General Public License
f9426a
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
f9426a
+
f9426a
+set testfile "py-completion"
f9426a
+
f9426a
+load_lib gdb-python.exp
f9426a
+
f9426a
+gdb_exit
f9426a
+gdb_start
f9426a
+
f9426a
+# Skip all tests if Python scripting is not enabled.
f9426a
+if { [skip_python_tests] } { continue }
f9426a
+
f9426a
+gdb_test_no_output "source ${srcdir}/${subdir}/${testfile}.py"
f9426a
+
f9426a
+# Create a temporary directory
f9426a
+set testdir "${objdir}/${subdir}/py-completion-testdir/"
f9426a
+set testdir_regex [string_to_regexp $testdir]
f9426a
+set testdir_complete "${objdir}/${subdir}/py-completion-test"
f9426a
+file mkdir $testdir
f9426a
+
f9426a
+# This one should always pass.
f9426a
+send_gdb "completefileinit ${testdir_complete}\t"
f9426a
+gdb_test_multiple "" "completefileinit completion" {
f9426a
+    -re "^completefileinit ${testdir_regex}$" {
f9426a
+        pass "completefileinit completion"
f9426a
+    }
f9426a
+}
f9426a
+
f9426a
+# Just discarding whatever we typed.
f9426a
+send_gdb "\n"
f9426a
+gdb_test "print" ".*"
f9426a
+
f9426a
+# This is the problematic one.
f9426a
+send_gdb "completefilemethod ${testdir_complete}\t"
f9426a
+gdb_test_multiple "" "completefilemethod completion" {
f9426a
+    -re "^completefilemethod ${testdir_regex} $" {
f9426a
+        fail "completefilemethod completion (completed filename as wrong command arg)"
f9426a
+    }
f9426a
+    -re "^completefilemethod ${testdir_regex}$" {
f9426a
+        pass "completefilemethod completion"
f9426a
+    }
f9426a
+}
f9426a
+
f9426a
+# Discarding again
f9426a
+send_gdb "\n"
f9426a
+gdb_test "print" ".*"
f9426a
+
f9426a
+# Another problematic
f9426a
+send_gdb "completefilecommandcond ${objdir}/${subdir}/py-completion-t\t"
f9426a
+gdb_test_multiple "" "completefilecommandcond completion" {
f9426a
+    -re "^completefilecommandcond ${testdir}$" {
f9426a
+	fail "completefilecommandcond completion (completed filename instead of command)"
f9426a
+    }
f9426a
+    -re "^completefilecommandcond ${objdir}/${subdir}/py-completion-t$" {
f9426a
+	pass "completefilecommandcond completion"
f9426a
+    }
f9426a
+}
f9426a
Index: gdb-7.7.90.20140627/gdb/testsuite/gdb.python/py-completion.py
f9426a
===================================================================
f9426a
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
f9426a
+++ gdb-7.7.90.20140627/gdb/testsuite/gdb.python/py-completion.py	2014-07-07 20:53:55.431110209 +0200
f9426a
@@ -0,0 +1,58 @@
f9426a
+# Copyright (C) 2014 Free Software Foundation, Inc.
f9426a
+
f9426a
+# This program is free software; you can redistribute it and/or modify
f9426a
+# it under the terms of the GNU General Public License as published by
f9426a
+# the Free Software Foundation; either version 3 of the License, or
f9426a
+# (at your option) any later version.
f9426a
+#
f9426a
+# This program is distributed in the hope that it will be useful,
f9426a
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
f9426a
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f9426a
+# GNU General Public License for more details.
f9426a
+#
f9426a
+# You should have received a copy of the GNU General Public License
f9426a
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
f9426a
+
f9426a
+# This testcase tests PR python/16699
f9426a
+
f9426a
+import gdb
f9426a
+
f9426a
+class CompleteFileInit(gdb.Command):
f9426a
+	def __init__(self):
f9426a
+		gdb.Command.__init__(self,'completefileinit',gdb.COMMAND_USER,gdb.COMPLETE_FILENAME)
f9426a
+
f9426a
+	def invoke(self,argument,from_tty):
f9426a
+		raise gdb.GdbError('not implemented')
f9426a
+
f9426a
+class CompleteFileMethod(gdb.Command):
f9426a
+	def __init__(self):
f9426a
+		gdb.Command.__init__(self,'completefilemethod',gdb.COMMAND_USER)
f9426a
+
f9426a
+	def invoke(self,argument,from_tty):
f9426a
+		raise gdb.GdbError('not implemented')
f9426a
+
f9426a
+	def complete(self,text,word):
f9426a
+		return gdb.COMPLETE_FILENAME
f9426a
+
f9426a
+class CompleteFileCommandCond(gdb.Command):
f9426a
+	def __init__(self):
f9426a
+		gdb.Command.__init__(self,'completefilecommandcond',gdb.COMMAND_USER)
f9426a
+
f9426a
+	def invoke(self,argument,from_tty):
f9426a
+		raise gdb.GdbError('not implemented')
f9426a
+
f9426a
+	def complete(self,text,word):
f9426a
+		# This is a test made to know if the command
f9426a
+		# completion still works fine.  When the user asks to
f9426a
+		# complete something like "completefilecommandcond
f9426a
+		# /path/to/py-completion-t", it should not complete to
f9426a
+		# "/path/to/py-completion-test/", but instead just
f9426a
+		# wait for input.
f9426a
+		if "py-completion-t" in text:
f9426a
+			return gdb.COMPLETE_COMMAND
f9426a
+		else:
f9426a
+			return gdb.COMPLETE_FILENAME
f9426a
+
f9426a
+CompleteFileInit()
f9426a
+CompleteFileMethod()
f9426a
+CompleteFileCommandCond()