mirror of
https://github.com/google/blockly.git
synced 2025-12-15 13:50:08 +01:00
fix: Remove float() coercion in Python loops (#6259)
Coercion to a float is a very JavaScript thing to do, Python is not JavaScript. From this discussion: https://groups.google.com/g/blockly/c/OF_j9rKQgpI
This commit is contained in:
@@ -134,14 +134,11 @@ def ${Python.FUNCTION_NAME_PLACEHOLDER_}(start, stop, step):
|
||||
if (stringUtils.isNumber(arg)) {
|
||||
// Simple number.
|
||||
arg = Number(arg);
|
||||
} else if (arg.match(/^\w+$/)) {
|
||||
// Variable.
|
||||
arg = 'float(' + arg + ')';
|
||||
} else {
|
||||
// It's complicated.
|
||||
} else if (!arg.match(/^\w+$/)) {
|
||||
// Not a variable, it's complicated.
|
||||
const varName = Python.nameDB_.getDistinctName(
|
||||
variable0 + suffix, NameType.VARIABLE);
|
||||
code += varName + ' = float(' + arg + ')\n';
|
||||
code += varName + ' = ' + arg + '\n';
|
||||
arg = varName;
|
||||
}
|
||||
return arg;
|
||||
|
||||
@@ -223,22 +223,22 @@ def test_count_by():
|
||||
loglist.append(x)
|
||||
assertEquals(loglist, [1, 2.5, 4, 5.5, 7], 'count with floats')
|
||||
loglist = []
|
||||
x_start = float(1 + 0)
|
||||
x_end = float(8 + 0)
|
||||
x_inc = float(1 - 2)
|
||||
x_start = 1 + 0
|
||||
x_end = 8 + 0
|
||||
x_inc = 1 - 2
|
||||
for x in (x_start <= x_end) and upRange(x_start, x_end, x_inc) or downRange(x_start, x_end, x_inc):
|
||||
loglist.append(x)
|
||||
assertEquals(loglist, [1, 2, 3, 4, 5, 6, 7, 8], 'count up non-trivial ints')
|
||||
loglist = []
|
||||
x_start2 = float(8 + 0)
|
||||
x_end2 = float(1 + 0)
|
||||
x_start2 = 8 + 0
|
||||
x_end2 = 1 + 0
|
||||
for x in (x_start2 <= x_end2) and upRange(x_start2, x_end2, 2) or downRange(x_start2, x_end2, 2):
|
||||
loglist.append(x)
|
||||
assertEquals(loglist, [8, 6, 4, 2], 'count down non-trivial ints')
|
||||
loglist = []
|
||||
x_start3 = float(5 + 0.5)
|
||||
x_end3 = float(1 + 0)
|
||||
x_inc2 = float(1 + 0)
|
||||
x_start3 = 5 + 0.5
|
||||
x_end3 = 1 + 0
|
||||
x_inc2 = 1 + 0
|
||||
for x in (x_start3 <= x_end3) and upRange(x_start3, x_end3, x_inc2) or downRange(x_start3, x_end3, x_inc2):
|
||||
loglist.append(x)
|
||||
assertEquals(loglist, [5.5, 4.5, 3.5, 2.5, 1.5], 'count with floats')
|
||||
@@ -255,14 +255,14 @@ def test_count_loops():
|
||||
log = str(log) + str(x)
|
||||
assertEquals(log, '87654321', 'count down')
|
||||
loglist = []
|
||||
x_start4 = float(1 + 0)
|
||||
x_end4 = float(4 + 0)
|
||||
x_start4 = 1 + 0
|
||||
x_end4 = 4 + 0
|
||||
for x in (x_start4 <= x_end4) and upRange(x_start4, x_end4, 1) or downRange(x_start4, x_end4, 1):
|
||||
loglist.append(x)
|
||||
assertEquals(loglist, [1, 2, 3, 4], 'count up non-trivial')
|
||||
loglist = []
|
||||
x_start5 = float(3 + 1)
|
||||
x_end5 = float(1 + 0)
|
||||
x_start5 = 3 + 1
|
||||
x_end5 = 1 + 0
|
||||
for x in (x_start5 <= x_end5) and upRange(x_start5, x_end5, 1) or downRange(x_start5, x_end5, 1):
|
||||
loglist.append(x)
|
||||
assertEquals(loglist, [4, 3, 2, 1], 'count down non-trivial')
|
||||
|
||||
Reference in New Issue
Block a user