OSDN Git Service

912845f248bd55ab6254bf6190cc70a1d7d9698c
[pf3gnuchains/pf3gnuchains3x.git] / gdb / python / python-utils.c
1 /* General utility routines for GDB/Python.
2
3    Copyright (C) 2008 Free Software Foundation, Inc.
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
28 static void
29 py_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
43 struct cleanup *
44 make_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.  */
59 PyObject *
60 python_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))
67     unicode_str = obj;
68   else if (PyString_Check (obj))
69     unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL);
70   else
71     {
72       PyErr_SetString (PyExc_TypeError,
73                        _("Expected a string or unicode object."));
74       unicode_str = NULL;
75     }
76
77   return unicode_str;
78 }
79
80 /* Returns a newly allocated string with the contents of the given unicode
81    string object converted to the target's charset.  If an error occurs during
82    the conversion, NULL will be returned and a python exception will be set.
83
84    The caller is responsible for xfree'ing the string.  */
85 char *
86 unicode_to_target_string (PyObject *unicode_str)
87 {
88   char *target_string;
89   PyObject *string;
90
91   /* Translate string to target's charset.  */
92   string = PyUnicode_AsEncodedString (unicode_str, target_charset (), NULL);
93   if (string == NULL)
94     return NULL;
95
96   target_string = xstrdup (PyString_AsString (string));
97
98   Py_DECREF (string);
99
100   return target_string;
101 }
102
103 /* Converts a python string (8-bit or unicode) to a target string in
104    the target's charset.  Returns NULL on error, with a python exception set.
105
106    The caller is responsible for xfree'ing the string.  */
107 char *
108 python_string_to_target_string (PyObject *obj)
109 {
110   PyObject *str;
111
112   str = python_string_to_unicode (obj);
113   if (str == NULL)
114     return NULL;
115
116   return unicode_to_target_string (str);
117 }