Skip to content

Commit

Permalink
Merge pull request #816 from kngwyu/expose-pyany
Browse files Browse the repository at this point in the history
Expose PyAny to lib.rs and prelude
  • Loading branch information
kngwyu authored Mar 18, 2020
2 parents da40095 + 02ee7a5 commit 6998660
Show file tree
Hide file tree
Showing 31 changed files with 90 additions and 154 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* Bumped minimum Rust version to `1.42.0-nightly 2020-01-21`. [#761](https://github.com/PyO3/pyo3/pull/761)
* `PyRef` and `PyRefMut` are renewed for `PyCell`. [#770](https://github.com/PyO3/pyo3/pull/770)
* Some new FFI functions for Python 3.8. [#784](https://github.com/PyO3/pyo3/pull/784)
* `PyAny` is now on the top level module and prelude. [#816](https://github.com/PyO3/pyo3/pull/816)

### Added
* `PyCell`, which has RefCell-like features. [#770](https://github.com/PyO3/pyo3/pull/770)
Expand Down
3 changes: 1 addition & 2 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Specifically, the following implementation is generated:

```rust
use pyo3::prelude::*;
use pyo3::types::PyAny;

/// Class for demonstration
struct MyClass {
Expand Down Expand Up @@ -326,7 +325,7 @@ that inherit native types. Even in such cases, you can unsafely get a base class

```rust
# use pyo3::prelude::*;
use pyo3::types::{PyAny, PyDict};
use pyo3::types::PyDict;
use pyo3::{AsPyPointer, PyNativeType};
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion guide/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ let obj_ref_mut: &mut MyClass = obj.extract().unwrap();
After:
```
# use pyo3::prelude::*;
# use pyo3::types::{PyAny, IntoPyDict};
# use pyo3::types::IntoPyDict;
# #[pyclass] #[derive(Clone)] struct MyClass {}
# #[pymethods] impl MyClass { #[new]fn new() -> Self { MyClass {} }}
# let gil = Python::acquire_gil();
Expand Down
6 changes: 3 additions & 3 deletions pyo3-derive-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Default for PyClassArgs {
// We need the 0 as value for the constant we're later building using quote for when there
// are no other flags
flags: vec![parse_quote! { 0 }],
base: parse_quote! { pyo3::types::PyAny },
base: parse_quote! { pyo3::PyAny },
has_extends: false,
}
}
Expand Down Expand Up @@ -364,12 +364,12 @@ fn impl_class(
let base_layout = if attr.has_extends {
quote! { <Self::BaseType as pyo3::derive_utils::PyBaseTypeUtils>::LayoutAsBase }
} else {
quote! { pyo3::pycell::PyCellBase<pyo3::types::PyAny> }
quote! { pyo3::pycell::PyCellBase<pyo3::PyAny> }
};
let base_nativetype = if attr.has_extends {
quote! { <Self::BaseType as pyo3::derive_utils::PyBaseTypeUtils>::BaseNativeType }
} else {
quote! { pyo3::types::PyAny }
quote! { pyo3::PyAny }
};

// If #cls is not extended type, we allow Self->PyObject conversion
Expand Down
6 changes: 1 addition & 5 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@

//! `PyBuffer` implementation
use crate::err::{self, PyResult};
use crate::exceptions;
use crate::ffi;
use crate::types::PyAny;
use crate::AsPyPointer;
use crate::Python;
use crate::{exceptions, ffi, AsPyPointer, PyAny, Python};
use std::ffi::CStr;
use std::os::raw;
use std::pin::Pin;
Expand Down
10 changes: 5 additions & 5 deletions src/class/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
use crate::callback::{BoolCallbackConverter, HashConverter, PyObjectCallbackConverter};
use crate::class::methods::PyMethodDef;
use crate::err::{PyErr, PyResult};
use crate::objectprotocol::ObjectProtocol;
use crate::types::PyAny;
use crate::{exceptions, ffi, FromPyObject, IntoPy, IntoPyPointer, PyClass, PyObject, Python};
use crate::{
exceptions, ffi, FromPyObject, IntoPy, IntoPyPointer, ObjectProtocol, PyAny, PyClass, PyErr,
PyObject, PyResult, Python,
};
use std::os::raw::c_int;

/// Operators for the __richcmp__ method
Expand Down Expand Up @@ -232,7 +232,7 @@ where
}

let slf = py.from_borrowed_ptr::<crate::PyCell<T>>(slf);
let arg = py.from_borrowed_ptr::<crate::types::PyAny>(arg);
let arg = py.from_borrowed_ptr::<crate::PyAny>(arg);
call_ref_with_converter!(
slf,
PyObjectCallbackConverter::<T::Success>(std::marker::PhantomData),
Expand Down
32 changes: 16 additions & 16 deletions src/class/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ macro_rules! py_binary_func {
let py = $crate::Python::assume_gil_acquired();
let _pool = $crate::GILPool::new(py);
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let arg = py.from_borrowed_ptr::<$crate::types::PyAny>(arg);
let arg = py.from_borrowed_ptr::<$crate::PyAny>(arg);
$call!(slf, $conv, py, $f, arg)
}
Some(wrap::<$class>)
Expand All @@ -111,8 +111,8 @@ macro_rules! py_binary_num_func {
use $crate::ObjectProtocol;
let py = $crate::Python::assume_gil_acquired();
let _pool = $crate::GILPool::new(py);
let lhs = py.from_borrowed_ptr::<$crate::types::PyAny>(lhs);
let rhs = py.from_borrowed_ptr::<$crate::types::PyAny>(rhs);
let lhs = py.from_borrowed_ptr::<$crate::PyAny>(lhs);
let rhs = py.from_borrowed_ptr::<$crate::PyAny>(rhs);

let result = match lhs.extract() {
Ok(lhs) => match rhs.extract() {
Expand Down Expand Up @@ -144,7 +144,7 @@ macro_rules! py_binary_self_func {
let py = $crate::Python::assume_gil_acquired();
let _pool = $crate::GILPool::new(py);
let slf_ = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let arg = py.from_borrowed_ptr::<$crate::types::PyAny>(arg);
let arg = py.from_borrowed_ptr::<$crate::PyAny>(arg);
let result = call_mut!(slf_, $f, arg);
match result {
Ok(_) => {
Expand Down Expand Up @@ -207,8 +207,8 @@ macro_rules! py_ternary_func {
let py = $crate::Python::assume_gil_acquired();
let _pool = $crate::GILPool::new(py);
let slf = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2);
let arg1 = py.from_borrowed_ptr::<$crate::PyAny>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::PyAny>(arg2);

call_ref_with_converter!(slf, $conv, py, $f, arg1, arg2)
}
Expand All @@ -233,9 +233,9 @@ macro_rules! py_ternary_num_func {

let py = $crate::Python::assume_gil_acquired();
let _pool = $crate::GILPool::new(py);
let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2);
let arg3 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg3);
let arg1 = py.from_borrowed_ptr::<$crate::PyAny>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::PyAny>(arg2);
let arg3 = py.from_borrowed_ptr::<$crate::PyAny>(arg3);

let result = match arg1.extract() {
Ok(arg1) => match arg2.extract() {
Expand Down Expand Up @@ -271,8 +271,8 @@ macro_rules! py_ternary_self_func {
let py = $crate::Python::assume_gil_acquired();
let _pool = $crate::GILPool::new(py);
let slf_cell = py.from_borrowed_ptr::<$crate::PyCell<T>>(slf);
let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2);
let arg1 = py.from_borrowed_ptr::<$crate::PyAny>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::PyAny>(arg2);
let result = call_mut!(slf_cell, $f, arg1, arg2);
match result {
Ok(_) => slf,
Expand Down Expand Up @@ -307,8 +307,8 @@ macro_rules! py_func_set {
),
))
} else {
let name = py.from_borrowed_ptr::<$crate::types::PyAny>(name);
let value = py.from_borrowed_ptr::<$crate::types::PyAny>(value);
let name = py.from_borrowed_ptr::<$crate::PyAny>(name);
let value = py.from_borrowed_ptr::<$crate::PyAny>(value);
call_mut!(slf, $fn_set, name, value)
};
match result {
Expand Down Expand Up @@ -338,7 +338,7 @@ macro_rules! py_func_del {

let result = if value.is_null() {
let slf = py.from_borrowed_ptr::<$crate::PyCell<U>>(slf);
let name = py.from_borrowed_ptr::<$crate::types::PyAny>(name);
let name = py.from_borrowed_ptr::<$crate::PyAny>(name);

call_mut!(slf, $fn_del, name)
} else {
Expand Down Expand Up @@ -371,12 +371,12 @@ macro_rules! py_func_set_del {
let py = $crate::Python::assume_gil_acquired();
let _pool = $crate::GILPool::new(py);
let slf = py.from_borrowed_ptr::<$crate::PyCell<$generic>>(slf);
let name = py.from_borrowed_ptr::<$crate::types::PyAny>(name);
let name = py.from_borrowed_ptr::<$crate::PyAny>(name);

let result = if value.is_null() {
call_mut!(slf, $fn_del, name)
} else {
let value = py.from_borrowed_ptr::<$crate::types::PyAny>(value);
let value = py.from_borrowed_ptr::<$crate::PyAny>(value);
call_mut!(slf, $fn_set, name, value)
};
match result {
Expand Down
3 changes: 1 addition & 2 deletions src/class/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
use crate::callback::{BoolCallbackConverter, LenResultConverter, PyObjectCallbackConverter};
use crate::err::{PyErr, PyResult};
use crate::objectprotocol::ObjectProtocol;
use crate::types::PyAny;
use crate::{exceptions, ffi, FromPyObject, IntoPy, PyClass, PyObject, Python};
use crate::{exceptions, ffi, FromPyObject, IntoPy, PyAny, PyClass, PyObject, Python};
use std::os::raw::c_int;

/// Sequence interface
Expand Down
3 changes: 1 addition & 2 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
use crate::err::{self, PyDowncastError, PyResult};
use crate::object::PyObject;
use crate::type_object::{PyDowncastImpl, PyTypeInfo};
use crate::types::PyAny;
use crate::types::PyTuple;
use crate::{ffi, gil, Py, PyCell, PyClass, PyNativeType, PyRef, PyRefMut, Python};
use crate::{ffi, gil, Py, PyAny, PyCell, PyClass, PyNativeType, PyRef, PyRefMut, Python};
use std::ptr::NonNull;

/// This trait represents that **we can do zero-cost conversion from the object
Expand Down
5 changes: 1 addition & 4 deletions src/gil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

//! Interaction with python's global interpreter lock
use crate::ffi;
use crate::internal_tricks::Unsendable;
use crate::types::PyAny;
use crate::Python;
use crate::{ffi, internal_tricks::Unsendable, PyAny, Python};
use std::ptr::NonNull;
use std::{any, sync};

Expand Down
5 changes: 2 additions & 3 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use crate::gil;
use crate::object::PyObject;
use crate::objectprotocol::ObjectProtocol;
use crate::type_object::{PyBorrowFlagLayout, PyDowncastImpl};
use crate::types::PyAny;
use crate::{
ffi, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, PyCell, PyClass, PyClassInitializer,
PyRef, PyRefMut, PyTypeInfo, Python, ToPyObject,
ffi, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, PyAny, PyCell, PyClass,
PyClassInitializer, PyRef, PyRefMut, PyTypeInfo, Python, ToPyObject,
};
use std::marker::PhantomData;
use std::mem;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ pub use crate::pyclass::PyClass;
pub use crate::pyclass_init::PyClassInitializer;
pub use crate::python::{prepare_freethreaded_python, Python};
pub use crate::type_object::{type_flags, PyTypeInfo};
// Since PyAny is as important as PyObject, we expose it to the top level.
pub use crate::types::PyAny;

// Re-exported for wrap_function
#[doc(hidden)]
Expand Down
4 changes: 2 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use crate::pycell::{PyCell, PyRef, PyRefMut};
pub use crate::pyclass_init::PyClassInitializer;
pub use crate::python::Python;
pub use crate::{FromPy, FromPyObject, IntoPy, IntoPyPointer, PyTryFrom, PyTryInto, ToPyObject};
// This is only part of the prelude because we need it for the pymodule function
pub use crate::types::PyModule;
// PyModule is only part of the prelude because we need it for the pymodule function
pub use crate::types::{PyAny, PyModule};
pub use pyo3cls::pymodule;
pub use pyo3cls::{pyclass, pyfunction, pymethods, pyproto};
3 changes: 1 addition & 2 deletions src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::conversion::{AsPyPointer, FromPyPointer, ToPyObject};
use crate::pyclass_init::PyClassInitializer;
use crate::pyclass_slots::{PyClassDict, PyClassWeakRef};
use crate::type_object::{PyBorrowFlagLayout, PyDowncastImpl, PyLayout, PySizedLayout, PyTypeInfo};
use crate::types::PyAny;
use crate::{ffi, FromPy, PyClass, PyErr, PyNativeType, PyObject, PyResult, Python};
use crate::{ffi, FromPy, PyAny, PyClass, PyErr, PyNativeType, PyObject, PyResult, Python};
use std::cell::{Cell, UnsafeCell};
use std::fmt;
use std::mem::ManuallyDrop;
Expand Down
12 changes: 4 additions & 8 deletions src/types/boolobject.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
use crate::ffi;
use crate::internal_tricks::Unsendable;
use crate::object::PyObject;
use crate::types::PyAny;
use crate::FromPyObject;
use crate::PyResult;
use crate::Python;
use crate::{AsPyPointer, FromPy};
use crate::{PyTryFrom, ToPyObject};
use crate::{
ffi, AsPyPointer, FromPy, FromPyObject, PyAny, PyObject, PyResult, PyTryFrom, Python,
ToPyObject,
};

/// Represents a Python `bool`.
#[repr(transparent)]
Expand Down
12 changes: 4 additions & 8 deletions src/types/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use crate::conversion::FromPyObject;
use crate::conversion::{PyTryFrom, ToPyObject};
use crate::err::PyResult;
use crate::internal_tricks::Unsendable;
use crate::object::PyObject;
use crate::types::PyAny;
use crate::AsPyPointer;
use crate::Python;
use crate::{ffi, FromPy};
use crate::{
ffi, AsPyPointer, FromPy, FromPyObject, PyAny, PyObject, PyResult, PyTryFrom, Python,
ToPyObject,
};
use std::ops::Index;
use std::os::raw::c_char;
use std::slice::SliceIndex;
Expand Down
5 changes: 1 addition & 4 deletions src/types/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ impl<'py> Neg for &'py PyComplex {
#[cfg(feature = "num-complex")]
mod complex_conversion {
use super::*;
use crate::err::PyErr;
use crate::types::PyAny;
use crate::PyResult;
use crate::{FromPyObject, ToPyObject};
use crate::{FromPyObject, PyAny, PyErr, PyResult, ToPyObject};
use num_complex::Complex;

impl PyComplex {
Expand Down
16 changes: 4 additions & 12 deletions src/types/floatob.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
//
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython

use crate::err::PyErr;
use crate::ffi;
use crate::instance::PyNativeType;
use crate::internal_tricks::Unsendable;
use crate::object::PyObject;
use crate::objectprotocol::ObjectProtocol;
use crate::types::PyAny;
use crate::FromPyObject;
use crate::PyResult;
use crate::Python;
use crate::ToPyObject;
use crate::{AsPyPointer, FromPy};
use crate::{
ffi, AsPyPointer, FromPy, FromPyObject, ObjectProtocol, PyAny, PyErr, PyNativeType, PyObject,
PyResult, Python, ToPyObject,
};
use std::os::raw::c_double;

/// Represents a Python `float` object.
Expand Down
7 changes: 1 addition & 6 deletions src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
//
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython

use crate::err::{PyDowncastError, PyErr, PyResult};
use crate::ffi;
use crate::instance::PyNativeType;
use crate::types::PyAny;
use crate::AsPyPointer;
use crate::Python;
use crate::{ffi, AsPyPointer, PyAny, PyDowncastError, PyErr, PyNativeType, PyResult, Python};

/// A Python iterator object.
///
Expand Down
11 changes: 4 additions & 7 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

use crate::err::{self, PyResult};
use crate::ffi::{self, Py_ssize_t};
use crate::instance::PyNativeType;
use crate::internal_tricks::Unsendable;
use crate::object::PyObject;
use crate::types::PyAny;
use crate::IntoPyPointer;
use crate::Python;
use crate::{AsPyPointer, IntoPy};
use crate::{ToBorrowedObject, ToPyObject};
use crate::{
AsPyPointer, IntoPy, IntoPyPointer, PyAny, PyNativeType, PyObject, Python, ToBorrowedObject,
ToPyObject,
};

/// Represents a Python `list`.
#[repr(transparent)]
Expand Down
Loading

0 comments on commit 6998660

Please sign in to comment.