* remote-utils.c (putpkt_binary_1): Call readchar instead of read.
[deliverable/binutils-gdb.git] / gdb / python / py-utils.c
CommitLineData
d57a3c85
TJB
1/* General utility routines for GDB/Python.
2
4c38e0a4 3 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
d57a3c85
TJB
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "charset.h"
22#include "python-internal.h"
23
24
25/* This is a cleanup function which decrements the refcount on a
26 Python object. */
27
28static void
29py_decref (void *p)
30{
31 PyObject *py = p;
32 /* Note that we need the extra braces in this 'if' to avoid a
33 warning from gcc. */
34 if (py)
35 {
36 Py_DECREF (py);
37 }
38}
39
40/* Return a new cleanup which will decrement the Python object's
41 refcount when run. */
42
43struct cleanup *
44make_cleanup_py_decref (PyObject *py)
45{
46 return make_cleanup (py_decref, (void *) py);
47}
48
49/* Converts a Python 8-bit string to a unicode string object. Assumes the
50 8-bit string is in the host charset. If an error occurs during conversion,
51 returns NULL with a python exception set.
52
53 As an added bonus, the functions accepts a unicode string and returns it
54 right away, so callers don't need to check which kind of string they've
55 got.
56
57 If the given object is not one of the mentioned string types, NULL is
58 returned, with the TypeError python exception set. */
59PyObject *
60python_string_to_unicode (PyObject *obj)
61{
62 PyObject *unicode_str;
63
64 /* If obj is already a unicode string, just return it.
65 I wish life was always that simple... */
66 if (PyUnicode_Check (obj))
83390453
PM
67 {
68 unicode_str = obj;
69 Py_INCREF (obj);
70 }
71
d57a3c85
TJB
72 else if (PyString_Check (obj))
73 unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL);
74 else
75 {
76 PyErr_SetString (PyExc_TypeError,
77 _("Expected a string or unicode object."));
78 unicode_str = NULL;
79 }
80
81 return unicode_str;
82}
83
84/* Returns a newly allocated string with the contents of the given unicode
08c637de
TJB
85 string object converted to CHARSET. If an error occurs during the
86 conversion, NULL will be returned and a python exception will be set.
d57a3c85
TJB
87
88 The caller is responsible for xfree'ing the string. */
08c637de
TJB
89static char *
90unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
d57a3c85 91{
08c637de 92 char *result;
d57a3c85
TJB
93 PyObject *string;
94
08c637de
TJB
95 /* Translate string to named charset. */
96 string = PyUnicode_AsEncodedString (unicode_str, charset, NULL);
d57a3c85
TJB
97 if (string == NULL)
98 return NULL;
99
08c637de 100 result = xstrdup (PyString_AsString (string));
d57a3c85
TJB
101
102 Py_DECREF (string);
103
08c637de
TJB
104 return result;
105}
106
fbb8f299
PM
107/* Returns a PyObject with the contents of the given unicode string
108 object converted to a named charset. If an error occurs during
109 the conversion, NULL will be returned and a python exception will
110 be set. */
111static PyObject *
112unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset)
113{
114 PyObject *string;
115
116 /* Translate string to named charset. */
117 string = PyUnicode_AsEncodedString (unicode_str, charset, NULL);
118 if (string == NULL)
119 return NULL;
120
121 return string;
122}
123
08c637de
TJB
124/* Returns a newly allocated string with the contents of the given unicode
125 string object converted to the target's charset. If an error occurs during
126 the conversion, NULL will be returned and a python exception will be set.
127
128 The caller is responsible for xfree'ing the string. */
129char *
130unicode_to_target_string (PyObject *unicode_str)
131{
f870a310
TT
132 return unicode_to_encoded_string (unicode_str,
133 target_charset (python_gdbarch));
d57a3c85
TJB
134}
135
fbb8f299
PM
136/* Returns a PyObject with the contents of the given unicode string
137 object converted to the target's charset. If an error occurs
138 during the conversion, NULL will be returned and a python exception
139 will be set. */
140PyObject *
141unicode_to_target_python_string (PyObject *unicode_str)
142{
f870a310
TT
143 return unicode_to_encoded_python_string (unicode_str,
144 target_charset (python_gdbarch));
fbb8f299
PM
145}
146
d57a3c85
TJB
147/* Converts a python string (8-bit or unicode) to a target string in
148 the target's charset. Returns NULL on error, with a python exception set.
149
150 The caller is responsible for xfree'ing the string. */
151char *
152python_string_to_target_string (PyObject *obj)
153{
154 PyObject *str;
83390453 155 char *result;
d57a3c85
TJB
156
157 str = python_string_to_unicode (obj);
158 if (str == NULL)
159 return NULL;
160
83390453
PM
161 result = unicode_to_target_string (str);
162 Py_DECREF (str);
163 return result;
d57a3c85 164}
08c637de 165
fbb8f299
PM
166/* Converts a python string (8-bit or unicode) to a target string in the
167 target's charset. Returns NULL on error, with a python exception
168 set. */
169PyObject *
170python_string_to_target_python_string (PyObject *obj)
171{
172 PyObject *str;
173 PyObject *result;
174
175 str = python_string_to_unicode (obj);
176 if (str == NULL)
177 return NULL;
178
179 result = unicode_to_target_python_string (str);
180 Py_DECREF (str);
181 return result;
182}
183
08c637de
TJB
184/* Converts a python string (8-bit or unicode) to a target string in
185 the host's charset. Returns NULL on error, with a python exception set.
186
187 The caller is responsible for xfree'ing the string. */
188char *
189python_string_to_host_string (PyObject *obj)
190{
191 PyObject *str;
83390453 192 char *result;
08c637de
TJB
193
194 str = python_string_to_unicode (obj);
195 if (str == NULL)
196 return NULL;
197
83390453
PM
198 result = unicode_to_encoded_string (str, host_charset ());
199 Py_DECREF (str);
200 return result;
08c637de
TJB
201}
202
b6cb8e7d
TJB
203/* Converts a target string of LENGTH bytes in the target's charset to a
204 Python Unicode string. If LENGTH is -1, convert until a null byte is found.
205
206 Returns NULL on error, with a python exception set. */
207PyObject *
208target_string_to_unicode (const gdb_byte *str, int length)
209{
210 if (length == -1)
211 length = strlen (str);
212
f870a310 213 return PyUnicode_Decode (str, length, target_charset (python_gdbarch), NULL);
b6cb8e7d
TJB
214}
215
08c637de
TJB
216/* Return true if OBJ is a Python string or unicode object, false
217 otherwise. */
218
219int
220gdbpy_is_string (PyObject *obj)
221{
222 return PyString_Check (obj) || PyUnicode_Check (obj);
223}
This page took 0.191978 seconds and 4 git commands to generate.