Skip to content

Commit

Permalink
Merge pull request #388 from kngwyu/pyany
Browse files Browse the repository at this point in the history
Rename PyObjectRef with PyAny
  • Loading branch information
konstin committed Mar 18, 2019
2 parents 0988357 + 59a9d4f commit 3e475b0
Show file tree
Hide file tree
Showing 38 changed files with 269 additions and 258 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

* Renamed `PyObjectRef` to `PyAny` in #388
* Renamed `add_function` to `add_wrapped` as it now also supports modules.
* Renamed `#[pymodinit]` to `#[pymodule]`
* `py.init(|| value)` becomes `Py::new(value)`
Expand Down
2 changes: 1 addition & 1 deletion pyo3-derive-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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::PyObjectRef},
base: parse_quote! {pyo3::types::PyAny},
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use crate::err::{self, PyResult};
use crate::exceptions;
use crate::ffi;
use crate::types::PyObjectRef;
use crate::types::PyAny;
use crate::AsPyPointer;
use crate::Python;
use libc;
Expand Down Expand Up @@ -163,7 +163,7 @@ fn validate(b: &ffi::Py_buffer) {

impl PyBuffer {
/// Get the underlying buffer from the specified python object.
pub fn get(py: Python, obj: &PyObjectRef) -> PyResult<PyBuffer> {
pub fn get(py: Python, obj: &PyAny) -> PyResult<PyBuffer> {
unsafe {
let mut buf = Box::new(mem::zeroed::<ffi::Py_buffer>());
err::error_on_minusone(
Expand Down
4 changes: 2 additions & 2 deletions src/class/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::exceptions;
use crate::ffi;
use crate::objectprotocol::ObjectProtocol;
use crate::type_object::PyTypeInfo;
use crate::types::PyObjectRef;
use crate::types::PyAny;
use crate::IntoPyPointer;
use crate::Python;
use crate::{FromPyObject, IntoPyObject};
Expand Down Expand Up @@ -440,7 +440,7 @@ where
let _pool = crate::GILPool::new();
let py = Python::assume_gil_acquired();
let slf = py.from_borrowed_ptr::<T>(slf);
let arg = py.from_borrowed_ptr::<PyObjectRef>(arg);
let arg = py.from_borrowed_ptr::<PyAny>(arg);

let res = match extract_op(op) {
Ok(op) => match arg.extract() {
Expand Down
10 changes: 5 additions & 5 deletions src/class/descr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@ use crate::class::methods::PyMethodDef;
use crate::err::PyResult;
use crate::ffi;
use crate::type_object::PyTypeInfo;
use crate::types::{PyObjectRef, PyType};
use crate::types::{PyAny, PyType};
use crate::{FromPyObject, IntoPyObject};
use std::os::raw::c_int;

/// Descriptor interface
#[allow(unused_variables)]
pub trait PyDescrProtocol<'p>: PyTypeInfo {
fn __get__(&'p self, instance: &'p PyObjectRef, owner: Option<&'p PyType>) -> Self::Result
fn __get__(&'p self, instance: &'p PyAny, owner: Option<&'p PyType>) -> Self::Result
where
Self: PyDescrGetProtocol<'p>,
{
unimplemented!()
}

fn __set__(&'p self, instance: &'p PyObjectRef, value: &'p PyObjectRef) -> Self::Result
fn __set__(&'p self, instance: &'p PyAny, value: &'p PyAny) -> Self::Result
where
Self: PyDescrSetProtocol<'p>,
{
unimplemented!()
}

fn __delete__(&'p self, instance: &'p PyObjectRef) -> Self::Result
fn __delete__(&'p self, instance: &'p PyAny) -> Self::Result
where
Self: PyDescrDeleteProtocol<'p>,
{
unimplemented!()
}

fn __set_name__(&'p self, instance: &'p PyObjectRef) -> Self::Result
fn __set_name__(&'p self, instance: &'p PyAny) -> Self::Result
where
Self: PyDescrSetNameProtocol<'p>,
{
Expand Down
32 changes: 16 additions & 16 deletions src/class/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ macro_rules! py_binary_func {
let _pool = $crate::GILPool::new();
let py = $crate::Python::assume_gil_acquired();
let slf = py.mut_from_borrowed_ptr::<T>(slf);
let arg = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(arg);
let arg = py.from_borrowed_ptr::<$crate::types::PyAny>(arg);

let result = match arg.extract() {
Ok(arg) => slf.$f(arg).into(),
Expand All @@ -114,8 +114,8 @@ macro_rules! py_binary_num_func {
use $crate::ObjectProtocol;
let _pool = $crate::GILPool::new();
let py = $crate::Python::assume_gil_acquired();
let lhs = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(lhs);
let rhs = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(rhs);
let lhs = py.from_borrowed_ptr::<$crate::types::PyAny>(lhs);
let rhs = py.from_borrowed_ptr::<$crate::types::PyAny>(rhs);

let result = match lhs.extract() {
Ok(lhs) => match rhs.extract() {
Expand Down Expand Up @@ -147,7 +147,7 @@ macro_rules! py_binary_self_func {
let _pool = $crate::GILPool::new();
let py = $crate::Python::assume_gil_acquired();
let slf1 = py.mut_from_borrowed_ptr::<T>(slf);
let arg = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(arg);
let arg = py.from_borrowed_ptr::<$crate::types::PyAny>(arg);

let result = match arg.extract() {
Ok(arg) => slf1.$f(arg).into(),
Expand Down Expand Up @@ -216,8 +216,8 @@ macro_rules! py_ternary_func {
let _pool = $crate::GILPool::new();
let py = $crate::Python::assume_gil_acquired();
let slf = py.mut_from_borrowed_ptr::<T>(slf);
let arg1 = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(arg2);
let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2);

let result = match arg1.extract() {
Ok(arg1) => match arg2.extract() {
Expand Down Expand Up @@ -249,9 +249,9 @@ macro_rules! py_ternary_num_func {

let _pool = $crate::GILPool::new();
let py = $crate::Python::assume_gil_acquired();
let arg1 = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(arg2);
let arg3 = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(arg3);
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 result = match arg1.extract() {
Ok(arg1) => match arg2.extract() {
Expand Down Expand Up @@ -287,8 +287,8 @@ macro_rules! py_ternary_self_func {
let _pool = $crate::GILPool::new();
let py = $crate::Python::assume_gil_acquired();
let slf1 = py.mut_from_borrowed_ptr::<T>(slf);
let arg1 = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(arg2);
let arg1 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg1);
let arg2 = py.from_borrowed_ptr::<$crate::types::PyAny>(arg2);

let result = match arg1.extract() {
Ok(arg1) => match arg2.extract() {
Expand Down Expand Up @@ -335,8 +335,8 @@ macro_rules! py_func_set {
),
))
} else {
let name = py.mut_from_borrowed_ptr::<$crate::types::PyObjectRef>(name);
let value = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(value);
let name = py.mut_from_borrowed_ptr::<$crate::types::PyAny>(name);
let value = py.from_borrowed_ptr::<$crate::types::PyAny>(value);
match name.extract() {
Ok(name) => match value.extract() {
Ok(value) => slf.$fn_set(name, value).into(),
Expand Down Expand Up @@ -376,7 +376,7 @@ macro_rules! py_func_del {

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

match name.extract() {
Ok(name) => slf.$fn_del(name).into(),
Expand Down Expand Up @@ -416,15 +416,15 @@ macro_rules! py_func_set_del {
let _pool = $crate::GILPool::new();
let py = $crate::Python::assume_gil_acquired();
let slf = py.mut_from_borrowed_ptr::<$generic>(slf);
let name = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(name);
let name = py.from_borrowed_ptr::<$crate::types::PyAny>(name);

let result = if value.is_null() {
match name.extract() {
Ok(name) => slf.$fn_del(name).into(),
Err(e) => Err(e.into()),
}
} else {
let value = py.from_borrowed_ptr::<$crate::types::PyObjectRef>(value);
let value = py.from_borrowed_ptr::<$crate::types::PyAny>(value);
match name.extract() {
Ok(name) => match value.extract() {
Ok(value) => slf.$fn_set(name, value).into(),
Expand Down
6 changes: 3 additions & 3 deletions src/class/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::exceptions;
use crate::ffi;
use crate::objectprotocol::ObjectProtocol;
use crate::type_object::PyTypeInfo;
use crate::types::PyObjectRef;
use crate::types::PyAny;
use crate::Python;
use crate::{FromPyObject, IntoPyObject};
use std::os::raw::c_int;
Expand Down Expand Up @@ -241,7 +241,7 @@ where
stringify!(T)
)))
} else {
let value = py.from_borrowed_ptr::<PyObjectRef>(value);
let value = py.from_borrowed_ptr::<PyAny>(value);
match value.extract() {
Ok(value) => slf.__setitem__(key as isize, value).into(),
Err(e) => Err(e),
Expand Down Expand Up @@ -358,7 +358,7 @@ mod sq_ass_item_impl {
let result = if value.is_null() {
slf.__delitem__(key as isize).into()
} else {
let value = py.from_borrowed_ptr::<PyObjectRef>(value);
let value = py.from_borrowed_ptr::<PyAny>(value);
match value.extract() {
Ok(value) => slf.__setitem__(key as isize, value).into(),
Err(e) => Err(e),
Expand Down
44 changes: 20 additions & 24 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::err::{PyDowncastError, PyResult};
use crate::ffi;
use crate::object::PyObject;
use crate::type_object::PyTypeInfo;
use crate::types::PyObjectRef;
use crate::types::PyAny;
use crate::types::PyTuple;
use crate::Py;
use crate::Python;
Expand Down Expand Up @@ -182,7 +182,7 @@ pub trait IntoPyObject {
/// the inherent method `PyObject::extract()` can be used.
pub trait FromPyObject<'source>: Sized {
/// Extracts `Self` from the source `PyObject`.
fn extract(ob: &'source PyObjectRef) -> PyResult<Self>;
fn extract(ob: &'source PyAny) -> PyResult<Self>;
}

/// Identity conversion: allows using existing `PyObject` instances where
Expand Down Expand Up @@ -262,7 +262,7 @@ where
T: PyTryFrom<'a>,
{
#[inline]
default fn extract(ob: &'a PyObjectRef) -> PyResult<&'a T> {
default fn extract(ob: &'a PyAny) -> PyResult<&'a T> {
Ok(T::try_from(ob)?)
}
}
Expand All @@ -273,7 +273,7 @@ where
T: PyTryFrom<'a>,
{
#[inline]
default fn extract(ob: &'a PyObjectRef) -> PyResult<&'a mut T> {
default fn extract(ob: &'a PyAny) -> PyResult<&'a mut T> {
Ok(T::try_from_mut(ob)?)
}
}
Expand All @@ -282,7 +282,7 @@ impl<'a, T> FromPyObject<'a> for Option<T>
where
T: FromPyObject<'a>,
{
fn extract(obj: &'a PyObjectRef) -> PyResult<Self> {
fn extract(obj: &'a PyAny) -> PyResult<Self> {
if obj.as_ptr() == unsafe { ffi::Py_None() } {
Ok(None)
} else {
Expand Down Expand Up @@ -314,31 +314,29 @@ pub trait PyTryInto<T>: Sized {
/// This trait is similar to `std::convert::TryFrom`
pub trait PyTryFrom<'v>: Sized {
/// Cast from a concrete Python object type to PyObject.
fn try_from<V: Into<&'v PyObjectRef>>(value: V) -> Result<&'v Self, PyDowncastError>;
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError>;

/// Cast from a concrete Python object type to PyObject. With exact type check.
fn try_from_exact<V: Into<&'v PyObjectRef>>(value: V) -> Result<&'v Self, PyDowncastError>;
fn try_from_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError>;

/// Cast from a concrete Python object type to PyObject.
fn try_from_mut<V: Into<&'v PyObjectRef>>(value: V) -> Result<&'v mut Self, PyDowncastError>;
fn try_from_mut<V: Into<&'v PyAny>>(value: V) -> Result<&'v mut Self, PyDowncastError>;

/// Cast from a concrete Python object type to PyObject. With exact type check.
fn try_from_mut_exact<V: Into<&'v PyObjectRef>>(
value: V,
) -> Result<&'v mut Self, PyDowncastError>;
fn try_from_mut_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v mut Self, PyDowncastError>;

/// Cast a PyObjectRef to a specific type of PyObject. The caller must
/// Cast a PyAny to a specific type of PyObject. The caller must
/// have already verified the reference is for this type.
unsafe fn try_from_unchecked<V: Into<&'v PyObjectRef>>(value: V) -> &'v Self;
unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self;

/// Cast a PyObjectRef to a specific type of PyObject. The caller must
/// Cast a PyAny to a specific type of PyObject. The caller must
/// have already verified the reference is for this type.
#[allow(clippy::mut_from_ref)]
unsafe fn try_from_mut_unchecked<V: Into<&'v PyObjectRef>>(value: V) -> &'v mut Self;
unsafe fn try_from_mut_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v mut Self;
}

// TryFrom implies TryInto
impl<U> PyTryInto<U> for PyObjectRef
impl<U> PyTryInto<U> for PyAny
where
U: for<'v> PyTryFrom<'v>,
{
Expand All @@ -360,7 +358,7 @@ impl<'v, T> PyTryFrom<'v> for T
where
T: PyTypeInfo,
{
fn try_from<V: Into<&'v PyObjectRef>>(value: V) -> Result<&'v T, PyDowncastError> {
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v T, PyDowncastError> {
let value = value.into();
unsafe {
if T::is_instance(value) {
Expand All @@ -371,7 +369,7 @@ where
}
}

fn try_from_exact<V: Into<&'v PyObjectRef>>(value: V) -> Result<&'v T, PyDowncastError> {
fn try_from_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v T, PyDowncastError> {
let value = value.into();
unsafe {
if T::is_exact_instance(value) {
Expand All @@ -382,7 +380,7 @@ where
}
}

fn try_from_mut<V: Into<&'v PyObjectRef>>(value: V) -> Result<&'v mut T, PyDowncastError> {
fn try_from_mut<V: Into<&'v PyAny>>(value: V) -> Result<&'v mut T, PyDowncastError> {
let value = value.into();
unsafe {
if T::is_instance(value) {
Expand All @@ -393,9 +391,7 @@ where
}
}

fn try_from_mut_exact<V: Into<&'v PyObjectRef>>(
value: V,
) -> Result<&'v mut T, PyDowncastError> {
fn try_from_mut_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v mut T, PyDowncastError> {
let value = value.into();
unsafe {
if T::is_exact_instance(value) {
Expand All @@ -407,7 +403,7 @@ where
}

#[inline]
unsafe fn try_from_unchecked<V: Into<&'v PyObjectRef>>(value: V) -> &'v T {
unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v T {
let value = value.into();
let ptr = if T::OFFSET == 0 {
value as *const _ as *const u8 as *const T
Expand All @@ -418,7 +414,7 @@ where
}

#[inline]
unsafe fn try_from_mut_unchecked<V: Into<&'v PyObjectRef>>(value: V) -> &'v mut T {
unsafe fn try_from_mut_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v mut T {
let value = value.into();
let ptr = if T::OFFSET == 0 {
value as *const _ as *mut u8 as *mut T
Expand Down
4 changes: 2 additions & 2 deletions src/derive_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::err::PyResult;
use crate::exceptions::TypeError;
use crate::ffi;
use crate::init_once;
use crate::types::{PyDict, PyModule, PyObjectRef, PyString, PyTuple};
use crate::types::{PyAny, PyDict, PyModule, PyString, PyTuple};
use crate::GILPool;
use crate::Python;
use crate::{IntoPyObject, PyTryFrom};
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn parse_fn_args<'p>(
kwargs: Option<&'p PyDict>,
accept_args: bool,
accept_kwargs: bool,
output: &mut [Option<&'p PyObjectRef>],
output: &mut [Option<&'p PyAny>],
) -> PyResult<()> {
let nargs = args.len();
let nkeywords = kwargs.map_or(0, PyDict::len);
Expand Down
Loading

0 comments on commit 3e475b0

Please sign in to comment.