Skip to content
Open

No std #5856

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ parking_lot = { version = "0.12.3", features = ["arc_lock"] }
pyo3-build-config = { path = "pyo3-build-config", version = "=0.28.2", features = ["resolve-config"] }

[features]
default = ["macros"]
default = ["std", "macros"]

std = []

# Enables support for `async fn` for `#[pyfunction]` and `#[pymethods]`.
experimental-async = ["macros", "pyo3-macros/experimental-async"]
Expand Down
1 change: 1 addition & 0 deletions newsfragments/5856.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add feature `std` which is enabled by default. Implemetations of `pyo3` traits on `std` types are gated behind this feature.
8 changes: 4 additions & 4 deletions pyo3-ffi/src/abstract_.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use crate::object::*;
use crate::pyport::Py_ssize_t;
use core::ffi::{c_char, c_int};
#[cfg(any(Py_3_12, all(Py_3_8, not(Py_LIMITED_API))))]
use libc::size_t;
use std::ffi::{c_char, c_int};

#[inline]
#[cfg(all(
not(Py_3_13), // CPython exposed as a function in 3.13, in object.h
not(all(PyPy, not(Py_3_11))) // PyPy exposed as a function until PyPy 3.10, macro in 3.11+
))]
pub unsafe fn PyObject_DelAttrString(o: *mut PyObject, attr_name: *const c_char) -> c_int {
PyObject_SetAttrString(o, attr_name, std::ptr::null_mut())
PyObject_SetAttrString(o, attr_name, core::ptr::null_mut())
}

#[inline]
Expand All @@ -19,7 +19,7 @@ pub unsafe fn PyObject_DelAttrString(o: *mut PyObject, attr_name: *const c_char)
not(all(PyPy, not(Py_3_11))) // PyPy exposed as a function until PyPy 3.10, macro in 3.11+
))]
pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_int {
PyObject_SetAttr(o, attr_name, std::ptr::null_mut())
PyObject_SetAttr(o, attr_name, core::ptr::null_mut())
}

extern "C" {
Expand Down Expand Up @@ -81,7 +81,7 @@ extern "C" {
}
#[cfg(any(Py_3_12, all(Py_3_8, not(Py_LIMITED_API))))]
pub const PY_VECTORCALL_ARGUMENTS_OFFSET: size_t =
1 << (8 * std::mem::size_of::<size_t>() as size_t - 1);
1 << (8 * core::mem::size_of::<size_t>() as size_t - 1);

extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyObject_Vectorcall")]
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/boolobject.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(not(GraalPy))]
use crate::longobject::PyLongObject;
use crate::object::*;
use std::ffi::{c_int, c_long};
use std::ptr::addr_of_mut;
use core::ffi::{c_int, c_long};
use core::ptr::addr_of_mut;

#[inline]
pub unsafe fn PyBool_Check(op: *mut PyObject) -> c_int {
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/bytearrayobject.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::object::*;
use crate::pyport::Py_ssize_t;
use std::ffi::{c_char, c_int};
use std::ptr::addr_of_mut;
use core::ffi::{c_char, c_int};
use core::ptr::addr_of_mut;

#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))]
#[repr(C)]
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/bytesobject.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::object::*;
use crate::pyport::Py_ssize_t;
use std::ffi::{c_char, c_int};
use std::ptr::addr_of_mut;
use core::ffi::{c_char, c_int};
use core::ptr::addr_of_mut;

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/ceval.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::object::PyObject;
use crate::pytypedefs::PyThreadState;
use std::ffi::{c_char, c_int, c_void};
use core::ffi::{c_char, c_int, c_void};

extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyEval_EvalCode")]
Expand Down Expand Up @@ -39,7 +39,7 @@ extern "C" {
#[inline]
pub unsafe fn PyEval_CallObject(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject {
#[allow(deprecated)]
PyEval_CallObjectWithKeywords(func, arg, std::ptr::null_mut())
PyEval_CallObjectWithKeywords(func, arg, core::ptr::null_mut())
}

extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion pyo3-ffi/src/codecs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::object::PyObject;
use std::ffi::{c_char, c_int};
use core::ffi::{c_char, c_int};

extern "C" {
pub fn PyCodec_Register(search_function: *mut PyObject) -> c_int;
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/compat/py_3_10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ compat_function!(
#[inline]
pub unsafe fn PyModule_AddObjectRef(
module: *mut crate::PyObject,
name: *const std::ffi::c_char,
name: *const core::ffi::c_char,
value: *mut crate::PyObject,
) -> std::ffi::c_int {
) -> core::ffi::c_int {
if value.is_null() && crate::PyErr_Occurred().is_null() {
crate::PyErr_SetString(
crate::PyExc_SystemError,
Expand Down
24 changes: 12 additions & 12 deletions pyo3-ffi/src/compat/py_3_13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ compat_function!(
dp: *mut crate::PyObject,
key: *mut crate::PyObject,
result: *mut *mut crate::PyObject,
) -> std::ffi::c_int {
) -> core::ffi::c_int {
use crate::{compat::Py_NewRef, PyDict_GetItemWithError, PyErr_Occurred};

let item = PyDict_GetItemWithError(dp, key);
if !item.is_null() {
*result = Py_NewRef(item);
return 1; // found
}
*result = std::ptr::null_mut();
*result = core::ptr::null_mut();
if PyErr_Occurred().is_null() {
return 0; // not found
}
Expand Down Expand Up @@ -43,7 +43,7 @@ compat_function!(

#[inline]
pub unsafe fn PyImport_AddModuleRef(
name: *const std::ffi::c_char,
name: *const core::ffi::c_char,
) -> *mut crate::PyObject {
use crate::{compat::Py_XNewRef, PyImport_AddModule};

Expand All @@ -58,25 +58,25 @@ compat_function!(
pub unsafe fn PyWeakref_GetRef(
reference: *mut crate::PyObject,
pobj: *mut *mut crate::PyObject,
) -> std::ffi::c_int {
) -> core::ffi::c_int {
use crate::{
compat::Py_NewRef, PyErr_SetString, PyExc_TypeError, PyWeakref_Check,
PyWeakref_GetObject, Py_None,
};

if !reference.is_null() && PyWeakref_Check(reference) == 0 {
*pobj = std::ptr::null_mut();
*pobj = core::ptr::null_mut();
PyErr_SetString(PyExc_TypeError, c"expected a weakref".as_ptr());
return -1;
}
let obj = PyWeakref_GetObject(reference);
if obj.is_null() {
// SystemError if reference is NULL
*pobj = std::ptr::null_mut();
*pobj = core::ptr::null_mut();
return -1;
}
if obj == Py_None() {
*pobj = std::ptr::null_mut();
*pobj = core::ptr::null_mut();
return 0;
}
*pobj = Py_NewRef(obj);
Expand All @@ -91,7 +91,7 @@ compat_function!(
pub unsafe fn PyList_Extend(
list: *mut crate::PyObject,
iterable: *mut crate::PyObject,
) -> std::ffi::c_int {
) -> core::ffi::c_int {
crate::PyList_SetSlice(list, crate::PY_SSIZE_T_MAX, crate::PY_SSIZE_T_MAX, iterable)
}
);
Expand All @@ -100,8 +100,8 @@ compat_function!(
originally_defined_for(Py_3_13);

#[inline]
pub unsafe fn PyList_Clear(list: *mut crate::PyObject) -> std::ffi::c_int {
crate::PyList_SetSlice(list, 0, crate::PY_SSIZE_T_MAX, std::ptr::null_mut())
pub unsafe fn PyList_Clear(list: *mut crate::PyObject) -> core::ffi::c_int {
crate::PyList_SetSlice(list, 0, crate::PY_SSIZE_T_MAX, core::ptr::null_mut())
}
);

Expand All @@ -111,9 +111,9 @@ compat_function!(
#[inline]
pub unsafe fn PyModule_Add(
module: *mut crate::PyObject,
name: *const std::ffi::c_char,
name: *const core::ffi::c_char,
value: *mut crate::PyObject,
) -> std::ffi::c_int {
) -> core::ffi::c_int {
let result = crate::compat::PyModule_AddObjectRef(module, name, value);
crate::Py_XDECREF(value);
result
Expand Down
6 changes: 3 additions & 3 deletions pyo3-ffi/src/compat/py_3_14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ compat_function!(

#[inline]
pub unsafe fn Py_HashBuffer(
ptr: *const std::ffi::c_void,
ptr: *const core::ffi::c_void,
len: crate::Py_ssize_t,
) -> crate::Py_hash_t {
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
Expand All @@ -13,7 +13,7 @@ compat_function!(

#[cfg(any(Py_LIMITED_API, PyPy))]
{
let bytes = crate::PyBytes_FromStringAndSize(ptr as *const std::ffi::c_char, len);
let bytes = crate::PyBytes_FromStringAndSize(ptr as *const core::ffi::c_char, len);
if bytes.is_null() {
-1
} else {
Expand All @@ -32,7 +32,7 @@ compat_function!(
pub unsafe fn PyIter_NextItem(
iter: *mut crate::PyObject,
item: *mut *mut crate::PyObject,
) -> std::ffi::c_int {
) -> core::ffi::c_int {
*item = crate::PyIter_Next(iter);
if !(*item).is_null() {
1
Expand Down
34 changes: 17 additions & 17 deletions pyo3-ffi/src/compat/py_3_15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ compat_function!(

if size < 0 {
crate::PyErr_SetString(crate::PyExc_ValueError, c"size must be >= 0".as_ptr() as *const _);
return std::ptr::null_mut();
return core::ptr::null_mut();
}

let writer: *mut PyBytesWriter = crate::PyMem_Malloc(std::mem::size_of::<PyBytesWriter>()).cast();
let writer: *mut PyBytesWriter = crate::PyMem_Malloc(core::mem::size_of::<PyBytesWriter>()).cast();
if writer.is_null() {
crate::PyErr_NoMemory();
return std::ptr::null_mut();
return core::ptr::null_mut();
}

(*writer).obj = std::ptr::null_mut();
(*writer).obj = core::ptr::null_mut();
(*writer).size = 0;

if size >=1 {
if _PyBytesWriter_Resize_impl(writer, size, 0) < 0 {
PyBytesWriter_Discard(writer);
return std::ptr::null_mut();
return core::ptr::null_mut();
}

(*writer).size = size;
Expand Down Expand Up @@ -75,9 +75,9 @@ compat_function!(
} else {
if size != crate::PyBytes_Size((*writer).obj) && crate::_PyBytes_Resize(&mut (*writer).obj, size) < 0 {
PyBytesWriter_Discard(writer);
return std::ptr::null_mut();
return core::ptr::null_mut();
}
std::mem::replace(&mut (*writer).obj, std::ptr::null_mut())
core::mem::replace(&mut (*writer).obj, core::ptr::null_mut())
};

PyBytesWriter_Discard(writer);
Expand All @@ -90,7 +90,7 @@ compat_function!(
originally_defined_for(all(Py_3_15, not(Py_LIMITED_API)));

#[inline]
pub unsafe fn PyBytesWriter_GetData(writer: *mut PyBytesWriter) -> *mut std::ffi::c_void {
pub unsafe fn PyBytesWriter_GetData(writer: *mut PyBytesWriter) -> *mut core::ffi::c_void {
if (*writer).obj.is_null() {
(*writer).small_buffer.as_ptr() as *mut _
} else {
Expand All @@ -114,7 +114,7 @@ compat_function!(
originally_defined_for(all(Py_3_15, not(Py_LIMITED_API)));

#[inline]
pub unsafe fn PyBytesWriter_Resize(writer: *mut PyBytesWriter, size: crate::Py_ssize_t) -> std::ffi::c_int {
pub unsafe fn PyBytesWriter_Resize(writer: *mut PyBytesWriter, size: crate::Py_ssize_t) -> core::ffi::c_int {
if size < 0 {
crate::PyErr_SetString(crate::PyExc_ValueError, c"size must be >= 0".as_ptr());
return -1;
Expand All @@ -130,7 +130,7 @@ compat_function!(
#[repr(C)]
#[cfg(not(any(Py_3_15, Py_LIMITED_API)))]
pub struct PyBytesWriter {
small_buffer: [std::ffi::c_char; 256],
small_buffer: [core::ffi::c_char; 256],
obj: *mut crate::PyObject,
size: crate::Py_ssize_t,
}
Expand All @@ -140,13 +140,13 @@ pub struct PyBytesWriter {
unsafe fn _PyBytesWriter_Resize_impl(
writer: *mut PyBytesWriter,
mut size: crate::Py_ssize_t,
resize: std::ffi::c_int,
) -> std::ffi::c_int {
resize: core::ffi::c_int,
) -> core::ffi::c_int {
let overallocate = resize;
assert!(size >= 0);

let allocated = if (*writer).obj.is_null() {
std::mem::size_of_val(&(*writer).small_buffer) as _
core::mem::size_of_val(&(*writer).small_buffer) as _
} else {
crate::PyBytes_Size((*writer).obj)
};
Expand All @@ -173,18 +173,18 @@ unsafe fn _PyBytesWriter_Resize_impl(
}
assert!(!(*writer).obj.is_null())
} else {
(*writer).obj = crate::PyBytes_FromStringAndSize(std::ptr::null_mut(), size);
(*writer).obj = crate::PyBytes_FromStringAndSize(core::ptr::null_mut(), size);
if (*writer).obj.is_null() {
return -1;
}

if resize > 0 {
assert!((size as usize) > std::mem::size_of_val(&(*writer).small_buffer));
assert!((size as usize) > core::mem::size_of_val(&(*writer).small_buffer));

std::ptr::copy_nonoverlapping(
core::ptr::copy_nonoverlapping(
(*writer).small_buffer.as_ptr(),
crate::PyBytes_AS_STRING((*writer).obj) as *mut _,
std::mem::size_of_val(&(*writer).small_buffer),
core::mem::size_of_val(&(*writer).small_buffer),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/compat/py_3_9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ compat_function!(

#[inline]
pub unsafe fn PyObject_CallNoArgs(obj: *mut crate::PyObject) -> *mut crate::PyObject {
crate::PyObject_CallObject(obj, std::ptr::null_mut())
crate::PyObject_CallObject(obj, core::ptr::null_mut())
}
);

Expand All @@ -15,6 +15,6 @@ compat_function!(

#[inline]
pub unsafe fn PyObject_CallMethodNoArgs(obj: *mut crate::PyObject, name: *mut crate::PyObject) -> *mut crate::PyObject {
crate::PyObject_CallMethodObjArgs(obj, name, std::ptr::null_mut::<crate::PyObject>())
crate::PyObject_CallMethodObjArgs(obj, name, core::ptr::null_mut::<crate::PyObject>())
}
);
2 changes: 1 addition & 1 deletion pyo3-ffi/src/compile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::c_int;
use core::ffi::c_int;

pub const Py_single_input: c_int = 256;
pub const Py_file_input: c_int = 257;
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/complexobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::object::*;
use std::ffi::{c_double, c_int};
use std::ptr::addr_of_mut;
use core::ffi::{c_double, c_int};
use core::ptr::addr_of_mut;

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down
4 changes: 2 additions & 2 deletions pyo3-ffi/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::object::{PyObject, PyTypeObject, Py_TYPE};
use std::ffi::{c_char, c_int};
use std::ptr::addr_of_mut;
use core::ffi::{c_char, c_int};
use core::ptr::addr_of_mut;

extern "C" {
pub static mut PyContext_Type: PyTypeObject;
Expand Down
Loading
Loading