Skip to content

Commit 7974ec8

Browse files
authored
Add support for set.copy (pyccel#2019)
Add C and Fortran support for `set.copy`. Fixes pyccel#1919
1 parent 0e6fc86 commit 7974ec8

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ All notable changes to this project will be documented in this file.
1212
- #1700 : Add Python support for list method `sort()`.
1313
- #1696 : Add Python support for list method `copy()`.
1414
- #1693 : Add Python support for list method `remove()`.
15-
- #1739 : Add Python support for set method `clear()`.
16-
- #1740 : Add Python support for set method `copy()`.
1715
- #1750 : Add Python support for set method `remove()`.
1816
- #1743 : Add Python support for set method `discard()`.
1917
- #1754 : Add Python support for set method `update()`.
@@ -38,7 +36,8 @@ All notable changes to this project will be documented in this file.
3836
- #1690 : Add C support for list method `pop()`.
3937
- #1877 : Add C and Fortran Support for set method `pop()`.
4038
- #1917 : Add C and Fortran support for set method `add()`.
41-
- #1918 : Add C and Fortran support for set method `clear()`.
39+
- #1918 : Add support for set method `clear()`.
40+
- #1918 : Add support for set method `copy()`.
4241
- #1936 : Add missing C output for inline decorator example in documentation
4342
- #1937 : Optimise `pyccel.ast.basic.PyccelAstNode.substitute` method.
4443
- #1544 : Add support for `typing.TypeAlias`.

docs/builtin-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Python contains a limited number of builtin functions defined [here](https://doc
9898
|----------|-----------|
9999
| **`add`** | **Yes** |
100100
| **`clear`** | **Yes** |
101-
| `copy` | Python-only |
101+
| **`copy`** | **Yes** |
102102
| `difference` | No |
103103
| `difference_update` | No |
104104
| `discard` | Python-only |

pyccel/codegen/printing/ccode.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,12 +2524,6 @@ def _print_CommentBlock(self, expr):
25242524
def _print_EmptyNode(self, expr):
25252525
return ''
25262526

2527-
def _print_SetAdd(self, expr):
2528-
var_type = self.get_declare_type(expr.set_variable)
2529-
set_var = self._print(ObjectAddress(expr.set_variable))
2530-
arg = self._print(expr.args[0])
2531-
return f'{var_type}_push({set_var}, {arg});\n'
2532-
25332527
#=================== OMP ==================
25342528

25352529
def _print_OmpAnnotatedComment(self, expr):
@@ -2627,6 +2621,17 @@ def _print_SetClear(self, expr):
26272621
set_var = self._print(ObjectAddress(expr.set_variable))
26282622
return f'{var_type}_clear({set_var});\n'
26292623

2624+
def _print_SetAdd(self, expr):
2625+
var_type = self.get_declare_type(expr.set_variable)
2626+
set_var = self._print(ObjectAddress(expr.set_variable))
2627+
arg = self._print(expr.args[0])
2628+
return f'{var_type}_push({set_var}, {arg});\n'
2629+
2630+
def _print_SetCopy(self, expr):
2631+
var_type = self.get_declare_type(expr.set_variable)
2632+
set_var = self._print(expr.set_variable)
2633+
return f'{var_type}_clone({set_var})'
2634+
26302635
#=================== MACROS ==================
26312636

26322637
def _print_MacroShape(self, expr):

pyccel/codegen/printing/fcode.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,11 @@ def _print_SetPop(self, expr):
13321332
self.add_import(self._build_gFTL_extension_module(expr_type))
13331333
return f'{type_name}_pop({var_code})\n'
13341334

1335+
def _print_SetCopy(self, expr):
1336+
var_code = self._print(expr.set_variable)
1337+
type_name = self._print(expr.class_type)
1338+
return f'{type_name}({var_code})'
1339+
13351340
#========================== Numpy Elements ===============================#
13361341

13371342
def _print_NumpySum(self, expr):

tests/epyccel/test_epyccel_sets.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,42 +77,46 @@ def clear_complex():
7777
python_result = clear_complex()
7878
assert python_result == pyccel_result
7979

80-
def test_copy_int(python_only_language):
80+
def test_copy_int(language):
8181
def copy_int():
8282
se = {1, 2, 4, 5}
8383
cop = se.copy()
84-
return cop
85-
epyccel_copy = epyccel(copy_int, language = python_only_language)
84+
size = len(cop)
85+
a,b,c,d = cop.pop(), cop.pop(), cop.pop(), cop.pop()
86+
return size, len(se), a,b,c,d
87+
epyccel_copy = epyccel(copy_int, language = language)
8688
pyccel_result = epyccel_copy()
8789
python_result = copy_int()
8890
assert isinstance(python_result, type(pyccel_result))
89-
assert python_result == pyccel_result
90-
assert all(isinstance(elem, type(pyccel_result.pop())) for elem in python_result)
91+
assert python_result[0] == pyccel_result[0]
92+
assert python_result[1] == pyccel_result[1]
93+
assert set(python_result[2:]) == set(pyccel_result[2:])
9194

9295

93-
def test_copy_float(python_only_language):
96+
def test_copy_float(language):
9497
def copy_float():
9598
se = {5.7, 6.2, 4.3, 9.8}
9699
cop = se.copy()
97-
return cop
98-
epyccel_copy = epyccel(copy_float, language = python_only_language)
100+
return len(cop), cop.pop(), cop.pop(), cop.pop(), cop.pop(), len(se)
101+
epyccel_copy = epyccel(copy_float, language = language)
99102
pyccel_result = epyccel_copy()
100103
python_result = copy_float()
101104
assert isinstance(python_result, type(pyccel_result))
102-
assert python_result == pyccel_result
103-
assert all(isinstance(elem, type(pyccel_result.pop())) for elem in python_result)
105+
assert python_result[0] == pyccel_result[0]
106+
assert python_result[-1] == pyccel_result[-1]
107+
assert set(python_result[1:-1]) == set(pyccel_result[1:-1])
104108

105-
def test_copy_complex(python_only_language):
109+
def test_copy_complex(language):
106110
def copy_complex():
107111
se = {7j, 6j, 9j}
108112
cop = se.copy()
109-
return cop
110-
epyccel_copy = epyccel(copy_complex, language = python_only_language)
113+
return len(cop), cop.pop(), cop.pop(), cop.pop()
114+
epyccel_copy = epyccel(copy_complex, language = language)
111115
pyccel_result = epyccel_copy()
112116
python_result = copy_complex()
113117
assert isinstance(python_result, type(pyccel_result))
114-
assert python_result == pyccel_result
115-
assert all(isinstance(elem, type(pyccel_result.pop())) for elem in python_result)
118+
assert python_result[0] == pyccel_result[0]
119+
assert set(python_result[1:]) == set(pyccel_result[1:])
116120

117121
def test_remove_complex(python_only_language):
118122
def remove_complex():

0 commit comments

Comments
 (0)