This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [python][patch] Preserve string length, and embedded-nulls inpretty-print string emission.


On 06/15/2009 09:49 PM, Phil Muldoon wrote:
On 06/11/2009 06:11 PM, Tom Tromey wrote: Phil> +unicode_to_encoded_string (PyObject *unicode_str, const char *charset, int *size)

[...]


Phil> + str_size = PyString_Size (string) + 1;
Phil> + result = xmemdup (PyString_AsString (string), str_size, str_size);
Phil> + if (size != NULL)
Phil> + *size = str_size;


How about a variant of this function that just returns the
intermediate 'string' PyObject?


Phil> + output = python_string_to_target_string (py_str,&len);
Phil> + if (output)
Phil> + {
Phil> + struct cleanup *old = make_cleanup (xfree, output);

Adding a new unicode_to_target_string variant (or
python_string_to_target_string, or whatever is nicest) means you can
avoid an allocation here.



I decided to take a crack at the mechanics of this change this morning. This produces the following hunks (in rough). I can see how just borrowing the underlying data from the PyObject avoids an allocation (the results of PyString_AsString must not be deallocated, etc). Is this what you had in mind? I ended up having to add 3 new variant string methods for this in the end.



/* Instantiate a pretty-printer given a constructor, CONS, and a
@@ -906,18 +901,27 @@ print_string_repr (PyObject *printer, const char *hint,
const struct value_print_options *options,
const struct language_defn *language)
{
- char *output;
struct value *replacement = NULL;
+ PyObject *py_str = NULL;


- output = pretty_print_one_value (printer, &replacement);
- if (output)
+ py_str = pretty_print_one_value (printer, &replacement);
+ if (py_str)
{
- if (hint && !strcmp (hint, "string"))
- LA_PRINT_STRING (stream, (gdb_byte *) output, strlen (output),
- 1, 0, options);
- else
- fputs_filtered (output, stream);
- xfree (output);
+ int len;
+ PyObject *string = NULL;
+ string = python_string_to_target_pystring (py_str, &len);
+ if (string)
+ {
+ gdb_byte *output = PyString_AsString (string);
+ if (hint && !strcmp (hint, "string"))
+ LA_PRINT_STRING (stream, output,
+ len, 1, 0, options);
+ else
+ fputs_filtered (output, stream);
+ Py_DECREF (string);
+ }
+ Py_DECREF (py_str);
}
else if (replacement)
common_val_print (replacement, stream, recurse, options, language);
@@ -1232,28 +1236,31 @@ apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
return result;
}



+/* Returns a PyObject with the contents of the given unicode
+ string object converted to a named charset. The parameter *SIZE
+ will be set to the size of the string. If *SIZE is NULL no size
+ information will be returned. If an error occurs during the
+ conversion, NULL will be returned and a python exception will be
+ set. */
+static PyObject *
+unicode_to_encoded_pystring (PyObject *unicode_str, const char *charset, int *size)
+{
+ PyObject *string;
+
+ /* Translate string to named charset. */
+
+ string = PyUnicode_AsEncodedString (unicode_str, charset, NULL);
+ if (string == NULL)
+ return NULL;
+
+ *size = PyString_Size (string) + 1;
+ return string;
+}



+/* Returns a PyObject with the contents of the given + unicode string object converted to the target's charset. The + parameter *SIZE will be set to the size of the string. If *SIZE is + NULL no size information will be returned. If an error occurs + during the conversion, NULL will be returned and a python exception + will be set. */ +PyObject * +unicode_to_target_pystring (PyObject *unicode_str, int *size) +{ + return unicode_to_encoded_pystring (unicode_str, target_charset (), size); +}


+/* Converts a python string (8-bit or unicode) to a target string in + the target's charset. The parameter *SIZE will be set to the + size of the string. If *SIZE is NULL no size information will be + returned. Returns NULL on error, with a python exception set. */ +PyObject * +python_string_to_target_pystring (PyObject *obj, int *size) +{ + PyObject *str; + PyObject *result; + + str = python_string_to_unicode (obj); + if (str == NULL) + return NULL; + + result = unicode_to_target_pystring (str, size); + Py_DECREF (str); + return result; +}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]