Skip to content

Commit 473ae24

Browse files
committed
use PyStringRef in new
1 parent 84d61a9 commit 473ae24

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

tests/snippets/bytes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
assert bytes("bla", "utf8") == bytes("bla", encoding="utf-8") == b"bla"
1010
with assertRaises(TypeError):
1111
bytes("bla")
12+
with assertRaises(TypeError):
13+
bytes("bla", encoding = b"jilj")
1214

1315
assert (
1416
b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"

vm/src/obj/objbyteinner.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::vm::VirtualMachine;
77

88
use crate::pyobject::{PyResult, TypeProtocol};
99

10-
use crate::obj::objstr::PyString;
10+
use crate::obj::objstr::{PyString, PyStringRef};
1111
use std::collections::hash_map::DefaultHasher;
1212
use std::hash::{Hash, Hasher};
1313

@@ -30,37 +30,30 @@ pub struct PyByteInner {
3030
}
3131

3232
#[derive(FromArgs)]
33-
pub struct BytesNewOptions {
33+
pub struct ByteInnerNewOptions {
3434
#[pyarg(positional_only, optional = true)]
3535
val_option: OptionalArg<PyObjectRef>,
3636
#[pyarg(positional_or_keyword, optional = true)]
37-
encoding: OptionalArg<PyObjectRef>,
37+
encoding: OptionalArg<PyStringRef>,
3838
}
3939

40-
impl BytesNewOptions {
40+
impl ByteInnerNewOptions {
4141
pub fn get_value(self, vm: &VirtualMachine) -> PyResult<PyByteInner> {
4242
// First handle bytes(string, encoding[, errors])
4343
if let OptionalArg::Present(enc) = self.encoding {
4444
if let OptionalArg::Present(eval) = self.val_option {
4545
if let Ok(input) = eval.downcast::<PyString>() {
46-
if let Ok(encoding) = enc.clone().downcast::<PyString>() {
47-
if &encoding.value.to_lowercase() == "utf8"
48-
|| &encoding.value.to_lowercase() == "utf-8"
49-
// TODO: different encoding
50-
{
51-
return Ok(PyByteInner {
52-
elements: input.value.as_bytes().to_vec(),
53-
});
54-
} else {
55-
return Err(
56-
vm.new_value_error(format!("unknown encoding: {}", encoding.value)), //should be lookup error
57-
);
58-
}
46+
let encoding = enc.as_str();
47+
if encoding.to_lowercase() == "utf8" || encoding.to_lowercase() == "utf-8"
48+
// TODO: different encoding
49+
{
50+
return Ok(PyByteInner {
51+
elements: input.value.as_bytes().to_vec(),
52+
});
5953
} else {
60-
return Err(vm.new_type_error(format!(
61-
"bytes() argument 2 must be str, not {}",
62-
enc.class().name
63-
)));
54+
return Err(
55+
vm.new_value_error(format!("unknown encoding: {}", encoding)), //should be lookup error
56+
);
6457
}
6558
} else {
6659
return Err(vm.new_type_error("encoding without a string argument".to_string()));

vm/src/obj/objbytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::ops::Deref;
77
use crate::function::OptionalArg;
88
use crate::pyobject::{PyClassImpl, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue};
99

10-
use super::objbyteinner::{BytesNewOptions, PyByteInner};
10+
use super::objbyteinner::{ByteInnerNewOptions, PyByteInner};
1111
use super::objiter;
1212
use super::objslice::PySlice;
1313
use super::objtype::PyClassRef;
@@ -77,7 +77,7 @@ impl PyBytesRef {
7777
#[pymethod(name = "__new__")]
7878
fn bytes_new(
7979
cls: PyClassRef,
80-
options: BytesNewOptions,
80+
options: ByteInnerNewOptions,
8181
vm: &VirtualMachine,
8282
) -> PyResult<PyBytesRef> {
8383
PyBytes {

0 commit comments

Comments
 (0)