Skip to content

Commit

Permalink
Remove PyToken completely; Fixes #94
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Nov 12, 2018
1 parent fb2349b commit 57afb51
Show file tree
Hide file tree
Showing 21 changed files with 121 additions and 190 deletions.
2 changes: 1 addition & 1 deletion examples/rustapi_module/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub struct TzClass {}
impl TzClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| TzClass {})
obj.init(|| TzClass {})
}

fn utcoffset(&self, py: Python, _dt: &PyDateTime) -> PyResult<Py<PyDelta>> {
Expand Down
2 changes: 1 addition & 1 deletion examples/rustapi_module/src/othermod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct ModClass {
impl ModClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| ModClass {
obj.init(|| ModClass {
_somefield: String::from("contents"),
})
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rustapi_module/src/subclassing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Subclassable {}
impl Subclassable {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| Subclassable {})
obj.init(|| Subclassable {})
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/word-count/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct WordCounter {
impl WordCounter {
#[new]
fn __new__(obj: &PyRawObject, path: String) -> PyResult<()> {
obj.init(|_| WordCounter {
obj.init(|| WordCounter {
path: PathBuf::from(path),
})
}
Expand Down
6 changes: 3 additions & 3 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl MyClass {

#[new]
fn __new__(obj: &PyRawObject, num: i32) -> PyResult<()> {
obj.init(|_| {
obj.init(|| {
MyClass {
num,
}
Expand Down Expand Up @@ -101,7 +101,7 @@ struct BaseClass {
impl BaseClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| BaseClass{ val1: 10 })
obj.init(|| BaseClass{ val1: 10 })
}

pub fn method(&self) -> PyResult<()> {
Expand All @@ -118,7 +118,7 @@ struct SubClass {
impl SubClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| SubClass{ val2: 10 });
obj.init(|| SubClass{ val2: 10 });
BaseClass::__new__(obj)
}

Expand Down
2 changes: 1 addition & 1 deletion guide/src/rust-cpython.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct MyClass {
impl MyClass {
#[new]
fn __new__(obj: &PyRawObject, num: u32) -> PyResult<()> {
obj.init(|token| {
obj.init(|| {
MyClass {
num,
}
Expand Down
68 changes: 6 additions & 62 deletions pyo3-derive-backend/src/py_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,20 @@ use utils;
pub fn build_py_class(class: &mut syn::ItemStruct, attr: &Vec<syn::Expr>) -> TokenStream {
let (params, flags, base) = parse_attribute(attr);
let doc = utils::get_doc(&class.attrs, true);
let mut token: Option<syn::Ident> = None;
let mut descriptors = Vec::new();

if let syn::Fields::Named(ref mut fields) = class.fields {
for field in fields.named.iter_mut() {
if is_python_token(field) {
if token.is_none() {
token = field.ident.clone();
} else {
panic!("You can only have one PyToken per class");
}
} else {
let field_descs = parse_descriptors(field);
if !field_descs.is_empty() {
descriptors.push((field.clone(), field_descs));
}
let field_descs = parse_descriptors(field);
if !field_descs.is_empty() {
descriptors.push((field.clone(), field_descs));
}
}
} else {
panic!("#[pyclass] can only be used with C-style structs")
}

impl_class(&class.ident, &base, token, doc, params, flags, descriptors)
impl_class(&class.ident, &base, doc, params, flags, descriptors)
}

fn parse_descriptors(item: &mut syn::Field) -> Vec<FnType> {
Expand Down Expand Up @@ -72,7 +63,6 @@ fn parse_descriptors(item: &mut syn::Field) -> Vec<FnType> {
fn impl_class(
cls: &syn::Ident,
base: &syn::TypePath,
token: Option<syn::Ident>,
doc: syn::Lit,
params: HashMap<&'static str, syn::Expr>,
flags: Vec<syn::Expr>,
Expand All @@ -83,38 +73,6 @@ fn impl_class(
None => quote! { #cls }.to_string(),
};

let extra = if let Some(token) = token {
Some(quote! {
impl ::pyo3::PyObjectWithToken for #cls {
fn py<'p>(&'p self) -> ::pyo3::Python<'p> {
self.#token.py()
}
}
impl<'a> ::std::convert::From<&'a mut #cls> for &'a #cls
{
fn from(ob: &'a mut #cls) -> Self {
unsafe{std::mem::transmute(ob)}
}
}
impl ::std::fmt::Debug for #cls {
fn fmt(&self, f : &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
use ::pyo3::ObjectProtocol;
let s = self.repr().map_err(|_| ::std::fmt::Error)?;
f.write_str(&s.to_string_lossy())
}
}
impl ::std::fmt::Display for #cls {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
use ::pyo3::ObjectProtocol;
let s = self.str().map_err(|_| ::std::fmt::Error)?;
f.write_str(&s.to_string_lossy())
}
}
})
} else {
None
};

let extra = {
if let Some(freelist) = params.get("freelist") {
Some(quote! {
Expand All @@ -133,11 +91,9 @@ fn impl_class(
}
}
}

#extra
})
} else {
extra
None
}
};

Expand Down Expand Up @@ -209,7 +165,7 @@ fn impl_class(
// objects, so for now I'm keeping it
impl ::pyo3::IntoPyObject for #cls {
fn into_object(self, py: ::pyo3::Python) -> ::pyo3::PyObject {
::pyo3::Py::new(py, |_| self).unwrap().into_object(py)
::pyo3::Py::new(py, || self).unwrap().into_object(py)
}
}

Expand Down Expand Up @@ -337,18 +293,6 @@ fn impl_descriptors(cls: &syn::Type, descriptors: Vec<(syn::Field, Vec<FnType>)>
}
}

fn is_python_token(field: &syn::Field) -> bool {
match field.ty {
syn::Type::Path(ref typath) => {
if let Some(segment) = typath.path.segments.last() {
return segment.value().ident.to_string() == "PyToken";
}
}
_ => (),
}
return false;
}

fn parse_attribute(
args: &Vec<syn::Expr>,
) -> (
Expand Down
21 changes: 3 additions & 18 deletions src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) 2017-present PyO3 Project and Contributors

use std;
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::rc::Rc;

use crate::conversion::{FromPyObject, IntoPyObject, ToPyObject};
use crate::err::{PyErr, PyResult};
Expand All @@ -17,19 +15,6 @@ use crate::typeob::PyTypeCreate;
use crate::typeob::{PyTypeInfo, PyTypeObject};
use crate::types::PyObjectRef;

pub struct PyToken(PhantomData<Rc<()>>);

impl PyToken {
pub(crate) fn new() -> PyToken {
PyToken(PhantomData)
}

#[inline]
pub fn py(&self) -> Python {
unsafe { Python::assume_gil_acquired() }
}
}

/// Any instance that is managed Python can have access to `gil`.
pub trait PyObjectWithToken: Sized {
fn py(&self) -> Python;
Expand Down Expand Up @@ -174,7 +159,7 @@ where
/// Returns `Py<T>`.
pub fn new<F>(py: Python, f: F) -> PyResult<Py<T>>
where
F: FnOnce(crate::PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeObject + PyTypeInfo,
{
let ob = <T as PyTypeCreate>::create(py)?;
Expand All @@ -188,7 +173,7 @@ where
/// Returns references to `T`
pub fn new_ref<F>(py: Python, f: F) -> PyResult<&T>
where
F: FnOnce(crate::PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeObject + PyTypeInfo,
{
let ob = <T as PyTypeCreate>::create(py)?;
Expand All @@ -201,7 +186,7 @@ where
/// Returns mutable references to `T`
pub fn new_mut<F>(py: Python, f: F) -> PyResult<&mut T>
where
F: FnOnce(crate::PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeObject + PyTypeInfo,
{
let ob = <T as PyTypeCreate>::create(py)?;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub use crate::conversion::{
ToBorrowedObject, ToPyObject,
};
pub use crate::err::{PyDowncastError, PyErr, PyErrArguments, PyErrValue, PyResult};
pub use crate::instance::{AsPyRef, Py, PyNativeType, PyObjectWithToken, PyToken};
pub use crate::instance::{AsPyRef, Py, PyNativeType, PyObjectWithToken};
pub use crate::noargs::NoArgs;
pub use crate::object::PyObject;
pub use crate::objectprotocol::ObjectProtocol;
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
pub use crate::conversion::{FromPyObject, IntoPyObject, PyTryFrom, PyTryInto, ToPyObject};
pub use crate::err::{PyErr, PyResult};
pub use crate::instance::{AsPyRef, Py, PyToken};
pub use crate::instance::{AsPyRef, Py};
pub use crate::noargs::NoArgs;
pub use crate::object::PyObject;
pub use crate::objectprotocol::ObjectProtocol;
Expand Down
8 changes: 4 additions & 4 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::conversion::PyTryFrom;
use crate::err::{PyDowncastError, PyErr, PyResult};
use crate::ffi;
use crate::instance::{AsPyRef, Py, PyToken};
use crate::instance::{AsPyRef, Py};
use crate::object::PyObject;
use crate::pythonrun::{self, GILGuard};
use crate::typeob::PyTypeCreate;
Expand Down Expand Up @@ -256,7 +256,7 @@ impl<'p> Python<'p> {
#[inline]
pub fn init<T, F>(self, f: F) -> PyResult<Py<T>>
where
F: FnOnce(PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeCreate,
{
Py::new(self, f)
Expand All @@ -267,7 +267,7 @@ impl<'p> Python<'p> {
#[inline]
pub fn init_ref<T, F>(self, f: F) -> PyResult<&'p T>
where
F: FnOnce(PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeCreate,
{
Py::new_ref(self, f)
Expand All @@ -278,7 +278,7 @@ impl<'p> Python<'p> {
#[inline]
pub fn init_mut<T, F>(self, f: F) -> PyResult<&'p mut T>
where
F: FnOnce(PyToken) -> T,
F: FnOnce() -> T,
T: PyTypeCreate,
{
Py::new_mut(self, f)
Expand Down
Loading

0 comments on commit 57afb51

Please sign in to comment.