Skip to content

Commit 7b55bee

Browse files
authored
Merge pull request #2261 from kabra1110/dict_corrections
Corrections to `dict` write and pop
2 parents 8c1665a + fd4ff4f commit 7b55bee

File tree

9 files changed

+334
-115
lines changed

9 files changed

+334
-115
lines changed

integration_tests/test_dict_14.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from lpython import i32
2+
3+
def test_dict():
4+
d_i32: dict[i32, i32] = {5: 1, 5: 2}
5+
d_str: dict[str, i32] = {'a': 1, 'a': 2}
6+
l_str_1: list[str] = []
7+
l_str_2: list[str] = []
8+
l_i32_1: list[i32] = []
9+
l_i32_2: list[i32] = []
10+
i: i32
11+
s: str
12+
13+
assert len(d_i32) == 1
14+
d_i32.pop(5)
15+
assert len(d_i32) == 0
16+
17+
assert len(d_str) == 1
18+
d_str.pop('a')
19+
assert len(d_str) == 0
20+
21+
d_str = {'a': 2, 'a': 2, 'b': 2, 'c': 3, 'a': 5}
22+
assert len(d_str) == 3
23+
d_str.pop('a')
24+
assert len(d_str) == 2
25+
d_str.pop('b')
26+
assert len(d_str) == 1
27+
28+
d_str['a'] = 20
29+
assert len(d_str) == 2
30+
d_str.pop('c')
31+
assert len(d_str) == 1
32+
33+
l_str_1 = d_str.keys()
34+
for s in l_str_1:
35+
l_str_2.append(s)
36+
assert l_str_2 == ['a']
37+
l_i32_1 = d_str.values()
38+
for i in l_i32_1:
39+
l_i32_2.append(i)
40+
assert l_i32_2 == [20]
41+
42+
d_i32 = {5: 2, 5: 2, 6: 2, 7: 3, 5: 5}
43+
assert len(d_i32) == 3
44+
d_i32.pop(5)
45+
assert len(d_i32) == 2
46+
d_i32.pop(6)
47+
assert len(d_i32) == 1
48+
49+
d_i32[6] = 30
50+
assert len(d_i32) == 2
51+
d_i32.pop(7)
52+
assert len(d_i32) == 1
53+
54+
l_i32_1 = d_i32.keys()
55+
l_i32_2.clear()
56+
for i in l_i32_1:
57+
l_i32_2.append(i)
58+
assert l_i32_2 == [6]
59+
l_i32_1 = d_i32.values()
60+
l_i32_2.clear()
61+
for i in l_i32_1:
62+
l_i32_2.append(i)
63+
assert l_i32_2 == [30]
64+
65+
test_dict()

src/libasr/codegen/llvm_utils.cpp

Lines changed: 224 additions & 104 deletions
Large diffs are not rendered by default.

src/libasr/codegen/llvm_utils.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ namespace LCompilers {
588588
void write_item(llvm::Value* dict, llvm::Value* key,
589589
llvm::Value* value, llvm::Module* module,
590590
ASR::ttype_t* key_asr_type, ASR::ttype_t* value_asr_type,
591-
std::map<std::string, std::map<std::string, int>>& name2memidx) = 0;
591+
std::map<std::string, std::map<std::string, int>>& name2memidx);
592592

593593
virtual
594594
llvm::Value* read_item(llvm::Value* dict, llvm::Value* key,
@@ -693,11 +693,6 @@ namespace LCompilers {
693693
ASR::ttype_t* value_asr_type,
694694
std::map<std::string, std::map<std::string, int>>& name2memidx);
695695

696-
void write_item(llvm::Value* dict, llvm::Value* key,
697-
llvm::Value* value, llvm::Module* module,
698-
ASR::ttype_t* key_asr_type, ASR::ttype_t* value_asr_type,
699-
std::map<std::string, std::map<std::string, int>>& name2memidx);
700-
701696
llvm::Value* read_item(llvm::Value* dict, llvm::Value* key,
702697
llvm::Module& module, ASR::Dict_t* key_asr_type, bool enable_bounds_checking,
703698
bool get_pointer=false);
@@ -847,11 +842,6 @@ namespace LCompilers {
847842
ASR::ttype_t* value_asr_type,
848843
std::map<std::string, std::map<std::string, int>>& name2memidx);
849844

850-
void write_item(llvm::Value* dict, llvm::Value* key,
851-
llvm::Value* value, llvm::Module* module,
852-
ASR::ttype_t* key_asr_type, ASR::ttype_t* value_asr_type,
853-
std::map<std::string, std::map<std::string, int>>& name2memidx);
854-
855845
llvm::Value* read_item(llvm::Value* dict, llvm::Value* key,
856846
llvm::Module& module, ASR::Dict_t* dict_type, bool enable_bounds_checking,
857847
bool get_pointer=false);

tests/errors/test_dict15.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from lpython import i32
2+
3+
def test_dict_pop():
4+
d: dict[i32, i32] = {1: 2}
5+
d.pop(1)
6+
d.pop(1)
7+
8+
test_dict_pop()

tests/errors/test_dict16.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from lpython import i32
2+
3+
def test_dict_pop():
4+
d: dict[str, i32] = {'a': 2}
5+
d.pop('a')
6+
d.pop('a')
7+
8+
test_dict_pop()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "runtime-test_dict15-6f3af0d",
3+
"cmd": "lpython {infile}",
4+
"infile": "tests/errors/test_dict15.py",
5+
"infile_hash": "6a0e507b9a9cf659cb433abbdc3435b4c63a6079eadcd7d2c765def1",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "runtime-test_dict15-6f3af0d.stderr",
11+
"stderr_hash": "cb46ef04db0862506d688ebe8830a50afaaead9b0d29b0c007dd149a",
12+
"returncode": 1
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KeyError: The dict does not contain the specified key
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "runtime-test_dict16-c5a958d",
3+
"cmd": "lpython {infile}",
4+
"infile": "tests/errors/test_dict16.py",
5+
"infile_hash": "7b00cfd7f6eac8338897bd99e5d953605f16927ee0f27683146b0182",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "runtime-test_dict16-c5a958d.stderr",
11+
"stderr_hash": "cb46ef04db0862506d688ebe8830a50afaaead9b0d29b0c007dd149a",
12+
"returncode": 1
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KeyError: The dict does not contain the specified key

0 commit comments

Comments
 (0)