From: Phil Muldoon <pmuldoon@redhat.com>
To: Matt Rice <ratmice@gmail.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [patch] [python] find_line_pc_range
Date: Sun, 03 Jul 2011 16:35:00 -0000 [thread overview]
Message-ID: <m3y60fmj76.fsf@redhat.com> (raw)
In-Reply-To: <CACTLOFptUbx6vtmykDBxtqv3GcrGTQXzX4m6q6O4X4ddw2WM1w@mail.gmail.com> (Matt Rice's message of "Fri, 1 Jul 2011 21:02:42 -0700")
Matt Rice <ratmice@gmail.com> writes:
> don't know a whole much about python but,
> would it be better to return None on error, instead of a tuple containing Nones?
Thanks. I would return either an exception or None depending on the
situational context of the failure. If a None type scenario is expected,
and normal then it is okay to return that, given that the Python
scripter would understand the reason for that. So documentation is key
here. I think find_line_pc_range is just a utility function,
which can very well not find the line range. So I would replicate that
functionality in your API, except I would return a single Py_None over a
tuple.
> +salpy_find_line_pc_range (PyObject *self, PyObject *args)
> +{
> + struct symtab_and_line *sal = NULL;
> + CORE_ADDR start_pc, end_pc;
> + PyObject *start;
> + PyObject *end;
> + PyObject *ret_tuple;
> +
> + SALPY_REQUIRE_VALID (self, sal);
> +
> + ret_tuple = PyTuple_New (2);
This call can fail and return NULL, so in this case you need to return
NULL to propagate the failure.
> + if (find_line_pc_range (*sal, &start_pc, &end_pc))
> + {
> +
> + start = gdb_py_long_from_longest (start_pc);
> + end = gdb_py_long_from_longest (end_pc);
> + PyTuple_SET_ITEM (ret_tuple, 0, start);
> + PyTuple_SET_ITEM (ret_tuple, 1, end);
> + }
start and end could be local to this block I think.
Also gdb_py_long_from_longest is macro that points to Py
Long_FromUnsignedLongLong. This can also return NULL which indicates a
failure, so similar to above, you need to check the return and return
NULL if one of them fails. If one of them does return NULL, you also
need to appropriately clean-up previous references that were created in
Python. We normally do this by creating a local error: goto.
A minor nit, SET_ITEM does not do any error checking, while SetItem
does. As this is a new tuple, then it is ok to use it. But normally
(and personally, in new tuples) I always use the error checking
equivalent.
+ else
+ {
+ PyTuple_SET_ITEM (ret_tuple, 0, Py_None);
+ PyTuple_SET_ITEM (ret_tuple, 1, Py_None);
+ }
+
In this case I would just return Py_None (see first paragraph). It
seems a little redundant to return a tuple with Py_None for both
elements, and this will always be the case on a lookup failure with
find_line_pc_range. Make sure you incref Py_None before returning it,
also.
Also, this patch needs tests.
Thanks for the patch and your work!
Cheers,
Phil
next prev parent reply other threads:[~2011-07-03 16:18 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-07-02 4:03 Matt Rice
2011-07-03 16:35 ` Phil Muldoon [this message]
2011-07-04 5:38 ` Phil Muldoon
2011-07-04 6:36 ` Matt Rice
2011-07-04 10:09 ` Phil Muldoon
2011-07-05 2:47 ` Matt Rice
2011-07-05 20:41 ` Phil Muldoon
2011-07-04 10:50 ` Eli Zaretskii
2011-07-04 10:56 ` Matt Rice
2011-07-04 13:14 ` Eli Zaretskii
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=m3y60fmj76.fsf@redhat.com \
--to=pmuldoon@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=ratmice@gmail.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