public inbox for archer@sourceware.org
 help / color / mirror / Atom feed
From: Phil Muldoon <pmuldoon@redhat.com>
To: Tom Tromey <tromey@redhat.com>
Cc: Thiago Jung Bauermann <bauerman@br.ibm.com>,
	Project Archer <archer@sourceware.org>
Subject: Re: [python][patch] Preserve string length, and embedded-nulls in pretty-print string emission.
Date: Tue, 16 Jun 2009 12:44:00 -0000	[thread overview]
Message-ID: <4A37937E.8060007@redhat.com> (raw)
In-Reply-To: <4A36B3EE.8020102@redhat.com>

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;
+}

  reply	other threads:[~2009-06-16 12:44 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-22 20:23 Phil Muldoon
2009-05-27 17:35 ` Tom Tromey
2009-05-27 18:24   ` Phil Muldoon
2009-05-27 20:22     ` Tom Tromey
2009-06-01 16:26       ` Phil Muldoon
2009-06-02  5:11         ` Thiago Jung Bauermann
2009-06-02 23:46           ` Tom Tromey
2009-06-08 10:31             ` Phil Muldoon
2009-06-11 17:12               ` Tom Tromey
2009-06-15 20:50                 ` Phil Muldoon
2009-06-16 12:44                   ` Phil Muldoon [this message]
2009-06-16 18:07                     ` Tom Tromey
2009-06-18 12:20                       ` Phil Muldoon
2009-06-18 20:17                         ` Tom Tromey
2009-06-18 20:21                         ` Tom Tromey
2009-06-23 14:37                           ` Phil Muldoon
2009-06-23 16:37                             ` Tom Tromey
2009-06-23 16:56                               ` Phil Muldoon
2009-06-02 15:38         ` Jonas Maebe
2009-06-02 20:48           ` Phil Muldoon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4A37937E.8060007@redhat.com \
    --to=pmuldoon@redhat.com \
    --cc=archer@sourceware.org \
    --cc=bauerman@br.ibm.com \
    --cc=tromey@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).