Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions crates/vm/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
AsObject, Py, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
builtins::{PyBaseExceptionRef, PyBytesRef, PyTuple, PyTupleRef, PyType, PyTypeRef},
builtins::{PyBaseExceptionRef, PyBytesRef, PyComplex, PyTuple, PyTupleRef, PyType, PyTypeRef},
common::{static_cell, str::wchar_t},
convert::ToPyObject,
exceptions,
Expand Down Expand Up @@ -780,28 +780,45 @@ struct PackFloatComplex(f32, f32);
struct PackDoubleComplex(f64, f64);

macro_rules! make_pack_complex {
($T:ty, $Elem:ty, $Bits:ty) => {
($T:ty, $Elem:ty, $Bits:ty, $fmt:literal) => {
impl Packable for $T {
fn pack<E: ByteOrder>(
vm: &VirtualMachine,
_code: FormatType,
arg: PyObjectRef,
data: &mut [u8],
) -> Result<(), PackError> {
let c = ArgIntoComplex::try_from_object(vm, arg)
.map_err(|_| {
PackError::new(
PackErrorKind::Type,
vm,
"required argument is not a complex",
)
})?
.into_complex();
let half = size_of::<$Elem>();
(c.re as $Elem).to_bits().pack_int::<E>(&mut data[..half]);
(c.im as $Elem)
.to_bits()
.pack_int::<E>(&mut data[half..half * 2]);
let c = if let Some(value) = arg.downcast_ref::<PyComplex>() {
value.to_complex()
} else {
ArgIntoComplex::try_from_object(vm, arg)
.map_err(|_| {
PackError::new(
PackErrorKind::Type,
vm,
"required argument is not a complex",
)
})?
.into_complex()
};
for (component, bytes) in [c.re, c.im]
.into_iter()
.zip(data.chunks_exact_mut(size_of::<$Elem>()))
{
let narrowed = component as $Elem;
// CPython uses native casts for matching-endian complex formats.
if E::convert(1u16) != 1 && narrowed.is_infinite() != component.is_infinite() {
return Err(PackError {
kind: PackErrorKind::Value,
exception: vm.new_overflow_error(concat!(
"float too large to pack with ",
$fmt,
" format"
)),
});
}
narrowed.to_bits().pack_int::<E>(bytes);
}
Ok(())
}

Expand All @@ -815,8 +832,8 @@ macro_rules! make_pack_complex {
};
}

make_pack_complex!(PackFloatComplex, f32, u32);
make_pack_complex!(PackDoubleComplex, f64, u64);
make_pack_complex!(PackFloatComplex, f32, u32, "f");
make_pack_complex!(PackDoubleComplex, f64, u64, "d");

impl Packable for f16 {
fn pack<E: ByteOrder>(
Expand Down
4 changes: 3 additions & 1 deletion crates/vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ impl PyObjectRef {
let result = method?.call((), vm)?;

let ret_class = result.class().to_owned();
if let Some(ret) = result.downcast_ref::<PyComplex>() {
if let Some(ret) = result.downcast_ref::<PyComplex>()
&& !result.class().is(vm.ctx.types.complex_type)
{
_warnings::warn(
vm.ctx.exceptions.deprecation_warning,
format!(
Expand Down
30 changes: 30 additions & 0 deletions extra_tests/snippets/stdlib_struct_complex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import struct
import sys
import warnings

from testutils import assert_raises


class ComplexLike:
def __complex__(self):
return 1 + 2j


class ComplexSubclass(complex):
def __complex__(self):
raise AssertionError("complex subclasses use their stored value")


with warnings.catch_warnings():
warnings.simplefilter("error")
for code in ("F", "D"):
expected = struct.pack(code, 1 + 2j)
assert struct.pack(code, ComplexLike()) == expected
assert struct.pack(code, ComplexSubclass(1, 2)) == expected

native = "<" if sys.byteorder == "little" else ">"
swapped = ">" if sys.byteorder == "little" else "<"
for value in (complex(1e300, 0), complex(0, 1e300)):
assert struct.pack(native + "F", value) == struct.pack("@F", value)
with assert_raises(OverflowError):
struct.pack(swapped + "F", value)
Loading