Skip to content

implement bigint serialization #5712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Next Next commit
implement bigint serialization
compatible with cpython for i < 2^-31 and i > 2^32

Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
  • Loading branch information
arihant2math committed Apr 18, 2025
commit 9039fe3d27eae75d72430f4e30566dea0165cec8
37 changes: 30 additions & 7 deletions vm/src/py_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<PyList>(self.vm) {
serialize_seq_elements(serializer, &list.borrow_vec())
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy