Skip to content

Commit d82c2b0

Browse files
authored
Merge pull request #4052 from moreal/correct-eval
Raise `ValueError` if `\x00` character exists for `eval` argument
2 parents aec155f + c672d8f commit d82c2b0

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ def test_verbose(self):
7575
rc, out, err = assert_python_ok('-vv')
7676
self.assertNotIn(b'stack overflow', err)
7777

78-
# TODO: RUSTPYTHON
79-
@unittest.expectedFailure
8078
@unittest.skipIf(interpreter_requires_environment(),
8179
'Cannot run -E tests when PYTHON env vars are required.')
8280
def test_xoptions(self):

Lib/test/test_subprocess.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,6 @@ def test_env(self):
761761
stdout, stderr = p.communicate()
762762
self.assertEqual(stdout, b"orange")
763763

764-
# TODO: RUSTPYTHON
765-
@unittest.expectedFailure
766764
# Windows requires at least the SYSTEMROOT environment variable to start
767765
# Python
768766
@unittest.skipIf(sys.platform == 'win32',

extra_tests/snippets/syntax_non_utf8.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
dir_path = os.path.dirname(os.path.realpath(__file__))
77

8-
# TODO: RUSTPYTHON, RustPython raises a SyntaxError here, but cpython raise a ValueError
9-
error = SyntaxError if platform.python_implementation() == 'RustPython' else ValueError
10-
with assert_raises(error):
8+
with assert_raises(ValueError):
119
with open(os.path.join(dir_path , "non_utf8.txt")) as f:
1210
eval(f.read())

vm/src/stdlib/builtins.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ mod builtins {
2424
format::call_object_format,
2525
function::Either,
2626
function::{
27-
ArgBytesLike, ArgCallable, ArgIntoBool, ArgIterable, ArgMapping, FuncArgs, KwArgs,
28-
OptionalArg, OptionalOption, PosArgs, PyArithmeticValue,
27+
ArgBytesLike, ArgCallable, ArgIntoBool, ArgIterable, ArgMapping, ArgStrOrBytesLike,
28+
FuncArgs, KwArgs, OptionalArg, OptionalOption, PosArgs, PyArithmeticValue,
2929
},
3030
protocol::{PyIter, PyIterReturn},
3131
py_io,
@@ -248,11 +248,34 @@ mod builtins {
248248
#[cfg(feature = "rustpython-compiler")]
249249
#[pyfunction]
250250
fn eval(
251-
source: Either<PyStrRef, PyRef<crate::builtins::PyCode>>,
251+
source: Either<ArgStrOrBytesLike, PyRef<crate::builtins::PyCode>>,
252252
scope: ScopeArgs,
253253
vm: &VirtualMachine,
254254
) -> PyResult {
255-
run_code(vm, source, scope, compile::Mode::Eval, "eval")
255+
// source as string
256+
let code = match source {
257+
Either::A(either) => {
258+
let source: &[u8] = &either.borrow_bytes();
259+
if source.contains(&0) {
260+
return Err(vm.new_value_error(
261+
"source code string cannot contain null bytes".to_owned(),
262+
));
263+
}
264+
265+
let source = std::str::from_utf8(source).map_err(|err| {
266+
let msg = format!(
267+
"(unicode error) 'utf-8' codec can't decode byte 0x{:x?} in position {}: invalid start byte",
268+
source[err.valid_up_to()],
269+
err.valid_up_to()
270+
);
271+
272+
vm.new_exception_msg(vm.ctx.exceptions.syntax_error.to_owned(), msg)
273+
})?;
274+
Ok(Either::A(vm.ctx.new_str(source)))
275+
}
276+
Either::B(code) => Ok(Either::B(code)),
277+
}?;
278+
run_code(vm, code, scope, compile::Mode::Eval, "eval")
256279
}
257280

258281
/// Implements `exec`

0 commit comments

Comments
 (0)