Skip to content

Commit 11e2120

Browse files
Set support : copy method (pyccel#1765)
Add support for the `copy()` method of Python sets to the semantic stage. Add handling of that method to the Python printer. This fixes pyccel#1740.
1 parent e886b3e commit 11e2120

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77

88
- #1739 : Add Python support for set method `clear()`.
99
- #1739 : Add abstract class `SetMethod` to handle calls to various set methods.
10+
- #1740 : Add Python support for set method `copy()`.
1011

1112
### Fixed
1213

pyccel/ast/builtin_methods/set_methods.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from pyccel.ast.datatypes import NativeVoid, NativeGeneric
1313
from pyccel.ast.internals import PyccelInternalFunction
1414

15-
__all__ = ('SetAdd', 'SetClear', 'SetMethod')
15+
__all__ = ('SetAdd', 'SetClear', 'SetMethod', 'SetCopy')
16+
1617

1718
class SetMethod(PyccelInternalFunction):
1819
"""
@@ -26,7 +27,7 @@ class SetMethod(PyccelInternalFunction):
2627
set_variable : TypedAstNode
2728
The set on which the method will operate.
2829
29-
*args : iterable
30+
*args : TypedAstNode
3031
The arguments passed to the function call.
3132
"""
3233
__slots__ = ('_set_variable',)
@@ -44,6 +45,7 @@ def set_variable(self):
4445
"""
4546
return self._set_variable
4647

48+
4749
class SetAdd(SetMethod) :
4850
"""
4951
Represents a call to the .add() method.
@@ -81,6 +83,7 @@ def __init__(self, set_variable, new_elem) -> None:
8183
raise TypeError("Expecting an argument of the same type as the elements of the set")
8284
super().__init__(set_variable, new_elem)
8385

86+
8487
class SetClear(SetMethod):
8588
"""
8689
Represents a call to the .clear() method.
@@ -104,3 +107,28 @@ class SetClear(SetMethod):
104107

105108
def __init__(self, set_variable):
106109
super().__init__(set_variable)
110+
111+
112+
class SetCopy(SetMethod):
113+
"""
114+
Represents a call to the .copy() method.
115+
116+
The copy() method in set class creates a shallow
117+
copy of a set object and returns it.
118+
119+
Parameters
120+
----------
121+
set_variable : TypedAstNode
122+
The set on which the method will operate.
123+
"""
124+
__slots__ = ("_dtype","_shape", "_order", "_rank", "_precision", "_class_type",)
125+
name = 'copy'
126+
127+
def __init__(self, set_variable):
128+
self._dtype = set_variable._dtype
129+
self._shape = set_variable._shape
130+
self._order = set_variable._order
131+
self._rank = set_variable._rank
132+
self._precision = set_variable._precision
133+
self._class_type = set_variable._class_type
134+
super().__init__(set_variable)

pyccel/ast/class_defs.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
This module contains all types which define a python class which is automatically recognised by pyccel
77
"""
88

9-
from pyccel.ast.builtin_methods.set_methods import SetAdd, SetClear
9+
from pyccel.ast.builtin_methods.set_methods import SetAdd, SetClear, SetCopy
1010
from pyccel.ast.builtin_methods.list_methods import ListAppend, ListInsert, ListPop, ListClear
1111

1212

@@ -153,10 +153,9 @@
153153

154154
SetClass = ClassDef('set', class_type=NativeHomogeneousSet(),
155155
methods=[
156-
PyccelFunctionDef('add', func_class = SetAdd,
157-
decorators = {}),
158-
PyccelFunctionDef('clear', func_class = SetClear,
159-
decorators={}),
156+
PyccelFunctionDef('add', func_class = SetAdd ),
157+
PyccelFunctionDef('clear', func_class = SetClear),
158+
PyccelFunctionDef('copy', func_class = SetCopy),
160159
])
161160

162161
#=======================================================================================

tests/epyccel/test_epyccel_sets.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,40 @@ def clear_complex():
7777
python_result = clear_complex()
7878
assert python_result == pyccel_result
7979

80+
def test_copy_int(language):
81+
def copy_int():
82+
se = {1, 2, 4, 5}
83+
cop = se.copy()
84+
return cop
85+
epyccel_copy = epyccel(copy_int, language = language)
86+
pyccel_result = epyccel_copy()
87+
python_result = copy_int()
88+
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+
92+
93+
def test_copy_float(language):
94+
def copy_float():
95+
se = {5.7, 6.2, 4.3, 9.8}
96+
cop = se.copy()
97+
return cop
98+
epyccel_copy = epyccel(copy_float, language = language)
99+
pyccel_result = epyccel_copy()
100+
python_result = copy_float()
101+
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)
104+
105+
def test_copy_complex(language):
106+
def copy_complex():
107+
se = {7j, 6j, 9j}
108+
cop = se.copy()
109+
return cop
110+
epyccel_copy = epyccel(copy_complex, language = language)
111+
pyccel_result = epyccel_copy()
112+
python_result = copy_complex()
113+
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)
116+

0 commit comments

Comments
 (0)