mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Implement bytes.__bytes__
This commit implement `bytes.__bytes__` in cpython 3.11 for typing module. * https://docs.python.org/3.11/whatsnew/3.11.html#other-cpython-implementation-changes * https://bugs.python.org/issue24234 Signed-off-by: snowapril <sinjihng@gmail.com>
This commit is contained in:
@@ -615,6 +615,16 @@ assert b'\xe4\xb8\xad\xe6\x96\x87\xe5\xad\x97'.decode('utf-8') == '中文字'
|
||||
assert b'rust%bpython%b' % (b' ', b'!') == b'rust python!'
|
||||
assert b'x=%i y=%f' % (1, 2.5) == b'x=1 y=2.500000'
|
||||
|
||||
# __bytes__
|
||||
foo = b'foo\x00bar'
|
||||
assert foo.__bytes__() == foo
|
||||
assert type(foo.__bytes__()) == bytes
|
||||
class bytes_subclass(bytes):
|
||||
pass
|
||||
bar = bytes_subclass(b'bar\x00foo')
|
||||
assert bar.__bytes__() == bar
|
||||
assert type(bar.__bytes__()) == bytes
|
||||
|
||||
class A:
|
||||
def __bytes__(self):
|
||||
return b"bytess"
|
||||
|
||||
@@ -128,6 +128,15 @@ impl PyBytes {
|
||||
&self.inner.elements
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn bytes(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<Self> {
|
||||
if zelf.is(&vm.ctx.types.bytes_type) {
|
||||
zelf
|
||||
} else {
|
||||
PyBytes::from(zelf.inner.clone()).into_ref(vm)
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn sizeof(&self) -> usize {
|
||||
size_of::<Self>() + self.inner.elements.len() * size_of::<u8>()
|
||||
|
||||
Reference in New Issue
Block a user