Skip to content

Commit 108e1ef

Browse files
committed
Rework pystruct, again
1 parent 0b58527 commit 108e1ef

File tree

6 files changed

+243
-270
lines changed

6 files changed

+243
-270
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ rustpython-jit = { path = "../jit", optional = true, version = "0.1.2" }
4747
rustpython-pylib = { path = "pylib-crate", optional = true, version = "0.1.0" }
4848
serde = { version = "1.0.66", features = ["derive"] }
4949
serde_json = "1.0"
50-
byteorder = "1.2.6"
5150
regex = "1"
5251
rustc_version_runtime = "0.1.*"
5352
statrs = "0.12.0"

vm/src/builtins/complex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ fn try_complex(obj: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Option<(Compl
435435
if let Some(complex) = obj.payload_if_subclass::<PyComplex>(vm) {
436436
return Ok(Some((complex.value, true)));
437437
}
438-
if let Some(float) = float::try_float(obj, vm)? {
438+
if let Some(float) = float::try_float_opt(obj, vm)? {
439439
return Ok(Some((Complex64::new(float, 0.0), false)));
440440
}
441441
Ok(None)

vm/src/builtins/float.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl From<f64> for PyFloat {
5555
}
5656
}
5757

58-
pub(crate) fn try_float(obj: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Option<f64>> {
58+
pub fn try_float_opt(obj: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Option<f64>> {
5959
if let Some(float) = obj.payload_if_exact::<PyFloat>(vm) {
6060
return Ok(Some(float.value));
6161
}
@@ -76,6 +76,11 @@ pub(crate) fn try_float(obj: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Opti
7676
Ok(None)
7777
}
7878

79+
pub fn try_float(obj: &PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
80+
try_float_opt(obj, vm)?
81+
.ok_or_else(|| vm.new_type_error(format!("must be real number, not {}", obj.class().name)))
82+
}
83+
7984
pub(crate) fn to_op_float(obj: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Option<f64>> {
8085
let v = if let Some(float) = obj.payload_if_subclass::<PyFloat>(vm) {
8186
Some(float.value)
@@ -176,7 +181,7 @@ impl PyFloat {
176181
val
177182
};
178183

179-
if let Some(f) = try_float(&val, vm)? {
184+
if let Some(f) = try_float_opt(&val, vm)? {
180185
f
181186
} else if let Some(s) = val.payload_if_subclass::<PyStr>(vm) {
182187
float_ops::parse_str(s.borrow_value().trim()).ok_or_else(|| {
@@ -581,9 +586,7 @@ impl IntoPyFloat {
581586

582587
impl TryFromObject for IntoPyFloat {
583588
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
584-
let value = try_float(&obj, vm)?.ok_or_else(|| {
585-
vm.new_type_error(format!("must be real number, not {}", obj.class().name))
586-
})?;
589+
let value = try_float(&obj, vm)?;
587590
Ok(IntoPyFloat { value })
588591
}
589592
}

vm/src/builtins/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl PyMemoryView {
500500
.get_pos(i)
501501
.ok_or_else(|| vm.new_index_error("index out of range".to_owned()))?;
502502
let itemsize = zelf.options.itemsize;
503-
let data = zelf.format_spec.pack(&[value], vm)?;
503+
let data = zelf.format_spec.pack(vec![value], vm)?;
504504
zelf.obj_bytes_mut()[i..i + itemsize].copy_from_slice(&data);
505505
Ok(())
506506
}

0 commit comments

Comments
 (0)