From robertsteckroth at gmail.com Wed Nov 7 02:21:37 2012 From: robertsteckroth at gmail.com (Robert Steckroth) Date: Tue, 6 Nov 2012 20:21:37 -0500 Subject: [capi-sig] Better way to PyArg_ParseTuple Message-ID: Hello Gang, I was wondering about a better way to accept dummy arguments in a object. *PyArg_ParseTuple(args, "|ssi", &meh, &path, &font_size) )* I would like to omit the* meh variable for a null pointer instead like this. * *PyArg_ParseTuple(args, "|ssi", NULL, &path, &font_size) )* This become necessary when args are passing from one object to another with Py_Tuple creations. Any thoughts? -- Bust0ut, Surgemcgee: Systems Engineer --- surgemcgee.com Django_Teamplate3d From ideasman42 at gmail.com Wed Nov 7 03:34:34 2012 From: ideasman42 at gmail.com (Campbell Barton) Date: Wed, 7 Nov 2012 13:34:34 +1100 Subject: [capi-sig] Better way to PyArg_ParseTuple In-Reply-To: References: Message-ID: AFAIK there isn't, if you wanted to avoid parsing it you could replace 's' with 'O' so at least its type won't be checked. On Wed, Nov 7, 2012 at 12:21 PM, Robert Steckroth wrote: > Hello Gang, I was wondering about a better way to accept dummy arguments in > a object. > *PyArg_ParseTuple(args, "|ssi", &meh, &path, &font_size) )* > > I would like to omit the* meh variable for a null pointer instead like this. > * > *PyArg_ParseTuple(args, "|ssi", NULL, &path, &font_size) )* > > This become necessary when args are passing from one object to another with > Py_Tuple creations. > > > Any thoughts? > > > -- > Bust0ut, Surgemcgee: Systems Engineer --- > surgemcgee.com > Django_Teamplate3d > _______________________________________________ > capi-sig mailing list > capi-sig at python.org > http://mail.python.org/mailman/listinfo/capi-sig -- - Campbell From robertsteckroth at gmail.com Fri Nov 9 00:09:36 2012 From: robertsteckroth at gmail.com (Robert Steckroth) Date: Thu, 8 Nov 2012 18:09:36 -0500 Subject: [capi-sig] Keyword creation in a module function Message-ID: Hey Gang, I have come across the need to pass both keywords and arguments from one function to another. The keywords are passing just fine with a PyObjectCall command. However, if the user does not specify any keywords at the interpretor, the command will seg-fault. The docs state that keywords argument can be NULL, but this does not work with NULL set objects. How do I build a keywords dictionary which can function correctly if there are none to start with or if the user defined some? I am thinking it can be done with a PyDict() like command? The below snippet is the scenario I am trying to achieve --> if ( kwds ) { self->c_surface = PyObject_Call((PyObject *) &PyMyObject_Type, object_args, kwds); } else { self->c_surface = PyObject_Call((PyObject *) &PyMyObject_Type, object_args, NULL); } -- Bust0ut, Surgemcgee: Systems Engineer --- surgemcgee.com Django_Teamplate3d From robertsteckroth at gmail.com Fri Nov 9 04:54:24 2012 From: robertsteckroth at gmail.com (Robert Steckroth) Date: Thu, 8 Nov 2012 22:54:24 -0500 Subject: [capi-sig] Keyword creation in a module function In-Reply-To: References: Message-ID: Well, turns out that while the keyword dictionary thing is complex, my problem was elsewhere. Is it possible that the documentation is wrong? The capital "S" in the docs state this -> S (object) [PyObject *] Same as O. here http://docs.python.org/2/c-api/arg.html?highlight=keyword#Py_BuildValue But "S" it has string checking properties as opposed to "O". E.g. "if ( ! PyArg_ParseTupleAndKeywords(args, kwds, "|OSllS"... != "if ( ! PyArg_ParseTupleAndKeywords(args, kwds, "|OOllO"... Anyway, got my thing figured out :) On Thu, Nov 8, 2012 at 6:09 PM, Robert Steckroth wrote: > Hey Gang, I have come across the need to pass both keywords and arguments > from one function to another. The keywords are passing just fine with a > PyObjectCall command. However, if the user does not specify any keywords at > the interpretor, the command will seg-fault. > The docs state that keywords argument can be NULL, but this does not work > with NULL set objects. > How do I build a keywords dictionary which can function correctly if there > are none to start with or if the user defined some? I am thinking it can > be done with a PyDict() like command? > > The below snippet is the scenario I am trying to achieve --> > > if ( kwds ) { > self->c_surface = PyObject_Call((PyObject *) &PyMyObject_Type, > object_args, kwds); } > else { self->c_surface = PyObject_Call((PyObject *) &PyMyObject_Type, > object_args, NULL); } > > > > > -- > Bust0ut, Surgemcgee: Systems Engineer --- > surgemcgee.com > Django_Teamplate3d > -- Bust0ut, Surgemcgee: Systems Engineer --- surgemcgee.com Django_Teamplate3d From python_capi at behnel.de Fri Nov 9 08:54:06 2012 From: python_capi at behnel.de (Stefan Behnel) Date: Fri, 09 Nov 2012 08:54:06 +0100 Subject: [capi-sig] Keyword creation in a module function In-Reply-To: References: Message-ID: <509CB69E.7030407@behnel.de> Robert Steckroth, 09.11.2012 04:54: > Well, turns out that while the keyword dictionary thing is complex, my > problem was elsewhere. Have you considered using Cython? It would allow you to concentrate on the real problems, i.e. the functionality you want to implement, rather than having to dig into this kind of low-level issues. Stefan From robertsteckroth at gmail.com Tue Nov 20 16:23:38 2012 From: robertsteckroth at gmail.com (Robert Steckroth) Date: Tue, 20 Nov 2012 10:23:38 -0500 Subject: [capi-sig] Dynamically add members to a object at runtime Message-ID: Hello Gang, I have come across the need to dynamically add members to an object at runtime. I would like to attach many objects of a single separate type into a object for processing. Is there a way to dynamically create object members while maintaining the (de)allocation structure? It would be nice to create an array of objects in a single PyObject but then a Py_DECREF() would not work on all of the objects in the array. Has anyone come across the need for this? It would make the module I am creating very easy to use. If not, a short "no" will not hurt my feelings ,') E.g. w = DateTime() x = DateTime() my_object.add(w) my_object.add(x) And then have some internal stuff possess those objects, E.g. my_object.combine_dates() -- Bust0ut, Surgemcgee: Systems Engineer --- surgemcgee.com Django_Teamplate3d From ideasman42 at gmail.com Tue Nov 20 23:02:52 2012 From: ideasman42 at gmail.com (Campbell Barton) Date: Wed, 21 Nov 2012 09:02:52 +1100 Subject: [capi-sig] Dynamically add members to a object at runtime In-Reply-To: References: Message-ID: Internally you can use a list in your custom PyObject's struct, (or possibly a tuple if you are sure its kept for internal use - since you'd have to resize it). if you do hold a reference to other PyObject's from a PyTypeObject you define, be sure to fill in PyTypeObject.tp_traverse and include Py_TPFLAGS_HAVE_GC in your tp_flags. for example see: Another option is to attach a PyObject* dict to your struct and define PyTypeObject.tp_dictoffset for example see: https://svn.blender.org/svnroot/bf-blender/trunk/blender/source/blender/python/intern/bpy_library.c lines, 72, 156 This has the advantage you can add whatever you like into your object and name your members too, without messing with tp_traverse. On Wed, Nov 21, 2012 at 2:23 AM, Robert Steckroth wrote: > Hello Gang, I have come across the need to dynamically add members to an > object at runtime. I would like to attach many objects of a > single separate type into a object for processing. > Is there a way to dynamically create object members while maintaining the > (de)allocation structure? > It would be nice to create an array of objects in a single PyObject but > then a Py_DECREF() > would not work on all of the objects in the array. Has anyone > come across the need for this? It would make the module I am creating very > easy to use. If not, a short "no" will not hurt my feelings ,') > > > E.g. > w = DateTime() > x = DateTime() > my_object.add(w) > my_object.add(x) > > And then have some internal stuff possess those objects, E.g. > my_object.combine_dates() > > > > -- > Bust0ut, Surgemcgee: Systems Engineer --- > surgemcgee.com > Django_Teamplate3d > _______________________________________________ > capi-sig mailing list > capi-sig at python.org > http://mail.python.org/mailman/listinfo/capi-sig -- - Campbell From mal at egenix.com Wed Nov 21 08:58:49 2012 From: mal at egenix.com (M.-A. Lemburg) Date: Wed, 21 Nov 2012 08:58:49 +0100 Subject: [capi-sig] Dynamically add members to a object at runtime In-Reply-To: References: Message-ID: <50AC89B9.5080408@egenix.com> On 20.11.2012 16:23, Robert Steckroth wrote: > Hello Gang, I have come across the need to dynamically add members to an > object at runtime. I would like to attach many objects of a > single separate type into a object for processing. > Is there a way to dynamically create object members while maintaining the > (de)allocation structure? > It would be nice to create an array of objects in a single PyObject but > then a Py_DECREF() > would not work on all of the objects in the array. Has anyone > come across the need for this? It would make the module I am creating very > easy to use. If not, a short "no" will not hurt my feelings ,') > > > E.g. > w = DateTime() > x = DateTime() > my_object.add(w) > my_object.add(x) > > And then have some internal stuff possess those objects, E.g. > my_object.combine_dates() Dynamically adding members is possible, but requires extra work. I'd simply stick all the extra objects into a dictionary, have the object reference (and hold the reference to) the dictionary and release it when the object is deallocated. If you want garbage collection to work on your object, you will also have to implement the traversal API, so that cyclic references can be dealt with by the Python GC. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Nov 21 2012) >>> Python Projects, Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From nirmoy.aiemd at gmail.com Wed Nov 21 12:38:36 2012 From: nirmoy.aiemd at gmail.com (nirmoy das) Date: Wed, 21 Nov 2012 17:08:36 +0530 Subject: [capi-sig] expression as argument of a object Message-ID: Can anyone tell me how to use expression in PyObject_CallObject() as argument of object ex:column_family('keyspace', 'CF', comparator_type=comparator) From nirmoy.aiemd at gmail.com Wed Nov 21 18:53:02 2012 From: nirmoy.aiemd at gmail.com (nirmoy das) Date: Wed, 21 Nov 2012 23:23:02 +0530 Subject: [capi-sig] expression as argument of a object In-Reply-To: References: Message-ID: can any one give a example .It gonna help me a lot On Wed, Nov 21, 2012 at 5:08 PM, nirmoy das wrote: > Can anyone tell me how to use expression in PyObject_CallObject() > as argument of object > > ex:column_family('keyspace', 'CF', comparator_type=comparator) > > > From python_capi at behnel.de Wed Nov 21 19:35:19 2012 From: python_capi at behnel.de (Stefan Behnel) Date: Wed, 21 Nov 2012 19:35:19 +0100 Subject: [capi-sig] expression as argument of a object In-Reply-To: References: Message-ID: <50AD1EE7.3010909@behnel.de> nirmoy das, 21.11.2012 12:38: > Can anyone tell me how to use expression in PyObject_CallObject() > as argument of object > > ex:column_family('keyspace', 'CF', comparator_type=comparator) My (biased) advice is to use Cython. It allows you to call functions in plain and simple Python syntax, including keyword arguments and the like. So your example above completely solves your problem already. If you want to do it manually, and assuming that with "expression" you mean "keyword arguments", as in your example above, you have to first build a dict with PyDict_New(), then add a new value to it, then pass the dict into the call, and then decref the reference to the dict. Stefan From nirmoy.aiemd at gmail.com Wed Nov 21 20:00:21 2012 From: nirmoy.aiemd at gmail.com (nirmoy das) Date: Thu, 22 Nov 2012 00:30:21 +0530 Subject: [capi-sig] Tips for developing application using python capi Message-ID: Hey guys, I developing a application in c but i need python to support Cassandra Database. I wrote Few API but felt bit complex while coding python C-API. Give some tips to for developing application using python capi. From francis.bolduc at cm-labs.com Wed Nov 21 21:00:37 2012 From: francis.bolduc at cm-labs.com (Francis Bolduc) Date: Wed, 21 Nov 2012 15:00:37 -0500 Subject: [capi-sig] Tips for developing application using python capi In-Reply-To: References: Message-ID: The CPython API documentation is extensive, but it is geared towards very simple use-cases. When you do more advanced stuff, you're on your own. I usually end-up having to read the CPython code itself to find out what does what and how things work. Having a debugger to step in the code is essential. Following the call graph will give you more understanding than reading any documentation or examples. So, my tip is to build CPython yourself and get first-hand experience working with it's code. On Wed, Nov 21, 2012 at 2:00 PM, nirmoy das wrote: > Hey guys, > > I developing a application in c but i need python to support > Cassandra Database. I wrote Few API but felt bit complex while coding > python C-API. > Give some tips to for developing application using python capi. > _______________________________________________ > capi-sig mailing list > capi-sig at python.org > http://mail.python.org/mailman/listinfo/capi-sig -- Francis Bolduc, B.Sc. CM-Labs Simulation Inc. From hniksic at xemacs.org Thu Nov 22 08:43:14 2012 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 22 Nov 2012 08:43:14 +0100 Subject: [capi-sig] expression as argument of a object In-Reply-To: (nirmoy das's message of "Wed, 21 Nov 2012 17:08:36 +0530") References: Message-ID: <877gpeccbx.fsf@xemacs.org> nirmoy das writes: > ex:column_family('keyspace', 'CF', comparator_type=comparator) You cannot use PyObject_CallObject to pass keyword arguments to a function; use PyObject_Call. The easiest way is to use Py_BuildValue to create the positional and the keyword arguments. For example: /* error checking omitted for brevity */ PyObject *args, *kwds, *ret; args = Py_BuildValue("ss", "keyspace", "CF"); kwds = Py_BuildValue("sO", "comparator_type", comparator"); ret = PyObject_Call(column_family, args, kwds) Py_DECREF(args); Py_DECREF(kwds); From hniksic at xemacs.org Thu Nov 22 08:40:06 2012 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 22 Nov 2012 08:40:06 +0100 Subject: [capi-sig] expression as argument of a object In-Reply-To: <50AD1EE7.3010909@behnel.de> (Stefan Behnel's message of "Wed, 21 Nov 2012 19:35:19 +0100") References: <50AD1EE7.3010909@behnel.de> Message-ID: <87boeqcch5.fsf@xemacs.org> Stefan Behnel writes: > nirmoy das, 21.11.2012 12:38: >> Can anyone tell me how to use expression in PyObject_CallObject() >> as argument of object >> >> ex:column_family('keyspace', 'CF', comparator_type=comparator) > > My (biased) advice is to use Cython. That is not a useful advice for this list. This list is dedicated to development with the Python/C API, not for Cython advocacy. From mal at egenix.com Thu Nov 22 08:56:50 2012 From: mal at egenix.com (M.-A. Lemburg) Date: Thu, 22 Nov 2012 08:56:50 +0100 Subject: [capi-sig] expression as argument of a object In-Reply-To: <877gpeccbx.fsf@xemacs.org> References: <877gpeccbx.fsf@xemacs.org> Message-ID: <50ADDAC2.6030006@egenix.com> On 22.11.2012 08:43, Hrvoje Niksic wrote: > nirmoy das writes: > >> ex:column_family('keyspace', 'CF', comparator_type=comparator) > > You cannot use PyObject_CallObject to pass keyword arguments to a > function; use PyObject_Call. The easiest way is to use Py_BuildValue to > create the positional and the keyword arguments. For example: > > /* error checking omitted for brevity */ > PyObject *args, *kwds, *ret; > args = Py_BuildValue("ss", "keyspace", "CF"); > kwds = Py_BuildValue("sO", "comparator_type", comparator"); Small typo: kwds = Py_BuildValue("{sO}", "comparator_type", comparator); The braces tell Py_BuildValue to create a dict. For better readability, it also good to add the parens to the args format string: args = Py_BuildValue("(ss)", "keyspace", "CF"); See http://docs.python.org/3/c-api/arg.html#Py_BuildValue for more information. > ret = PyObject_Call(column_family, args, kwds) > Py_DECREF(args); > Py_DECREF(kwds); You should probably also have a look at this tutorial: http://docs.python.org/3/extending/extending.html#calling-python-functions-from-c -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Nov 22 2012) >>> Python Projects, Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From python_capi at behnel.de Thu Nov 22 16:52:05 2012 From: python_capi at behnel.de (Stefan Behnel) Date: Thu, 22 Nov 2012 16:52:05 +0100 Subject: [capi-sig] expression as argument of a object In-Reply-To: <87boeqcch5.fsf@xemacs.org> References: <50AD1EE7.3010909@behnel.de> <87boeqcch5.fsf@xemacs.org> Message-ID: <50AE4A25.2070100@behnel.de> Hrvoje Niksic, 22.11.2012 08:40: > Stefan Behnel writes: >> nirmoy das, 21.11.2012 12:38: >>> Can anyone tell me how to use expression in PyObject_CallObject() >>> as argument of object >>> >>> ex:column_family('keyspace', 'CF', comparator_type=comparator) >> >> My (biased) advice is to use Cython. > > That is not a useful advice for this list. It's a useful advice for the OP, though, in case he or she doesn't know it yet. Most new users who struggle with the C-API simply do so because they do not know Cython and because the official CPython docs tell them that the C-API is the one way to extend CPython (unless they want to use ctypes). It's not really my "fault" that we allow normal users on this list, in addition to those who know what they are doing. Stefan From robertsteckroth at gmail.com Thu Nov 22 18:25:29 2012 From: robertsteckroth at gmail.com (Robert Steckroth) Date: Thu, 22 Nov 2012 12:25:29 -0500 Subject: [capi-sig] Getting doc strings into Py_ErrFormat Message-ID: Is it possible to get a attribute doc/info string into a PyErr_Format(). I have the following error message and it seems like a good thing to print the help message for the align attribute. How would this be accomplished? Do any of you think this is foolish or a non-nonsensical? return PyErr_Format(PyExc_TypeError, "align=[%s] is not a valid option.", PyString_AsString(align)); -- Bust0ut, Surgemcgee: Systems Engineer --- surgemcgee.com Django_Teamplate3d From hniksic at xemacs.org Thu Nov 22 20:21:20 2012 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 22 Nov 2012 20:21:20 +0100 Subject: [capi-sig] expression as argument of a object In-Reply-To: <50AE4A25.2070100@behnel.de> (Stefan Behnel's message of "Thu, 22 Nov 2012 16:52:05 +0100") References: <50AD1EE7.3010909@behnel.de> <87boeqcch5.fsf@xemacs.org> <50AE4A25.2070100@behnel.de> Message-ID: <87sj81bg0f.fsf@xemacs.org> Stefan Behnel writes: > Hrvoje Niksic, 22.11.2012 08:40: >> Stefan Behnel writes: >>> nirmoy das, 21.11.2012 12:38: >>>> Can anyone tell me how to use expression in PyObject_CallObject() >>>> as argument of object >>>> >>>> ex:column_family('keyspace', 'CF', comparator_type=comparator) >>> >>> My (biased) advice is to use Cython. >> >> That is not a useful advice for this list. > > It's a useful advice for the OP, though, in case he or she doesn't know it > yet. Still, this list is not the venue for Cython (or Pyrex, or PyPy, or Jython...) advocacy. If you absolutely must pitch Cython, then at least include a Python/C advice along with the pitch. From hniksic at xemacs.org Thu Nov 22 20:23:41 2012 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 22 Nov 2012 20:23:41 +0100 Subject: [capi-sig] expression as argument of a object In-Reply-To: <50ADDAC2.6030006@egenix.com> (M.'s message of "Thu, 22 Nov 2012 08:56:50 +0100") References: <877gpeccbx.fsf@xemacs.org> <50ADDAC2.6030006@egenix.com> Message-ID: <87obipbfwi.fsf@xemacs.org> "M.-A. Lemburg" writes: >> kwds = Py_BuildValue("sO", "comparator_type", comparator"); > > Small typo: > > kwds = Py_BuildValue("{sO}", "comparator_type", comparator); > > The braces tell Py_BuildValue to create a dict. Yup, sorry about that. > For better readability, it also good to add the parens to > the args format string: > > args = Py_BuildValue("(ss)", "keyspace", "CF"); I concur. Likewise, dict can be expressed as "{s:O}", which reminds of the dict building syntax in Python. From python_capi at behnel.de Thu Nov 22 20:53:14 2012 From: python_capi at behnel.de (Stefan Behnel) Date: Thu, 22 Nov 2012 20:53:14 +0100 Subject: [capi-sig] expression as argument of a object In-Reply-To: <87sj81bg0f.fsf@xemacs.org> References: <50AD1EE7.3010909@behnel.de> <87boeqcch5.fsf@xemacs.org> <50AE4A25.2070100@behnel.de> <87sj81bg0f.fsf@xemacs.org> Message-ID: <50AE82AA.7090309@behnel.de> Hrvoje Niksic, 22.11.2012 20:21: > Stefan Behnel writes: >> Hrvoje Niksic, 22.11.2012 08:40: >>> Stefan Behnel writes: >>>> nirmoy das, 21.11.2012 12:38: >>>>> Can anyone tell me how to use expression in PyObject_CallObject() >>>>> as argument of object >>>>> >>>>> ex:column_family('keyspace', 'CF', comparator_type=comparator) >>>> >>>> My (biased) advice is to use Cython. >>> >>> That is not a useful advice for this list. >> >> It's a useful advice for the OP, though, in case he or she doesn't know it >> yet. > > Still, this list is not the venue for Cython (or Pyrex, or PyPy, or > Jython...) advocacy. If you absolutely must pitch Cython, then at least > include a Python/C advice along with the pitch. You might want to re-read my initial answer. Or rather both of them. Stefan From robertsteckroth at gmail.com Fri Nov 23 17:26:19 2012 From: robertsteckroth at gmail.com (Robert Steckroth) Date: Fri, 23 Nov 2012 11:26:19 -0500 Subject: [capi-sig] Best practice for object attribute use in other objects... Message-ID: Hey Gang, I am using object attributes from other objects in my module. Can anyone tell me the best way to retrieve the object attributes without un-necessary overhead? Is it better to simply call a PyObject_GetAttrString(My_object, "width")? Does it create un-necessary overhead to declare the object in the head of the function as so? > My_object *m_obj; w = m_obj->width; Also, would I need to --> Py_XDECREF(m_obj) at the end of the function if this is a "clean" way of doing it? Is there any performance concerns between the two? -- Bust0ut, Surgemcgee: Systems Engineer --- surgemcgee.com Django_Teamplate3d From python_capi at behnel.de Fri Nov 23 18:18:49 2012 From: python_capi at behnel.de (Stefan Behnel) Date: Fri, 23 Nov 2012 18:18:49 +0100 Subject: [capi-sig] Best practice for object attribute use in other objects... In-Reply-To: References: Message-ID: <50AFAFF9.8050008@behnel.de> Robert Steckroth, 23.11.2012 17:26: > Hey Gang, I am using object attributes from other objects in my module. Can > anyone tell me the best way to retrieve the object attributes without > un-necessary overhead? Is it better to simply call > a PyObject_GetAttrString(My_object, "width")? You should read the source. It will create an intermediate Python object for the attribute name. > Does it create un-necessary overhead to declare the object in the head of > the function as so? > > My_object *m_obj; > w = m_obj->width; If the field you are accessing is a C field in an extension type, then yes, that's the way to do it. This is a straight C pointer dereference that is way more efficient than a dict lookup of a Python attribute and the packing or unpacking of a Python object value that tends to go with it. > Also, would I need to --> Py_XDECREF(m_obj) at the end of the function if > this is a "clean" way of doing it? Depends on where you got the value from. If you own the reference, you have to decref it at the end, yes. > Is there any performance concerns between the two? Not unless your code is performance critical. If it is, you will prefer the direct pointer dereference. Otherwise, you don't have to care. In any case, you should first write your code, then benchmark it, then see if it's fast enough or if it needs improvements and/or optimisation. C code is not Python code. It's way harder to write and to get right. So a) avoid writing it in the first place and b) if you find that you really have to, write as little of it as possible and avoid premature optimisations on the way. I keep repeating myself, but there are tools that take care of all of the above for you automatically. Stefan From robertsteckroth at gmail.com Wed Nov 28 04:01:00 2012 From: robertsteckroth at gmail.com (Robert Steckroth) Date: Tue, 27 Nov 2012 22:01:00 -0500 Subject: [capi-sig] What's up with PyTypeObject and functions? Message-ID: Hey Gang, why is the below snippet for error checking not working? I wan't to be able to pass in different PyTypeObjects to the CHECK_TYPE function and check an object for that type. It works fine when I insert put &PyString_Type directly into the function like so --> if ( ! PyObject_TypeCheck(s_obj, &PyString_type) ) However, the CHECK_TYPE function will return -1 if I pass the PyTypeObject in, like at the bottom of this message. Why does PyObject_TypeCheck not work if PyTypeObject is first passed into a function? int CHECK_TYPE(PyObject *s_obj, PyTypeObject obj_type) { if ( ! s_obj ) return 1; if ( ! PyObject_TypeCheck(s_obj, &obj_type) ) { PyErr_Format(PyExc_TypeError, "[ %s ] object needs to be of type [ %s ]\n\nUsage: %s", PyString_AsString(PyObject_Repr(s_obj)), obj_type.tp_name, obj_type.tp_doc ); return -1; } return 1; } if ( !CHECK_TYPE(align, PyString_Type) ) return NULL; -- Bust0ut, Surgemcgee: Systems Engineer --- surgemcgee.com Django_Teamplate3d From mal at egenix.com Wed Nov 28 10:02:09 2012 From: mal at egenix.com (M.-A. Lemburg) Date: Wed, 28 Nov 2012 10:02:09 +0100 Subject: [capi-sig] What's up with PyTypeObject and functions? In-Reply-To: References: Message-ID: <50B5D311.6090807@egenix.com> On 28.11.2012 04:01, Robert Steckroth wrote: > Hey Gang, why is the below snippet for error checking not working? I wan't > to be able to pass in different PyTypeObjects > to the CHECK_TYPE function and check an object for that type. > It works fine when I insert put &PyString_Type directly into > the function like so --> > if ( ! PyObject_TypeCheck(s_obj, &PyString_type) ) > However, the CHECK_TYPE function will return -1 if I pass the PyTypeObject > in, like at the bottom of this message. > Why does PyObject_TypeCheck not work if PyTypeObject is first passed into a > function? > > > int CHECK_TYPE(PyObject *s_obj, PyTypeObject obj_type) { You need *obj_type here, since you need the pointer to the object, not the object itself. > if ( ! s_obj ) > return 1; > > if ( ! PyObject_TypeCheck(s_obj, &obj_type) ) { This needs to be obj_type. Your code will pass in the address of the PyTypeObject on the stack, which will not match any of the type objects in the Python type system. > PyErr_Format(PyExc_TypeError, "[ %s ] object needs to be of > type [ %s ]\n\nUsage: %s", PyString_AsString(PyObject_Repr(s_obj)), > obj_type.tp_name, obj_type.tp_doc ); This code leaks memory since the representation object is not freed. > return -1; } > > return 1; > > } > > if ( !CHECK_TYPE(align, PyString_Type) ) return NULL; This needs to the &PyString_Type -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Nov 28 2012) >>> Python Projects, Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/