Skip to content

Commit

Permalink
pyo3_path: rename internal types/variables to Crate/krate
Browse files Browse the repository at this point in the history
  • Loading branch information
birkenfeld committed Dec 9, 2021
1 parent 2d49dc3 commit d0381c2
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 102 deletions.
6 changes: 3 additions & 3 deletions pyo3-macros-backend/src/attributes.rs
Expand Up @@ -45,14 +45,14 @@ impl Parse for NameAttribute {

/// For specifying the path to the pyo3 crate.
#[derive(Clone, Debug, PartialEq)]
pub struct PyO3PathAttribute(pub Path);
pub struct CrateAttribute(pub Path);

impl Parse for PyO3PathAttribute {
impl Parse for CrateAttribute {
fn parse(input: ParseStream) -> Result<Self> {
let _: Token![crate] = input.parse()?;
let _: Token![=] = input.parse()?;
let string_literal: LitStr = input.parse()?;
string_literal.parse().map(PyO3PathAttribute)
string_literal.parse().map(CrateAttribute)
}
}

Expand Down
22 changes: 11 additions & 11 deletions pyo3-macros-backend/src/from_pyobject.rs
@@ -1,6 +1,6 @@
use crate::{
attributes::{self, get_pyo3_options, FromPyWithAttribute, PyO3PathAttribute},
utils::get_pyo3_path,
attributes::{self, get_pyo3_options, CrateAttribute, FromPyWithAttribute},
utils::get_pyo3_crate,
};
use proc_macro2::TokenStream;
use quote::quote;
Expand Down Expand Up @@ -310,7 +310,7 @@ struct ContainerOptions {
/// Change the name of an enum variant in the generated error message.
annotation: Option<syn::LitStr>,
/// Change the path for the pyo3 crate
pyo3_path: Option<PyO3PathAttribute>,
krate: Option<CrateAttribute>,
}

/// Attributes for deriving FromPyObject scoped on containers.
Expand All @@ -321,7 +321,7 @@ enum ContainerPyO3Attribute {
/// Change the name of an enum variant in the generated error message.
ErrorAnnotation(LitStr),
/// Change the path for the pyo3 crate
PyO3Path(PyO3PathAttribute),
Crate(CrateAttribute),
}

impl Parse for ContainerPyO3Attribute {
Expand All @@ -335,7 +335,7 @@ impl Parse for ContainerPyO3Attribute {
let _: Token![=] = input.parse()?;
input.parse().map(ContainerPyO3Attribute::ErrorAnnotation)
} else if lookahead.peek(Token![crate]) {
input.parse().map(ContainerPyO3Attribute::PyO3Path)
input.parse().map(ContainerPyO3Attribute::Crate)
} else {
Err(lookahead.error())
}
Expand Down Expand Up @@ -364,12 +364,12 @@ impl ContainerOptions {
);
options.annotation = Some(lit_str);
}
ContainerPyO3Attribute::PyO3Path(path) => {
ContainerPyO3Attribute::Crate(path) => {
ensure_spanned!(
options.pyo3_path.is_none(),
path.0.span() => "`pyo3_path` may only be provided once"
options.krate.is_none(),
path.0.span() => "`crate` may only be provided once"
);
options.pyo3_path = Some(path);
options.krate = Some(path);
}
}
}
Expand Down Expand Up @@ -515,7 +515,7 @@ pub fn build_derive_from_pyobject(tokens: &DeriveInput) -> Result<TokenStream> {
.push(parse_quote!(#gen_ident: FromPyObject<#lt_param>))
}
let options = ContainerOptions::from_attrs(&tokens.attrs)?;
let pyo3_path = get_pyo3_path(&options.pyo3_path);
let krate = get_pyo3_crate(&options.krate);
let derives = match &tokens.data {
syn::Data::Enum(en) => {
if options.transparent || options.annotation.is_some() {
Expand All @@ -541,7 +541,7 @@ pub fn build_derive_from_pyobject(tokens: &DeriveInput) -> Result<TokenStream> {
let ident = &tokens.ident;
Ok(quote!(
const _: () = {
use #pyo3_path as _pyo3;
use #krate as _pyo3;

#[automatically_derived]
impl#trait_generics _pyo3::FromPyObject<#lt_param> for #ident#generics #where_clause {
Expand Down
46 changes: 23 additions & 23 deletions pyo3-macros-backend/src/method.rs
Expand Up @@ -5,7 +5,7 @@ use crate::deprecations::Deprecation;
use crate::params::{accept_args_kwargs, impl_arg_params};
use crate::pyfunction::PyFunctionOptions;
use crate::pyfunction::{PyFunctionArgPyO3Attributes, PyFunctionSignature};
use crate::utils::{self, get_pyo3_path, PythonDoc};
use crate::utils::{self, get_pyo3_crate, PythonDoc};
use crate::{deprecations::Deprecations, pyfunction::Argument};
use proc_macro2::{Span, TokenStream};
use quote::ToTokens;
Expand Down Expand Up @@ -228,7 +228,7 @@ pub struct FnSpec<'a> {
pub deprecations: Deprecations,
pub convention: CallingConvention,
pub text_signature: Option<TextSignatureAttribute>,
pub pyo3_path: syn::Path,
pub krate: syn::Path,
}

pub fn get_return_info(output: &syn::ReturnType) -> syn::Type {
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<'a> FnSpec<'a> {
) -> Result<FnSpec<'a>> {
let PyFunctionOptions {
text_signature,
pyo3_path,
krate,
name,
mut deprecations,
..
Expand All @@ -281,7 +281,7 @@ impl<'a> FnSpec<'a> {
let name = &sig.ident;
let ty = get_return_info(&sig.output);
let python_name = python_name.as_ref().unwrap_or(name).unraw();
let pyo3_path = get_pyo3_path(&pyo3_path);
let krate = get_pyo3_crate(&krate);

let doc = utils::get_doc(
meth_attrs,
Expand Down Expand Up @@ -315,7 +315,7 @@ impl<'a> FnSpec<'a> {
doc,
deprecations,
text_signature,
pyo3_path,
krate,
})
}

Expand Down Expand Up @@ -477,16 +477,16 @@ impl<'a> FnSpec<'a> {
};
let rust_call =
quote! { _pyo3::callback::convert(#py, #rust_name(#self_arg #(#arg_names),*)) };
let pyo3_path = &self.pyo3_path;
let krate = &self.krate;
Ok(match self.convention {
CallingConvention::Noargs => {
quote! {
unsafe extern "C" fn #ident (
_slf: *mut #pyo3_path::ffi::PyObject,
_args: *mut #pyo3_path::ffi::PyObject,
) -> *mut #pyo3_path::ffi::PyObject
_slf: *mut #krate::ffi::PyObject,
_args: *mut #krate::ffi::PyObject,
) -> *mut #krate::ffi::PyObject
{
use #pyo3_path as _pyo3;
use #krate as _pyo3;
#deprecations
_pyo3::callback::handle_panic(|#py| {
#self_conversion
Expand All @@ -499,12 +499,12 @@ impl<'a> FnSpec<'a> {
let arg_convert_and_rust_call = impl_arg_params(self, cls, rust_call, &py, true)?;
quote! {
unsafe extern "C" fn #ident (
_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) -> *mut #pyo3_path::ffi::PyObject
_slf: *mut #krate::ffi::PyObject,
_args: *const *mut #krate::ffi::PyObject,
_nargs: #krate::ffi::Py_ssize_t,
_kwnames: *mut #krate::ffi::PyObject) -> *mut #krate::ffi::PyObject
{
use #pyo3_path as _pyo3;
use #krate as _pyo3;
#deprecations
_pyo3::callback::handle_panic(|#py| {
#self_conversion
Expand All @@ -527,11 +527,11 @@ impl<'a> FnSpec<'a> {
let arg_convert_and_rust_call = impl_arg_params(self, cls, rust_call, &py, false)?;
quote! {
unsafe extern "C" fn #ident (
_slf: *mut #pyo3_path::ffi::PyObject,
_args: *mut #pyo3_path::ffi::PyObject,
_kwargs: *mut #pyo3_path::ffi::PyObject) -> *mut #pyo3_path::ffi::PyObject
_slf: *mut #krate::ffi::PyObject,
_args: *mut #krate::ffi::PyObject,
_kwargs: *mut #krate::ffi::PyObject) -> *mut #krate::ffi::PyObject
{
use #pyo3_path as _pyo3;
use #krate as _pyo3;
#deprecations
_pyo3::callback::handle_panic(|#py| {
#self_conversion
Expand All @@ -548,11 +548,11 @@ impl<'a> FnSpec<'a> {
let arg_convert_and_rust_call = impl_arg_params(self, cls, rust_call, &py, false)?;
quote! {
unsafe extern "C" fn #ident (
subtype: *mut #pyo3_path::ffi::PyTypeObject,
_args: *mut #pyo3_path::ffi::PyObject,
_kwargs: *mut #pyo3_path::ffi::PyObject) -> *mut #pyo3_path::ffi::PyObject
subtype: *mut #krate::ffi::PyTypeObject,
_args: *mut #krate::ffi::PyObject,
_kwargs: *mut #krate::ffi::PyObject) -> *mut #krate::ffi::PyObject
{
use #pyo3_path as _pyo3;
use #krate as _pyo3;
#deprecations
use _pyo3::callback::IntoPyCallbackOutput;
_pyo3::callback::handle_panic(|#py| {
Expand Down
27 changes: 13 additions & 14 deletions pyo3-macros-backend/src/module.rs
Expand Up @@ -3,11 +3,10 @@

use crate::{
attributes::{
self, is_attribute_ident, take_attributes, take_pyo3_options, NameAttribute,
PyO3PathAttribute,
self, is_attribute_ident, take_attributes, take_pyo3_options, CrateAttribute, NameAttribute,
},
pyfunction::{impl_wrap_pyfunction, PyFunctionOptions},
utils::{get_pyo3_path, PythonDoc},
utils::{get_pyo3_crate, PythonDoc},
};
use proc_macro2::{Span, TokenStream};
use quote::quote;
Expand All @@ -21,7 +20,7 @@ use syn::{

#[derive(Default)]
pub struct PyModuleOptions {
pyo3_path: Option<PyO3PathAttribute>,
krate: Option<CrateAttribute>,
name: Option<syn::Ident>,
}

Expand All @@ -32,7 +31,7 @@ impl PyModuleOptions {
for option in take_pyo3_options(attrs)? {
match option {
PyModulePyO3Option::Name(name) => options.set_name(name.0)?,
PyModulePyO3Option::PyO3Path(path) => options.set_pyo3_path(path)?,
PyModulePyO3Option::Crate(path) => options.set_crate(path)?,
}
}

Expand All @@ -49,13 +48,13 @@ impl PyModuleOptions {
Ok(())
}

fn set_pyo3_path(&mut self, path: PyO3PathAttribute) -> Result<()> {
fn set_crate(&mut self, path: CrateAttribute) -> Result<()> {
ensure_spanned!(
self.pyo3_path.is_none(),
path.0.span() => "`pyo3_path` may only be specified once"
self.krate.is_none(),
path.0.span() => "`crate` may only be specified once"
);

self.pyo3_path = Some(path);
self.krate = Some(path);
Ok(())
}
}
Expand All @@ -64,16 +63,16 @@ impl PyModuleOptions {
/// module
pub fn py_init(fnname: &Ident, options: PyModuleOptions, doc: PythonDoc) -> TokenStream {
let name = options.name.unwrap_or_else(|| fnname.unraw());
let pyo3_path = get_pyo3_path(&options.pyo3_path);
let krate = get_pyo3_crate(&options.krate);
let cb_name = Ident::new(&format!("PyInit_{}", name), Span::call_site());

quote! {
#[no_mangle]
#[allow(non_snake_case)]
/// This autogenerated function is called by the python interpreter when importing
/// the module.
pub unsafe extern "C" fn #cb_name() -> *mut #pyo3_path::ffi::PyObject {
use #pyo3_path as _pyo3;
pub unsafe extern "C" fn #cb_name() -> *mut #krate::ffi::PyObject {
use #krate as _pyo3;
use _pyo3::derive_utils::ModuleDef;
static NAME: &str = concat!(stringify!(#name), "\0");
static DOC: &str = #doc;
Expand Down Expand Up @@ -161,7 +160,7 @@ fn get_pyfn_attr(attrs: &mut Vec<syn::Attribute>) -> syn::Result<Option<PyFnArgs
}

enum PyModulePyO3Option {
PyO3Path(PyO3PathAttribute),
Crate(CrateAttribute),
Name(NameAttribute),
}

Expand All @@ -171,7 +170,7 @@ impl Parse for PyModulePyO3Option {
if lookahead.peek(attributes::kw::name) {
input.parse().map(PyModulePyO3Option::Name)
} else if lookahead.peek(syn::Token![crate]) {
input.parse().map(PyModulePyO3Option::PyO3Path)
input.parse().map(PyModulePyO3Option::Crate)
} else {
Err(lookahead.error())
}
Expand Down

0 comments on commit d0381c2

Please sign in to comment.