Skip to content

Commit

Permalink
[constant folding] Correctly model restrict_assign in constant folding
Browse files Browse the repository at this point in the history
Fix #2247
  • Loading branch information
serge-sans-paille committed Nov 6, 2024
1 parent bb5e0f1 commit 9dc4fba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pythran/optimizations/constant_folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import gast as ast
from copy import deepcopy
import logging
import numpy
import sys

logger = logging.getLogger('pythran')
Expand Down Expand Up @@ -64,6 +65,10 @@ def is_none(val):
def make_shape(*args):
return args

@staticmethod
def restrict_assign(expr, value):
numpy.copyto(expr, value)

class BreakLoop(Exception):
pass

Expand Down
14 changes: 14 additions & 0 deletions pythran/tests/test_optimizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ def test_constant_fold_nan(self):
code = "def constant_fold_nan(a): from numpy import nan; a[0] = nan; return a"
self.run_test(code, [1., 2.], constant_fold_nan=[List[float]])

def test_constant_fold_restrict_assign(self):
code = """
import numpy as np
def make_signal():
y = np.ones((44100,2))
y[:, 1:] = 0. #<--- the issue.
return y
def constant_fold_restrict_assign():
x = make_signal()
return x
"""
self.run_test(code, constant_fold_restrict_assign=[])

def test_constant_fold_subscript(self):
code = '''
def aux(n):
Expand Down

0 comments on commit 9dc4fba

Please sign in to comment.