[PyQt] A gobal variable problem in sip

D.Y Feng yyfeng88625 at gmail.com
Wed Oct 24 08:52:12 BST 2012


I have already sent the same email to the mail list.But I can't find the
mail in my inbox.I do not know whether the message is sent successfully.So
I send it again.

I meet a problem in my code:

hello.h:

#include<QtCore>
>
> class Hello : public QObject{
>     Q_OBJECT
> public:
>     Hello(QObject *parent = 0);
>     static int test(){
>         static int i=0;
>         qDebug()<<i++;
>         return 0;
>     }
> };
>

hello.cpp:

> #include "hello.h"
>
> const int i=Hello::test();
>
> Hello::Hello (QObject *parent)
> {
> }
>

hello.sip

%Module hello
>
> %Import QtCore/QtCoremod.sip
>
>
> class Hello : public QObject {
>
> %TypeHeaderCode
> #include "hello.h"
> %End
>
> public:
>      Hello(QObject *parent = 0);
>      static int test();
> };
>

I use the* const int i=Hello::test();* to invoke test() before
*main*function.It's uesful in factory
pattern when you want to auto  register driver.

the python test code

> import hello
> hello.Hello.test1()
>

It seems *static int i*  in *test() *changed after *import hello*

Breakpoint 1, Hello::test1 () at hello.h:8
> 8           static int test1(){
> (gdb) p i
> $1 = 0
> (gdb) p &i
> $2 = (const int *) 0x7ffff60300d0
> (gdb) c
> Continuing.
> 0
>
> Breakpoint 1, Hello::test1 () at main.h:8
> 8           static int test1(){
> (gdb) p &i
> $3 = (const int *) 0x7ffff60300d0
> (gdb) n
> 10              qDebug()<<i++;
> (gdb) p &i
> $4 = (int *) 0x7ffff62355c0       <---What happen here?
>

in siphelloHello.cpp:

> extern "C" {static PyObject *meth_Hello_test1(PyObject *, PyObject *);}
> static PyObject *meth_Hello_test1(PyObject *, PyObject *sipArgs)
> {
>     PyObject *sipParseErr = NULL;
>
>     {
>         if (sipParseArgs(&sipParseErr, sipArgs, ""))
>         {
>             int sipRes;
>
>             Py_BEGIN_ALLOW_THREADS
>             sipRes = Hello::test1();
>             Py_END_ALLOW_THREADS
>
>             return SIPLong_FromLong(sipRes);
>         }
>     }
>
>     /* Raise an exception if the arguments couldn't be parsed. */
>     sipNoMethod(sipParseErr, sipName_Hello, sipName_test1, NULL);
>
>     return NULL;
> }
>

the *Hello* object in the siphelloHello.cpp is different from
*const int i=Hello::test();*


Thx for reading.


-- 


DY.Feng(叶毅锋)
yyfeng88625 at twitter
Department of Applied Mathematics
Guangzhou University,China
dyfeng at stu.gzhu.edu.cn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20121024/7a2e84ff/attachment.html>


More information about the PyQt mailing list