extracting null pointer address from PyCObject with ctypes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gordon Allott

    extracting null pointer address from PyCObject with ctypes

    Hello :)

    The result of various incompatibiliti es has left me needing to somehow
    extract the address that a null pointer is pointing to with the null
    pointer being exposed to python via PyCObject_FromV oidPtr

    the code that creates the PyCObject is as follows:
    tmp = PyCObject_FromV oidPtr (info.info.x11. display, NULL);
    PyDict_SetItemS tring (dict, "display", tmp);
    Py_DECREF (tmp);

    which is exposed to python via a dictionary (the 'display' key). python
    identifies that its a PyCObject but doesn't give any way to expose the
    functionality. Essentially I am after the address that the void pointer
    'info.info.x11. display' points to (as a long type)

    As far as I can tell ctypes will only expose the pyObject type to me and
    not actually let me deal with the data I am after, but being rather new
    to ctypes I'm not sure weather this is correct.

    --
    Gord Allott (gordallott@gma il.com)


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFI7y2SlVw 7T9GIvRsRArcbAK CjmewtdkEiahPXT HhhCUMy9D7NEgCf YTIQ
    dE7a69MTHmR/3WUUSCzoSmc=
    =Outo
    -----END PGP SIGNATURE-----

  • Aaron \Castironpi\ Brady

    #2
    Re: extracting null pointer address from PyCObject with ctypes

    On Oct 10, 5:24 am, Gordon Allott <gordall...@gma il.comwrote:
    Hello :)
    >
    The result of various incompatibiliti es has left me needing to somehow
    extract the address that a null pointer is pointing to with the null
    pointer being exposed to python via PyCObject_FromV oidPtr
    >
    the code that creates the PyCObject is as follows:
        tmp = PyCObject_FromV oidPtr (info.info.x11. display, NULL);
        PyDict_SetItemS tring (dict, "display", tmp);
        Py_DECREF (tmp);
    >
    Did you try:

    tmp= PyLong_FromLong ( ( long ) info.info.x11.d isplay );
    PyDict_SetItemS tring (dict, "display", tmp);
    Py_DECREF (tmp);

    Or also try:

    PyCObject_AsVoi dPtr( tmp );

    Comment

    • Gordon Allott

      #3
      Re: extracting null pointer address from PyCObject with ctypes

      Aaron "Castironpi " Brady wrote:
      Did you try:

      tmp= PyLong_FromLong ( ( long ) info.info.x11.d isplay );
      PyDict_SetItemS tring (dict, "display", tmp);
      Py_DECREF (tmp);

      Or also try:

      PyCObject_AsVoi dPtr( tmp );
      --
      http://mail.python.org/mailman/listinfo/python-list
      the problem is that I can't edit the C code - well I can and might
      submit a patch to the project but I also need a solution that works from
      the python side of things.


      --
      Gord Allott (gordallott@gma il.com)


      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.6 (GNU/Linux)
      Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

      iD8DBQFI74sQlVw 7T9GIvRsRAtw+AJ 4i0lte6DnzeJ6Nm +BbaYGUCDDEIQCe IW4m
      rlzxbYBLYI4j3hF ceifTuLM=
      =nfIm
      -----END PGP SIGNATURE-----

      Comment

      • Aaron \Castironpi\ Brady

        #4
        Re: extracting null pointer address from PyCObject with ctypes

        On Oct 10, 12:04 pm, Gordon Allott <gordall...@gma il.comwrote:
        Aaron "Castironpi " Brady wrote:
        Did you try:
        >
        tmp= PyLong_FromLong ( ( long ) info.info.x11.d isplay );
        PyDict_SetItemS tring (dict, "display", tmp);
        Py_DECREF (tmp);
        >
        Or also try:
        >
        PyCObject_AsVoi dPtr( tmp );
        --
        http://mail.python.org/mailman/listinfo/python-list
        >
        the problem is that I can't edit the C code - well I can and might
        submit a patch to the project but I also need a solution that works from
        the python side of things.
        >
        I see. If I understand, you have a PyCObject in a dictionary.

        Look at the 'ctypes' module and try calling PyCObject_AsVoi dPtr. Its
        return type should be 'c_void_p', and you can use 'result.value' to
        get the original pointer.

        Comment

        • Gordon Allott

          #5
          Re: extracting null pointer address from PyCObject with ctypes

          Aaron "Castironpi " Brady wrote:
          I see. If I understand, you have a PyCObject in a dictionary.

          Look at the 'ctypes' module and try calling PyCObject_AsVoi dPtr. Its
          return type should be 'c_void_p', and you can use 'result.value' to
          get the original pointer.
          --
          http://mail.python.org/mailman/listinfo/python-list
          I have a hard time following that, if using ctypes you used PyDLL to
          call PyCObject_AsVoi dPtr on the PyCObject I already have surely it would
          give me back a pointer (void for sake of simplicity) but it would be a
          pointer to a new PyCObject and thus calling result.value on it would
          only return the memory address of the new PyCObject?
          --
          Gord Allott (gordallott@gma il.com)


          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.4.6 (GNU/Linux)
          Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

          iD8DBQFI78YSlVw 7T9GIvRsRAm3eAJ 0c9k7HFhXbEcq3O 3jGYlMsziKIHACf S09u
          5QTYKVR/SH6fPqToXhK9WVA =
          =uL6v
          -----END PGP SIGNATURE-----

          Comment

          • Aaron \Castironpi\ Brady

            #6
            Re: extracting null pointer address from PyCObject with ctypes

            On Oct 10, 4:16 pm, Gordon Allott <gordall...@gma il.comwrote:
            Aaron "Castironpi " Brady wrote:
            I see.  If I understand, you have a PyCObject in a dictionary.
            >
            Look at the 'ctypes' module and try calling PyCObject_AsVoi dPtr.  Its
            return type should be 'c_void_p', and you can use 'result.value' to
            get the original pointer.
            --
            http://mail.python.org/mailman/listinfo/python-list
            >
            I have a hard time following that, if using ctypes you used PyDLL to
            call PyCObject_AsVoi dPtr on the PyCObject I already have surely it would
             give me back a pointer (void for sake of simplicity) but it would be a
            pointer to a new PyCObject and thus calling result.value on it would
            only return the memory address of the new PyCObject?
            --
            Gord Allott (gordall...@gma il.com)
            >
             signature.asc
            < 1KViewDownload

            Yes, well said. But no, not true, not necessarily. You can choose/
            change return types with your code. If the call is defined already
            and you can't change the return, just define a new one that returns
            long.

            Comment

            • Gordon Allott

              #7
              Re: extracting null pointer address from PyCObject with ctypes

              Aaron "Castironpi " Brady wrote:
              Yes, well said. But no, not true, not necessarily. You can choose/
              change return types with your code. If the call is defined already
              and you can't change the return, just define a new one that returns
              long.
              --
              http://mail.python.org/mailman/listinfo/python-list
              the problem is that the pointer or long or whatever it is thats returned
              won't be the data I am after. the code for PyCObject_FromV oidPtr is as
              follows:

              PyObject *
              PyCObject_FromV oidPtr(void *cobj, void (*destr)(void *))
              {
              PyCObject *self;

              self = PyObject_NEW(Py CObject, &PyCObject_Type );
              if (self == NULL)
              return NULL;
              self->cobject=cobj ;
              self->destructor=des tr;
              self->desc=NULL;

              return (PyObject *)self;
              }


              it obviously creates a new PyObject and returns that, which has already
              happened once (the address I am after is passed to python via
              PyCObject_FromV oidPtr(adress_i _am_after, NULL), doing that puts the
              address I am after into the .cobject attribute of a new pyobject
              structure and passes that to the python script via the 'display' key in
              a dictionary.

              If I were to then take the pycobject in this display key and pass it via
              ctypes into PyCObject_FromV oidPtr it would simply create a new pycobject
              and put a pointer to the old pycobject in the new pycobject's .cobject
              attribute. it just means that I am getting further and further away from
              where I want to be surely? if I were to take the current pointer at this
              stage, to get to the address I actually want in C it would have to
              follow something along the lines of
              long address_i_want = (long)(new_pyco bj->cobject->cobject);

              What would be great is if there is some easy simple way of accessing the
              .cobject attribute of the first pycobject thats passed via the
              dictionary to python.

              --
              Gord Allott (gordallott@gma il.com)


              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.4.6 (GNU/Linux)
              Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

              iD8DBQFI7/p9lVw7T9GIvRsRA r2rAJ9gxOUnLb8o lXN6siH88ZLMwv2 erwCfWD1q
              +Y5jmOl0PrUzLTv mc3nMK/U=
              =rL5l
              -----END PGP SIGNATURE-----

              Comment

              • Aaron \Castironpi\ Brady

                #8
                Re: extracting null pointer address from PyCObject with ctypes

                On Oct 10, 7:59 pm, Gordon Allott <gordall...@gma il.comwrote:
                Aaron "Castironpi " Brady wrote:
                Yes, well said.  But no, not true, not necessarily.  You can choose/
                change return types with your code.  If the call is defined already
                and you can't change the return, just define a new one that returns
                long.
                --
                http://mail.python.org/mailman/listinfo/python-list
                >
                the problem is that the pointer or long or whatever it is thats returned
                won't be the data I am after. the code for PyCObject_FromV oidPtr is as
                follows:
                >
                PyObject *
                PyCObject_FromV oidPtr(void *cobj, void (*destr)(void *))
                {
                    PyCObject *self;
                >
                    self = PyObject_NEW(Py CObject, &PyCObject_Type );
                    if (self == NULL)
                        return NULL;
                    self->cobject=cobj ;
                    self->destructor=des tr;
                    self->desc=NULL;
                >
                    return (PyObject *)self;
                >
                }
                >
                it obviously creates a new PyObject and returns that, which has already
                happened once (the address I am after is passed to python via
                PyCObject_FromV oidPtr(adress_i _am_after, NULL), doing that puts the
                address I am after into the .cobject attribute of a new pyobject
                structure and passes that to the python script via the 'display' key in
                a dictionary.
                >
                If I were to then take the pycobject in this display key and pass it via
                ctypes into PyCObject_FromV oidPtr it would simply create a new pycobject
                 and put a pointer to the old pycobject in the new pycobject's .cobject
                attribute. it just means that I am getting further and further away from
                where I want to be surely? if I were to take the current pointer at this
                stage, to get to the address I actually want in C it would have to
                follow something along the lines of
                long address_i_want = (long)(new_pyco bj->cobject->cobject);
                >
                What would be great is if there is some easy simple way of accessing the
                .cobject attribute of the first pycobject thats passed via the
                dictionary to python.
                >
                --
                Gord Allott (gordall...@gma il.com)
                >
                 signature.asc
                < 1KViewDownload
                You are hard to follow. There is the 'cast' function, which I've had
                some success with, even in adding pointers and offsets. It took a
                look at the code for it though, and calling an undocumented version of
                it. I can post that later if you don't have luck the same. You can
                write extension modules to do that as well, and there's always a
                Google search which personally I forget half the time too. Last, you
                haven't mentioned an attempt with PyCObject_AsVoi dPtr yet:

                void* PyCObject_AsVoi dPtr(PyObject* self)
                Return the object void * that the PyCObject self was created with.

                Where does that get you?

                Comment

                • Gordon Allott

                  #9
                  Re: extracting null pointer address from PyCObject with ctypes

                  Aaron "Castironpi " Brady wrote:

                  You are hard to follow. There is the 'cast' function, which I've had
                  some success with, even in adding pointers and offsets. It took a
                  look at the code for it though, and calling an undocumented version of
                  it. I can post that later if you don't have luck the same. You can
                  write extension modules to do that as well, and there's always a
                  Google search which personally I forget half the time too. Last, you
                  haven't mentioned an attempt with PyCObject_AsVoi dPtr yet:

                  void* PyCObject_AsVoi dPtr(PyObject* self)
                  Return the object void * that the PyCObject self was created with.

                  Where does that get you?
                  --
                  http://mail.python.org/mailman/listinfo/python-list
                  sorry yes you were right, I was reading PyCObject_AsVoi dPtr as
                  PyCObject_FromV oidPtr :)

                  using AsVoidPtr is a little confusing, this is the code I am using:
                  display = pygame.display. get_wm_info()['display']
                  pyobj = py_object(displ ay)
                  ref = pointer(pyobj)

                  print pythonapi.PyCOb ject_AsVoidPtr( ref)

                  it produces the following traceback:
                  Traceback (most recent call last):
                  File "pygametest.py" , line 125, in <module>
                  app = PyGameOGREApp()
                  File "pygametest.py" , line 33, in __init__
                  self._createWin dow(width, height, fullscreen)
                  File "pygametest.py" , line 64, in _createWindow
                  print pythonapi.PyCOb ject_AsVoidPtr( ref)
                  TypeError: PyCObject_AsVoi dPtr with non-C-object

                  - I think that's because its a pointer to the ctypes py_object() rather
                  than the PyCObject we are dealing with but I have no idea how to create
                  a pointer to that.

                  --
                  Gord Allott (gordallott@gma il.com)


                  -----BEGIN PGP SIGNATURE-----
                  Version: GnuPG v1.4.6 (GNU/Linux)
                  Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                  iD8DBQFI8CN1lVw 7T9GIvRsRAuo0AK CSzSQgnnRPanYLS tdBLGGIA669rQCe Jsm7
                  G/Dnr2uDs8CaZJ988 UnF2kU=
                  =/HxT
                  -----END PGP SIGNATURE-----

                  Comment

                  • Aaron \Castironpi\ Brady

                    #10
                    Re: extracting null pointer address from PyCObject with ctypes

                    On Oct 10, 10:54 pm, Gordon Allott <gordall...@gma il.comwrote:
                    Aaron "Castironpi " Brady wrote:
                    snip
                     Last, you
                    haven't mentioned an attempt with PyCObject_AsVoi dPtr yet:
                    >
                    void* PyCObject_AsVoi dPtr(PyObject* self)
                        Return the object void * that the PyCObject self was created with.
                    >>
                    sorry yes you were right, I was reading PyCObject_AsVoi dPtr as
                    PyCObject_FromV oidPtr :)
                    >
                    using AsVoidPtr is a little confusing, this is the code I am using:
                            display = pygame.display. get_wm_info()['display']
                            pyobj = py_object(displ ay)
                            ref = pointer(pyobj)
                    >
                            print pythonapi.PyCOb ject_AsVoidPtr( ref)
                    >
                    it produces the following traceback:
                    Traceback (most recent call last):
                      File "pygametest.py" , line 125, in <module>
                        app = PyGameOGREApp()
                      File "pygametest.py" , line 33, in __init__
                        self._createWin dow(width, height, fullscreen)
                      File "pygametest.py" , line 64, in _createWindow
                        print pythonapi.PyCOb ject_AsVoidPtr( ref)
                    TypeError: PyCObject_AsVoi dPtr with non-C-object
                    >
                    - I think that's because its a pointer to the ctypes py_object() rather
                    than the PyCObject we are dealing with but I have no idea how to create
                    a pointer to that.
                    >
                    My pygame install just returns an integer in get_wm_info. Take a
                    look:
                    >>pygame.displa y.get_wm_info()
                    {'window': 1180066, 'hglrc': 0}
                    >>pygame.displa y.get_wm_info()['window']
                    1180066
                    >>ctypes.c_void _p( _ )
                    c_void_p(118006 6)

                    You're suggesting yours looks like this:
                    >>pygame.displa y.get_wm_info()
                    { ... 'display': ctypes.py_objec t( 1180066 ), ... }

                    What does type( display ) give you?

                    Comment

                    • Gordon Allott

                      #11
                      Re: extracting null pointer address from PyCObject with ctypes

                      Aaron "Castironpi " Brady wrote:

                      My pygame install just returns an integer in get_wm_info. Take a
                      look:
                      >>>pygame.displ ay.get_wm_info( )
                      {'window': 1180066, 'hglrc': 0}
                      >>>pygame.displ ay.get_wm_info( )['window']
                      1180066
                      >>>ctypes.c_voi d_p( _ )
                      c_void_p(118006 6)

                      You're suggesting yours looks like this:
                      >>>pygame.displ ay.get_wm_info( )
                      { ... 'display': ctypes.py_objec t( 1180066 ), ... }

                      What does type( display ) give you?
                      --
                      http://mail.python.org/mailman/listinfo/python-list
                      yes its different on windows and linux, windows only has a few items
                      where linux has many more. 'window' is just the window 'id' at any rate
                      which is not the data I am after (which is internally an address to an
                      xlib structure)
                      this is what pygame.display. get_wm_info() returns on linux:
                      {'fswindow': 31457283, 'wmwindow': 31457284, 'window': 31457294,
                      'lock_func': <PyCObject object at 0x89dfa70>, 'unlock_func': <PyCObject
                      object at 0x89dfa88>, 'display': <PyCObject object at 0x89dfa58>}

                      note how the display object is a PyCObject, thats got the address I want
                      inside it.


                      --
                      Gord Allott (gordallott@gma il.com)


                      -----BEGIN PGP SIGNATURE-----
                      Version: GnuPG v1.4.6 (GNU/Linux)
                      Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                      iD8DBQFI8LwNlVw 7T9GIvRsRAixJAJ wIe97b5eJ3tUrfo lqZ7D15DCsV8ACg p4UL
                      idODQg3cBrUXy5B xEezExtM=
                      =Jvca
                      -----END PGP SIGNATURE-----

                      Comment

                      • Aaron \Castironpi\ Brady

                        #12
                        Re: extracting null pointer address from PyCObject with ctypes

                        On Oct 11, 9:45 am, Gordon Allott <gordall...@gma il.comwrote:
                        Aaron "Castironpi " Brady wrote:
                        >
                        My pygame install just returns an integer in get_wm_info.  Take a
                        look:
                        >
                        >>pygame.displa y.get_wm_info()
                        {'window': 1180066, 'hglrc': 0}
                        >>pygame.displa y.get_wm_info()['window']
                        1180066
                        >>ctypes.c_void _p( _ )
                        c_void_p(118006 6)
                        >
                        You're suggesting yours looks like this:
                        >
                        >>pygame.displa y.get_wm_info()
                        { ... 'display': ctypes.py_objec t( 1180066 ), ... }
                        >
                        What does type( display ) give you?
                        --
                        http://mail.python.org/mailman/listinfo/python-list
                        >
                        yes its different on windows and linux, windows only has a few items
                        where linux has many more. 'window' is just the window 'id'  at any rate
                        which is not the data I am after (which is internally an address to an
                        xlib structure)
                        this is what pygame.display. get_wm_info() returns on linux:
                        {'fswindow': 31457283, 'wmwindow': 31457284, 'window': 31457294,
                        'lock_func': <PyCObject object at 0x89dfa70>, 'unlock_func': <PyCObject
                        object at 0x89dfa88>, 'display': <PyCObject object at 0x89dfa58>}
                        >
                        note how the display object is a PyCObject, thats got the address I want
                         inside it.
                        >
                        What does print pythonapi.PyCOb ject_AsVoidPtr( display) give you?

                        Comment

                        • Gordon Allott

                          #13
                          Re: extracting null pointer address from PyCObject with ctypes

                          Aaron "Castironpi " Brady wrote:

                          What does print pythonapi.PyCOb ject_AsVoidPtr( display) give you?
                          --
                          http://mail.python.org/mailman/listinfo/python-list
                          Traceback (most recent call last):
                          File "pygametest.py" , line 125, in <module>
                          app = PyGameOGREApp()
                          File "pygametest.py" , line 33, in __init__
                          self._createWin dow(width, height, fullscreen)
                          File "pygametest.py" , line 65, in _createWindow
                          print pythonapi.PyCOb ject_AsVoidPtr( display)
                          ctypes.Argument Error: argument 1: <type 'exceptions.Typ eError'>: Don't
                          know how to convert parameter 1


                          --
                          Gord Allott (gordallott@gma il.com)


                          -----BEGIN PGP SIGNATURE-----
                          Version: GnuPG v1.4.6 (GNU/Linux)
                          Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                          iD8DBQFI8PeLlVw 7T9GIvRsRAhJyAJ 9BtoEeYhpXOPe6j tcFYQkfccdrTwCc CLbj
                          FIJ4SeuH1lwP+F5 IV3JoV+A=
                          =uwxx
                          -----END PGP SIGNATURE-----

                          Comment

                          • Aaron \Castironpi\ Brady

                            #14
                            Re: extracting null pointer address from PyCObject with ctypes

                            On Oct 11, 1:59 pm, Gordon Allott <gordall...@gma il.comwrote:
                            Aaron "Castironpi " Brady wrote:
                            >
                            What does print pythonapi.PyCOb ject_AsVoidPtr( display) give you?
                            --
                            http://mail.python.org/mailman/listinfo/python-list
                            >
                            Traceback (most recent call last):
                              File "pygametest.py" , line 125, in <module>
                                app = PyGameOGREApp()
                              File "pygametest.py" , line 33, in __init__
                                self._createWin dow(width, height, fullscreen)
                              File "pygametest.py" , line 65, in _createWindow
                                print pythonapi.PyCOb ject_AsVoidPtr( display)
                            ctypes.Argument Error: argument 1: <type 'exceptions.Typ eError'>: Don't
                            know how to convert parameter 1
                            >
                            --
                            Gord Allott (gordall...@gma il.com)
                            >
                             signature.asc
                            < 1KViewDownload
                            If anyone knows this better, feel free to step in.

                            Put this before the call:

                            ctypes.pythonap i.PyCObject_AsV oidPtr.restype = ctypes.c_void_p
                            ctypes.pythonap i.PyCObject_AsV oidPtr.argtypes = [ ctypes.py_objec t ]

                            The reason is that this is the default signature, which is wrong:
                            >>ctypes.python api.PyCObject_A sVoidPtr.restyp e
                            <class 'ctypes.c_long' >
                            >>ctypes.python api.PyCObject_A sVoidPtr.argtyp es
                            >>>
                            There are other ways to prototype it if you like, too.

                            Comment

                            • Gordon Allott

                              #15
                              Re: extracting null pointer address from PyCObject with ctypes

                              Aaron "Castironpi " Brady wrote:
                              >>>ctypes.pytho napi.PyCObject_ AsVoidPtr.resty pe
                              <class 'ctypes.c_long' >
                              >>>ctypes.pytho napi.PyCObject_ AsVoidPtr.argty pes
                              >>>>
                              thank you so much, this works perfectly :)


                              --
                              Gord Allott (gordallott@gma il.com)


                              -----BEGIN PGP SIGNATURE-----
                              Version: GnuPG v1.4.6 (GNU/Linux)
                              Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                              iD8DBQFI8QEJlVw 7T9GIvRsRAuK2AJ 9DmCWrRbpirVX34 12i5qg6rQpOYQCf RDmw
                              eBbJD28vTEPZYch 0jz2f8hM=
                              =yEi0
                              -----END PGP SIGNATURE-----

                              Comment

                              Working...