1 /* General python/gdb code
3 Copyright (C) 2008-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "arch-utils.h"
24 #include "cli/cli-script.h"
26 #include "progspace.h"
30 #include "exceptions.h"
31 #include "event-loop.h"
33 #include "readline/tilde.h"
35 #include "cli/cli-utils.h"
39 /* Declared constants and enum for python stack printing. */
40 static const char python_excp_none
[] = "none";
41 static const char python_excp_full
[] = "full";
42 static const char python_excp_message
[] = "message";
44 /* "set python print-stack" choices. */
45 static const char *const python_excp_enums
[] =
53 /* The exception printing variable. 'full' if we want to print the
54 error message and stack, 'none' if we want to print nothing, and
55 'message' if we only want to print the error message. 'message' is
57 static const char *gdbpy_should_print_stack
= python_excp_message
;
61 #include "libiberty.h"
62 #include "cli/cli-decode.h"
66 #include "python-internal.h"
71 #include "gdbthread.h"
74 #include "event-top.h"
76 /* True if Python has been successfully initialized, false
79 int gdb_python_initialized
;
81 static PyMethodDef GdbMethods
[];
84 static struct PyModuleDef GdbModuleDef
;
88 PyObject
*gdb_python_module
;
90 /* Some string constants we may wish to use. */
91 PyObject
*gdbpy_to_string_cst
;
92 PyObject
*gdbpy_children_cst
;
93 PyObject
*gdbpy_display_hint_cst
;
94 PyObject
*gdbpy_doc_cst
;
95 PyObject
*gdbpy_enabled_cst
;
96 PyObject
*gdbpy_value_cst
;
98 /* The GdbError exception. */
99 PyObject
*gdbpy_gdberror_exc
;
101 /* The `gdb.error' base class. */
102 PyObject
*gdbpy_gdb_error
;
104 /* The `gdb.MemoryError' exception. */
105 PyObject
*gdbpy_gdb_memory_error
;
107 /* Architecture and language to be used in callbacks from
108 the Python interpreter. */
109 struct gdbarch
*python_gdbarch
;
110 const struct language_defn
*python_language
;
112 /* Restore global language and architecture and Python GIL state
113 when leaving the Python interpreter. */
117 PyGILState_STATE state
;
118 struct gdbarch
*gdbarch
;
119 const struct language_defn
*language
;
120 PyObject
*error_type
, *error_value
, *error_traceback
;
124 restore_python_env (void *p
)
126 struct python_env
*env
= (struct python_env
*)p
;
128 /* Leftover Python error is forbidden by Python Exception Handling. */
129 if (PyErr_Occurred ())
131 /* This order is similar to the one calling error afterwards. */
132 gdbpy_print_stack ();
133 warning (_("internal error: Unhandled Python exception"));
136 PyErr_Restore (env
->error_type
, env
->error_value
, env
->error_traceback
);
138 PyGILState_Release (env
->state
);
139 python_gdbarch
= env
->gdbarch
;
140 python_language
= env
->language
;
144 /* Called before entering the Python interpreter to install the
145 current language and architecture to be used for Python values. */
148 ensure_python_env (struct gdbarch
*gdbarch
,
149 const struct language_defn
*language
)
151 struct python_env
*env
= xmalloc (sizeof *env
);
153 /* We should not ever enter Python unless initialized. */
154 if (!gdb_python_initialized
)
155 error (_("Python not initialized"));
157 env
->state
= PyGILState_Ensure ();
158 env
->gdbarch
= python_gdbarch
;
159 env
->language
= python_language
;
161 python_gdbarch
= gdbarch
;
162 python_language
= language
;
164 /* Save it and ensure ! PyErr_Occurred () afterwards. */
165 PyErr_Fetch (&env
->error_type
, &env
->error_value
, &env
->error_traceback
);
167 return make_cleanup (restore_python_env
, env
);
170 /* Clear the quit flag. */
173 clear_quit_flag (void)
175 /* This clears the flag as a side effect. */
176 PyOS_InterruptOccurred ();
179 /* Set the quit flag. */
184 PyErr_SetInterrupt ();
187 /* Return true if the quit flag has been set, false otherwise. */
190 check_quit_flag (void)
192 return PyOS_InterruptOccurred ();
195 /* Evaluate a Python command like PyRun_SimpleString, but uses
196 Py_single_input which prints the result of expressions, and does
197 not automatically print the stack on errors. */
200 eval_python_command (const char *command
)
204 m
= PyImport_AddModule ("__main__");
208 d
= PyModule_GetDict (m
);
211 v
= PyRun_StringFlags (command
, Py_single_input
, d
, d
, NULL
);
224 /* Implementation of the gdb "python-interactive" command. */
227 python_interactive_command (char *arg
, int from_tty
)
229 struct cleanup
*cleanup
;
232 cleanup
= make_cleanup_restore_integer (&interpreter_async
);
233 interpreter_async
= 0;
235 arg
= skip_spaces (arg
);
237 ensure_python_env (get_current_arch (), current_language
);
241 int len
= strlen (arg
);
242 char *script
= xmalloc (len
+ 2);
244 strcpy (script
, arg
);
246 script
[len
+ 1] = '\0';
247 err
= eval_python_command (script
);
252 err
= PyRun_InteractiveLoop (instream
, "<stdin>");
258 gdbpy_print_stack ();
259 error (_("Error while executing Python code."));
262 do_cleanups (cleanup
);
265 /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
268 On Windows hosts few users would build Python themselves (this is no
269 trivial task on this platform), and thus use binaries built by
270 someone else instead. There may happen situation where the Python
271 library and GDB are using two different versions of the C runtime
272 library. Python, being built with VC, would use one version of the
273 msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll.
274 A FILE * from one runtime does not necessarily operate correctly in
277 To work around this potential issue, we create on Windows hosts the
278 FILE object using Python routines, thus making sure that it is
279 compatible with the Python library. */
282 python_run_simple_file (FILE *file
, const char *filename
)
286 PyRun_SimpleFile (file
, filename
);
291 PyObject
*python_file
;
292 struct cleanup
*cleanup
;
294 /* Because we have a string for a filename, and are using Python to
295 open the file, we need to expand any tilde in the path first. */
296 full_path
= tilde_expand (filename
);
297 cleanup
= make_cleanup (xfree
, full_path
);
298 python_file
= PyFile_FromString (full_path
, "r");
301 do_cleanups (cleanup
);
302 gdbpy_print_stack ();
303 error (_("Error while opening file: %s"), full_path
);
306 make_cleanup_py_decref (python_file
);
307 PyRun_SimpleFile (PyFile_AsFile (python_file
), filename
);
308 do_cleanups (cleanup
);
313 /* Given a command_line, return a command string suitable for passing
314 to Python. Lines in the string are separated by newlines. The
315 return value is allocated using xmalloc and the caller is
316 responsible for freeing it. */
319 compute_python_string (struct command_line
*l
)
321 struct command_line
*iter
;
326 for (iter
= l
; iter
; iter
= iter
->next
)
327 size
+= strlen (iter
->line
) + 1;
329 script
= xmalloc (size
+ 1);
331 for (iter
= l
; iter
; iter
= iter
->next
)
333 int len
= strlen (iter
->line
);
335 strcpy (&script
[here
], iter
->line
);
337 script
[here
++] = '\n';
343 /* Take a command line structure representing a 'python' command, and
344 evaluate its body using the Python interpreter. */
347 eval_python_from_control_command (struct command_line
*cmd
)
351 struct cleanup
*cleanup
;
353 if (cmd
->body_count
!= 1)
354 error (_("Invalid \"python\" block structure."));
356 cleanup
= ensure_python_env (get_current_arch (), current_language
);
358 script
= compute_python_string (cmd
->body_list
[0]);
359 ret
= PyRun_SimpleString (script
);
362 error (_("Error while executing Python code."));
364 do_cleanups (cleanup
);
367 /* Implementation of the gdb "python" command. */
370 python_command (char *arg
, int from_tty
)
372 struct cleanup
*cleanup
;
374 cleanup
= ensure_python_env (get_current_arch (), current_language
);
376 make_cleanup_restore_integer (&interpreter_async
);
377 interpreter_async
= 0;
379 arg
= skip_spaces (arg
);
382 if (PyRun_SimpleString (arg
))
383 error (_("Error while executing Python code."));
387 struct command_line
*l
= get_command_line (python_control
, "");
389 make_cleanup_free_command_lines (&l
);
390 execute_control_command_untraced (l
);
393 do_cleanups (cleanup
);
398 /* Transform a gdb parameters's value into a Python value. May return
399 NULL (and set a Python exception) on error. Helper function for
402 gdbpy_parameter_value (enum var_types type
, void *var
)
407 case var_string_noescape
:
408 case var_optional_filename
:
412 char *str
= * (char **) var
;
416 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
427 case var_auto_boolean
:
429 enum auto_boolean ab
= * (enum auto_boolean
*) var
;
431 if (ab
== AUTO_BOOLEAN_TRUE
)
433 else if (ab
== AUTO_BOOLEAN_FALSE
)
440 if ((* (int *) var
) == INT_MAX
)
444 return PyLong_FromLong (* (int *) var
);
448 unsigned int val
= * (unsigned int *) var
;
452 return PyLong_FromUnsignedLong (val
);
456 return PyErr_Format (PyExc_RuntimeError
,
457 _("Programmer error: unhandled type."));
460 /* A Python function which returns a gdb parameter's value as a Python
464 gdbpy_parameter (PyObject
*self
, PyObject
*args
)
466 struct cmd_list_element
*alias
, *prefix
, *cmd
;
470 volatile struct gdb_exception except
;
472 if (! PyArg_ParseTuple (args
, "s", &arg
))
475 newarg
= concat ("show ", arg
, (char *) NULL
);
477 TRY_CATCH (except
, RETURN_MASK_ALL
)
479 found
= lookup_cmd_composition (newarg
, &alias
, &prefix
, &cmd
);
482 GDB_PY_HANDLE_EXCEPTION (except
);
484 return PyErr_Format (PyExc_RuntimeError
,
485 _("Could not find parameter `%s'."), arg
);
488 return PyErr_Format (PyExc_RuntimeError
,
489 _("`%s' is not a parameter."), arg
);
490 return gdbpy_parameter_value (cmd
->var_type
, cmd
->var
);
493 /* Wrapper for target_charset. */
496 gdbpy_target_charset (PyObject
*self
, PyObject
*args
)
498 const char *cset
= target_charset (python_gdbarch
);
500 return PyUnicode_Decode (cset
, strlen (cset
), host_charset (), NULL
);
503 /* Wrapper for target_wide_charset. */
506 gdbpy_target_wide_charset (PyObject
*self
, PyObject
*args
)
508 const char *cset
= target_wide_charset (python_gdbarch
);
510 return PyUnicode_Decode (cset
, strlen (cset
), host_charset (), NULL
);
513 /* A Python function which evaluates a string using the gdb CLI. */
516 execute_gdb_command (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
519 PyObject
*from_tty_obj
= NULL
, *to_string_obj
= NULL
;
520 int from_tty
, to_string
;
521 volatile struct gdb_exception except
;
522 static char *keywords
[] = {"command", "from_tty", "to_string", NULL
};
525 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|O!O!", keywords
, &arg
,
526 &PyBool_Type
, &from_tty_obj
,
527 &PyBool_Type
, &to_string_obj
))
533 int cmp
= PyObject_IsTrue (from_tty_obj
);
542 int cmp
= PyObject_IsTrue (to_string_obj
);
548 TRY_CATCH (except
, RETURN_MASK_ALL
)
550 /* Copy the argument text in case the command modifies it. */
551 char *copy
= xstrdup (arg
);
552 struct cleanup
*cleanup
= make_cleanup (xfree
, copy
);
554 make_cleanup_restore_integer (&interpreter_async
);
555 interpreter_async
= 0;
557 prevent_dont_repeat ();
559 result
= execute_command_to_string (copy
, from_tty
);
563 execute_command (copy
, from_tty
);
566 do_cleanups (cleanup
);
568 GDB_PY_HANDLE_EXCEPTION (except
);
570 /* Do any commands attached to breakpoint we stopped at. */
571 bpstat_do_actions ();
575 PyObject
*r
= PyString_FromString (result
);
582 /* Implementation of gdb.solib_name (Long) -> String.
583 Returns the name of the shared library holding a given address, or None. */
586 gdbpy_solib_name (PyObject
*self
, PyObject
*args
)
592 if (!PyArg_ParseTuple (args
, GDB_PY_LL_ARG
, &pc
))
595 soname
= solib_name_from_address (current_program_space
, pc
);
597 str_obj
= PyString_Decode (soname
, strlen (soname
), host_charset (), NULL
);
607 /* A Python function which is a wrapper for decode_line_1. */
610 gdbpy_decode_line (PyObject
*self
, PyObject
*args
)
612 struct symtabs_and_lines sals
= { NULL
, 0 }; /* Initialize to
614 struct symtab_and_line sal
;
615 const char *arg
= NULL
;
616 char *copy_to_free
= NULL
, *copy
= NULL
;
617 struct cleanup
*cleanups
;
618 PyObject
*result
= NULL
;
619 PyObject
*return_result
= NULL
;
620 PyObject
*unparsed
= NULL
;
621 volatile struct gdb_exception except
;
623 if (! PyArg_ParseTuple (args
, "|s", &arg
))
626 cleanups
= make_cleanup (null_cleanup
, NULL
);
629 TRY_CATCH (except
, RETURN_MASK_ALL
)
633 copy
= xstrdup (arg
);
635 sals
= decode_line_1 (©
, 0, 0, 0);
639 set_default_source_symtab_and_line ();
640 sal
= get_current_source_symtab_and_line ();
646 if (sals
.sals
!= NULL
&& sals
.sals
!= &sal
)
648 make_cleanup (xfree
, copy_to_free
);
649 make_cleanup (xfree
, sals
.sals
);
652 if (except
.reason
< 0)
654 do_cleanups (cleanups
);
655 /* We know this will always throw. */
656 GDB_PY_HANDLE_EXCEPTION (except
);
663 result
= PyTuple_New (sals
.nelts
);
666 for (i
= 0; i
< sals
.nelts
; ++i
)
670 obj
= symtab_and_line_to_sal_object (sals
.sals
[i
]);
677 PyTuple_SetItem (result
, i
, obj
);
686 return_result
= PyTuple_New (2);
693 if (copy
&& strlen (copy
) > 0)
695 unparsed
= PyString_FromString (copy
);
696 if (unparsed
== NULL
)
699 Py_DECREF (return_result
);
700 return_result
= NULL
;
710 PyTuple_SetItem (return_result
, 0, unparsed
);
711 PyTuple_SetItem (return_result
, 1, result
);
714 do_cleanups (cleanups
);
716 return return_result
;
719 /* Parse a string and evaluate it as an expression. */
721 gdbpy_parse_and_eval (PyObject
*self
, PyObject
*args
)
723 const char *expr_str
;
724 struct value
*result
= NULL
;
725 volatile struct gdb_exception except
;
727 if (!PyArg_ParseTuple (args
, "s", &expr_str
))
730 TRY_CATCH (except
, RETURN_MASK_ALL
)
732 result
= parse_and_eval (expr_str
);
734 GDB_PY_HANDLE_EXCEPTION (except
);
736 return value_to_value_object (result
);
739 /* Implementation of gdb.find_pc_line function.
740 Returns the gdb.Symtab_and_line object corresponding to a PC value. */
743 gdbpy_find_pc_line (PyObject
*self
, PyObject
*args
)
745 gdb_py_ulongest pc_llu
;
746 volatile struct gdb_exception except
;
747 PyObject
*result
= NULL
; /* init for gcc -Wall */
749 if (!PyArg_ParseTuple (args
, GDB_PY_LLU_ARG
, &pc_llu
))
752 TRY_CATCH (except
, RETURN_MASK_ALL
)
754 struct symtab_and_line sal
;
757 pc
= (CORE_ADDR
) pc_llu
;
758 sal
= find_pc_line (pc
, 0);
759 result
= symtab_and_line_to_sal_object (sal
);
761 GDB_PY_HANDLE_EXCEPTION (except
);
766 /* Read a file as Python code.
767 FILE is the file to run. FILENAME is name of the file FILE.
768 This does not throw any errors. If an exception occurs python will print
769 the traceback and clear the error indicator. */
772 source_python_script (FILE *file
, const char *filename
)
774 struct cleanup
*cleanup
;
776 cleanup
= ensure_python_env (get_current_arch (), current_language
);
777 python_run_simple_file (file
, filename
);
778 do_cleanups (cleanup
);
783 /* Posting and handling events. */
785 /* A single event. */
788 /* The Python event. This is just a callable object. */
790 /* The next event. */
791 struct gdbpy_event
*next
;
794 /* All pending events. */
795 static struct gdbpy_event
*gdbpy_event_list
;
796 /* The final link of the event list. */
797 static struct gdbpy_event
**gdbpy_event_list_end
;
799 /* We use a file handler, and not an async handler, so that we can
800 wake up the main thread even when it is blocked in poll(). */
801 static struct serial
*gdbpy_event_fds
[2];
803 /* The file handler callback. This reads from the internal pipe, and
804 then processes the Python event queue. This will always be run in
805 the main gdb thread. */
808 gdbpy_run_events (struct serial
*scb
, void *context
)
810 struct cleanup
*cleanup
;
812 cleanup
= ensure_python_env (get_current_arch (), current_language
);
814 /* Flush the fd. Do this before flushing the events list, so that
815 any new event post afterwards is sure to re-awake the event
817 while (serial_readchar (gdbpy_event_fds
[0], 0) >= 0)
820 while (gdbpy_event_list
)
822 PyObject
*call_result
;
824 /* Dispatching the event might push a new element onto the event
825 loop, so we update here "atomically enough". */
826 struct gdbpy_event
*item
= gdbpy_event_list
;
827 gdbpy_event_list
= gdbpy_event_list
->next
;
828 if (gdbpy_event_list
== NULL
)
829 gdbpy_event_list_end
= &gdbpy_event_list
;
832 call_result
= PyObject_CallObject (item
->event
, NULL
);
833 if (call_result
== NULL
)
836 Py_XDECREF (call_result
);
837 Py_DECREF (item
->event
);
841 do_cleanups (cleanup
);
844 /* Submit an event to the gdb thread. */
846 gdbpy_post_event (PyObject
*self
, PyObject
*args
)
848 struct gdbpy_event
*event
;
852 if (!PyArg_ParseTuple (args
, "O", &func
))
855 if (!PyCallable_Check (func
))
857 PyErr_SetString (PyExc_RuntimeError
,
858 _("Posted event is not callable"));
864 /* From here until the end of the function, we have the GIL, so we
865 can operate on our global data structures without worrying. */
866 wakeup
= gdbpy_event_list
== NULL
;
868 event
= XNEW (struct gdbpy_event
);
871 *gdbpy_event_list_end
= event
;
872 gdbpy_event_list_end
= &event
->next
;
874 /* Wake up gdb when needed. */
877 char c
= 'q'; /* Anything. */
879 if (serial_write (gdbpy_event_fds
[1], &c
, 1))
880 return PyErr_SetFromErrno (PyExc_IOError
);
886 /* Initialize the Python event handler. */
888 gdbpy_initialize_events (void)
890 if (serial_pipe (gdbpy_event_fds
) == 0)
892 gdbpy_event_list_end
= &gdbpy_event_list
;
893 serial_async (gdbpy_event_fds
[0], gdbpy_run_events
, NULL
);
902 before_prompt_hook (const char *current_gdb_prompt
)
904 struct cleanup
*cleanup
;
907 if (!gdb_python_initialized
)
910 cleanup
= ensure_python_env (get_current_arch (), current_language
);
912 if (gdb_python_module
913 && PyObject_HasAttrString (gdb_python_module
, "prompt_hook"))
917 hook
= PyObject_GetAttrString (gdb_python_module
, "prompt_hook");
921 make_cleanup_py_decref (hook
);
923 if (PyCallable_Check (hook
))
926 PyObject
*current_prompt
;
928 current_prompt
= PyString_FromString (current_gdb_prompt
);
929 if (current_prompt
== NULL
)
932 result
= PyObject_CallFunctionObjArgs (hook
, current_prompt
, NULL
);
934 Py_DECREF (current_prompt
);
939 make_cleanup_py_decref (result
);
941 /* Return type should be None, or a String. If it is None,
942 fall through, we will not set a prompt. If it is a
943 string, set PROMPT. Anything else, set an exception. */
944 if (result
!= Py_None
&& ! PyString_Check (result
))
946 PyErr_Format (PyExc_RuntimeError
,
947 _("Return from prompt_hook must " \
948 "be either a Python string, or None"));
952 if (result
!= Py_None
)
954 prompt
= python_string_to_host_string (result
);
959 make_cleanup (xfree
, prompt
);
964 /* If a prompt has been set, PROMPT will not be NULL. If it is
965 NULL, do not set the prompt. */
969 do_cleanups (cleanup
);
973 gdbpy_print_stack ();
974 do_cleanups (cleanup
);
982 /* A python function to write a single string using gdb's filtered
983 output stream . The optional keyword STREAM can be used to write
984 to a particular stream. The default stream is to gdb_stdout. */
987 gdbpy_write (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
990 static char *keywords
[] = {"text", "stream", NULL
};
992 volatile struct gdb_exception except
;
994 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "s|i", keywords
, &arg
,
998 TRY_CATCH (except
, RETURN_MASK_ALL
)
1000 switch (stream_type
)
1004 fprintf_filtered (gdb_stderr
, "%s", arg
);
1009 fprintf_filtered (gdb_stdlog
, "%s", arg
);
1013 fprintf_filtered (gdb_stdout
, "%s", arg
);
1016 GDB_PY_HANDLE_EXCEPTION (except
);
1021 /* A python function to flush a gdb stream. The optional keyword
1022 STREAM can be used to flush a particular stream. The default stream
1026 gdbpy_flush (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
1028 static char *keywords
[] = {"stream", NULL
};
1029 int stream_type
= 0;
1031 if (! PyArg_ParseTupleAndKeywords (args
, kw
, "|i", keywords
,
1035 switch (stream_type
)
1039 gdb_flush (gdb_stderr
);
1044 gdb_flush (gdb_stdlog
);
1048 gdb_flush (gdb_stdout
);
1054 /* Print a python exception trace, print just a message, or print
1055 nothing and clear the python exception, depending on
1056 gdbpy_should_print_stack. Only call this if a python exception is
1059 gdbpy_print_stack (void)
1061 volatile struct gdb_exception except
;
1063 /* Print "none", just clear exception. */
1064 if (gdbpy_should_print_stack
== python_excp_none
)
1068 /* Print "full" message and backtrace. */
1069 else if (gdbpy_should_print_stack
== python_excp_full
)
1072 /* PyErr_Print doesn't necessarily end output with a newline.
1073 This works because Python's stdout/stderr is fed through
1075 TRY_CATCH (except
, RETURN_MASK_ALL
)
1080 /* Print "message", just error print message. */
1083 PyObject
*ptype
, *pvalue
, *ptraceback
;
1084 char *msg
= NULL
, *type
= NULL
;
1086 PyErr_Fetch (&ptype
, &pvalue
, &ptraceback
);
1088 /* Fetch the error message contained within ptype, pvalue. */
1089 msg
= gdbpy_exception_to_string (ptype
, pvalue
);
1090 type
= gdbpy_obj_to_string (ptype
);
1092 TRY_CATCH (except
, RETURN_MASK_ALL
)
1096 /* An error occurred computing the string representation of the
1098 fprintf_filtered (gdb_stderr
,
1099 _("Error occurred computing Python error" \
1103 fprintf_filtered (gdb_stderr
, "Python Exception %s %s: \n",
1108 Py_XDECREF (pvalue
);
1109 Py_XDECREF (ptraceback
);
1116 /* Return the current Progspace.
1117 There always is one. */
1120 gdbpy_get_current_progspace (PyObject
*unused1
, PyObject
*unused2
)
1124 result
= pspace_to_pspace_object (current_program_space
);
1130 /* Return a sequence holding all the Progspaces. */
1133 gdbpy_progspaces (PyObject
*unused1
, PyObject
*unused2
)
1135 struct program_space
*ps
;
1138 list
= PyList_New (0);
1144 PyObject
*item
= pspace_to_pspace_object (ps
);
1146 if (!item
|| PyList_Append (list
, item
) == -1)
1158 /* The "current" objfile. This is set when gdb detects that a new
1159 objfile has been loaded. It is only set for the duration of a call to
1160 source_python_script_for_objfile; it is NULL at other times. */
1161 static struct objfile
*gdbpy_current_objfile
;
1163 /* Set the current objfile to OBJFILE and then read FILE named FILENAME
1164 as Python code. This does not throw any errors. If an exception
1165 occurs python will print the traceback and clear the error indicator. */
1168 source_python_script_for_objfile (struct objfile
*objfile
, FILE *file
,
1169 const char *filename
)
1171 struct cleanup
*cleanups
;
1173 if (!gdb_python_initialized
)
1176 cleanups
= ensure_python_env (get_objfile_arch (objfile
), current_language
);
1177 gdbpy_current_objfile
= objfile
;
1179 python_run_simple_file (file
, filename
);
1181 do_cleanups (cleanups
);
1182 gdbpy_current_objfile
= NULL
;
1185 /* Return the current Objfile, or None if there isn't one. */
1188 gdbpy_get_current_objfile (PyObject
*unused1
, PyObject
*unused2
)
1192 if (! gdbpy_current_objfile
)
1195 result
= objfile_to_objfile_object (gdbpy_current_objfile
);
1201 /* Return a sequence holding all the Objfiles. */
1204 gdbpy_objfiles (PyObject
*unused1
, PyObject
*unused2
)
1206 struct objfile
*objf
;
1209 list
= PyList_New (0);
1215 PyObject
*item
= objfile_to_objfile_object (objf
);
1217 if (!item
|| PyList_Append (list
, item
) == -1)
1227 /* Compute the list of active type printers and return it. The result
1228 of this function can be passed to apply_type_printers, and should
1229 be freed by free_type_printers. */
1232 start_type_printers (void)
1234 struct cleanup
*cleanups
;
1235 PyObject
*type_module
, *func
= NULL
, *result_obj
= NULL
;
1237 if (!gdb_python_initialized
)
1240 cleanups
= ensure_python_env (get_current_arch (), current_language
);
1242 type_module
= PyImport_ImportModule ("gdb.types");
1243 if (type_module
== NULL
)
1245 gdbpy_print_stack ();
1249 func
= PyObject_GetAttrString (type_module
, "get_type_recognizers");
1252 gdbpy_print_stack ();
1256 result_obj
= PyObject_CallFunctionObjArgs (func
, (char *) NULL
);
1257 if (result_obj
== NULL
)
1258 gdbpy_print_stack ();
1261 Py_XDECREF (type_module
);
1263 do_cleanups (cleanups
);
1267 /* If TYPE is recognized by some type printer, return a newly
1268 allocated string holding the type's replacement name. The caller
1269 is responsible for freeing the string. Otherwise, return NULL.
1271 This function has a bit of a funny name, since it actually applies
1272 recognizers, but this seemed clearer given the start_type_printers
1273 and free_type_printers functions. */
1276 apply_type_printers (void *printers
, struct type
*type
)
1278 struct cleanup
*cleanups
;
1279 PyObject
*type_obj
, *type_module
= NULL
, *func
= NULL
;
1280 PyObject
*result_obj
= NULL
;
1281 PyObject
*printers_obj
= printers
;
1282 char *result
= NULL
;
1284 if (printers_obj
== NULL
)
1287 if (!gdb_python_initialized
)
1290 cleanups
= ensure_python_env (get_current_arch (), current_language
);
1292 type_obj
= type_to_type_object (type
);
1293 if (type_obj
== NULL
)
1295 gdbpy_print_stack ();
1299 type_module
= PyImport_ImportModule ("gdb.types");
1300 if (type_module
== NULL
)
1302 gdbpy_print_stack ();
1306 func
= PyObject_GetAttrString (type_module
, "apply_type_recognizers");
1309 gdbpy_print_stack ();
1313 result_obj
= PyObject_CallFunctionObjArgs (func
, printers_obj
,
1314 type_obj
, (char *) NULL
);
1315 if (result_obj
== NULL
)
1317 gdbpy_print_stack ();
1321 if (result_obj
!= Py_None
)
1323 result
= python_string_to_host_string (result_obj
);
1325 gdbpy_print_stack ();
1329 Py_XDECREF (type_obj
);
1330 Py_XDECREF (type_module
);
1332 Py_XDECREF (result_obj
);
1333 do_cleanups (cleanups
);
1337 /* Free the result of start_type_printers. */
1340 free_type_printers (void *arg
)
1342 struct cleanup
*cleanups
;
1343 PyObject
*printers
= arg
;
1345 if (printers
== NULL
)
1348 if (!gdb_python_initialized
)
1351 cleanups
= ensure_python_env (get_current_arch (), current_language
);
1352 Py_DECREF (printers
);
1353 do_cleanups (cleanups
);
1356 #else /* HAVE_PYTHON */
1358 /* Dummy implementation of the gdb "python-interactive" and "python"
1362 python_interactive_command (char *arg
, int from_tty
)
1364 arg
= skip_spaces (arg
);
1366 error (_("Python scripting is not supported in this copy of GDB."));
1369 struct command_line
*l
= get_command_line (python_control
, "");
1370 struct cleanup
*cleanups
= make_cleanup_free_command_lines (&l
);
1372 execute_control_command_untraced (l
);
1373 do_cleanups (cleanups
);
1378 python_command (char *arg
, int from_tty
)
1380 python_interactive_command (arg
, from_tty
);
1384 eval_python_from_control_command (struct command_line
*cmd
)
1386 error (_("Python scripting is not supported in this copy of GDB."));
1390 source_python_script (FILE *file
, const char *filename
)
1392 throw_error (UNSUPPORTED_ERROR
,
1393 _("Python scripting is not supported in this copy of GDB."));
1397 gdbpy_should_stop (struct breakpoint_object
*bp_obj
)
1399 internal_error (__FILE__
, __LINE__
,
1400 _("gdbpy_should_stop called when Python scripting is " \
1405 gdbpy_breakpoint_has_py_cond (struct breakpoint_object
*bp_obj
)
1407 internal_error (__FILE__
, __LINE__
,
1408 _("gdbpy_breakpoint_has_py_cond called when Python " \
1409 "scripting is not supported."));
1413 start_type_printers (void)
1419 apply_type_printers (void *ignore
, struct type
*type
)
1425 free_type_printers (void *arg
)
1430 apply_frame_filter (struct frame_info
*frame
, int flags
,
1431 enum py_frame_args args_type
,
1432 struct ui_out
*out
, int frame_low
,
1435 return PY_BT_NO_FILTERS
;
1438 #endif /* HAVE_PYTHON */
1442 /* Lists for 'set python' commands. */
1444 static struct cmd_list_element
*user_set_python_list
;
1445 static struct cmd_list_element
*user_show_python_list
;
1447 /* Function for use by 'set python' prefix command. */
1450 user_set_python (char *args
, int from_tty
)
1452 help_list (user_set_python_list
, "set python ", all_commands
,
1456 /* Function for use by 'show python' prefix command. */
1459 user_show_python (char *args
, int from_tty
)
1461 cmd_show_list (user_show_python_list
, from_tty
, "");
1464 /* Initialize the Python code. */
1468 /* This is installed as a final cleanup and cleans up the
1469 interpreter. This lets Python's 'atexit' work. */
1472 finalize_python (void *ignore
)
1474 /* We don't use ensure_python_env here because if we ever ran the
1475 cleanup, gdb would crash -- because the cleanup calls into the
1476 Python interpreter, which we are about to destroy. It seems
1477 clearer to make the needed calls explicitly here than to create a
1478 cleanup and then mysteriously discard it. */
1479 (void) PyGILState_Ensure ();
1480 python_gdbarch
= target_gdbarch ();
1481 python_language
= current_language
;
1487 /* Provide a prototype to silence -Wmissing-prototypes. */
1488 extern initialize_file_ftype _initialize_python
;
1491 _initialize_python (void)
1496 size_t progsize
, count
;
1498 wchar_t *progname_copy
;
1501 add_com ("python-interactive", class_obscure
,
1502 python_interactive_command
,
1505 Start an interactive Python prompt.\n\
1507 To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\
1510 Alternatively, a single-line Python command can be given as an\n\
1511 argument, and if the command is an expression, the result will be\n\
1512 printed. For example:\n\
1514 (gdb) python-interactive 2 + 3\n\
1517 #else /* HAVE_PYTHON */
1519 Start a Python interactive prompt.\n\
1521 Python scripting is not supported in this copy of GDB.\n\
1522 This command is only a placeholder.")
1523 #endif /* HAVE_PYTHON */
1525 add_com_alias ("pi", "python-interactive", class_obscure
, 1);
1527 add_com ("python", class_obscure
, python_command
,
1530 Evaluate a Python command.\n\
1532 The command can be given as an argument, for instance:\n\
1536 If no argument is given, the following lines are read and used\n\
1537 as the Python commands. Type a line containing \"end\" to indicate\n\
1538 the end of the command.")
1539 #else /* HAVE_PYTHON */
1541 Evaluate a Python command.\n\
1543 Python scripting is not supported in this copy of GDB.\n\
1544 This command is only a placeholder.")
1545 #endif /* HAVE_PYTHON */
1547 add_com_alias ("py", "python", class_obscure
, 1);
1549 /* Add set/show python print-stack. */
1550 add_prefix_cmd ("python", no_class
, user_show_python
,
1551 _("Prefix command for python preference settings."),
1552 &user_show_python_list
, "show python ", 0,
1555 add_prefix_cmd ("python", no_class
, user_set_python
,
1556 _("Prefix command for python preference settings."),
1557 &user_set_python_list
, "set python ", 0,
1560 add_setshow_enum_cmd ("print-stack", no_class
, python_excp_enums
,
1561 &gdbpy_should_print_stack
, _("\
1562 Set mode for Python stack dump on error."), _("\
1563 Show the mode of Python stack printing on error."), _("\
1564 none == no stack or message will be printed.\n\
1565 full == a message and a stack will be printed.\n\
1566 message == an error message without a stack will be printed."),
1568 &user_set_python_list
,
1569 &user_show_python_list
);
1572 #ifdef WITH_PYTHON_PATH
1573 /* Work around problem where python gets confused about where it is,
1574 and then can't find its libraries, etc.
1575 NOTE: Python assumes the following layout:
1577 /foo/lib/pythonX.Y/...
1578 This must be done before calling Py_Initialize. */
1579 progname
= concat (ldirname (python_libdir
), SLASH_STRING
, "bin",
1580 SLASH_STRING
, "python", NULL
);
1582 oldloc
= setlocale (LC_ALL
, NULL
);
1583 setlocale (LC_ALL
, "");
1584 progsize
= strlen (progname
);
1585 if (progsize
== (size_t) -1)
1587 fprintf (stderr
, "Could not convert python path to string\n");
1590 progname_copy
= PyMem_Malloc ((progsize
+ 1) * sizeof (wchar_t));
1593 fprintf (stderr
, "out of memory\n");
1596 count
= mbstowcs (progname_copy
, progname
, progsize
+ 1);
1597 if (count
== (size_t) -1)
1599 fprintf (stderr
, "Could not convert python path to string\n");
1602 setlocale (LC_ALL
, oldloc
);
1604 /* Note that Py_SetProgramName expects the string it is passed to
1605 remain alive for the duration of the program's execution, so
1606 it is not freed after this call. */
1607 Py_SetProgramName (progname_copy
);
1609 Py_SetProgramName (progname
);
1614 PyEval_InitThreads ();
1617 gdb_module
= PyModule_Create (&GdbModuleDef
);
1618 /* Add _gdb module to the list of known built-in modules. */
1619 _PyImport_FixupBuiltin (gdb_module
, "_gdb");
1621 gdb_module
= Py_InitModule ("_gdb", GdbMethods
);
1623 if (gdb_module
== NULL
)
1626 /* The casts to (char*) are for python 2.4. */
1627 if (PyModule_AddStringConstant (gdb_module
, "VERSION", (char*) version
) < 0
1628 || PyModule_AddStringConstant (gdb_module
, "HOST_CONFIG",
1629 (char*) host_name
) < 0
1630 || PyModule_AddStringConstant (gdb_module
, "TARGET_CONFIG",
1631 (char*) target_name
) < 0)
1634 /* Add stream constants. */
1635 if (PyModule_AddIntConstant (gdb_module
, "STDOUT", 0) < 0
1636 || PyModule_AddIntConstant (gdb_module
, "STDERR", 1) < 0
1637 || PyModule_AddIntConstant (gdb_module
, "STDLOG", 2) < 0)
1640 gdbpy_gdb_error
= PyErr_NewException ("gdb.error", PyExc_RuntimeError
, NULL
);
1641 if (gdbpy_gdb_error
== NULL
1642 || PyModule_AddObject (gdb_module
, "error", gdbpy_gdb_error
) < 0)
1645 gdbpy_gdb_memory_error
= PyErr_NewException ("gdb.MemoryError",
1646 gdbpy_gdb_error
, NULL
);
1647 if (gdbpy_gdb_memory_error
== NULL
1648 || PyModule_AddObject (gdb_module
, "MemoryError",
1649 gdbpy_gdb_memory_error
) < 0)
1652 gdbpy_gdberror_exc
= PyErr_NewException ("gdb.GdbError", NULL
, NULL
);
1653 if (gdbpy_gdberror_exc
== NULL
1654 || PyModule_AddObject (gdb_module
, "GdbError", gdbpy_gdberror_exc
) < 0)
1657 gdbpy_initialize_gdb_readline ();
1659 if (gdbpy_initialize_auto_load () < 0
1660 || gdbpy_initialize_values () < 0
1661 || gdbpy_initialize_frames () < 0
1662 || gdbpy_initialize_commands () < 0
1663 || gdbpy_initialize_symbols () < 0
1664 || gdbpy_initialize_symtabs () < 0
1665 || gdbpy_initialize_blocks () < 0
1666 || gdbpy_initialize_functions () < 0
1667 || gdbpy_initialize_parameters () < 0
1668 || gdbpy_initialize_types () < 0
1669 || gdbpy_initialize_pspace () < 0
1670 || gdbpy_initialize_objfile () < 0
1671 || gdbpy_initialize_breakpoints () < 0
1672 || gdbpy_initialize_finishbreakpoints () < 0
1673 || gdbpy_initialize_lazy_string () < 0
1674 || gdbpy_initialize_thread () < 0
1675 || gdbpy_initialize_inferior () < 0
1676 || gdbpy_initialize_events () < 0
1677 || gdbpy_initialize_eventregistry () < 0
1678 || gdbpy_initialize_py_events () < 0
1679 || gdbpy_initialize_event () < 0
1680 || gdbpy_initialize_stop_event () < 0
1681 || gdbpy_initialize_signal_event () < 0
1682 || gdbpy_initialize_breakpoint_event () < 0
1683 || gdbpy_initialize_continue_event () < 0
1684 || gdbpy_initialize_exited_event () < 0
1685 || gdbpy_initialize_thread_event () < 0
1686 || gdbpy_initialize_new_objfile_event () < 0
1687 || gdbpy_initialize_arch () < 0)
1690 observer_attach_before_prompt (before_prompt_hook
);
1692 gdbpy_to_string_cst
= PyString_FromString ("to_string");
1693 if (gdbpy_to_string_cst
== NULL
)
1695 gdbpy_children_cst
= PyString_FromString ("children");
1696 if (gdbpy_children_cst
== NULL
)
1698 gdbpy_display_hint_cst
= PyString_FromString ("display_hint");
1699 if (gdbpy_display_hint_cst
== NULL
)
1701 gdbpy_doc_cst
= PyString_FromString ("__doc__");
1702 if (gdbpy_doc_cst
== NULL
)
1704 gdbpy_enabled_cst
= PyString_FromString ("enabled");
1705 if (gdbpy_enabled_cst
== NULL
)
1707 gdbpy_value_cst
= PyString_FromString ("value");
1708 if (gdbpy_value_cst
== NULL
)
1711 /* Release the GIL while gdb runs. */
1712 PyThreadState_Swap (NULL
);
1713 PyEval_ReleaseLock ();
1715 make_final_cleanup (finalize_python
, NULL
);
1717 gdb_python_initialized
= 1;
1721 gdbpy_print_stack ();
1722 /* Do not set 'gdb_python_initialized'. */
1725 #endif /* HAVE_PYTHON */
1730 /* Perform the remaining python initializations.
1731 These must be done after GDB is at least mostly initialized.
1732 E.g., The "info pretty-printer" command needs the "info" prefix
1733 command installed. */
1736 finish_python_initialization (void)
1739 char *gdb_pythondir
;
1741 struct cleanup
*cleanup
;
1743 cleanup
= ensure_python_env (get_current_arch (), current_language
);
1745 /* Add the initial data-directory to sys.path. */
1747 gdb_pythondir
= concat (gdb_datadir
, SLASH_STRING
, "python", NULL
);
1748 make_cleanup (xfree
, gdb_pythondir
);
1750 sys_path
= PySys_GetObject ("path");
1752 /* If sys.path is not defined yet, define it first. */
1753 if (!(sys_path
&& PyList_Check (sys_path
)))
1756 PySys_SetPath (L
"");
1760 sys_path
= PySys_GetObject ("path");
1762 if (sys_path
&& PyList_Check (sys_path
))
1764 PyObject
*pythondir
;
1767 pythondir
= PyString_FromString (gdb_pythondir
);
1768 if (pythondir
== NULL
)
1771 err
= PyList_Insert (sys_path
, 0, pythondir
);
1775 Py_DECREF (pythondir
);
1780 /* Import the gdb module to finish the initialization, and
1781 add it to __main__ for convenience. */
1782 m
= PyImport_AddModule ("__main__");
1786 gdb_python_module
= PyImport_ImportModule ("gdb");
1787 if (gdb_python_module
== NULL
)
1789 gdbpy_print_stack ();
1790 /* This is passed in one call to warning so that blank lines aren't
1791 inserted between each line of text. */
1793 "Could not load the Python gdb module from `%s'.\n"
1794 "Limited Python support is available from the _gdb module.\n"
1795 "Suggest passing --data-directory=/path/to/gdb/data-directory.\n"),
1797 do_cleanups (cleanup
);
1801 if (PyModule_AddObject (m
, "gdb", gdb_python_module
))
1804 /* Keep the reference to gdb_python_module since it is in a global
1807 do_cleanups (cleanup
);
1811 gdbpy_print_stack ();
1812 warning (_("internal error: Unhandled Python exception"));
1813 do_cleanups (cleanup
);
1816 #endif /* HAVE_PYTHON */
1822 static PyMethodDef GdbMethods
[] =
1824 { "history", gdbpy_history
, METH_VARARGS
,
1825 "Get a value from history" },
1826 { "execute", (PyCFunction
) execute_gdb_command
, METH_VARARGS
| METH_KEYWORDS
,
1827 "Execute a gdb command" },
1828 { "parameter", gdbpy_parameter
, METH_VARARGS
,
1829 "Return a gdb parameter's value" },
1831 { "breakpoints", gdbpy_breakpoints
, METH_NOARGS
,
1832 "Return a tuple of all breakpoint objects" },
1834 { "default_visualizer", gdbpy_default_visualizer
, METH_VARARGS
,
1835 "Find the default visualizer for a Value." },
1837 { "current_progspace", gdbpy_get_current_progspace
, METH_NOARGS
,
1838 "Return the current Progspace." },
1839 { "progspaces", gdbpy_progspaces
, METH_NOARGS
,
1840 "Return a sequence of all progspaces." },
1842 { "current_objfile", gdbpy_get_current_objfile
, METH_NOARGS
,
1843 "Return the current Objfile being loaded, or None." },
1844 { "objfiles", gdbpy_objfiles
, METH_NOARGS
,
1845 "Return a sequence of all loaded objfiles." },
1847 { "newest_frame", gdbpy_newest_frame
, METH_NOARGS
,
1848 "newest_frame () -> gdb.Frame.\n\
1849 Return the newest frame object." },
1850 { "selected_frame", gdbpy_selected_frame
, METH_NOARGS
,
1851 "selected_frame () -> gdb.Frame.\n\
1852 Return the selected frame object." },
1853 { "frame_stop_reason_string", gdbpy_frame_stop_reason_string
, METH_VARARGS
,
1854 "stop_reason_string (Integer) -> String.\n\
1855 Return a string explaining unwind stop reason." },
1857 { "lookup_type", (PyCFunction
) gdbpy_lookup_type
,
1858 METH_VARARGS
| METH_KEYWORDS
,
1859 "lookup_type (name [, block]) -> type\n\
1860 Return a Type corresponding to the given name." },
1861 { "lookup_symbol", (PyCFunction
) gdbpy_lookup_symbol
,
1862 METH_VARARGS
| METH_KEYWORDS
,
1863 "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\
1864 Return a tuple with the symbol corresponding to the given name (or None) and\n\
1865 a boolean indicating if name is a field of the current implied argument\n\
1866 `this' (when the current language is object-oriented)." },
1867 { "lookup_global_symbol", (PyCFunction
) gdbpy_lookup_global_symbol
,
1868 METH_VARARGS
| METH_KEYWORDS
,
1869 "lookup_global_symbol (name [, domain]) -> symbol\n\
1870 Return the symbol corresponding to the given name (or None)." },
1871 { "block_for_pc", gdbpy_block_for_pc
, METH_VARARGS
,
1872 "Return the block containing the given pc value, or None." },
1873 { "solib_name", gdbpy_solib_name
, METH_VARARGS
,
1874 "solib_name (Long) -> String.\n\
1875 Return the name of the shared library holding a given address, or None." },
1876 { "decode_line", gdbpy_decode_line
, METH_VARARGS
,
1877 "decode_line (String) -> Tuple. Decode a string argument the way\n\
1878 that 'break' or 'edit' does. Return a tuple containing two elements.\n\
1879 The first element contains any unparsed portion of the String parameter\n\
1880 (or None if the string was fully parsed). The second element contains\n\
1881 a tuple that contains all the locations that match, represented as\n\
1882 gdb.Symtab_and_line objects (or None)."},
1883 { "parse_and_eval", gdbpy_parse_and_eval
, METH_VARARGS
,
1884 "parse_and_eval (String) -> Value.\n\
1885 Parse String as an expression, evaluate it, and return the result as a Value."
1887 { "find_pc_line", gdbpy_find_pc_line
, METH_VARARGS
,
1888 "find_pc_line (pc) -> Symtab_and_line.\n\
1889 Return the gdb.Symtab_and_line object corresponding to the pc value." },
1891 { "post_event", gdbpy_post_event
, METH_VARARGS
,
1892 "Post an event into gdb's event loop." },
1894 { "target_charset", gdbpy_target_charset
, METH_NOARGS
,
1895 "target_charset () -> string.\n\
1896 Return the name of the current target charset." },
1897 { "target_wide_charset", gdbpy_target_wide_charset
, METH_NOARGS
,
1898 "target_wide_charset () -> string.\n\
1899 Return the name of the current target wide charset." },
1901 { "string_to_argv", gdbpy_string_to_argv
, METH_VARARGS
,
1902 "string_to_argv (String) -> Array.\n\
1903 Parse String and return an argv-like array.\n\
1904 Arguments are separate by spaces and may be quoted."
1906 { "write", (PyCFunction
)gdbpy_write
, METH_VARARGS
| METH_KEYWORDS
,
1907 "Write a string using gdb's filtered stream." },
1908 { "flush", (PyCFunction
)gdbpy_flush
, METH_VARARGS
| METH_KEYWORDS
,
1909 "Flush gdb's filtered stdout stream." },
1910 { "selected_thread", gdbpy_selected_thread
, METH_NOARGS
,
1911 "selected_thread () -> gdb.InferiorThread.\n\
1912 Return the selected thread object." },
1913 { "selected_inferior", gdbpy_selected_inferior
, METH_NOARGS
,
1914 "selected_inferior () -> gdb.Inferior.\n\
1915 Return the selected inferior object." },
1916 { "inferiors", gdbpy_inferiors
, METH_NOARGS
,
1917 "inferiors () -> (gdb.Inferior, ...).\n\
1918 Return a tuple containing all inferiors." },
1919 {NULL
, NULL
, 0, NULL
}
1923 static struct PyModuleDef GdbModuleDef
=
1925 PyModuleDef_HEAD_INIT
,
1936 #endif /* HAVE_PYTHON */