[PyQt] A gobal variable problem in sip

D.Y Feng yyfeng88625 at gmail.com
Wed Oct 24 14:11:52 BST 2012


Thanks for reply.But the problem it's not this point....

Let's see.
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 kkk=Hello::test();
>
> Hello::Hello (QObject *parent)
> {
> }
>

test.py

> from hello import Hello
> Hello.test()
>

Here is the result:

> $ python -v test.py
> # installing zipimport hook
> import zipimport # builtin
> ....
> ......stuff ouput.....
> ....
> import encodings.utf_8 # precompiled from
> /usr/lib/python2.7/encodings/utf_8.pyc
> Python 2.7.3 (default, Aug  1 2012, 05:14:39)
> [GCC 4.6.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> dlopen("/home/feng/fun/lab/pyqt/hello.so", 2);          <----------Here
> loal the hello.so
> 0           <----------    const int kkk=Hello::test(); Now the i in
> test() such be 1
> dlopen("/usr/lib/python2.7/dist-packages/sip.so", 2);
> # /usr/lib/python2.7/atexit.pyc matches /usr/lib/python2.7/atexit.py
> import atexit # precompiled from /usr/lib/python2.7/atexit.pyc
> import sip # dynamically loaded from
> /usr/lib/python2.7/dist-packages/sip.so
> import PyQt4 # directory /usr/lib/python2.7/dist-packages/PyQt4
> # /usr/lib/python2.7/dist-packages/PyQt4/__init__.pyc matches
> /usr/lib/python2.7/dist-packages/PyQt4/__init__.py
> import PyQt4 # precompiled from
> /usr/lib/python2.7/dist-packages/PyQt4/__init__.pyc
> dlopen("/usr/lib/python2.7/dist-packages/PyQt4/QtCore.so", 2);
> import PyQt4.QtCore # dynamically loaded from
> /usr/lib/python2.7/dist-packages/PyQt4/QtCore.so
> import hello # dynamically loaded from /home/feng/fun/lab/pyqt/hello.so
> 0 <------------ the python *Hello.test()* ,the i in test() is still 0
>

Let's print the *Hello::test()*

>     static int test(){
>         static int i=0;
>         printf("%p\n",&Hello::test);
>         return 0;
>     }
>

run again:

>  $ python  test.py
> 0x7f8fc467bfe0        <----the *Hello::test() is *difference
> 0x7f8fc488053f
>

When the pothon load the module

dlopen("/home/feng/fun/lab/pyqt/hello.so", 2);
> ......
> import hello # dynamically loaded from /home/feng/fun/lab/pyqt/hello.so
> <----the *Hello::test() is *difference


I think it's cause by twice load the Hello.

Thank you





On 24 October 2012 19:56, <Mathias.Born at gmx.de> wrote:

> On 24.10.2012, 00:11:13 D.Y Feng wrote:
> > 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
>
> Now, I haven't tried that myself yet. On the other hand, I don't see any
> problem.
>
> > Breakpoint 1, Hello::test1 () at hello.h:8
> > 8           static int test1(){
> > (gdb) p i
> > $1 = 0
> > (gdb) p &i
> > $2 = (const int *) 0x7ffff60300d0
>
> This displays the address of your global variable "const int i".
>
> > (gdb) c
> > Continuing.
> > 0
>
> > Breakpoint 1, Hello::test1 () at main.h:8
> > 8           static int test1(){
> > (gdb) p &i
> > $3 = (const int *) 0x7ffff60300d0
>
> Again, address of your global variable "const int i".
>
> > (gdb) n
> > 10              qDebug()<<i++;
> > (gdb) p &i
> > $4 = (int *) 0x7ffff62355c0       <---What happen here?
>
> This shows the address of your local static variable "static int i",
> which ought to be different.
> The names of these objects, which are both "i", come into existence at
> different times. While this is perfectly legal C++, it obviously
> obfuscates your program. Just naming them differently should already
> help a lot.
>
> In summary, I don't think this is an issue with PyQt. But let me quote
> Bjarne Stroustrup: "C makes it easy to shoot yourself in the foot; C++
> makes it harder, but when you do it blows your whole leg off."
>
> Best Regards,
> Mathias Born
>
>
>


-- 


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/55f9e515/attachment.html>


More information about the PyQt mailing list