GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add number protocol functions to c-api by bschoenmaeckers · Pull Request #8005 · RustPython/RustPython

Add number protocol functions to c-api #8005

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.

Sign up for GitHub

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

Merged
youknowone merged 1 commit into RustPython:main from bschoenmaeckers:c-api-number
Jun 3, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Add number protocol functions to c-api #8005

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
d9c591b
Add number protocol functions
bschoenmaeckers Jun 1, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

2 changes: 2 additions & 0 deletions 2 crates/capi/src/abstract_.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use crate::{PyObject, pystate::with_vm};
use alloc::slice;
use core::ffi::c_int;
pub use mapping::*;
pub use number::*;
use rustpython_vm::builtins::{PyDict, PyStr, PyTuple};
use rustpython_vm::function::{FuncArgs, KwArgs, PosArgs};
use rustpython_vm::{AsObject, Py, PyObjectRef, PyResult, VirtualMachine};
pub use sequence::*;

mod mapping;
mod number;
mod sequence;

const PY_VECTORCALL_ARGUMENTS_OFFSET: usize = 1usize << (usize::BITS as usize - 1);
Expand Down
86 changes: 86 additions & 0 deletions 86 crates/capi/src/abstract_/number.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use crate::{PyObject, pystate::with_vm};

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyNumber_Add(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject {
with_vm(|vm| vm._add(unsafe { &*o1 }, unsafe { &*o2 }))
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyNumber_Index(obj: *mut PyObject) -> *mut PyObject {
with_vm(|vm| unsafe { &*obj }.try_index(vm))
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyNumber_Lshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject {
with_vm(|vm| vm._lshift(unsafe { &*o1 }, unsafe { &*o2 }))
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyNumber_Or(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject {
with_vm(|vm| vm._or(unsafe { &*o1 }, unsafe { &*o2 }))
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyNumber_Rshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject {
with_vm(|vm| vm._rshift(unsafe { &*o1 }, unsafe { &*o2 }))
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyNumber_Subtract(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject {
with_vm(|vm| vm._sub(unsafe { &*o1 }, unsafe { &*o2 }))
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


#[cfg(false)]
mod tests {
Comment thread
bschoenmaeckers marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

use pyo3::prelude::*;

#[test]
fn add() {
Python::attach(|py| {
let lhs = 40i64.into_pyobject(py).unwrap();
let rhs = 2i64.into_pyobject(py).unwrap();
let out = lhs.add(rhs).unwrap();
assert_eq!(out.extract::<i64>().unwrap(), 42);
})
}

#[test]
fn lshift() {
Python::attach(|py| {
let lhs = 1i64.into_pyobject(py).unwrap();
let rhs = 5i64.into_pyobject(py).unwrap();
let out = lhs.lshift(rhs).unwrap();
assert_eq!(out.extract::<i64>().unwrap(), 32);
})
}

#[test]
fn bit_or() {
Python::attach(|py| {
let lhs = 0b1010i64.into_pyobject(py).unwrap();
let rhs = 0b0110i64.into_pyobject(py).unwrap();
let out = lhs.bitor(rhs).unwrap();
assert_eq!(out.extract::<i64>().unwrap(), 0b1110);
})
}

#[test]
fn rshift() {
Python::attach(|py| {
let lhs = 128i64.into_pyobject(py).unwrap();
let rhs = 3i64.into_pyobject(py).unwrap();
let out = lhs.rshift(rhs).unwrap();
assert_eq!(out.extract::<i64>().unwrap(), 16);
})
}

#[test]
fn subtract() {
Python::attach(|py| {
let lhs = 50i64.into_pyobject(py).unwrap();
let rhs = 8i64.into_pyobject(py).unwrap();
let out = lhs.sub(rhs).unwrap();
assert_eq!(out.extract::<i64>().unwrap(), 42);
})
}
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.