Skip to content

Commit b74e6f2

Browse files
committed
compile source may be bytes
1 parent aca64fb commit b74e6f2

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

vm/src/builtins.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
55
use std::char;
66
use std::io::{self, Write};
7+
use std::str;
78

89
use num_bigint::Sign;
910
use num_traits::{Signed, Zero};
1011

1112
use crate::compile;
1213
use crate::obj::objbool;
14+
use crate::obj::objbytes::PyBytesRef;
1315
use crate::obj::objcode::PyCodeRef;
1416
use crate::obj::objdict::PyDictRef;
1517
use crate::obj::objint::{self, PyIntRef};
@@ -20,7 +22,7 @@ use crate::obj::objtype::{self, PyClassRef};
2022
use crate::frame::Scope;
2123
use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs};
2224
use crate::pyobject::{
23-
IdProtocol, IntoPyObject, ItemProtocol, PyIterable, PyObjectRef, PyResult, PyValue,
25+
Either, IdProtocol, IntoPyObject, ItemProtocol, PyIterable, PyObjectRef, PyResult, PyValue,
2426
TryFromObject, TypeProtocol,
2527
};
2628
use crate::vm::VirtualMachine;
@@ -78,13 +80,19 @@ fn builtin_chr(i: u32, vm: &VirtualMachine) -> PyResult<String> {
7880
}
7981

8082
fn builtin_compile(
81-
source: PyStringRef,
83+
source: Either<PyStringRef, PyBytesRef>,
8284
filename: PyStringRef,
8385
mode: PyStringRef,
8486
vm: &VirtualMachine,
8587
) -> PyResult<PyCodeRef> {
88+
// TODO: compile::compile should probably get bytes
89+
let source = match source {
90+
Either::A(string) => string.value.to_string(),
91+
Either::B(bytes) => str::from_utf8(&bytes).unwrap().to_string(),
92+
};
93+
8694
// TODO: fix this newline bug:
87-
let source = format!("{}\n", &source.value);
95+
let source = format!("{}\n", source);
8896

8997
let mode = {
9098
let mode = &mode.value;

0 commit comments

Comments
 (0)