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/
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
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
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
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
nirmoy das <nirmoy.aiemd(a)gmail.com> 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);
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
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 <nirmoy.aiemd(a)gmail.com> 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(a)python.org
> http://mail.python.org/mailman/listinfo/capi-sig
--
Francis Bolduc, B.Sc.
CM-Labs Simulation Inc.
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.
can any one give a example .It gonna help me a lot
On Wed, Nov 21, 2012 at 5:08 PM, nirmoy das <nirmoy.aiemd(a)gmail.com> wrote:
> Can anyone tell me how to use expression in PyObject_CallObject()
> as argument of object
>
> ex:column_family('keyspace', 'CF', comparator_type=comparator)
>
>
>