@@ -7,7 +7,7 @@ use crate::vm::VirtualMachine;
7
7
8
8
use crate :: pyobject:: { PyResult , TypeProtocol } ;
9
9
10
- use crate :: obj:: objstr:: PyString ;
10
+ use crate :: obj:: objstr:: { PyString , PyStringRef } ;
11
11
use std:: collections:: hash_map:: DefaultHasher ;
12
12
use std:: hash:: { Hash , Hasher } ;
13
13
@@ -30,37 +30,30 @@ pub struct PyByteInner {
30
30
}
31
31
32
32
#[ derive( FromArgs ) ]
33
- pub struct BytesNewOptions {
33
+ pub struct ByteInnerNewOptions {
34
34
#[ pyarg( positional_only, optional = true ) ]
35
35
val_option : OptionalArg < PyObjectRef > ,
36
36
#[ pyarg( positional_or_keyword, optional = true ) ]
37
- encoding : OptionalArg < PyObjectRef > ,
37
+ encoding : OptionalArg < PyStringRef > ,
38
38
}
39
39
40
- impl BytesNewOptions {
40
+ impl ByteInnerNewOptions {
41
41
pub fn get_value ( self , vm : & VirtualMachine ) -> PyResult < PyByteInner > {
42
42
// First handle bytes(string, encoding[, errors])
43
43
if let OptionalArg :: Present ( enc) = self . encoding {
44
44
if let OptionalArg :: Present ( eval) = self . val_option {
45
45
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
+ } ) ;
59
53
} 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
+ ) ;
64
57
}
65
58
} else {
66
59
return Err ( vm. new_type_error ( "encoding without a string argument" . to_string ( ) ) ) ;
0 commit comments