b0ec7b3401505472271198729452194a6ed78349
1 /* General utility routines for GDB/Python.
3 Copyright (C) 2008, 2009 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/>. */
22 #include "python-internal.h"
25 /* This is a cleanup function which decrements the refcount on a
32 /* Note that we need the extra braces in this 'if' to avoid a
40 /* Return a new cleanup which will decrement the Python object's
44 make_cleanup_py_decref (PyObject
*py
)
46 return make_cleanup (py_decref
, (void *) py
);
49 /* A cleanup function to restore the thread state. */
52 py_gil_restore (void *p
)
54 PyGILState_STATE
*state
= p
;
55 PyGILState_Release (*state
);
58 /* Return a new cleanup which will restore the Python GIL state. */
61 make_cleanup_py_restore_gil (PyGILState_STATE
*state
)
63 return make_cleanup (py_gil_restore
, state
);
66 /* Converts a Python 8-bit string to a unicode string object. Assumes the
67 8-bit string is in the host charset. If an error occurs during conversion,
68 returns NULL with a python exception set.
70 As an added bonus, the functions accepts a unicode string and returns it
71 right away, so callers don't need to check which kind of string they've
74 If the given object is not one of the mentioned string types, NULL is
75 returned, with the TypeError python exception set. */
77 python_string_to_unicode (PyObject
*obj
)
79 PyObject
*unicode_str
;
81 /* If obj is already a unicode string, just return it.
82 I wish life was always that simple... */
83 if (PyUnicode_Check (obj
))
85 else if (PyString_Check (obj
))
86 unicode_str
= PyUnicode_FromEncodedObject (obj
, host_charset (), NULL
);
89 PyErr_SetString (PyExc_TypeError
,
90 _("Expected a string or unicode object."));
97 /* Returns a newly allocated string with the contents of the given unicode
98 string object converted to CHARSET. If an error occurs during the
99 conversion, NULL will be returned and a python exception will be set.
101 The caller is responsible for xfree'ing the string. */
103 unicode_to_encoded_string (PyObject
*unicode_str
, const char *charset
)
108 /* Translate string to named charset. */
109 string
= PyUnicode_AsEncodedString (unicode_str
, charset
, NULL
);
113 result
= xstrdup (PyString_AsString (string
));
120 /* Returns a newly allocated string with the contents of the given unicode
121 string object converted to the target's charset. If an error occurs during
122 the conversion, NULL will be returned and a python exception will be set.
124 The caller is responsible for xfree'ing the string. */
126 unicode_to_target_string (PyObject
*unicode_str
)
128 return unicode_to_encoded_string (unicode_str
, target_charset ());
131 /* Converts a python string (8-bit or unicode) to a target string in
132 the target's charset. Returns NULL on error, with a python exception set.
134 The caller is responsible for xfree'ing the string. */
136 python_string_to_target_string (PyObject
*obj
)
140 str
= python_string_to_unicode (obj
);
144 return unicode_to_target_string (str
);
147 /* Converts a python string (8-bit or unicode) to a target string in
148 the host's charset. Returns NULL on error, with a python exception set.
150 The caller is responsible for xfree'ing the string. */
152 python_string_to_host_string (PyObject
*obj
)
156 str
= python_string_to_unicode (obj
);
160 return unicode_to_encoded_string (str
, host_charset ());
163 /* Converts a target string of LENGTH bytes in the target's charset to a
164 Python Unicode string. If LENGTH is -1, convert until a null byte is found.
166 Returns NULL on error, with a python exception set. */
168 target_string_to_unicode (const gdb_byte
*str
, int length
)
171 length
= strlen (str
);
173 return PyUnicode_Decode (str
, length
, target_charset (), NULL
);
176 /* Return true if OBJ is a Python string or unicode object, false
180 gdbpy_is_string (PyObject
*obj
)
182 return PyString_Check (obj
) || PyUnicode_Check (obj
);
This page took 0.03224 seconds and 4 git commands to generate.