Re: [Python-Dev] Calling Methods from Pythons C API with Keywords
On Wed, 2007-06-20 at 00:21 +1000, Campbell Barton wrote:
> I want to add my own call's before and after PyLists standard functions
> but have a proplem with functons that use keywords and have no API
> equivalent.
> For example, I cant use the API's PyList_Sort because that dosnt support
> keywords like...
>
> ls.sort(key=lambda a: a.foo))
>
> And the Problem with PyObject_CallMethod is that it dosnt accept keywords.
Note that you can always simply call PyObject_Call on the bound method
object retrieved using PyObject_GetAttrString. The hardest part is
usually constructing the keywords dictionary, a job best left to
Py_BuildValue and friends. When I need that kind of thing in more than
one place, I end up with a utility function like this one:
/* Equivalent to PyObject_CallMethod but accepts keyword args. The
format... arguments should produce a dictionary that will be passed
as keyword arguments to obj.method.
Usage example:
PyObject *res = call_method(lst, "sort", "{s:O}", "key", keyfun));
*/
PyObject *
call_method(PyObject *obj, const char *methname, char *format, ...)
{
va_list va;
PyObject *meth = NULL, *args = NULL, *kwds = NULL, *ret = NULL;
args = PyTuple_New(0);
if (!args)
goto out;
meth = PyObject_GetAttrString(obj, methname);
if (!meth)
goto out;
va_start(va, format);
kwds = Py_VaBuildValue(format, va);
va_end(va);
if (!kwds)
goto out;
ret = PyObject_Call(meth, args, kwds);
out:
Py_XDECREF(meth);
Py_XDECREF(args);
Py_XDECREF(kwds);
return ret;
}
It would be nice for the Python C API to support a more convenient way
of calling objects and methods with keyword arguments.
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Calling Methods from Pythons C API with Keywords
Hrvoje Nikšić wrote:
> On Wed, 2007-06-20 at 00:21 +1000, Campbell Barton wrote:
>> I want to add my own call's before and after PyLists standard functions
>> but have a proplem with functons that use keywords and have no API
>> equivalent.
>> For example, I cant use the API's PyList_Sort because that dosnt support
>> keywords like...
>>
>> ls.sort(key=lambda a: a.foo))
>>
>> And the Problem with PyObject_CallMethod is that it dosnt accept keywords.
>
> Note that you can always simply call PyObject_Call on the bound method
> object retrieved using PyObject_GetAttrString. The hardest part is
> usually constructing the keywords dictionary, a job best left to
> Py_BuildValue and friends. When I need that kind of thing in more than
> one place, I end up with a utility function like this one:
>
> /* Equivalent to PyObject_CallMethod but accepts keyword args. The
>format... arguments should produce a dictionary that will be passed
>as keyword arguments to obj.method.
>
>Usage example:
> PyObject *res = call_method(lst, "sort", "{s:O}", "key", keyfun));
> */
>
> PyObject *
> call_method(PyObject *obj, const char *methname, char *format, ...)
> {
> va_list va;
> PyObject *meth = NULL, *args = NULL, *kwds = NULL, *ret = NULL;
>
> args = PyTuple_New(0);
> if (!args)
> goto out;
> meth = PyObject_GetAttrString(obj, methname);
> if (!meth)
> goto out;
>
> va_start(va, format);
> kwds = Py_VaBuildValue(format, va);
> va_end(va);
> if (!kwds)
> goto out;
>
> ret = PyObject_Call(meth, args, kwds);
> out:
> Py_XDECREF(meth);
> Py_XDECREF(args);
> Py_XDECREF(kwds);
> return ret;
> }
>
> It would be nice for the Python C API to support a more convenient way
> of calling objects and methods with keyword arguments.
Thanks for the hint, I ended up using PyObject_Call.
This seems to work, EXPP_PyTuple_New_Prepend - is a utility function
that returns a new tuple with self at the start (needed so args starts
with self)
I dont think I can use PyObject_GetAttrString because the subtype would
return a reference to this function - rather then the lists original
function, Id need an instance of a list and dont have one at that point.
__
static PyObject * MaterialList_sort(BPy_MaterialList *self, PyObject
*args, PyObject *keywds )
{
PyObject *ret;
PyObject *newargs = EXPP_PyTuple_New_Prepend(args, (PyObject *)self);
sync_list_from_materials__internal(self); # makes sure the list matches
blenders materials
ret = PyObject_Call(PyDict_GetItemString(PyList_Type.tp_dict, "sort"),
newargs, keywds);
Py_DECREF(newargs);
if (ret)
sync_materials_from_list__internal(self); # makes blenders
materials
match the lists
return ret;
}
_
Later on Ill probably avoid using PyDict_GetItemString on
PyList_Type.tp_dict all the time since the methods for lists does not
change during python running. - Can probably be assigned to a constant.
--
Campbell J Barton (ideasman42)
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Calling Methods from Pythons C API with Keywords
[ Note that this discussion, except maybe for the suggestion to add a simpler way to call a method with keyword args, is off-topic to python-dev. ] On Wed, 2007-06-20 at 20:17 +1000, Campbell Barton wrote: > I dont think I can use PyObject_GetAttrString because the subtype would > return a reference to this function - rather then the lists original > function, Id need an instance of a list and dont have one at that point. Note that PyList_Type is a full-fledged PyObject, so PyObject_GetAttrString works on it just fine. Of course, you would also need to add the "self" argument before the keywords, but that's a trivial change to the function. Calling PyObject_GetAttrString feels cleaner than accessing tp_dict directly, and most importantly call_method as written delegates creation of the dictionaty to Py_BuildValue. ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Calling Methods from Pythons C API with Keywords
Hrvoje NikÅ¡iÄ wrote: > [ Note that this discussion, except maybe for the suggestion to add a > simpler way to call a method with keyword args, is off-topic to > python-dev. ] Is there a list for this kind of discussion? Iv tried asking questions on the freenode python chat room but almost very few people there do C/Python api development. -- Campbell J Barton (ideasman42) ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python 3000 Status Update (Long!)
Guido van Rossum wrote: > I've written up a comprehensive status report on Python 3000. Please read: > > http://www.artima.com/weblogs/viewpost.jsp?thread=208549 One doubt: In Miscellaneus you say: Ordering comparisons (<, <=, >, >=) will raise TypeError by default instead of returning arbitrary results. Equality comparisons (==, !=) will compare for object identity (is, is not) by default. I *guess* that you're talking about comparisons between different datatypes... but you didn't explicit that in your blog. Am I right? -- . Facundo . Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/ ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Calling Methods from Pythons C API with Keywords
On Wed, 2007-06-20 at 22:12 +1000, Campbell Barton wrote: > Hrvoje Nikšić wrote: > > [ Note that this discussion, except maybe for the suggestion to add a > > simpler way to call a method with keyword args, is off-topic to > > python-dev. ] > > Is there a list for this kind of discussion? I believe the appropriate list would be the general Python list/newsgroup. I agree that response about the Python/C API tends to be sparse on general-purpose lists, though. If there is a forum dedicated to discussing the *use* of Python at the C level, I'd like to know about it as well. ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Python 3000 Status Update (Long!)
On 6/20/07, Facundo Batista <[EMAIL PROTECTED]> wrote: > Guido van Rossum wrote: > > > I've written up a comprehensive status report on Python 3000. Please read: > > > > http://www.artima.com/weblogs/viewpost.jsp?thread=208549 > > One doubt: In Miscellaneus you say: > > Ordering comparisons (<, <=, >, >=) will raise TypeError by default > instead of returning arbitrary results. Equality comparisons (==, !=) > will compare for object identity (is, is not) by default. > > I *guess* that you're talking about comparisons between different > datatypes... but you didn't explicit that in your blog. > > Am I right? No. The *default* comparison always raises an exception. Of course, most types have a comparison that does the right thing for objects of the same type -- but they still raise an exception when compared (for ordering) to objects of different types (except subtypes or related types). -- --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] cleaning up the email addresses in the PEPs
I am working on some code in the sandbox to automatically generate PEP 0. This is also leading to code that checks all the PEPs follow some basic guidelines. One of those guidelines is an author having a single email address. The Owners index at the bottom of PEP 0 is going to be created from the names and email addresses found in the PEPs themselves. But that doesn't work too well when an author has multiple addresses listed. If you are listed below, please choose a single address to use. You can either change the PEPs yourself or just reply with the email you prefer. I can tell you the multiple spellings if you want. If I don't hear from people I will just use my best judgement. And even better, if you spell your name multiple ways in the PEPs (e.g., Martin v. Loewis, Martin v. Löwis, Martin von Löwis) also let it be known which spelling you prefer (unifying name spelling comes after unifying the email addresses). Aahz Ka-Ping Yee: Neil Schemenauer David Goodger: Tim Peters: Martin v. Löwis: Paul Prescod: Jeremy Hylton: Clark C. Evans: Richard Jones: Alex Martelli: Moshe Zadka -Brett ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Calling Methods from Pythons C API with Keywords
Campbell Barton schrieb: > Hrvoje NikÅ¡iÄ wrote: >> [ Note that this discussion, except maybe for the suggestion to add a >> simpler way to call a method with keyword args, is off-topic to >> python-dev. ] > > Is there a list for this kind of discussion? Hrvoje wasn't explicit on *why* this discussion is inappropriate here, so I just add that for better understanding: python-dev is for the development *of* Python, not for the development *with* Python. So you post here if you propose an enhancement or discuss the resolution of a bug. Question of the "how do I" kind are off-topic - posters are expected to know and understand the options, and then discuss the flaws of these options, rather than asking what they are. As Hrvoje says: try python-list (aka comp.lang.python). If you don't get an answer, you didn't phrase your question interestingly enough, or nobody knows the answer, or nobody has the time to tell you. Regards, Martin ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] www.python.org outage
The scheduled outage starts now. Regards, Martin ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
