Skip to content

Commit 1b162b9

Browse files
committed
Override ctypes.c_void_p.__eq__ to compare pointers correctly
1 parent fe41a7b commit 1b162b9

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

integration_tests/bindc_01.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
def test_issue_1781():
99
p: CPtr = empty_c_void_p()
1010
assert p == empty_c_void_p()
11+
assert not (p != empty_c_void_p())
1112

1213

1314
test_issue_1781()

src/runtime/lpython/lpython.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,15 @@ def p_c_pointer(ptr, cptr):
510510
cptr.value = id(ptr)
511511

512512
def empty_c_void_p():
513-
return ctypes.c_void_p()
513+
class ctypes_c_void_p(ctypes.c_void_p):
514+
515+
def __eq__(self, value):
516+
return self.value == value.value
517+
518+
def __repr__(self):
519+
return str(self.value)
520+
521+
return ctypes_c_void_p()
514522

515523
def sizeof(arg):
516524
return ctypes.sizeof(convert_type_to_ctype(arg))

tests/reference/asr-bindc_01-6d521a9.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-bindc_01-6d521a9",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/bindc_01.py",
5-
"infile_hash": "4a4dbf4bd341ab197e648f5cbc66bfc49d87efcae7eaed047b002eb5",
5+
"infile_hash": "3cfb601d3294c470842a85777832f5582ab52cb5bd64c0e02d40deb6",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-bindc_01-6d521a9.stdout",
9-
"stdout_hash": "62629184ea444de549e9ad92da90c7f1c37861076a22e47704676527",
9+
"stdout_hash": "d02c57ff6ddb41568c291b11a31301870bf2bc3a970461a71ec23a9d",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-bindc_01-6d521a9.stdout

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@
132132
()
133133
)
134134
()
135+
)
136+
(Assert
137+
(LogicalNot
138+
(CPtrCompare
139+
(Var 2 p)
140+
NotEq
141+
(PointerNullConstant
142+
(CPtr)
143+
)
144+
(Logical 4 [])
145+
()
146+
)
147+
(Logical 4 [])
148+
()
149+
)
150+
()
135151
)]
136152
()
137153
Public

tests/reference/llvm-bindc_01-c984f09.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "llvm-bindc_01-c984f09",
33
"cmd": "lpython --no-color --show-llvm {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/bindc_01.py",
5-
"infile_hash": "4a4dbf4bd341ab197e648f5cbc66bfc49d87efcae7eaed047b002eb5",
5+
"infile_hash": "3cfb601d3294c470842a85777832f5582ab52cb5bd64c0e02d40deb6",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "llvm-bindc_01-c984f09.stdout",
9-
"stdout_hash": "11b95423195015653cffd81f8014d33dc5d96ab4cf35ecbfa16a583c",
9+
"stdout_hash": "c5c25a152a73e36c8391fe23496ff8492c2bed706491e6f3db345bfe",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/llvm-bindc_01-c984f09.stdout

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ source_filename = "LFortran"
77
@1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
88
@2 = private unnamed_addr constant [13 x i8] c"%lld%s%lld%s\00", align 1
99
@3 = private unnamed_addr constant [16 x i8] c"AssertionError\0A\00", align 1
10+
@4 = private unnamed_addr constant [16 x i8] c"AssertionError\0A\00", align 1
1011

1112
define void @__module__global_symbols__lpython_main_program() {
1213
.entry:
@@ -43,9 +44,24 @@ else: ; preds = %.entry
4344
br label %ifcont
4445

4546
ifcont: ; preds = %else, %then
47+
%3 = load void*, void** %p, align 8
48+
%4 = ptrtoint void* %3 to i64
49+
%5 = icmp ne i64 %4, 0
50+
%6 = xor i1 %5, true
51+
br i1 %6, label %then1, label %else2
52+
53+
then1: ; preds = %ifcont
54+
br label %ifcont3
55+
56+
else2: ; preds = %ifcont
57+
call void (i8*, ...) @_lcompilers_print_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @4, i32 0, i32 0))
58+
call void @exit(i32 1)
59+
br label %ifcont3
60+
61+
ifcont3: ; preds = %else2, %then1
4662
br label %return
4763

48-
return: ; preds = %ifcont
64+
return: ; preds = %ifcont3
4965
ret void
5066
}
5167

0 commit comments

Comments
 (0)