Skip to content

Commit

Permalink
Fail with a proper error when tying to modify attributes pythran cons…
Browse files Browse the repository at this point in the history
…iders immutable

Fix #2224
  • Loading branch information
serge-sans-paille committed Sep 22, 2024
1 parent b841c07 commit 5f51c69
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pythran/tests/test_normalize_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ def test_dispatch_update(self):
{1}, {1:1},
dispatch_update=[Set[int], Dict[int,int]])

def test_supported_attr_store(self):
self.run_test("def supported_attr_store(s): s.real += 1; return s",
numpy.ones(10, dtype=complex),
supported_attr_store=[NDArray[complex, :]])

def test_unsupported_attr_store(self):
with self.assertRaises(pythran.syntax.PythranSyntaxError):
self.run_test("def unsupported_attr_store(s): s.strides = 2; return s",
numpy.ones(10, dtype=complex),
unsupported_attr_store=[NDArray[complex, :]])

def test_invalid_method_call(self):
code = '''
def np_asarray7(sRate=44100):
Expand Down
6 changes: 5 additions & 1 deletion pythran/transformations/normalize_method_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ def visit_Attribute(self, node):
# the only situation where this arises is for real/imag of
# a ndarray. As a call is not valid for a store, add a slice
# to ends up with a valid lhs
assert node.attr in ('real', 'imag'), "only store to imag/real"
if node.attr not in ('real', 'imag'):
raise PythranSyntaxError(
"Unsupported store to attribute {}".format(node.attr),
node)

return ast.Subscript(call,
ast.Slice(None, None, None),
node.ctx)
Expand Down

0 comments on commit 5f51c69

Please sign in to comment.