rename token() to py()

This commit is contained in:
Nikolay Kim 2017-07-13 19:04:00 -07:00
parent fb8b32072f
commit c5f5620f77
24 changed files with 149 additions and 149 deletions

View File

@ -55,7 +55,7 @@ impl MyClass {
#[new]
fn __new__(cls: &PyType, ...) -> PyResult<Py<MyClass>> {
cls.token().init(|token| {
cls.py().init(|token| {
MyClass {
num: 10,
debug: False,

View File

@ -55,7 +55,7 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
Some(quote! {
impl _pyo3::PyObjectWithToken for #cls {
#[inline]
fn token<'p>(&'p self) -> _pyo3::Python<'p> {
fn py<'p>(&'p self) -> _pyo3::Python<'p> {
self.#token.py()
}
}
@ -226,7 +226,7 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
let ptr = (ob.as_ptr() as *mut u8).offset(offset) as *mut #cls;
Ok(ptr.as_ref().unwrap())
} else {
Err(_pyo3::PyDowncastError(ob.token(), None))
Err(_pyo3::PyDowncastError(ob.py(), None))
}
}
}
@ -258,7 +258,7 @@ fn impl_class(cls: &syn::Ident, base: &syn::Ident,
let ptr = (ob.as_ptr() as *mut u8).offset(offset) as *mut #cls;
Ok(ptr.as_mut().unwrap())
} else {
Err(_pyo3::PyDowncastError(ob.token(), None))
Err(_pyo3::PyDowncastError(ob.py(), None))
}
}
}

View File

@ -24,7 +24,7 @@ impl PyToken {
}
pub trait PyObjectWithToken: Sized {
fn token(&self) -> Python;
fn py(&self) -> Python;
}
pub trait PyNativeType: PyObjectWithToken {}

View File

@ -289,7 +289,7 @@ impl<'a> FromPyObject<'a> for PyObject
fn extract(ob: &'a PyObjectRef) -> PyResult<Self>
{
unsafe {
Ok(PyObject::from_borrowed_ptr(ob.token(), ob.as_ptr()))
Ok(PyObject::from_borrowed_ptr(ob.py(), ob.as_ptr()))
}
}
}

View File

@ -150,7 +150,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
#[inline]
fn hasattr<N>(&self, attr_name: N) -> PyResult<bool> where N: ToPyObject {
attr_name.with_borrowed_ptr(self.token(), |attr_name| unsafe {
attr_name.with_borrowed_ptr(self.py(), |attr_name| unsafe {
Ok(ffi::PyObject_HasAttr(self.as_ptr(), attr_name) != 0)
})
}
@ -158,8 +158,8 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
#[inline]
fn getattr<N>(&self, attr_name: N) -> PyResult<&PyObjectRef> where N: ToPyObject
{
attr_name.with_borrowed_ptr(self.token(), |attr_name| unsafe {
self.token().cast_from_ptr_or_err(
attr_name.with_borrowed_ptr(self.py(), |attr_name| unsafe {
self.py().cast_from_ptr_or_err(
ffi::PyObject_GetAttr(self.as_ptr(), attr_name))
})
}
@ -169,17 +169,17 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
where N: ToPyObject, V: ToPyObject
{
attr_name.with_borrowed_ptr(
self.token(), move |attr_name|
value.with_borrowed_ptr(self.token(), |value| unsafe {
self.py(), move |attr_name|
value.with_borrowed_ptr(self.py(), |value| unsafe {
err::error_on_minusone(
self.token(), ffi::PyObject_SetAttr(self.as_ptr(), attr_name, value))
self.py(), ffi::PyObject_SetAttr(self.as_ptr(), attr_name, value))
}))
}
#[inline]
fn delattr<N>(&self, attr_name: N) -> PyResult<()> where N: ToPyObject {
attr_name.with_borrowed_ptr(self.token(), |attr_name| unsafe {
err::error_on_minusone(self.token(),
attr_name.with_borrowed_ptr(self.py(), |attr_name| unsafe {
err::error_on_minusone(self.py(),
ffi::PyObject_DelAttr(self.as_ptr(), attr_name))
})
}
@ -209,17 +209,17 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
Err(PyErr::new::<::exc::TypeError, _>(py, "ObjectProtocol::compare(): All comparisons returned false"))
}
other.with_borrowed_ptr(self.token(), |other| unsafe {
do_compare(self.token(), self.as_ptr(), other)
other.with_borrowed_ptr(self.py(), |other| unsafe {
do_compare(self.py(), self.as_ptr(), other)
})
}
fn rich_compare<O>(&self, other: O, compare_op: ::CompareOp)
-> PyResult<PyObject> where O: ToPyObject {
unsafe {
other.with_borrowed_ptr(self.token(), |other| {
other.with_borrowed_ptr(self.py(), |other| {
PyObject::from_owned_ptr_or_err(
self.token(), ffi::PyObject_RichCompare(
self.py(), ffi::PyObject_RichCompare(
self.as_ptr(), other, compare_op as c_int))
})
}
@ -228,14 +228,14 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
#[inline]
fn repr(&self) -> PyResult<&PyString> {
unsafe {
self.token().cast_from_ptr_or_err(ffi::PyObject_Repr(self.as_ptr()))
self.py().cast_from_ptr_or_err(ffi::PyObject_Repr(self.as_ptr()))
}
}
#[inline]
fn str(&self) -> PyResult<&PyString> {
unsafe {
self.token().cast_from_ptr_or_err(ffi::PyObject_Str(self.as_ptr()))
self.py().cast_from_ptr_or_err(ffi::PyObject_Str(self.as_ptr()))
}
}
@ -250,12 +250,12 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
fn call<A>(&self, args: A, kwargs: Option<&PyDict>) -> PyResult<&PyObjectRef>
where A: IntoPyTuple
{
let t = args.into_tuple(self.token());
let t = args.into_tuple(self.py());
let result = unsafe {
self.token().cast_from_borrowed_ptr_or_err(
self.py().cast_from_borrowed_ptr_or_err(
ffi::PyObject_Call(self.as_ptr(), t.as_ptr(), kwargs.as_ptr()))
};
self.token().release(t);
self.py().release(t);
result
}
@ -264,12 +264,12 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
-> PyResult<&PyObjectRef>
where A: IntoPyTuple
{
name.with_borrowed_ptr(self.token(), |name| unsafe {
let t = args.into_tuple(self.token());
name.with_borrowed_ptr(self.py(), |name| unsafe {
let t = args.into_tuple(self.py());
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
let result = self.token().cast_from_borrowed_ptr_or_err(
let result = self.py().cast_from_borrowed_ptr_or_err(
ffi::PyObject_Call(ptr, t.as_ptr(), kwargs.as_ptr()));
self.token().release(t);
self.py().release(t);
result
})
}
@ -278,7 +278,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
fn hash(&self) -> PyResult<isize> {
let v = unsafe { ffi::PyObject_Hash(self.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else {
Ok(v)
}
@ -288,7 +288,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
fn is_true(&self) -> PyResult<bool> {
let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else {
Ok(v != 0)
}
@ -303,7 +303,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
fn len(&self) -> PyResult<usize> {
let v = unsafe { ffi::PyObject_Size(self.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else {
Ok(v as usize)
}
@ -311,8 +311,8 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
#[inline]
fn get_item<K>(&self, key: K) -> PyResult<&PyObjectRef> where K: ToPyObject {
key.with_borrowed_ptr(self.token(), |key| unsafe {
self.token().cast_from_ptr_or_err(
key.with_borrowed_ptr(self.py(), |key| unsafe {
self.py().cast_from_ptr_or_err(
ffi::PyObject_GetItem(self.as_ptr(), key))
})
}
@ -322,18 +322,18 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
where K: ToPyObject, V: ToPyObject
{
key.with_borrowed_ptr(
self.token(), move |key|
value.with_borrowed_ptr(self.token(), |value| unsafe {
self.py(), move |key|
value.with_borrowed_ptr(self.py(), |value| unsafe {
err::error_on_minusone(
self.token(), ffi::PyObject_SetItem(self.as_ptr(), key, value))
self.py(), ffi::PyObject_SetItem(self.as_ptr(), key, value))
}))
}
#[inline]
fn del_item<K>(&self, key: K) -> PyResult<()> where K: ToPyObject {
key.with_borrowed_ptr(self.token(), |key| unsafe {
key.with_borrowed_ptr(self.py(), |key| unsafe {
err::error_on_minusone(
self.token(), ffi::PyObject_DelItem(self.as_ptr(), key))
self.py(), ffi::PyObject_DelItem(self.as_ptr(), key))
})
}
@ -341,15 +341,15 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
fn iter<'p>(&'p self) -> PyResult<PyIterator<'p>> {
unsafe {
let ptr = PyObject::from_owned_ptr_or_err(
self.token(), ffi::PyObject_GetIter(self.as_ptr()))?;
PyIterator::from_object(self.token(), ptr).map_err(|e| e.into())
self.py(), ffi::PyObject_GetIter(self.as_ptr()))?;
PyIterator::from_object(self.py(), ptr).map_err(|e| e.into())
}
}
#[inline]
fn get_type(&self) -> &PyType {
unsafe {
PyType::from_type_ptr(self.token(), (*self.as_ptr()).ob_type)
PyType::from_type_ptr(self.py(), (*self.as_ptr()).ob_type)
}
}
@ -370,13 +370,13 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
}
fn clone_ref(&self, ptr: &PyObject) -> PyObject {
ptr.clone_ref(self.token())
ptr.clone_ref(self.py())
}
#[allow(non_snake_case)] // the Python keyword starts with uppercase
#[inline]
fn None(&self) -> PyObject {
unsafe { PyObject::from_borrowed_ptr(self.token(), ffi::Py_None()) }
unsafe { PyObject::from_borrowed_ptr(self.py(), ffi::Py_None()) }
}
fn get_refcnt(&self) -> isize {

View File

@ -64,7 +64,7 @@ impl PyByteArray {
if result == 0 {
Ok(())
} else {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
}
}
}

View File

@ -33,7 +33,7 @@ impl PyDict {
/// Corresponds to `dict(self)` in Python.
pub fn copy(&self) -> PyResult<&PyDict> {
unsafe {
self.token().cast_from_ptr_or_err::<PyDict>(ffi::PyDict_Copy(self.as_ptr()))
self.py().cast_from_ptr_or_err::<PyDict>(ffi::PyDict_Copy(self.as_ptr()))
}
}
@ -53,11 +53,11 @@ impl PyDict {
/// Determine if the dictionary contains the specified key.
/// This is equivalent to the Python expression `key in self`.
pub fn contains<K>(&self, key: K) -> PyResult<bool> where K: ToPyObject {
key.with_borrowed_ptr(self.token(), |key| unsafe {
key.with_borrowed_ptr(self.py(), |key| unsafe {
match ffi::PyDict_Contains(self.as_ptr(), key) {
1 => Ok(true),
0 => Ok(false),
_ => Err(PyErr::fetch(self.token()))
_ => Err(PyErr::fetch(self.py()))
}
})
}
@ -65,8 +65,8 @@ impl PyDict {
/// Gets an item from the dictionary.
/// Returns None if the item is not present, or if an error occurs.
pub fn get_item<K>(&self, key: K) -> Option<&PyObjectRef> where K: ToPyObject {
key.with_borrowed_ptr(self.token(), |key| unsafe {
self.token().cast_from_borrowed_ptr_or_opt(
key.with_borrowed_ptr(self.py(), |key| unsafe {
self.py().cast_from_borrowed_ptr_or_opt(
ffi::PyDict_GetItem(self.as_ptr(), key))
})
}
@ -77,10 +77,10 @@ impl PyDict {
where K: ToPyObject, V: ToPyObject
{
key.with_borrowed_ptr(
self.token(), move |key|
value.with_borrowed_ptr(self.token(), |value| unsafe {
self.py(), move |key|
value.with_borrowed_ptr(self.py(), |value| unsafe {
err::error_on_minusone(
self.token(), ffi::PyDict_SetItem(self.as_ptr(), key, value))
self.py(), ffi::PyDict_SetItem(self.as_ptr(), key, value))
}))
}
@ -88,9 +88,9 @@ impl PyDict {
/// This is equivalent to the Python expression `del self[key]`.
pub fn del_item<K>(&self, key: K) -> PyResult<()> where K: ToPyObject
{
key.with_borrowed_ptr(self.token(), |key| unsafe {
key.with_borrowed_ptr(self.py(), |key| unsafe {
err::error_on_minusone(
self.token(), ffi::PyDict_DelItem(self.as_ptr(), key))
self.py(), ffi::PyDict_DelItem(self.as_ptr(), key))
})
}
@ -98,7 +98,7 @@ impl PyDict {
/// This is equivalent to the python expression `list(dict.items())`.
pub fn items_list(&self) -> &PyList {
unsafe {
self.token().cast_from_ptr::<PyList>(
self.py().cast_from_ptr::<PyList>(
ffi::PyDict_Items(self.as_ptr()))
}
}
@ -114,8 +114,8 @@ impl PyDict {
let mut key: *mut ffi::PyObject = mem::uninitialized();
let mut value: *mut ffi::PyObject = mem::uninitialized();
while ffi::PyDict_Next(self.as_ptr(), &mut pos, &mut key, &mut value) != 0 {
vec.push((PyObject::from_borrowed_ptr(self.token(), key),
PyObject::from_borrowed_ptr(self.token(), value)));
vec.push((PyObject::from_borrowed_ptr(self.py(), key),
PyObject::from_borrowed_ptr(self.py(), value)));
}
}
vec

View File

@ -50,8 +50,8 @@ impl IntoPyObject for f64 {
pyobject_extract!(py, obj to f64 => {
let v = unsafe { ffi::PyFloat_AsDouble(obj.as_ptr()) };
if v == -1.0 && PyErr::occurred(obj.token()) {
Err(PyErr::fetch(obj.token()))
if v == -1.0 && PyErr::occurred(obj.py()) {
Err(PyErr::fetch(obj.py()))
} else {
Ok(v)
}

View File

@ -42,7 +42,7 @@ impl <'p> Iterator for PyIterator<'p> {
/// Further `next()` calls after an exception occurs are likely
/// to repeatedly result in the same exception.
fn next(&mut self) -> Option<Self::Item> {
let py = self.0.token();
let py = self.0.py();
match unsafe {
py.cast_from_ptr_or_opt(ffi::PyIter_Next(self.0.as_ptr())) }

View File

@ -51,7 +51,7 @@ impl PyList {
pub fn get_item(&self, index: isize) -> &PyObjectRef {
unsafe {
let ptr = ffi::PyList_GetItem(self.as_ptr(), index as Py_ssize_t);
self.token().cast_from_borrowed_ptr(ptr)
self.py().cast_from_borrowed_ptr(ptr)
}
}
@ -61,7 +61,7 @@ impl PyList {
pub fn get_parked_item(&self, index: isize) -> PyObject {
unsafe {
let ptr = ffi::PyList_GetItem(self.as_ptr(), index as Py_ssize_t);
PyObject::from_borrowed_ptr(self.token(), ptr)
PyObject::from_borrowed_ptr(self.py(), ptr)
}
}
@ -71,9 +71,9 @@ impl PyList {
pub fn set_item<I>(&self, index: isize, item: I) -> PyResult<()>
where I: ToPyObject
{
item.with_borrowed_ptr(self.token(), |item| unsafe {
item.with_borrowed_ptr(self.py(), |item| unsafe {
err::error_on_minusone(
self.token(), ffi::PyList_SetItem(self.as_ptr(), index, item))
self.py(), ffi::PyList_SetItem(self.as_ptr(), index, item))
})
}
@ -83,9 +83,9 @@ impl PyList {
pub fn insert_item<I>(&self, index: isize, item: I) -> PyResult<()>
where I: ToPyObject
{
item.with_borrowed_ptr(self.token(), |item| unsafe {
item.with_borrowed_ptr(self.py(), |item| unsafe {
err::error_on_minusone(
self.token(), ffi::PyList_Insert(self.as_ptr(), index, item))
self.py(), ffi::PyList_Insert(self.as_ptr(), index, item))
})
}

View File

@ -42,7 +42,7 @@ macro_rules! pyobject_downcast(
if $crate::ffi::$checkfunction(ob.as_ptr()) > 0 {
Ok($crate::std::mem::transmute(ob))
} else {
Err($crate::PyDowncastError(ob.token(), None))
Err($crate::PyDowncastError(ob.py(), None))
}
}
}
@ -68,7 +68,7 @@ macro_rules! pyobject_downcast(
if $crate::ffi::$checkfunction(ob.as_ptr()) != 0 {
Ok($crate::std::mem::transmute(ob))
} else {
Err($crate::PyDowncastError(ob.token(), None).into())
Err($crate::PyDowncastError(ob.py(), None).into())
}
}
}
@ -97,7 +97,7 @@ macro_rules! pyobject_nativetype(
}
impl $crate::PyObjectWithToken for $name {
#[inline]
fn token<'p>(&'p self) -> $crate::Python<'p> {
fn py<'p>(&'p self) -> $crate::Python<'p> {
unsafe { $crate::Python::assume_gil_acquired() }
}
}

View File

@ -46,21 +46,21 @@ impl PyModule {
/// this object is the same as the `__dict__` attribute of the module object.
pub fn dict(&self) -> &PyDict {
unsafe {
self.token().cast_from_ptr::<PyDict>(
self.py().cast_from_ptr::<PyDict>(
ffi::PyModule_GetDict(self.as_ptr()))
}
}
unsafe fn str_from_ptr<'a>(&'a self, ptr: *const c_char) -> PyResult<&'a str> {
if ptr.is_null() {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else {
let slice = CStr::from_ptr(ptr).to_bytes();
match std::str::from_utf8(slice) {
Ok(s) => Ok(s),
Err(e) => Err(PyErr::from_instance(
self.token(),
try!(exc::UnicodeDecodeError::new_utf8(self.token(), slice, e))))
self.py(),
try!(exc::UnicodeDecodeError::new_utf8(self.py(), slice, e))))
}
}
}
@ -112,16 +112,16 @@ impl PyModule {
let mut ty = unsafe{<T as ::typeob::PyTypeInfo>::type_object()};
let ty = if (ty.tp_flags & ffi::Py_TPFLAGS_READY) != 0 {
unsafe { PyType::from_type_ptr(self.token(), ty) }
unsafe { PyType::from_type_ptr(self.py(), ty) }
} else {
// automatically initialize the class
let name = self.name()?;
::typeob::initialize_type::<T>(
self.token(), Some(name), ty)
self.py(), Some(name), ty)
.expect(
format!("An error occurred while initializing class {}", T::NAME).as_ref());
unsafe { PyType::from_type_ptr(self.token(), ty) }
unsafe { PyType::from_type_ptr(self.py(), ty) }
};
self.setattr(T::NAME, ty)

View File

@ -81,12 +81,12 @@ macro_rules! int_fits_c_long(
}
pyobject_extract!(py, obj to $rust_type => {
let val = unsafe { ffi::PyLong_AsLong(obj.as_ptr()) };
if val == -1 && PyErr::occurred(obj.token()) {
return Err(PyErr::fetch(obj.token()));
if val == -1 && PyErr::occurred(obj.py()) {
return Err(PyErr::fetch(obj.py()));
}
match cast::<c_long, $rust_type>(val) {
Some(v) => Ok(v),
None => Err(overflow_error(obj.token()))
None => Err(overflow_error(obj.py()))
}
});
)
@ -110,7 +110,7 @@ macro_rules! int_fits_larger_int(
let val = try!(obj.extract::<$larger_type>());
match cast::<$larger_type, $rust_type>(val) {
Some(v) => Ok(v),
None => Err(overflow_error(obj.token()))
None => Err(overflow_error(obj.py()))
}
});
)
@ -158,17 +158,17 @@ macro_rules! int_convert_u64_or_i64 (
let ptr = obj.as_ptr();
unsafe {
if ffi::PyLong_Check(ptr) != 0 {
err_if_invalid_value(obj.token(), !0, $pylong_as_ull_or_ull(ptr))
err_if_invalid_value(obj.py(), !0, $pylong_as_ull_or_ull(ptr))
} else if ffi::PyInt_Check(ptr) != 0 {
match cast::<c_long, $rust_type>(ffi::PyInt_AS_LONG(ptr)) {
Some(v) => Ok(v),
None => Err(overflow_error(obj.token()))
None => Err(overflow_error(obj.py()))
}
} else {
let num = PyObject::from_owned_ptr_or_err(
obj.token(), ffi::PyNumber_Long(ptr))?;
obj.py(), ffi::PyNumber_Long(ptr))?;
err_if_invalid_value(
obj.token(), !0, $pylong_as_ull_or_ull(num.into_ptr()))
obj.py(), !0, $pylong_as_ull_or_ull(num.into_ptr()))
}
}
}

View File

@ -45,12 +45,12 @@ macro_rules! int_fits_c_long(
}
pyobject_extract!(py, obj to $rust_type => {
let val = unsafe { ffi::PyLong_AsLong(obj.as_ptr()) };
if val == -1 && PyErr::occurred(obj.token()) {
return Err(PyErr::fetch(obj.token()));
if val == -1 && PyErr::occurred(obj.py()) {
return Err(PyErr::fetch(obj.py()));
}
match cast::<c_long, $rust_type>(val) {
Some(v) => Ok(v),
None => Err(overflow_error(obj.token()))
None => Err(overflow_error(obj.py()))
}
});
)
@ -74,7 +74,7 @@ macro_rules! int_fits_larger_int(
let val = try!(obj.extract::<$larger_type>());
match cast::<$larger_type, $rust_type>(val) {
Some(v) => Ok(v),
None => Err(overflow_error(obj.token()))
None => Err(overflow_error(obj.py()))
}
});
)
@ -115,13 +115,13 @@ macro_rules! int_convert_u64_or_i64 (
let ptr = ob.as_ptr();
unsafe {
if ffi::PyLong_Check(ptr) != 0 {
err_if_invalid_value(ob.token(), !0, $pylong_as_ull_or_ull(ptr))
err_if_invalid_value(ob.py(), !0, $pylong_as_ull_or_ull(ptr))
} else {
let num = ffi::PyNumber_Long(ptr);
if num.is_null() {
Err(PyErr::fetch(ob.token()))
Err(PyErr::fetch(ob.py()))
} else {
err_if_invalid_value(ob.token(), !0, $pylong_as_ull_or_ull(num))
err_if_invalid_value(ob.py(), !0, $pylong_as_ull_or_ull(num))
}
}
}

View File

@ -28,7 +28,7 @@ impl PySequence {
pub fn len(&self) -> PyResult<isize> {
let v = unsafe { ffi::PySequence_Size(self.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else {
Ok(v as isize)
}
@ -38,7 +38,7 @@ impl PySequence {
#[inline]
pub fn concat(&self, other: &PySequence) -> PyResult<&PySequence> {
unsafe {
self.token().cast_from_ptr_or_err::<PySequence>(
self.py().cast_from_ptr_or_err::<PySequence>(
ffi::PySequence_Concat(self.as_ptr(), other.as_ptr()))
}
}
@ -49,7 +49,7 @@ impl PySequence {
#[inline]
pub fn repeat(&self, count: isize) -> PyResult<&PySequence> {
unsafe {
self.token().cast_from_ptr_or_err::<PySequence>(
self.py().cast_from_ptr_or_err::<PySequence>(
ffi::PySequence_Repeat(self.as_ptr(), count as Py_ssize_t))
}
}
@ -60,7 +60,7 @@ impl PySequence {
unsafe {
let ptr = ffi::PySequence_InPlaceConcat(self.as_ptr(), other.as_ptr());
if ptr.is_null() {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else {
Ok(())
}
@ -75,7 +75,7 @@ impl PySequence {
unsafe {
let ptr = ffi::PySequence_InPlaceRepeat(self.as_ptr(), count as Py_ssize_t);
if ptr.is_null() {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else {
Ok(())
}
@ -86,7 +86,7 @@ impl PySequence {
#[inline]
pub fn get_item(&self, index: isize) -> PyResult<&PyObjectRef> {
unsafe {
self.token().cast_from_ptr_or_err(
self.py().cast_from_ptr_or_err(
ffi::PySequence_GetItem(self.as_ptr(), index as Py_ssize_t))
}
}
@ -96,7 +96,7 @@ impl PySequence {
#[inline]
pub fn get_slice(&self, begin: isize, end: isize) -> PyResult<&PyObjectRef> {
unsafe {
self.token().cast_from_ptr_or_err(
self.py().cast_from_ptr_or_err(
ffi::PySequence_GetSlice(
self.as_ptr(), begin as Py_ssize_t, end as Py_ssize_t))
}
@ -108,7 +108,7 @@ impl PySequence {
pub fn set_item(&self, i: isize, v: &PyObjectRef) -> PyResult<()> {
unsafe {
err::error_on_minusone(
self.token(),
self.py(),
ffi::PySequence_SetItem(self.as_ptr(), i as Py_ssize_t, v.as_ptr()))
}
}
@ -119,7 +119,7 @@ impl PySequence {
pub fn del_item(&self, i: isize) -> PyResult<()> {
unsafe {
err::error_on_minusone(
self.token(),
self.py(),
ffi::PySequence_DelItem(self.as_ptr(), i as Py_ssize_t))
}
}
@ -130,7 +130,7 @@ impl PySequence {
pub fn set_slice(&self, i1: isize, i2: isize, v: &PyObjectRef) -> PyResult<()> {
unsafe {
err::error_on_minusone(
self.token(), ffi::PySequence_SetSlice(
self.py(), ffi::PySequence_SetSlice(
self.as_ptr(), i1 as Py_ssize_t, i2 as Py_ssize_t, v.as_ptr()))
}
}
@ -141,7 +141,7 @@ impl PySequence {
pub fn del_slice(&self, i1: isize, i2: isize) -> PyResult<()> {
unsafe {
err::error_on_minusone(
self.token(),
self.py(),
ffi::PySequence_DelSlice(self.as_ptr(), i1 as Py_ssize_t, i2 as Py_ssize_t))
}
}
@ -151,11 +151,11 @@ impl PySequence {
#[inline]
pub fn count<V>(&self, value: V) -> PyResult<usize> where V: ToPyObject
{
let r = value.with_borrowed_ptr(self.token(), |ptr| unsafe {
let r = value.with_borrowed_ptr(self.py(), |ptr| unsafe {
ffi::PySequence_Count(self.as_ptr(), ptr)
});
if r == -1 {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else {
Ok(r as usize)
}
@ -165,13 +165,13 @@ impl PySequence {
#[inline]
pub fn contains<V>(&self, value: V) -> PyResult<bool> where V: ToPyObject
{
let r = value.with_borrowed_ptr(self.token(), |ptr| unsafe {
let r = value.with_borrowed_ptr(self.py(), |ptr| unsafe {
ffi::PySequence_Contains(self.as_ptr(), ptr)
});
match r {
0 => Ok(false),
1 => Ok(true),
_ => Err(PyErr::fetch(self.token()))
_ => Err(PyErr::fetch(self.py()))
}
}
@ -180,11 +180,11 @@ impl PySequence {
#[inline]
pub fn index<V>(&self, value: V) -> PyResult<usize> where V: ToPyObject
{
let r = value.with_borrowed_ptr(self.token(), |ptr| unsafe {
let r = value.with_borrowed_ptr(self.py(), |ptr| unsafe {
ffi::PySequence_Index(self.as_ptr(), ptr)
});
if r == -1 {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else {
Ok(r as usize)
}

View File

@ -48,33 +48,33 @@ impl PySet {
/// Determine if the set contains the specified key.
/// This is equivalent to the Python expression `key in self`.
pub fn contains<K>(&self, key: K) -> PyResult<bool> where K: ToPyObject {
key.with_borrowed_ptr(self.token(), |key| unsafe {
key.with_borrowed_ptr(self.py(), |key| unsafe {
match ffi::PySet_Contains(self.as_ptr(), key) {
1 => Ok(true),
0 => Ok(false),
_ => Err(PyErr::fetch(self.token()))
_ => Err(PyErr::fetch(self.py()))
}
})
}
/// Remove element from the set if it is present.
pub fn discard<K>(&self, key: K) where K: ToPyObject {
key.with_borrowed_ptr(self.token(), |key| unsafe {
key.with_borrowed_ptr(self.py(), |key| unsafe {
ffi::PySet_Discard(self.as_ptr(), key);
})
}
/// Add element to the set.
pub fn add<K>(&self, key: K) -> PyResult<()> where K: ToPyObject {
key.with_borrowed_ptr(self.token(), move |key| unsafe {
err::error_on_minusone(self.token(), ffi::PySet_Add(self.as_ptr(), key))
key.with_borrowed_ptr(self.py(), move |key| unsafe {
err::error_on_minusone(self.py(), ffi::PySet_Add(self.as_ptr(), key))
})
}
/// Remove and return an arbitrary element from the set
pub fn pop(&self) -> Option<PyObject> {
unsafe {
PyObject::from_owned_ptr_or_opt(self.token(), ffi::PySet_Pop(self.as_ptr()))
PyObject::from_owned_ptr_or_opt(self.py(), ffi::PySet_Pop(self.as_ptr()))
}
}
}
@ -130,11 +130,11 @@ impl PyFrozenSet {
/// Determine if the set contains the specified key.
/// This is equivalent to the Python expression `key in self`.
pub fn contains<K>(&self, key: K) -> PyResult<bool> where K: ToPyObject {
key.with_borrowed_ptr(self.token(), |key| unsafe {
key.with_borrowed_ptr(self.py(), |key| unsafe {
match ffi::PySet_Contains(self.as_ptr(), key) {
1 => Ok(true),
0 => Ok(false),
_ => Err(PyErr::fetch(self.token()))
_ => Err(PyErr::fetch(self.py()))
}
})
}

View File

@ -72,7 +72,7 @@ impl PySlice {
slicelength: slicelen,
})
} else {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
}
}
}

View File

@ -49,7 +49,7 @@ impl PyString {
-> PyResult<&'p PyString>
{
unsafe {
src.token().cast_from_ptr_or_err::<PyString>(
src.py().cast_from_ptr_or_err::<PyString>(
ffi::PyUnicode_FromEncodedObject(
src.as_ptr(),
encoding.as_ptr() as *const i8,
@ -65,7 +65,7 @@ impl PyString {
let mut size: ffi::Py_ssize_t = mem::uninitialized();
let data = ffi::PyUnicode_AsUTF8AndSize(self.0.as_ptr(), &mut size) as *const u8;
if data.is_null() {
PyErr::fetch(self.token()).print(self.token());
PyErr::fetch(self.py()).print(self.py());
panic!("PyUnicode_AsUTF8AndSize failed");
}
PyStringData::Utf8(std::slice::from_raw_parts(data, size as usize))
@ -77,7 +77,7 @@ impl PyString {
/// Returns a `UnicodeDecodeError` if the input is not valid unicode
/// (containing unpaired surrogates).
pub fn to_string(&self) -> PyResult<Cow<str>> {
self.data().to_string(self.token())
self.data().to_string(self.py())
}
/// Convert the `PyString` into a Rust string.

View File

@ -54,7 +54,7 @@ impl PyString {
pub fn from_object(src: &PyObjectRef, encoding: &str, errors: &str) -> PyResult<Py<PyString>> {
unsafe {
Ok(Py::from_owned_ptr_or_err(
src.token(), ffi::PyUnicode_FromEncodedObject(
src.py(), ffi::PyUnicode_FromEncodedObject(
src.as_ptr(), encoding.as_ptr() as *const i8, errors.as_ptr() as *const i8))?
)
}
@ -84,7 +84,7 @@ impl PyString {
/// (containing unpaired surrogates, or a Python 2.7 byte string that is
/// not valid UTF-8).
pub fn to_string(&self) -> PyResult<Cow<str>> {
self.data().to_string(self.token())
self.data().to_string(self.py())
}
/// Convert the `PyString` into a Rust string.
@ -139,7 +139,7 @@ impl PyUnicode {
{
unsafe {
Ok(Py::from_owned_ptr_or_err(
src.token(), ffi::PyUnicode_FromEncodedObject(
src.py(), ffi::PyUnicode_FromEncodedObject(
src.as_ptr(),
encoding.as_ptr() as *const i8,
errors.as_ptr() as *const i8))?)
@ -160,7 +160,7 @@ impl PyUnicode {
/// Returns a `UnicodeDecodeError` if the input is not valid unicode
/// (containing unpaired surrogates).
pub fn to_string(&self) -> PyResult<Cow<str>> {
self.data().to_string(self.token())
self.data().to_string(self.py())
}
/// Convert the `PyString` into a Rust string.

View File

@ -59,7 +59,7 @@ impl PyTuple {
// It's quite inconsistent that this method takes `Python` when `len()` does not.
assert!(index < self.len());
unsafe {
self.token().cast_from_borrowed_ptr(
self.py().cast_from_borrowed_ptr(
ffi::PyTuple_GET_ITEM(self.as_ptr(), index as Py_ssize_t))
}
}
@ -150,10 +150,10 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
let slice = t.as_slice();
if t.len() == $length {
Ok((
$( try!(slice[$n].extract::<$T>(obj.token())), )+
$( try!(slice[$n].extract::<$T>(obj.py())), )+
))
} else {
Err(wrong_tuple_length(obj.token(), t, $length))
Err(wrong_tuple_length(obj.py(), t, $length))
}
}
}
@ -228,7 +228,7 @@ pyobject_extract!(py, obj to NoArgs => {
if t.len() == 0 {
Ok(NoArgs)
} else {
Err(wrong_tuple_length(obj.token(), t, 0))
Err(wrong_tuple_length(obj.py(), t, 0))
}
});

View File

@ -47,10 +47,10 @@ impl PyType {
where T: PyTypeObject
{
let result = unsafe {
ffi::PyObject_IsSubclass(self.as_ptr(), T::type_object(self.token()).as_ptr())
ffi::PyObject_IsSubclass(self.as_ptr(), T::type_object(self.py()).as_ptr())
};
if result == -1 {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else if result == 1 {
Ok(true)
} else {
@ -64,7 +64,7 @@ impl PyType {
ffi::PyObject_IsInstance(obj.as_ptr(), self.as_ptr())
};
if result == -1 {
Err(PyErr::fetch(self.token()))
Err(PyErr::fetch(self.py()))
} else if result == 1 {
Ok(true)
} else {

View File

@ -20,7 +20,7 @@ impl class::PyBufferProtocol for TestClass {
fn bf_getbuffer(&self, view: *mut ffi::Py_buffer, flags: c_int) -> PyResult<()> {
if view == ptr::null_mut() {
return Err(PyErr::new::<exc::BufferError, _>(self.token(), "View is null"))
return Err(PyErr::new::<exc::BufferError, _>(self.py(), "View is null"))
}
unsafe {
@ -28,7 +28,7 @@ impl class::PyBufferProtocol for TestClass {
}
if (flags & ffi::PyBUF_WRITABLE) == ffi::PyBUF_WRITABLE {
return Err(PyErr::new::<exc::BufferError, _>(self.token(), "Object is not writable"))
return Err(PyErr::new::<exc::BufferError, _>(self.py(), "Object is not writable"))
}
let bytes = &self.vec;

View File

@ -102,7 +102,7 @@ struct EmptyClassWithNew {
impl EmptyClassWithNew {
#[__new__]
fn __new__(cls: &PyType) -> PyResult<Py<EmptyClassWithNew>> {
Ok(Py::new(cls.token(), |t| EmptyClassWithNew{token: t})?.into())
Ok(Py::new(cls.py(), |t| EmptyClassWithNew{token: t})?.into())
}
}
@ -124,7 +124,7 @@ struct NewWithOneArg {
impl NewWithOneArg {
#[new]
fn __new__(cls: &PyType, arg: i32) -> PyResult<&mut NewWithOneArg> {
cls.token().init_mut(|t| NewWithOneArg{_data: arg, token: t})
cls.py().init_mut(|t| NewWithOneArg{_data: arg, token: t})
}
}
@ -152,7 +152,7 @@ impl NewWithTwoArgs {
fn __new__(cls: &PyType, arg1: i32, arg2: i32) -> PyResult<Py<NewWithTwoArgs>>
{
Py::new(
cls.token(),
cls.py(),
|t| NewWithTwoArgs{_data1: arg1, _data2: arg2, token: t})
}
}
@ -341,7 +341,7 @@ struct ClassMethod {token: PyToken}
impl ClassMethod {
#[new]
fn __new__(cls: &PyType) -> PyResult<Py<ClassMethod>> {
cls.token().init(|t| ClassMethod{token: t})
cls.py().init(|t| ClassMethod{token: t})
}
#[classmethod]
@ -392,7 +392,7 @@ struct StaticMethod {
impl StaticMethod {
#[new]
fn __new__(cls: &PyType) -> PyResult<&StaticMethod> {
Ok(cls.token().init_mut(|t| StaticMethod{token: t})?.into())
Ok(cls.py().init_mut(|t| StaticMethod{token: t})?.into())
}
#[staticmethod]
@ -451,7 +451,7 @@ impl PyGCProtocol for GCIntegration {
}
fn __clear__(&mut self) {
*self.self_ref.borrow_mut() = self.token().None();
*self.self_ref.borrow_mut() = self.py().None();
}
}
@ -551,11 +551,11 @@ impl<'p> PyObjectProtocol<'p> for StringMethods {
}
fn __unicode__(&self) -> PyResult<PyObject> {
Ok(PyString::new(self.token(), "unicode").into())
Ok(PyString::new(self.py(), "unicode").into())
}
fn __bytes__(&self) -> PyResult<PyObject> {
Ok(PyBytes::new(self.token(), b"bytes").into())
Ok(PyBytes::new(self.py(), b"bytes").into())
}
}
@ -634,7 +634,7 @@ impl PySequenceProtocol for Sequence {
fn __getitem__(&self, key: isize) -> PyResult<isize> {
if key == 5 {
return Err(PyErr::new::<exc::IndexError, NoArgs>(self.token(), NoArgs));
return Err(PyErr::new::<exc::IndexError, NoArgs>(self.py(), NoArgs));
}
Ok(key)
}
@ -948,9 +948,9 @@ impl PyObjectProtocol for RichComparisons2 {
fn __richcmp__(&self, other: &'p PyObjectRef, op: CompareOp) -> PyResult<PyObject> {
match op {
CompareOp::Eq => Ok(true.to_object(self.token())),
CompareOp::Ne => Ok(false.to_object(self.token())),
_ => Ok(self.token().NotImplemented())
CompareOp::Eq => Ok(true.to_object(self.py())),
CompareOp::Ne => Ok(false.to_object(self.py())),
_ => Ok(self.py().NotImplemented())
}
}
}
@ -1112,7 +1112,7 @@ impl<'p> PyContextProtocol<'p> for ContextManager {
value: Option<&'p PyObjectRef>,
traceback: Option<&'p PyObjectRef>) -> PyResult<bool> {
self.exit_called = true;
if ty == Some(self.token().get_type::<exc::ValueError>()) {
if ty == Some(self.py().get_type::<exc::ValueError>()) {
Ok(true)
} else {
Ok(false)
@ -1196,7 +1196,7 @@ impl MethArgs {
}
#[args(args="*", kwargs="**")]
fn get_kwargs(&self, args: &PyTuple, kwargs: Option<&PyDict>) -> PyResult<PyObject> {
Ok([args.into(), kwargs.to_object(self.token())].to_object(self.token()))
Ok([args.into(), kwargs.to_object(self.py())].to_object(self.py()))
}
}

View File

@ -32,15 +32,15 @@ impl<'p> PyMappingProtocol<'p> for Test
if let Ok(slice) = idx.cast_as::<PySlice>() {
let indices = slice.indices(1000)?;
if indices.start == 100 && indices.stop == 200 && indices.step == 1 {
return Ok("slice".into_object(self.token()))
return Ok("slice".into_object(self.py()))
}
}
else if let Ok(idx) = idx.extract::<isize>() {
if idx == 1 {
return Ok("int".into_object(self.token()))
return Ok("int".into_object(self.py()))
}
}
Err(PyErr::new::<exc::ValueError, _>(self.token(), "error"))
Err(PyErr::new::<exc::ValueError, _>(self.py(), "error"))
}
}