diff --git a/src/conversion.rs b/src/conversion.rs index c4c76bd1..a4ae6426 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -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}; diff --git a/src/dict.rs b/src/dict.rs deleted file mode 100644 index c3865613..00000000 --- a/src/dict.rs +++ /dev/null @@ -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!() - } -} - diff --git a/src/err.rs b/src/err.rs index 87e0e88c..60fe0a4d 100644 --- a/src/err.rs +++ b/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() { diff --git a/src/lib.rs b/src/lib.rs index 3142a927..2cd17d95 100644 --- a/src/lib.rs +++ b/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; diff --git a/src/module.rs b/src/module.rs deleted file mode 100644 index 432e4e9c..00000000 --- a/src/module.rs +++ /dev/null @@ -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(py : Python<'p>, name : N) -> PyResult>> { - 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) -> 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 - (&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) - } - -} -*/ - diff --git a/src/objectprotocol.rs b/src/objectprotocol.rs index bbcbb872..2ac61c9b 100644 --- a/src/objectprotocol.rs +++ b/src/objectprotocol.rs @@ -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}; diff --git a/src/objects/dict.rs b/src/objects/dict.rs new file mode 100644 index 00000000..080bd062 --- /dev/null +++ b/src/objects/dict.rs @@ -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!() + } +} + diff --git a/src/objects/mod.rs b/src/objects/mod.rs new file mode 100644 index 00000000..10f475be --- /dev/null +++ b/src/objects/mod.rs @@ -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; + diff --git a/src/objects/module.rs b/src/objects/module.rs new file mode 100644 index 00000000..41ce4a07 --- /dev/null +++ b/src/objects/module.rs @@ -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(py : Python<'p>, name : N) -> PyResult>> { + 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) -> 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 + (&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) + } + +} +*/ + diff --git a/src/object.rs b/src/objects/object.rs similarity index 99% rename from src/object.rs rename to src/objects/object.rs index e2f45de1..edf3b201 100644 --- a/src/object.rs +++ b/src/objects/object.rs @@ -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> { diff --git a/src/typeobject.rs b/src/objects/typeobject.rs similarity index 99% rename from src/typeobject.rs rename to src/objects/typeobject.rs index 589f4ebc..1f5303a1 100644 --- a/src/typeobject.rs +++ b/src/objects/typeobject.rs @@ -1,5 +1,5 @@ use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PythonObjectWithTypeObject}; -use object::PyObject; +use objects::PyObject; use ffi; use libc::c_char; use std; diff --git a/src/pyptr.rs b/src/pyptr.rs index 8121c0a7..cb5dfae0 100644 --- a/src/pyptr.rs +++ b/src/pyptr.rs @@ -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; diff --git a/src/python.rs b/src/python.rs index f33018c0..3ae2556b 100644 --- a/src/python.rs +++ b/src/python.rs @@ -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.