+/* Clear the quit flag. */
+
+static void
+gdbpy_clear_quit_flag (const struct extension_language_defn *extlang)
+{
+ /* This clears the flag as a side effect. */
+ PyOS_InterruptOccurred ();
+}
+
+/* Set the quit flag. */
+
+static void
+gdbpy_set_quit_flag (const struct extension_language_defn *extlang)
+{
+ PyErr_SetInterrupt ();
+}
+
+/* Return true if the quit flag has been set, false otherwise. */
+
+static int
+gdbpy_check_quit_flag (const struct extension_language_defn *extlang)
+{
+ return PyOS_InterruptOccurred ();
+}
+
+/* Evaluate a Python command like PyRun_SimpleString, but uses
+ Py_single_input which prints the result of expressions, and does
+ not automatically print the stack on errors. */
+
+static int
+eval_python_command (const char *command)
+{
+ PyObject *m, *d, *v;
+
+ m = PyImport_AddModule ("__main__");
+ if (m == NULL)
+ return -1;
+
+ d = PyModule_GetDict (m);
+ if (d == NULL)
+ return -1;
+ v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
+ if (v == NULL)
+ return -1;
+
+ Py_DECREF (v);
+#ifndef IS_PY3K
+ if (Py_FlushLine ())
+ PyErr_Clear ();
+#endif
+
+ return 0;
+}
+
+/* Implementation of the gdb "python-interactive" command. */
+
+static void
+python_interactive_command (char *arg, int from_tty)
+{
+ struct cleanup *cleanup;
+ int err;
+
+ cleanup = make_cleanup_restore_integer (&interpreter_async);
+ interpreter_async = 0;
+
+ arg = skip_spaces (arg);
+
+ ensure_python_env (get_current_arch (), current_language);
+
+ if (arg && *arg)
+ {
+ int len = strlen (arg);
+ char *script = xmalloc (len + 2);
+
+ strcpy (script, arg);
+ script[len] = '\n';
+ script[len + 1] = '\0';
+ err = eval_python_command (script);
+ xfree (script);
+ }
+ else
+ {
+ err = PyRun_InteractiveLoop (instream, "<stdin>");
+ dont_repeat ();
+ }
+
+ if (err)
+ {
+ gdbpy_print_stack ();
+ error (_("Error while executing Python code."));
+ }
+
+ do_cleanups (cleanup);
+}
+
+/* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
+ named FILENAME.
+
+ On Windows hosts few users would build Python themselves (this is no
+ trivial task on this platform), and thus use binaries built by
+ someone else instead. There may happen situation where the Python
+ library and GDB are using two different versions of the C runtime
+ library. Python, being built with VC, would use one version of the
+ msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll.
+ A FILE * from one runtime does not necessarily operate correctly in
+ the other runtime.
+
+ To work around this potential issue, we create on Windows hosts the
+ FILE object using Python routines, thus making sure that it is
+ compatible with the Python library. */
+
+static void
+python_run_simple_file (FILE *file, const char *filename)
+{
+#ifndef _WIN32
+
+ PyRun_SimpleFile (file, filename);
+
+#else /* _WIN32 */
+
+ char *full_path;
+ PyObject *python_file;
+ struct cleanup *cleanup;
+
+ /* Because we have a string for a filename, and are using Python to
+ open the file, we need to expand any tilde in the path first. */
+ full_path = tilde_expand (filename);
+ cleanup = make_cleanup (xfree, full_path);
+ python_file = PyFile_FromString (full_path, "r");
+ if (! python_file)
+ {
+ do_cleanups (cleanup);
+ gdbpy_print_stack ();
+ error (_("Error while opening file: %s"), full_path);
+ }
+
+ make_cleanup_py_decref (python_file);
+ PyRun_SimpleFile (PyFile_AsFile (python_file), filename);
+ do_cleanups (cleanup);
+
+#endif /* _WIN32 */
+}