Skip to content

Commit e3fcee6

Browse files
authored
fix: byte literals with utf-32 characters can be decoded (#4)
1 parent 40f599f commit e3fcee6

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/unparser.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::ops::Deref;
2-
31
use rustpython_ast::{
42
text_size::TextRange, Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, ExceptHandler,
53
ExceptHandlerExceptHandler, Expr, ExprAttribute, ExprAwait, ExprBinOp, ExprBoolOp, ExprCall,
@@ -16,6 +14,7 @@ use rustpython_ast::{
1614
UnaryOp, WithItem,
1715
};
1816
use rustpython_ast::{Constant, ConversionFlag, Int};
17+
use std::ops::Deref;
1918

2019
enum Precedence {
2120
NamedExpr = 1,
@@ -1040,10 +1039,8 @@ impl Unparser {
10401039
}
10411040
}
10421041
Constant::Bytes(value) => {
1043-
let utf8 = String::from_utf8(value.to_owned());
1044-
self.write_str("b");
1045-
let escaped = rustpython_literal::escape::UnicodeEscape::new_repr(&utf8.unwrap())
1046-
.str_repr()
1042+
let escaped = rustpython_literal::escape::AsciiEscape::new_repr(value)
1043+
.bytes_repr()
10471044
.to_string()
10481045
.unwrap();
10491046
self.write_str(&escaped);

test_files/byte_literal.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
byte_literal = b"hello"
2+
3+
# extracted from pygments.lexer
4+
_encoding_map = [
5+
(b"\xef\xbb\xbf", "utf-8"),
6+
(b"\xff\xfe\0\0", "utf-32"),
7+
(b"\0\0\xfe\xff", "utf-32be"),
8+
(b"\xff\xfe", "utf-16"),
9+
(b"\xfe\xff", "utf-16be"),
10+
]

0 commit comments

Comments
 (0)