Content-Length: 5859 | pFad | http://github.com/RustPython/RustPython/pull/5712.patch

thub.com From 9039fe3d27eae75d72430f4e30566dea0165cec8 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Thu, 17 Apr 2025 19:28:07 -0700 Subject: [PATCH 1/3] implement bigint serialization compatible with cpython for i < 2^-31 and i > 2^32 Signed-off-by: Ashwin Naren --- vm/src/py_serde.rs | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/vm/src/py_serde.rs b/vm/src/py_serde.rs index 6c23f924e1..a048627986 100644 --- a/vm/src/py_serde.rs +++ b/vm/src/py_serde.rs @@ -71,14 +71,37 @@ impl serde::Serialize for PyObjectSerializer<'_> { } else if self.pyobject.fast_isinstance(self.vm.ctx.types.int_type) { let v = int::get_value(self.pyobject); let int_too_large = || serde::ser::Error::custom("int too large to serialize"); - // TODO: serialize BigInt when it does not fit into i64 - // BigInt implements serialization to a tuple of sign and a list of u32s, - // eg. -1 is [-1, [1]], 0 is [0, []], 12345678900000654321 is [1, [2710766577,2874452364]] - // CPython serializes big ints as long decimal integer literals - if v.is_positive() { - serializer.serialize_u64(v.to_u64().ok_or_else(int_too_large)?) + + // For backwards-compat + if v.to_u64().is_some() || v.to_i64().is_some() { + if v.is_positive() { + serializer.serialize_u64(v.to_u64().ok_or_else(int_too_large)?) + } else { + serializer.serialize_i64(v.to_i64().ok_or_else(int_too_large)?) + } } else { - serializer.serialize_i64(v.to_i64().ok_or_else(int_too_large)?) + // BigInt implements serialization to a tuple of sign and a list of u32s, + // eg. -1 is [-1, [1]], 0 is [0, []], 12345678900000654321 is [1, [2710766577,2874452364]] + // CPython serializes big ints as long decimal integer literals + + let (sign, mut magnitude) = v.to_bytes_le(); + let first = if sign.is_negative() { + -1 + } else { + 1 + }; + let mut u32_list = Vec::new(); + for chunk in magnitude.chunks(4) { + let mut n = 0u32; + for (i, byte) in chunk.iter().enumerate() { + n |= (*byte as u32) << (i * 8); + } + u32_list.push(n); + } + let mut seq = serializer.serialize_seq(Some(2))?; + seq.serialize_element(&self.clone_with_object(&self.vm.ctx.new_int(first)))?; + seq.serialize_element(&self.clone_with_object(&self.vm.ctx.new_list(u32_list)))?; + seq.end() } } else if let Some(list) = self.pyobject.payload_if_subclass::(self.vm) { serialize_seq_elements(serializer, &list.borrow_vec()) From 35d412bf14499a06a7679430e1edba9147e7c0f0 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Thu, 17 Apr 2025 19:33:29 -0700 Subject: [PATCH 2/3] formatting Signed-off-by: Ashwin Naren --- vm/src/py_serde.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/vm/src/py_serde.rs b/vm/src/py_serde.rs index a048627986..e34360011d 100644 --- a/vm/src/py_serde.rs +++ b/vm/src/py_serde.rs @@ -85,11 +85,7 @@ impl serde::Serialize for PyObjectSerializer<'_> { // CPython serializes big ints as long decimal integer literals let (sign, mut magnitude) = v.to_bytes_le(); - let first = if sign.is_negative() { - -1 - } else { - 1 - }; + let first = if sign.is_negative() { -1 } else { 1 }; let mut u32_list = Vec::new(); for chunk in magnitude.chunks(4) { let mut n = 0u32; From 9df3c0ff65366e292f2193d2de9d9f8405a9eb86 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Thu, 17 Apr 2025 19:39:21 -0700 Subject: [PATCH 3/3] fix some errors Signed-off-by: Ashwin Naren --- vm/src/py_serde.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/src/py_serde.rs b/vm/src/py_serde.rs index e34360011d..b6f7ad0ae7 100644 --- a/vm/src/py_serde.rs +++ b/vm/src/py_serde.rs @@ -85,7 +85,7 @@ impl serde::Serialize for PyObjectSerializer<'_> { // CPython serializes big ints as long decimal integer literals let (sign, mut magnitude) = v.to_bytes_le(); - let first = if sign.is_negative() { -1 } else { 1 }; + let first = if sign == Sign::Minus { -1 } else { 1 }; let mut u32_list = Vec::new(); for chunk in magnitude.chunks(4) { let mut n = 0u32; @@ -95,8 +95,8 @@ impl serde::Serialize for PyObjectSerializer<'_> { u32_list.push(n); } let mut seq = serializer.serialize_seq(Some(2))?; - seq.serialize_element(&self.clone_with_object(&self.vm.ctx.new_int(first)))?; - seq.serialize_element(&self.clone_with_object(&self.vm.ctx.new_list(u32_list)))?; + seq.serialize_element(&self.clone_with_object(&self.vm.ctx.new_int(first).into()))?; + seq.serialize_element(&self.clone_with_object(&self.vm.ctx.new_list(u32_list.into_iter().map(|v| &self.vm.ctx.new_int(v).into()).collect())))?; seq.end() } } else if let Some(list) = self.pyobject.payload_if_subclass::(self.vm) {








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/RustPython/RustPython/pull/5712.patch

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy