Skip to content

CPython: Fix Union bugs #1917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ RUN(NAME enum_06 LABELS cpython llvm c)
RUN(NAME enum_07 IMPORT_PATH ..
LABELS cpython llvm c)
RUN(NAME union_01 LABELS cpython llvm c)
RUN(NAME union_02 LABELS llvm c)
RUN(NAME union_02 LABELS cpython llvm c)
RUN(NAME union_03 LABELS cpython llvm c)
RUN(NAME union_04 IMPORT_PATH ..
LABELS cpython llvm c)
Expand Down
6 changes: 3 additions & 3 deletions integration_tests/union_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class C:
@ccall
@union
class D(Union):
a: A = A()
b: B = B()
c: C = C()
a: A = A(0, 3.0)
b: B = B(i64(0), 2.0)
c: C = C(i64(0), 0.0, 1.0)

def test_struct_union():
d: D = D()
Expand Down
21 changes: 17 additions & 4 deletions src/runtime/lpython/lpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import platform
from dataclasses import dataclass as py_dataclass, is_dataclass as py_is_dataclass


# TODO: this does not seem to restrict other imports
__slots__ = ["i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64", "c32", "c64", "CPtr",
"overload", "ccall", "TypeVar", "pointer", "c_p_pointer", "Pointer",
Expand Down Expand Up @@ -89,10 +90,19 @@ def __init__(self, type, dims):
Const = ConstType("Const")
Callable = Type("Callable")
Allocatable = Type("Allocatable")
Union = ctypes.Union
Pointer = PointerType("Pointer")


class Union:
def __init__(self):
pass

def __setattr__(self, name: str, value):
self.__dict__[name] = value

def __getattr__(self, name: str):
return self.__dict__[name]

class Intent:
def __init__(self, type):
self._type = type
Expand Down Expand Up @@ -381,7 +391,6 @@ def convert_to_ctypes_Union(f):
for name in f.__annotations__:
ltype_ = f.__annotations__[name]
fields.append((name, convert_type_to_ctype(ltype_)))

f._fields_ = fields
f.__annotations__ = {}

Expand Down Expand Up @@ -467,12 +476,16 @@ def inner(fn):

def union(f):
fields = []
fa = {}
for name in f.__annotations__:
ltype_ = f.__annotations__[name]
fields.append((name, convert_type_to_ctype(ltype_)))
ltype_ = convert_type_to_ctype(ltype_)
fa[name] = ltype_

fields.append((name, ltype_))

f._fields_ = fields
f.__annotations__ = {}
f.__annotations__ = fa
return f

def pointer(x, type_=None):
Expand Down