Fix compiled float literals depending on the serialize_precision ini setting

This commit is contained in:
Nicolas Grekas
2026-10-09 17:38:55 +02:00
parent d79aaf70a5
commit e53806e99c
3 changed files with 21 additions and 0 deletions
+1
View File
@@ -1,5 +1,6 @@
# 3.31.0 (2026-XX-XX)
* Fix compiled float literals depending on the `serialize_precision` ini setting
* Fix the `loop` variable being undefined when used in the sequence of a nested `for` tag
* Reject a deprecated `Template` instance created by another environment in `Environment::resolveTemplate()`
* Fix a compilation error inside an `autoescape` tag leaking its escaping strategy into the next compiled template
+7
View File
@@ -166,8 +166,15 @@ class Compiler
setlocale(\LC_NUMERIC, 'C');
}
// var_export() follows serialize_precision, -1 gives the shortest representation that reads back as the same float
$precision = \is_float($value) ? ini_set('serialize_precision', '-1') : false;
$this->raw(var_export($value, true));
if (false !== $precision) {
ini_set('serialize_precision', $precision);
}
if (false !== $locale) {
setlocale(\LC_NUMERIC, $locale);
}
+13
View File
@@ -64,4 +64,17 @@ class CompilerTest extends TestCase
setlocale(\LC_NUMERIC, $locale);
}
public function testReprFloatDoesNotDependOnSerializePrecision(): void
{
$compiler = new Compiler(new Environment(new ArrayLoader()));
$precision = ini_set('serialize_precision', '17');
try {
$this->assertSame('0.1', $compiler->repr(0.1)->getSource());
$this->assertSame('17', \ini_get('serialize_precision'));
} finally {
ini_set('serialize_precision', $precision);
}
}
}