pyo3_path, part 4: rename to `crate` to keep consistent with serde

This commit is contained in:
Georg Brandl 2021-12-08 16:39:40 +01:00
parent 681217d8d9
commit a5b76bc365
14 changed files with 26 additions and 27 deletions

View File

@ -21,7 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `Py::setattr` method. [#2009](https://github.com/PyO3/pyo3/pull/2009) - Add `Py::setattr` method. [#2009](https://github.com/PyO3/pyo3/pull/2009)
- Add `PyCapsule`, exposing the [Capsule API](https://docs.python.org/3/c-api/capsule.html#capsules). [#1980](https://github.com/PyO3/pyo3/pull/1980) - Add `PyCapsule`, exposing the [Capsule API](https://docs.python.org/3/c-api/capsule.html#capsules). [#1980](https://github.com/PyO3/pyo3/pull/1980)
- All PyO3 proc-macros except the deprecated `#[pyproto]` now accept a supplemental attribute `#[pyo3(pyo3_path = "some::path")]` that specifies - All PyO3 proc-macros except the deprecated `#[pyproto]` now accept a supplemental attribute `#[pyo3(crate = "some::path")]` that specifies
where to find the `pyo3` crate, in case it has been renamed or is re-exported and not found at the crate root. [#2022](https://github.com/PyO3/pyo3/pull/2022) where to find the `pyo3` crate, in case it has been renamed or is re-exported and not found at the crate root. [#2022](https://github.com/PyO3/pyo3/pull/2022)
### Changed ### Changed

View File

@ -149,13 +149,13 @@ crate.
However, when the dependency is renamed, or your crate only indirectly depends However, when the dependency is renamed, or your crate only indirectly depends
on `pyo3`, you need to let the macro code know where to find the crate. This is on `pyo3`, you need to let the macro code know where to find the crate. This is
done with the `pyo3_path` attribute: done with the `crate` attribute:
```rust ```rust
# use pyo3::prelude::*; # use pyo3::prelude::*;
# pub extern crate pyo3; # pub extern crate pyo3;
# mod reexported { pub use ::pyo3; } # mod reexported { pub use ::pyo3; }
#[pyclass] #[pyclass]
#[pyo3(pyo3_path = "reexported::pyo3")] #[pyo3(crate = "reexported::pyo3")]
struct MyClass; struct MyClass;
``` ```

View File

@ -17,7 +17,6 @@ pub mod kw {
syn::custom_keyword!(signature); syn::custom_keyword!(signature);
syn::custom_keyword!(text_signature); syn::custom_keyword!(text_signature);
syn::custom_keyword!(transparent); syn::custom_keyword!(transparent);
syn::custom_keyword!(pyo3_path);
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -50,7 +49,7 @@ pub struct PyO3PathAttribute(pub Path);
impl Parse for PyO3PathAttribute { impl Parse for PyO3PathAttribute {
fn parse(input: ParseStream) -> Result<Self> { fn parse(input: ParseStream) -> Result<Self> {
let _: kw::pyo3_path = input.parse()?; let _: Token![crate] = input.parse()?;
let _: Token![=] = input.parse()?; let _: Token![=] = input.parse()?;
let string_literal: LitStr = input.parse()?; let string_literal: LitStr = input.parse()?;
string_literal.parse().map(PyO3PathAttribute) string_literal.parse().map(PyO3PathAttribute)

View File

@ -334,7 +334,7 @@ impl Parse for ContainerPyO3Attribute {
let _: attributes::kw::annotation = input.parse()?; let _: attributes::kw::annotation = input.parse()?;
let _: Token![=] = input.parse()?; let _: Token![=] = input.parse()?;
input.parse().map(ContainerPyO3Attribute::ErrorAnnotation) input.parse().map(ContainerPyO3Attribute::ErrorAnnotation)
} else if lookahead.peek(attributes::kw::pyo3_path) { } else if lookahead.peek(Token![crate]) {
input.parse().map(ContainerPyO3Attribute::PyO3Path) input.parse().map(ContainerPyO3Attribute::PyO3Path)
} else { } else {
Err(lookahead.error()) Err(lookahead.error())

View File

@ -170,7 +170,7 @@ impl Parse for PyModulePyO3Option {
let lookahead = input.lookahead1(); let lookahead = input.lookahead1();
if lookahead.peek(attributes::kw::name) { if lookahead.peek(attributes::kw::name) {
input.parse().map(PyModulePyO3Option::Name) input.parse().map(PyModulePyO3Option::Name)
} else if lookahead.peek(attributes::kw::pyo3_path) { } else if lookahead.peek(syn::Token![crate]) {
input.parse().map(PyModulePyO3Option::PyO3Path) input.parse().map(PyModulePyO3Option::PyO3Path)
} else { } else {
Err(lookahead.error()) Err(lookahead.error())

View File

@ -201,7 +201,7 @@ impl Parse for PyClassPyO3Option {
let lookahead = input.lookahead1(); let lookahead = input.lookahead1();
if lookahead.peek(attributes::kw::text_signature) { if lookahead.peek(attributes::kw::text_signature) {
input.parse().map(PyClassPyO3Option::TextSignature) input.parse().map(PyClassPyO3Option::TextSignature)
} else if lookahead.peek(attributes::kw::pyo3_path) { } else if lookahead.peek(Token![crate]) {
input.parse().map(PyClassPyO3Option::PyO3Path) input.parse().map(PyClassPyO3Option::PyO3Path)
} else { } else {
Err(lookahead.error()) Err(lookahead.error())

View File

@ -257,7 +257,7 @@ impl Parse for PyFunctionOptions {
if !input.is_empty() { if !input.is_empty() {
let _: Comma = input.parse()?; let _: Comma = input.parse()?;
} }
} else if lookahead.peek(attributes::kw::pyo3_path) { } else if lookahead.peek(syn::Token![crate]) {
// TODO needs duplicate check? // TODO needs duplicate check?
options.pyo3_path = Some(input.parse()?); options.pyo3_path = Some(input.parse()?);
} else { } else {
@ -292,7 +292,7 @@ impl Parse for PyFunctionOption {
input.parse().map(PyFunctionOption::Signature) input.parse().map(PyFunctionOption::Signature)
} else if lookahead.peek(attributes::kw::text_signature) { } else if lookahead.peek(attributes::kw::text_signature) {
input.parse().map(PyFunctionOption::TextSignature) input.parse().map(PyFunctionOption::TextSignature)
} else if lookahead.peek(attributes::kw::pyo3_path) { } else if lookahead.peek(syn::Token![crate]) {
input.parse().map(PyFunctionOption::PyO3Path) input.parse().map(PyFunctionOption::PyO3Path)
} else { } else {
Err(lookahead.error()) Err(lookahead.error())

View File

@ -3,7 +3,7 @@
use std::collections::HashSet; use std::collections::HashSet;
use crate::{ use crate::{
attributes::{self, take_pyo3_options, PyO3PathAttribute}, attributes::{take_pyo3_options, PyO3PathAttribute},
konst::{ConstAttributes, ConstSpec}, konst::{ConstAttributes, ConstSpec},
pyfunction::PyFunctionOptions, pyfunction::PyFunctionOptions,
pymethod::{self, is_proto_method}, pymethod::{self, is_proto_method},
@ -32,7 +32,7 @@ enum PyImplPyO3Option {
impl Parse for PyImplPyO3Option { impl Parse for PyImplPyO3Option {
fn parse(input: ParseStream) -> Result<Self> { fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1(); let lookahead = input.lookahead1();
if lookahead.peek(attributes::kw::pyo3_path) { if lookahead.peek(syn::Token![crate]) {
input.parse().map(PyImplPyO3Option::PyO3Path) input.parse().map(PyImplPyO3Option::PyO3Path)
} else { } else {
Err(lookahead.error()) Err(lookahead.error())

View File

@ -1,16 +1,16 @@
#![no_implicit_prelude] #![no_implicit_prelude]
#[derive(crate::FromPyObject)] #[derive(crate::FromPyObject)]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
struct Derive1(i32); // newtype case struct Derive1(i32); // newtype case
#[derive(crate::FromPyObject)] #[derive(crate::FromPyObject)]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
#[allow(dead_code)] #[allow(dead_code)]
struct Derive2(i32, i32); // tuple case struct Derive2(i32, i32); // tuple case
#[derive(crate::FromPyObject)] #[derive(crate::FromPyObject)]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
#[allow(dead_code)] #[allow(dead_code)]
struct Derive3 { struct Derive3 {
f: i32, f: i32,
@ -18,7 +18,7 @@ struct Derive3 {
} // struct case } // struct case
#[derive(crate::FromPyObject)] #[derive(crate::FromPyObject)]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
#[allow(dead_code)] #[allow(dead_code)]
enum Derive4 { enum Derive4 {
A(i32), A(i32),

View File

@ -2,12 +2,12 @@
#![allow(unused_variables)] #![allow(unused_variables)]
#[crate::pyclass] #[crate::pyclass]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
#[derive(::std::clone::Clone)] #[derive(::std::clone::Clone)]
pub struct Foo; pub struct Foo;
#[crate::pyclass] #[crate::pyclass]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
pub struct Foo2; pub struct Foo2;
#[crate::pyclass( #[crate::pyclass(
@ -19,7 +19,7 @@ pub struct Foo2;
extends = crate::types::PyAny, extends = crate::types::PyAny,
module = "Spam" module = "Spam"
)] )]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
pub struct Bar { pub struct Bar {
#[pyo3(get, set)] #[pyo3(get, set)]
a: u8, a: u8,

View File

@ -2,7 +2,7 @@
#![allow(unused_variables)] #![allow(unused_variables)]
#[crate::pyfunction] #[crate::pyfunction]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
fn do_something(x: i32) -> crate::PyResult<i32> { fn do_something(x: i32) -> crate::PyResult<i32> {
::std::result::Result::Ok(x) ::std::result::Result::Ok(x)
} }

View File

@ -2,15 +2,15 @@
#![allow(unused_variables)] #![allow(unused_variables)]
#[crate::pyclass] #[crate::pyclass]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
pub struct Dummy; pub struct Dummy;
#[crate::pyclass] #[crate::pyclass]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
pub struct DummyIter; pub struct DummyIter;
#[crate::pymethods] #[crate::pymethods]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
impl Dummy { impl Dummy {
////////////////////// //////////////////////
// Basic customization // Basic customization

View File

@ -2,19 +2,19 @@
#![allow(unused_variables)] #![allow(unused_variables)]
#[crate::pyfunction] #[crate::pyfunction]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
fn do_something(x: i32) -> crate::PyResult<i32> { fn do_something(x: i32) -> crate::PyResult<i32> {
::std::result::Result::Ok(x) ::std::result::Result::Ok(x)
} }
#[crate::pymodule] #[crate::pymodule]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
fn foo(_py: crate::Python, _m: &crate::types::PyModule) -> crate::PyResult<()> { fn foo(_py: crate::Python, _m: &crate::types::PyModule) -> crate::PyResult<()> {
::std::result::Result::Ok(()) ::std::result::Result::Ok(())
} }
#[crate::pymodule] #[crate::pymodule]
#[pyo3(pyo3_path = "crate")] #[pyo3(crate = "crate")]
fn my_module(_py: crate::Python, m: &crate::types::PyModule) -> crate::PyResult<()> { fn my_module(_py: crate::Python, m: &crate::types::PyModule) -> crate::PyResult<()> {
m.add_function(crate::wrap_pyfunction!(do_something, m)?)?; m.add_function(crate::wrap_pyfunction!(do_something, m)?)?;
m.add_wrapped(crate::wrap_pymodule!(foo))?; m.add_wrapped(crate::wrap_pymodule!(foo))?;

View File

@ -132,7 +132,7 @@ error: only one of `attribute` or `item` can be provided
118 | #[pyo3(item, attribute)] 118 | #[pyo3(item, attribute)]
| ^ | ^
error: expected one of: `transparent`, `annotation`, `pyo3_path` error: expected one of: `transparent`, `annotation`, `crate`
--> tests/ui/invalid_frompy_derive.rs:123:8 --> tests/ui/invalid_frompy_derive.rs:123:8
| |
123 | #[pyo3(unknown = "should not work")] 123 | #[pyo3(unknown = "should not work")]