fix api inconsystency

This commit is contained in:
Nikolay Kim 2017-06-29 11:23:55 +06:00
parent f494f0c3df
commit afe2324ecc
5 changed files with 25 additions and 11 deletions

View File

@ -264,6 +264,7 @@ fn wrap_fn(item: &mut syn::Item) -> Option<Box<syn::Block>> {
fn test() {
#item2
#[allow(unused_imports)]
{
use std;
use pyo3 as _pyo3;
@ -317,7 +318,7 @@ pub fn impl_wrap(name: &syn::Ident, spec: &method::FnSpec) -> Tokens {
let output = &spec.output;
quote! {
#[allow(unused_variables)]
#[allow(unused_variables, unused_imports)]
unsafe extern "C" fn wrap(_slf: *mut _pyo3::ffi::PyObject,
args: *mut _pyo3::ffi::PyObject,
kwargs: *mut _pyo3::ffi::PyObject) -> *mut _pyo3::ffi::PyObject

View File

@ -6,7 +6,7 @@ use pointer::PyObject;
use python::{ToPyPointer, Python};
use err::{PyErr, PyResult};
use ffi::{self, Py_ssize_t};
use instance::{Py, PyObjectWithToken};
use instance::PyObjectWithToken;
use conversion::ToPyObject;
/// Represents a Python `slice`. Only `c_long` indeces supprted
@ -40,12 +40,12 @@ impl PySliceIndices {
impl PySlice {
/// Construct a new slice with the given elements.
pub fn new(_py: Python, start: isize, stop: isize, step: isize) -> Py<PySlice> {
pub fn new<'p>(py: Python<'p>, start: isize, stop: isize, step: isize) -> &'p PySlice {
unsafe {
let ptr = ffi::PySlice_New(ffi::PyLong_FromLong(start as i64),
ffi::PyLong_FromLong(stop as i64),
ffi::PyLong_FromLong(step as i64));
Py::from_owned_ptr_or_panic(ptr)
py.cast_from_ptr(ptr)
}
}

View File

@ -45,14 +45,15 @@ impl PyString {
}
}
pub fn from_object(py: Python, src: &PyObjectRef, encoding: &str, errors: &str)
-> PyResult<Py<PyString>> {
pub fn from_object<'p>(src: &'p PyObjectRef, encoding: &str, errors: &str)
-> PyResult<&'p PyString>
{
unsafe {
Ok(Py::from_owned_ptr_or_err(
py, ffi::PyUnicode_FromEncodedObject(
src.token().cast_from_ptr_or_err::<PyString>(
ffi::PyUnicode_FromEncodedObject(
src.as_ptr(),
encoding.as_ptr() as *const i8,
errors.as_ptr() as *const i8))?)
errors.as_ptr() as *const i8))
}
}
@ -103,7 +104,7 @@ impl PyBytes {
}
/// Gets the Python string data as byte slice.
pub fn data(&self, _py: Python) -> &[u8] {
pub fn data(&self) -> &[u8] {
unsafe {
let buffer = ffi::PyBytes_AsString(self.as_ptr()) as *const u8;
let length = ffi::PyBytes_Size(self.as_ptr()) as usize;

View File

@ -129,6 +129,18 @@ impl PyObject {
unsafe { ffi::Py_None() == self.as_ptr() }
}
/// Returns whether the object is considered to be true.
/// This is equivalent to the Python expression: 'not not self'
#[inline]
pub fn is_true(&self, py: Python) -> PyResult<bool> {
let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(py))
} else {
Ok(v != 0)
}
}
/// Casts the PyObject to a concrete Python object type.
/// Fails with `PyDowncastError` if the object is not of the expected type.
#[inline]

View File

@ -12,7 +12,7 @@ fn test_basics() {
let py = gil.python();
let v = PySlice::new(py, 1, 10, 2);
let indices = v.as_ref(py).indices(100).unwrap();
let indices = v.indices(100).unwrap();
assert_eq!(1, indices.start);
assert_eq!(10, indices.stop);
assert_eq!(2, indices.step);