Objects module
This commit is contained in:
parent
c61e1e51f4
commit
540f934123
|
@ -2,7 +2,7 @@ use libc::c_char;
|
|||
use std;
|
||||
use ffi;
|
||||
use python::{Python, PythonObject, PythonObjectWithCheckedDowncast};
|
||||
use object::PyObject;
|
||||
use objects::PyObject;
|
||||
use err::{self, PyErr, PyResult};
|
||||
use pyptr::{PyPtr, PythonPointer};
|
||||
|
||||
|
|
46
src/dict.rs
46
src/dict.rs
|
@ -1,46 +0,0 @@
|
|||
use std;
|
||||
use ffi;
|
||||
use python::{Python, PythonObject, PythonObjectDowncast};
|
||||
use object::PyObject;
|
||||
use typeobject::PyType;
|
||||
use pyptr::PyPtr;
|
||||
use err::{self, PyResult};
|
||||
|
||||
pub struct PyDict<'p>(PyObject<'p>);
|
||||
|
||||
impl <'p> PythonObject<'p> for PyDict<'p> {
|
||||
#[inline]
|
||||
fn as_object<'a>(&'a self) -> &'a PyObject<'p> {
|
||||
&self.0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn unchecked_downcast_from<'a>(obj: &'a PyObject<'p>) -> &'a PyDict<'p> {
|
||||
std::mem::transmute(obj)
|
||||
}
|
||||
}
|
||||
|
||||
impl <'p> PythonObjectDowncast<'p> for PyDict<'p> {
|
||||
#[inline]
|
||||
fn downcast_from<'a>(obj : &'a PyObject<'p>) -> Option<&'a PyDict<'p>> {
|
||||
unsafe {
|
||||
if ffi::PyDict_Check(obj.as_ptr()) {
|
||||
Some(std::mem::transmute(obj))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn type_object(py: Python<'p>, _ : Option<&Self>) -> &'p PyType<'p> {
|
||||
unsafe { PyType::from_type_ptr(py, &mut ffi::PyDict_Type) }
|
||||
}
|
||||
}
|
||||
|
||||
impl <'p> PyDict<'p> {
|
||||
fn new(py: Python<'p>) -> PyResult<'p, PyPtr<'p, PyDict<'p>>> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
10
src/err.rs
10
src/err.rs
|
@ -1,5 +1,6 @@
|
|||
use std;
|
||||
use {PyObject, PythonObject, PyType, Python};
|
||||
use python::{PythonObject, Python};
|
||||
use objects::{PyObject, PyType};
|
||||
use pyptr::{PyPtr, PythonPointer};
|
||||
use ffi;
|
||||
use libc;
|
||||
|
@ -145,8 +146,8 @@ pub mod exception_types {
|
|||
macro_rules! exc_getter(
|
||||
($name:ident) => (
|
||||
#[inline]
|
||||
pub fn $name(py: ::python::Python) -> &::object::PyObject {
|
||||
unsafe { ::object::PyObject::from_ptr(py, ::ffi::$name) }
|
||||
pub fn $name(py: ::python::Python) -> &::objects::PyObject {
|
||||
unsafe { ::objects::PyObject::from_ptr(py, ::ffi::$name) }
|
||||
}
|
||||
)
|
||||
);
|
||||
|
@ -217,7 +218,8 @@ pub fn error_on_minusone(py : Python, result : libc::c_int) -> PyResult<()> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use {Python, PyType, PyErr};
|
||||
use {Python, PyErr};
|
||||
use objects::PyObject;
|
||||
|
||||
#[test]
|
||||
fn set_typeerror() {
|
||||
|
|
13
src/lib.rs
13
src/lib.rs
|
@ -9,27 +9,18 @@ extern crate libc;
|
|||
extern crate "python27-sys" as ffi;
|
||||
pub use ffi::Py_ssize_t;
|
||||
pub use err::{PyErr, PyResult};
|
||||
pub use objects::*;
|
||||
pub use err::exception_types::*;
|
||||
pub use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PythonObjectWithTypeObject};
|
||||
pub use object::PyObject;
|
||||
pub use typeobject::PyType;
|
||||
pub use pyptr::PyPtr;
|
||||
pub use module::PyModule;
|
||||
pub use conversion::{FromPyObject, ToPyObject};
|
||||
pub use objectprotocol::{ObjectProtocol};
|
||||
|
||||
// Fundamentals:
|
||||
mod python;
|
||||
mod pyptr;
|
||||
mod err;
|
||||
mod conversion;
|
||||
|
||||
// Object Types:
|
||||
mod object;
|
||||
mod typeobject;
|
||||
mod module;
|
||||
|
||||
// Python APIs:
|
||||
mod objects;
|
||||
mod objectprotocol;
|
||||
mod pythonrun;
|
||||
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
use std;
|
||||
use ffi;
|
||||
use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PythonObjectWithTypeObject};
|
||||
use object::PyObject;
|
||||
use typeobject::PyType;
|
||||
use pyptr::PyPtr;
|
||||
use err::{self, PyResult};
|
||||
|
||||
pub struct PyModule<'p>(PyObject<'p>);
|
||||
|
||||
impl <'p> PythonObject<'p> for PyModule<'p> {
|
||||
#[inline]
|
||||
fn as_object<'a>(&'a self) -> &'a PyObject<'p> {
|
||||
&self.0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn unchecked_downcast_from<'a>(obj: &'a PyObject<'p>) -> &'a PyModule<'p> {
|
||||
std::mem::transmute(obj)
|
||||
}
|
||||
}
|
||||
|
||||
impl <'p> PythonObjectWithCheckedDowncast<'p> for PyModule<'p> {
|
||||
#[inline]
|
||||
fn downcast_from<'a>(obj : &'a PyObject<'p>) -> Option<&'a PyModule<'p>> {
|
||||
unsafe {
|
||||
if ffi::PyModule_Check(obj.as_ptr()) {
|
||||
Some(PythonObject::unchecked_downcast_from(obj))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl <'p> PythonObjectWithTypeObject<'p> for PyModule<'p> {
|
||||
#[inline]
|
||||
fn type_object(py: Python<'p>, _ : Option<&Self>) -> &'p PyType<'p> {
|
||||
unsafe { PyType::from_type_ptr(py, &mut ffi::PyModule_Type) }
|
||||
}
|
||||
}
|
||||
|
||||
impl <'p> PyModule<'p> {
|
||||
pub fn import<N : std::c_str::ToCStr>(py : Python<'p>, name : N) -> PyResult<PyPtr<PyModule<'p>>> {
|
||||
let result = name.with_c_str(|name| unsafe {
|
||||
err::result_from_owned_ptr(py, ffi::PyImport_ImportModule(name))
|
||||
});
|
||||
try!(result).downcast_into()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn as_module<'p>(py : &'p Python, obj : PyPtr<PyObject>) -> PyResult<'p, PyPtr<'p, PyModule>> {
|
||||
if py.module_type().is_instance(obj.deref()) {
|
||||
Ok(unsafe { PyPtr::from_owned_ptr(py, obj.steal_ptr()) })
|
||||
} else {
|
||||
Err(PyErr::type_error(py, obj.deref(), py.module_type()))
|
||||
}
|
||||
}
|
||||
|
||||
impl PyModule {
|
||||
|
||||
pub fn add_object<Sized? S : ToCStr, Sized? T : ToPyObject>
|
||||
(&self, name : &S, value : &T) -> PyResult<()>
|
||||
{
|
||||
let value = try!(value.to_py_object(self.python())).steal_ptr();
|
||||
let rc = name.with_c_str(|name| unsafe {
|
||||
ffi::PyModule_AddObject(self.as_ptr(), name, value)
|
||||
});
|
||||
err::result_from_error_code(self.python(), rc)
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
|
@ -3,7 +3,7 @@ use std::cmp::Ordering;
|
|||
use ffi;
|
||||
use libc;
|
||||
use python::{Python, PythonObject, PythonObjectWithCheckedDowncast};
|
||||
use object::PyObject;
|
||||
use objects::PyObject;
|
||||
use pyptr::{PyPtr, PythonPointer, as_ptr};
|
||||
use conversion::ToPyObject;
|
||||
use err::{PyErr, PyResult, result_from_owned_ptr, error_on_minusone};
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
use std;
|
||||
use ffi;
|
||||
use python::{Python, PythonObject};
|
||||
use objects::PyObject;
|
||||
use pyptr::PyPtr;
|
||||
use err::{self, PyResult};
|
||||
|
||||
pyobject_newtype!(PyDict, PyDict_Check, PyDict_Type);
|
||||
|
||||
impl <'p> PyDict<'p> {
|
||||
fn new(py: Python<'p>) -> PyResult<'p, PyPtr<'p, PyDict<'p>>> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
pub use self::object::PyObject;
|
||||
pub use self::typeobject::PyType;
|
||||
pub use self::module::PyModule;
|
||||
|
||||
macro_rules! pythonobject_newtype_only_pythonobject(
|
||||
($name: ident) => (
|
||||
pub struct $name<'p>(::objects::PyObject<'p>);
|
||||
|
||||
impl <'p> ::python::PythonObject<'p> for $name<'p> {
|
||||
#[inline]
|
||||
fn as_object<'a>(&'a self) -> &'a ::objects::PyObject<'p> {
|
||||
&self.0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn unchecked_downcast_from<'a>(obj: &'a ::objects::PyObject<'p>) -> &'a $name<'p> {
|
||||
::std::mem::transmute(obj)
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
macro_rules! pyobject_newtype(
|
||||
($name: ident, $checkfunction: ident, $typeobject: ident) => (
|
||||
pythonobject_newtype_only_pythonobject!($name);
|
||||
|
||||
impl <'p> ::python::PythonObjectWithCheckedDowncast<'p> for $name<'p> {
|
||||
#[inline]
|
||||
fn downcast_from<'a>(obj : &'a ::objects::PyObject<'p>) -> Option<&'a $name<'p>> {
|
||||
unsafe {
|
||||
if ::ffi::$checkfunction(::python::PythonObject::as_ptr(obj)) {
|
||||
Some(::python::PythonObject::unchecked_downcast_from(obj))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl <'p> ::python::PythonObjectWithTypeObject<'p> for $name<'p> {
|
||||
#[inline]
|
||||
fn type_object(py: ::python::Python<'p>, _ : Option<&Self>) -> &'p ::objects::PyType<'p> {
|
||||
unsafe { ::objects::PyType::from_type_ptr(py, &mut ffi::$typeobject) }
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
mod object;
|
||||
mod typeobject;
|
||||
mod module;
|
||||
mod dict;
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
use std;
|
||||
use ffi;
|
||||
use python::Python;
|
||||
use objects::{PyObject, PyType};
|
||||
use pyptr::PyPtr;
|
||||
use err::{self, PyResult};
|
||||
|
||||
pyobject_newtype!(PyModule, PyModule_Check, PyModule_Type);
|
||||
|
||||
|
||||
impl <'p> PyModule<'p> {
|
||||
pub fn import<N : std::c_str::ToCStr>(py : Python<'p>, name : N) -> PyResult<PyPtr<PyModule<'p>>> {
|
||||
let result = name.with_c_str(|name| unsafe {
|
||||
err::result_from_owned_ptr(py, ffi::PyImport_ImportModule(name))
|
||||
});
|
||||
try!(result).downcast_into()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn as_module<'p>(py : &'p Python, obj : PyPtr<PyObject>) -> PyResult<'p, PyPtr<'p, PyModule>> {
|
||||
if py.module_type().is_instance(obj.deref()) {
|
||||
Ok(unsafe { PyPtr::from_owned_ptr(py, obj.steal_ptr()) })
|
||||
} else {
|
||||
Err(PyErr::type_error(py, obj.deref(), py.module_type()))
|
||||
}
|
||||
}
|
||||
|
||||
impl PyModule {
|
||||
|
||||
pub fn add_object<Sized? S : ToCStr, Sized? T : ToPyObject>
|
||||
(&self, name : &S, value : &T) -> PyResult<()>
|
||||
{
|
||||
let value = try!(value.to_py_object(self.python())).steal_ptr();
|
||||
let rc = name.with_c_str(|name| unsafe {
|
||||
ffi::PyModule_AddObject(self.as_ptr(), name, value)
|
||||
});
|
||||
err::result_from_error_code(self.python(), rc)
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
|
@ -2,7 +2,7 @@ use std;
|
|||
use libc;
|
||||
use ffi;
|
||||
use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PythonObjectWithTypeObject};
|
||||
use typeobject::PyType;
|
||||
use objects::PyType;
|
||||
use err::{PyErr, PyResult};
|
||||
|
||||
pub struct PyObject<'p> {
|
|
@ -1,5 +1,5 @@
|
|||
use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PythonObjectWithTypeObject};
|
||||
use object::PyObject;
|
||||
use objects::PyObject;
|
||||
use ffi;
|
||||
use libc::c_char;
|
||||
use std;
|
|
@ -2,7 +2,7 @@ use std;
|
|||
use std::ops::Deref;
|
||||
use ffi;
|
||||
use python::{Python, PythonObject, PythonObjectWithCheckedDowncast};
|
||||
use object::PyObject;
|
||||
use objects::PyObject;
|
||||
use err::{PyResult, PyErr};
|
||||
//use conversion::{FromPyObject, ToPyObject};
|
||||
//use PyResult;
|
||||
|
|
|
@ -2,8 +2,7 @@ use std;
|
|||
use std::kinds::marker::{NoSend, InvariantLifetime};
|
||||
use std::ptr;
|
||||
use ffi;
|
||||
use object::PyObject;
|
||||
use typeobject::PyType;
|
||||
use objects::{PyObject, PyType};
|
||||
use pythonrun::GILGuard;
|
||||
|
||||
/// The 'Python' struct is a zero-size marker struct that is required for most python operations.
|
||||
|
|
Loading…
Reference in New Issue