(clear_quit_flag, check_quit_flag, set_quit_flag): Declare.
	(QUIT): Use new functions.
	* event-top.c (command_handler): Use clear_quit_flag.
	(handle_sigint): Use set_quit_flag.
	(async_request_quit): Use check_quit_flag.  Don't check
	immediate_quit.
	* exceptions.c (throw_exception): Use clear_quit_flag.
	* main.c (captured_main): Use clear_quit_flag.
	* python/python.c (clear_quit_flag, set_quit_flag)
	(check_quit_flag): New functions.
	* remote-sim.c (gdb_os_poll_quit): Use check_quit_flag,
	clear_quit_flag.
	* remote.c (remote_wait_as): Use check_quit_flag,
	clear_quit_flag.
	(remote_start_remote): Call QUIT.
	* symfile.c (load_progress): Use check_quit_flag.
	* top.c (command_loop): Use clear_quit_flag.
	(command_line_input): Call QUIT.
	* utils.c (quit_flag): Conditionally define.
	(clear_quit_flag, check_quit_flag, set_quit_flag): New
	functions.
	(prompt_for_continue): Call QUIT.  Use quit, not
	async_request_quit.
	* remote-mips.c (mips_expect_timeout): Call QUIT.
	* monitor.c (monitor_expect): Call QUIT.
+2012-08-22  Tom Tromey  <tromey@redhat.com>
+
+       * defs.h (quit_flag): Don't declare.
+       (clear_quit_flag, check_quit_flag, set_quit_flag): Declare.
+       (QUIT): Use new functions.
+       * event-top.c (command_handler): Use clear_quit_flag.
+       (handle_sigint): Use set_quit_flag.
+       (async_request_quit): Use check_quit_flag.  Don't check
+       immediate_quit.
+       * exceptions.c (throw_exception): Use clear_quit_flag.
+       * main.c (captured_main): Use clear_quit_flag.
+       * python/python.c (clear_quit_flag, set_quit_flag)
+       (check_quit_flag): New functions.
+       * remote-sim.c (gdb_os_poll_quit): Use check_quit_flag,
+       clear_quit_flag.
+       * remote.c (remote_wait_as): Use check_quit_flag,
+       clear_quit_flag.
+       (remote_start_remote): Call QUIT.
+       * symfile.c (load_progress): Use check_quit_flag.
+       * top.c (command_loop): Use clear_quit_flag.
+       (command_line_input): Call QUIT.
+       * utils.c (quit_flag): Conditionally define.
+       (clear_quit_flag, check_quit_flag, set_quit_flag): New
+       functions.
+       (prompt_for_continue): Call QUIT.  Use quit, not
+       async_request_quit.
+       * remote-mips.c (mips_expect_timeout): Call QUIT.
+       * monitor.c (monitor_expect): Call QUIT.
+
 2012-08-22  Tom Tromey  <tromey@redhat.com>
 
        * event-top.c (sigwinch_token, handle_sigwinch): Remove.
 
 /* Search path for separate debug files.  */
 extern char *debug_file_directory;
 
-extern int quit_flag;
+/* GDB has two methods for handling SIGINT.  When immediate_quit is
+   nonzero, a SIGINT results in an immediate longjmp out of the signal
+   handler.  Otherwise, SIGINT simply sets a flag; code that might
+   take a long time, and which ought to be interruptible, checks this
+   flag using the QUIT macro.
+   
+   If GDB is built with Python support, it uses Python's low-level
+   interface to implement the flag.  This approach makes it possible
+   for Python and GDB SIGINT handling to coexist seamlessly.
+
+   If GDB is built without Python, it instead uses its traditional
+   variables.  */
+
+/* Clear the quit flag.  */
+extern void clear_quit_flag (void);
+/* Evaluate to non-zero if the quit flag is set, zero otherwise.  This
+   will clear the quit flag as a side effect.  */
+extern int check_quit_flag (void);
+/* Set the quit flag.  */
+extern void set_quit_flag (void);
+
 extern int immediate_quit;
 
 extern void quit (void);
    needed.  */
 
 #define QUIT { \
-  if (quit_flag) quit (); \
+  if (check_quit_flag ()) quit (); \
   if (deprecated_interactive_hook) deprecated_interactive_hook (); \
 }
 
 
   int stdin_is_tty = ISATTY (stdin);
   struct cleanup *stat_chain;
 
-  quit_flag = 0;
+  clear_quit_flag ();
   if (instream == stdin && stdin_is_tty)
     reinitialize_more_filter ();
 
      set quit_flag to 1 here.  Then if QUIT is called before we get to
      the event loop, we will unwind as expected.  */
 
-  quit_flag = 1;
+  set_quit_flag ();
 
   /* If immediate_quit is set, we go ahead and process the SIGINT right
      away, even if we usually would defer this to the event loop.  The
   /* If the quit_flag has gotten reset back to 0 by the time we get
      back here, that means that an exception was thrown to unwind the
      current command before we got back to the event loop.  So there
-     is no reason to call quit again here, unless immediate_quit is
-     set.  */
+     is no reason to call quit again here.  */
 
-  if (quit_flag || immediate_quit)
+  if (check_quit_flag ())
     quit ();
 }
 
 
 void
 throw_exception (struct gdb_exception exception)
 {
-  quit_flag = 0;
+  clear_quit_flag ();
   immediate_quit = 0;
 
   do_cleanups (all_cleanups ());
 
   dirarg = (char **) xmalloc (dirsize * sizeof (*dirarg));
   ndir = 0;
 
-  quit_flag = 0;
+  clear_quit_flag ();
   saved_command_line = (char *) xmalloc (saved_command_line_size);
   saved_command_line[0] = '\0';
   instream = stdin;
 
     }
 
   immediate_quit++;
+  QUIT;
   while (1)
     {
       if (buf)
 
   return make_cleanup (restore_python_env, env);
 }
 
+/* Clear the quit flag.  */
+
+void
+clear_quit_flag (void)
+{
+  /* This clears the flag as a side effect.  */
+  PyOS_InterruptOccurred ();
+}
+
+/* Set the quit flag.  */
+
+void
+set_quit_flag (void)
+{
+  PyErr_SetInterrupt ();
+}
+
+/* Return true if the quit flag has been set, false otherwise.  */
+
+int
+check_quit_flag (void)
+{
+  return PyOS_InterruptOccurred ();
+}
+
 /* A wrapper around PyRun_SimpleFile.  FILE is the Python script to run
    named FILENAME.
 
 
     }
 
   immediate_quit++;
+  QUIT;
   while (1)
     {
       int c;
 
   if (deprecated_ui_loop_hook != NULL)
     deprecated_ui_loop_hook (0);
 
-  if (quit_flag)               /* gdb's idea of quit */
+  if (check_quit_flag ())      /* gdb's idea of quit */
     {
-      quit_flag = 0;           /* we've stolen it */
+      clear_quit_flag ();      /* we've stolen it */
       return 1;
     }
   return 0;
 
   char *wait_status = NULL;
 
   immediate_quit++;            /* Allow user to interrupt it.  */
+  QUIT;
 
   if (interrupt_on_connect)
     send_interrupt_sequence ();
          ofunc = signal (SIGINT, remote_interrupt);
          /* If the user hit C-c before this packet, or between packets,
             pretend that it was hit right here.  */
-         if (quit_flag)
+         if (check_quit_flag ())
            {
-             quit_flag = 0;
+             clear_quit_flag ();
              remote_interrupt (SIGINT);
            }
        }
 
   args->buffer += bytes;
   totals->write_count += 1;
   args->section_sent += bytes;
-  if (quit_flag
+  if (check_quit_flag ()
       || (deprecated_ui_load_progress_hook != NULL
          && deprecated_ui_load_progress_hook (args->section_name,
                                               args->section_sent)))
 
       if (window_hook && instream == stdin)
        (*window_hook) (instream, get_prompt ());
 
-      quit_flag = 0;
+      clear_quit_flag ();
       if (instream == stdin && stdin_is_tty)
        reinitialize_more_filter ();
       old_chain = make_cleanup (null_cleanup, 0);
   /* Control-C quits instantly if typed while in this loop
      since it should not wait until the user types a newline.  */
   immediate_quit++;
+  QUIT;
 #ifdef STOP_SIGNAL
   if (job_control)
     signal (STOP_SIGNAL, handle_stop_sig);
 
 
 int job_control;
 
+#ifndef HAVE_PYTHON
 /* Nonzero means a quit has been requested.  */
 
 int quit_flag;
+#endif /* HAVE_PYTHON */
 
 /* Nonzero means quit immediately if Control-C is typed now, rather
    than waiting until QUIT is executed.  Be careful in setting this;
 
 int immediate_quit;
 
+#ifndef HAVE_PYTHON
+
+/* Clear the quit flag.  */
+
+void
+clear_quit_flag (void)
+{
+  quit_flag = 0;
+}
+
+/* Set the quit flag.  */
+
+void
+set_quit_flag (void)
+{
+  quit_flag = 1;
+}
+
+/* Return true if the quit flag has been set, false otherwise.  */
+
+int
+check_quit_flag (void)
+{
+  /* This is written in a particular way to avoid races.  */
+  if (quit_flag)
+    {
+      quit_flag = 0;
+      return 1;
+    }
+
+  return 0;
+}
+
+#endif /* HAVE_PYTHON */
+
 /* Nonzero means that strings with character values >0x7F should be printed
    as octal escapes.  Zero means just print the value (e.g. it's an
    international character, and the terminal or window can cope.)  */
   reinitialize_more_filter ();
 
   immediate_quit++;
+  QUIT;
   /* On a real operating system, the user can quit with SIGINT.
      But not on GO32.
 
       while (*p == ' ' || *p == '\t')
        ++p;
       if (p[0] == 'q')
-       async_request_quit (0);
+       quit ();
       xfree (ignore);
     }
   immediate_quit--;