Topics

[users] ctypes.Union access and use

Alex Earl
 

Does the same code work on C Python 2.7? If the same code works on C Python 2.7, then it is indeed a bug.


On Tue, Oct 16, 2018 at 1:21 PM <leonardopsantos@...> wrote:
Hello everybody! I'm trying to create a ctypes.Union derived class, but it isn't behaving like a c union. The following code fails:

import unittest
 
from ctypes import *
 
class foo(Union):
    __fields__ = [("t", c_uint), ("b", c_uint)]
 
class bar(Union):
    _fields_ = [("t", c_uint), ("b", c_uint)]
 
class test_ctypes_union(unittest.TestCase):
    def test_union(self):
        bart = foo()
        bart.t = 1
        bart.b = 2
 
        assert(bar.t == bar.b)
 
    def test_union_again(self):
        homer = bar()
        homer.t = 1
 
if __name__ == "__main__":
    unittest.main()

I'm using IronPython 2.7.9, tested on both 64 and 32-bit interpreters.

test_union fails with an AssertionError and test_union_again throws a SystemError: Object reference not set to an instance of an object. exception.

If I remove the line bart.b = 2 I also get a SystemError: Object reference not set to an instance of an object. exception.
 
Is this code supposed to work?
 
What's is the correct declaration for the fields member? _fields_ or __fields__ ? For classes derived from ctypes.Structure it seems that _fields_ should be the right way.
 
After reading ctypes module in version 2.7.2 can't create Union values, I still can't make this work.
 
Thanks!
 

Alex Earl
 

I get this when I try running it under C Python 2.7.15

======================================================================
FAIL: test_union (__main__.test_ctypes_union)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\USERNAME\Desktop\Debug\dummy.py", line 17, in test_union
    assert(bar.t == bar.b)
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=1)


On Tue, Oct 16, 2018 at 1:22 PM Alex Earl <slide.o.mix@...> wrote:
Does the same code work on C Python 2.7? If the same code works on C Python 2.7, then it is indeed a bug.

On Tue, Oct 16, 2018 at 1:21 PM <leonardopsantos@...> wrote:
Hello everybody! I'm trying to create a ctypes.Union derived class, but it isn't behaving like a c union. The following code fails:

import unittest
 
from ctypes import *
 
class foo(Union):
    __fields__ = [("t", c_uint), ("b", c_uint)]
 
class bar(Union):
    _fields_ = [("t", c_uint), ("b", c_uint)]
 
class test_ctypes_union(unittest.TestCase):
    def test_union(self):
        bart = foo()
        bart.t = 1
        bart.b = 2
 
        assert(bar.t == bar.b)
 
    def test_union_again(self):
        homer = bar()
        homer.t = 1
 
if __name__ == "__main__":
    unittest.main()

I'm using IronPython 2.7.9, tested on both 64 and 32-bit interpreters.

test_union fails with an AssertionError and test_union_again throws a SystemError: Object reference not set to an instance of an object. exception.

If I remove the line bart.b = 2 I also get a SystemError: Object reference not set to an instance of an object. exception.
 
Is this code supposed to work?
 
What's is the correct declaration for the fields member? _fields_ or __fields__ ? For classes derived from ctypes.Structure it seems that _fields_ should be the right way.
 
After reading ctypes module in version 2.7.2 can't create Union values, I still can't make this work.
 
Thanks!
 

Alex Earl
 

I forgot to add this in my replies previously. The proper way to define a Union is using the _fields_ (single underscore) definition.


On Tue, Oct 16, 2018 at 1:30 PM Alex Earl <slide.o.mix@...> wrote:
I get this when I try running it under C Python 2.7.15

======================================================================
FAIL: test_union (__main__.test_ctypes_union)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\USERNAME\Desktop\Debug\dummy.py", line 17, in test_union
    assert(bar.t == bar.b)
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=1)

On Tue, Oct 16, 2018 at 1:22 PM Alex Earl <slide.o.mix@...> wrote:
Does the same code work on C Python 2.7? If the same code works on C Python 2.7, then it is indeed a bug.

On Tue, Oct 16, 2018 at 1:21 PM <leonardopsantos@...> wrote:
Hello everybody! I'm trying to create a ctypes.Union derived class, but it isn't behaving like a c union. The following code fails:

import unittest
 
from ctypes import *
 
class foo(Union):
    __fields__ = [("t", c_uint), ("b", c_uint)]
 
class bar(Union):
    _fields_ = [("t", c_uint), ("b", c_uint)]
 
class test_ctypes_union(unittest.TestCase):
    def test_union(self):
        bart = foo()
        bart.t = 1
        bart.b = 2
 
        assert(bar.t == bar.b)
 
    def test_union_again(self):
        homer = bar()
        homer.t = 1
 
if __name__ == "__main__":
    unittest.main()

I'm using IronPython 2.7.9, tested on both 64 and 32-bit interpreters.

test_union fails with an AssertionError and test_union_again throws a SystemError: Object reference not set to an instance of an object. exception.

If I remove the line bart.b = 2 I also get a SystemError: Object reference not set to an instance of an object. exception.
 
Is this code supposed to work?
 
What's is the correct declaration for the fields member? _fields_ or __fields__ ? For classes derived from ctypes.Structure it seems that _fields_ should be the right way.
 
After reading ctypes module in version 2.7.2 can't create Union values, I still can't make this work.
 
Thanks!
 

leonardopsantos@...
 

The test_union method was wrong anyways.  The following code works on CPython 2.7.8 but fails on IronPython 2.7.9:

import unittest
 
from ctypes import *
 
class bar(Union):
    _fields_ = [("t", c_uint), ("b", c_uint)]
 
class test_ctypes_union(unittest.TestCase):
    def test_union_again(self):
        homer = bar()
        homer.t = 1
        assert(homer.t == homer.b)

 

if __name__ == "__main__":
unittest.main()


Thanks

Alex Earl
 

Please file an issue on GitHub in https://github.com/IronLanguages/ironpython2 with the reproduction code.


On Tue, Oct 16, 2018 at 1:42 PM <leonardopsantos@...> wrote:

The test_union method was wrong anyways.  The following code works on CPython 2.7.8 but fails on IronPython 2.7.9:

import unittest
 
from ctypes import *
 
class bar(Union):
    _fields_ = [("t", c_uint), ("b", c_uint)]
 
class test_ctypes_union(unittest.TestCase):
    def test_union_again(self):
        homer = bar()
        homer.t = 1
        assert(homer.t == homer.b)

 

if __name__ == "__main__":
unittest.main()


Thanks