@@ -21,8 +21,8 @@ use crate::slots::{BufferProtocol, Comparable, Hashable, Iterable, PyComparisonO
2121use crate :: utils:: Either ;
2222use crate :: vm:: VirtualMachine ;
2323use crate :: {
24- IntoPyObject , PyClassImpl , PyComparisonValue , PyContext , PyIterable , PyObjectRef , PyRef ,
25- PyResult , PyValue , TryFromObject , TypeProtocol ,
24+ IdProtocol , IntoPyObject , PyClassImpl , PyComparisonValue , PyContext , PyIterable , PyObjectRef ,
25+ PyRef , PyResult , PyValue , TryFromObject , TypeProtocol ,
2626} ;
2727
2828use crate :: builtins:: memory:: { BufferOptions , PyBuffer } ;
@@ -430,11 +430,19 @@ impl PyBytes {
430430
431431 #[ pymethod( name = "__mul__" ) ]
432432 #[ pymethod( name = "__rmul__" ) ]
433- fn mul ( & self , value : isize , vm : & VirtualMachine ) -> PyResult < PyBytes > {
434- if value > 0 && self . inner . len ( ) as isize > std:: isize:: MAX / value {
433+ fn mul ( zelf : PyRef < Self > , value : isize , vm : & VirtualMachine ) -> PyResult < PyRef < Self > > {
434+ if value > 0 && zelf . inner . len ( ) as isize > std:: isize:: MAX / value {
435435 return Err ( vm. new_overflow_error ( "repeated bytes are too long" . to_owned ( ) ) ) ;
436436 }
437- Ok ( self . inner . repeat ( value) . into ( ) )
437+ if value == 1 && zelf. class ( ) . is ( & vm. ctx . types . bytes_type ) {
438+ // Special case: when some `bytes` is multiplied by `1`,
439+ // nothing really happens, we need to return an object itself
440+ // with the same `id()` to be compatible with CPython.
441+ // This only works for `bytes` itself, not its subclasses.
442+ return Ok ( zelf) ;
443+ }
444+ let bytes: PyBytes = zelf. inner . repeat ( value) . into ( ) ;
445+ Ok ( bytes. into_ref ( vm) )
438446 }
439447
440448 #[ pymethod( name = "__mod__" ) ]
0 commit comments