Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions newsfragments/5809.packaging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`pyo3-macros-backend` no longer depends on `pyo3-build-config`.
4 changes: 0 additions & 4 deletions pyo3-macros-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ rust-version.workspace = true
[dependencies]
heck = "0.5"
proc-macro2 = { version = "1.0.60", default-features = false }
pyo3-build-config = { path = "../pyo3-build-config", version = "=0.28.2", features = ["resolve-config"] }
quote = { version = "1.0.37", default-features = false }

[dependencies.syn]
Expand All @@ -26,9 +25,6 @@ version = "2.0.59"
default-features = false
features = ["derive", "parsing", "printing", "clone-impls", "full", "extra-traits", "visit-mut"]

[build-dependencies]
pyo3-build-config = { path = "../pyo3-build-config", version = "=0.28.2" }

[lints]
workspace = true

Expand Down
4 changes: 0 additions & 4 deletions pyo3-macros-backend/build.rs

This file was deleted.

1 change: 0 additions & 1 deletion pyo3-macros-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ mod pyclass;
mod pyfunction;
mod pyimpl;
mod pymethod;
mod pyversions;
mod quotes;

pub use frompyobject::build_derive_from_pyobject;
Expand Down
33 changes: 12 additions & 21 deletions pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::params::is_forwarded_args;
#[cfg(feature = "experimental-inspect")]
use crate::py_expr::PyExpr;
use crate::pyfunction::{PyFunctionWarning, WarningFactory};
use crate::pyversions::is_abi3_before;
use crate::utils::Ctx;
use crate::{
attributes::{FromPyWithAttribute, TextSignatureAttribute, TextSignatureAttributeValue},
Expand Down Expand Up @@ -393,7 +392,7 @@ impl SelfType {
pub enum CallingConvention {
Noargs, // METH_NOARGS
Varargs, // METH_VARARGS | METH_KEYWORDS
Fastcall, // METH_FASTCALL | METH_KEYWORDS (not compatible with `abi3` feature before 3.10)
Fastcall, // METH_FASTCALL | METH_KEYWORDS
}

impl CallingConvention {
Expand All @@ -404,11 +403,7 @@ impl CallingConvention {
pub fn from_signature(signature: &FunctionSignature<'_>) -> Self {
if signature.python_signature.has_no_args() {
Self::Noargs
} else if signature.python_signature.kwargs.is_none() && !is_abi3_before(3, 10) {
// For functions that accept **kwargs, always prefer varargs for now based on
// historical performance testing.
//
// FASTCALL not compatible with `abi3` before 3.10
} else if signature.python_signature.kwargs.is_none() {
Self::Fastcall
} else {
Self::Varargs
Expand Down Expand Up @@ -852,19 +847,15 @@ impl<'a> FnSpec<'a> {
let call = rust_call(args, holders);

quote! {
unsafe fn #ident<'py>(
py: #pyo3_path::Python<'py>,
_slf: *mut #pyo3_path::ffi::PyObject,
_args: *const *mut #pyo3_path::ffi::PyObject,
_nargs: #pyo3_path::ffi::Py_ssize_t,
_kwnames: *mut #pyo3_path::ffi::PyObject
) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> {
let function = #rust_name; // Shadow the function name to avoid #3017
#arg_convert
#warnings
let result = #call;
result
}
#pyo3_path::impl_::pymethods::maybe_define_fastcall_function_with_keywords!(
#ident, py, _slf, _args, _nargs, _kwargs, {
let function = #rust_name; // Shadow the function name to avoid #3017
#arg_convert
#warnings
let result = #call;
result
}
);
}
}
CallingConvention::Varargs => {
Expand Down Expand Up @@ -908,7 +899,7 @@ impl<'a> FnSpec<'a> {
let trampoline = match convention {
CallingConvention::Noargs => Ident::new("noargs", Span::call_site()),
CallingConvention::Fastcall => {
Ident::new("fastcall_cfunction_with_keywords", Span::call_site())
Ident::new("maybe_fastcall_cfunction_with_keywords", Span::call_site())
}
CallingConvention::Varargs => Ident::new("cfunction_with_keywords", Span::call_site()),
};
Expand Down
9 changes: 6 additions & 3 deletions pyo3-macros-backend/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,15 @@ pub fn impl_arg_params(

let extract_expression = if fastcall {
quote! {
DESCRIPTION.extract_arguments_fastcall::<#args_handler, #kwargs_handler>(
#pyo3_path::impl_::pymethods::maybe_extract_arguments_fastcall!(
DESCRIPTION,
py,
_args,
_nargs,
_kwnames,
&mut #args_array
_kwargs,
#args_array,
#args_handler,
#kwargs_handler
)?
}
} else {
Expand Down
68 changes: 40 additions & 28 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ use crate::pymethod::{
MethodAndSlotDef, PropertyType, SlotDef, __GETITEM__, __HASH__, __INT__, __LEN__, __NEW__,
__REPR__, __RICHCMP__, __STR__,
};
use crate::pyversions::{is_abi3_before, is_py_before};
use crate::utils::{self, apply_renaming_rule, get_doc, Ctx, PythonDoc};
use crate::utils::{self, apply_renaming_rule, get_doc, locate_tokens_at, Ctx, PythonDoc};
use crate::PyFunctionOptions;

/// If the class is derived from a Rust `struct` or `enum`.
Expand Down Expand Up @@ -220,24 +219,14 @@ impl PyClassPyO3Options {

match option {
PyClassPyO3Option::Crate(krate) => set_option!(krate),
PyClassPyO3Option::Dict(dict) => {
ensure_spanned!(
!is_abi3_before(3, 9),
dict.span() => "`dict` requires Python >= 3.9 when using the `abi3` feature"
);
set_option!(dict);
}
PyClassPyO3Option::Dict(dict) => set_option!(dict),
PyClassPyO3Option::Eq(eq) => set_option!(eq),
PyClassPyO3Option::EqInt(eq_int) => set_option!(eq_int),
PyClassPyO3Option::Extends(extends) => set_option!(extends),
PyClassPyO3Option::Freelist(freelist) => set_option!(freelist),
PyClassPyO3Option::Frozen(frozen) => set_option!(frozen),
PyClassPyO3Option::GetAll(get_all) => set_option!(get_all),
PyClassPyO3Option::ImmutableType(immutable_type) => {
ensure_spanned!(
!(is_py_before(3, 10) || is_abi3_before(3, 14)),
immutable_type.span() => "`immutable_type` requires Python >= 3.10 or >= 3.14 (ABI3)"
);
set_option!(immutable_type)
}
PyClassPyO3Option::Hash(hash) => set_option!(hash),
Expand All @@ -252,13 +241,7 @@ impl PyClassPyO3Options {
PyClassPyO3Option::Str(str) => set_option!(str),
PyClassPyO3Option::Subclass(subclass) => set_option!(subclass),
PyClassPyO3Option::Unsendable(unsendable) => set_option!(unsendable),
PyClassPyO3Option::Weakref(weakref) => {
ensure_spanned!(
!is_abi3_before(3, 9),
weakref.span() => "`weakref` requires Python >= 3.9 when using the `abi3` feature"
);
set_option!(weakref);
}
PyClassPyO3Option::Weakref(weakref) => set_option!(weakref),
PyClassPyO3Option::Generic(generic) => set_option!(generic),
PyClassPyO3Option::SkipFromPyObject(skip_from_py_object) => {
ensure_spanned!(
Expand Down Expand Up @@ -2924,15 +2907,41 @@ impl<'a> PyClassImplsBuilder<'a> {
}
});

let assertions = if attr.options.unsendable.is_some() {
TokenStream::new()
} else {
let assert = quote_spanned! { cls.span() => #pyo3_path::impl_::pyclass::assert_pyclass_send_sync::<#cls>() };
quote! {
const _: () = #assert;
}
let mut assertions = TokenStream::new();

// Classes must implement send / sync, unless `#[pyclass(unsendable)]` is used
if attr.options.unsendable.is_none() {
let pyo3_path = locate_tokens_at(pyo3_path.to_token_stream(), cls.span());
assertions.extend(quote_spanned! { cls.span() => #pyo3_path::impl_::pyclass::assert_pyclass_send_sync::<#cls>(); });
};

if let Some(kw) = &attr.options.dict {
let pyo3_path = locate_tokens_at(pyo3_path.to_token_stream(), kw.span());
assertions.extend(quote_spanned! {
kw.span() =>
const ASSERT_DICT_SUPPORTED: () = #pyo3_path::impl_::pyclass::assert_dict_supported();

});
}

if let Some(kw) = &attr.options.weakref {
let pyo3_path = locate_tokens_at(pyo3_path.to_token_stream(), kw.span());
assertions.extend(quote_spanned! {
kw.span() => {
const ASSERT_WEAKREF_SUPPORTED: () = #pyo3_path::impl_::pyclass::assert_weakref_supported();
};
});
}

if let Some(kw) = &attr.options.immutable_type {
let pyo3_path = locate_tokens_at(pyo3_path.to_token_stream(), kw.span());
assertions.extend(quote_spanned! {
kw.span() => {
const ASSERT_IMMUTABLE_SUPPORTED: () = #pyo3_path::impl_::pyclass::assert_immutable_type_supported();
};
});
}

let deprecation = if self.attr.options.skip_from_py_object.is_none()
&& self.attr.options.from_py_object.is_none()
{
Expand Down Expand Up @@ -2976,7 +2985,10 @@ impl<'a> PyClassImplsBuilder<'a> {

#extract_pyclass_with_clone

#assertions
#[allow(dead_code)]
const _: () ={
#assertions
};

#pyclass_base_type_impl

Expand Down
11 changes: 0 additions & 11 deletions pyo3-macros-backend/src/pyversions.rs

This file was deleted.

36 changes: 35 additions & 1 deletion pyo3-macros-backend/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::attributes::{CrateAttribute, RenamingRule};
use proc_macro2::{Span, TokenStream};
use proc_macro2::{Span, TokenStream, TokenTree};
use quote::{quote, ToTokens, TokenStreamExt};
use std::ffi::CString;
use std::mem::take;
Expand Down Expand Up @@ -341,3 +341,37 @@ impl ToTokens for StaticIdent {
tokens.append(syn::Ident::new(self.0, Span::call_site()));
}
}

/// Adjusts a tokes stream so that the location for the stream comes from `Span`.
///
/// This affects where error messages will arise in the compiler output.
pub(crate) fn locate_tokens_at(tokens: TokenStream, span: Span) -> TokenStream {
fn set_span_recursively(tokens: TokenStream, span: Span) -> TokenStream {
tokens
.into_iter()
.map(|tt| match tt {
TokenTree::Group(g) => {
let inner = set_span_recursively(g.stream(), span);
let mut new_group = proc_macro2::Group::new(g.delimiter(), inner);
new_group.set_span(span);
TokenTree::Group(new_group)
}
TokenTree::Ident(mut ident) => {
ident.set_span(span);
TokenTree::Ident(ident)
}
TokenTree::Punct(mut punct) => {
punct.set_span(span);
TokenTree::Punct(punct)
}
TokenTree::Literal(mut lit) => {
lit.set_span(span);
TokenTree::Literal(lit)
}
})
.collect()
}

let output_span = tokens.span().located_at(span);
set_span_recursively(tokens, output_span)
}
27 changes: 27 additions & 0 deletions src/impl_/pyclass/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ where
{
}

#[track_caller]
#[allow(clippy::assertions_on_constants, reason = "invoked by a proc macro")]
pub const fn assert_dict_supported() {
assert!(
cfg!(any(not(Py_LIMITED_API), Py_3_9)),
"`dict` requires Python >= 3.9 when using the `abi3` feature"
);
}

#[track_caller]
#[allow(clippy::assertions_on_constants, reason = "invoked by a proc macro")]
pub const fn assert_weakref_supported() {
assert!(
cfg!(any(not(Py_LIMITED_API), Py_3_9)),
"`weakref` requires Python >= 3.9 when using the `abi3` feature"
)
}

#[track_caller]
#[allow(clippy::assertions_on_constants, reason = "invoked by a proc macro")]
pub const fn assert_immutable_type_supported() {
assert!(
cfg!(any(all(Py_3_10, not(Py_LIMITED_API)), Py_3_14)),
"`immutable_type` requires Python >= 3.10 (or >= 3.14 when using the `abi3` feature)"
);
}

mod tests {
#[cfg(feature = "macros")]
#[test]
Expand Down
Loading
Loading