Run rustfmt after datetime changes

Most of the datetime related changes were made before pyo3 switched to
using rustfmt, so I ran rustfmt only on the final commit to make it
easier to rewrite history as necessary (for fixups and whatnot).
This commit is contained in:
Paul Ganssle 2018-08-09 16:29:59 -04:00
parent 7fc1dae238
commit ee658de1fb
No known key found for this signature in database
GPG Key ID: CD54FCE3D964BEFB
8 changed files with 304 additions and 267 deletions

View File

@ -1,11 +1,12 @@
use std::os::raw::{c_int, c_char, c_uchar};
use std::ffi::CString;
use std::option::Option;
use ffi2::pyport::Py_hash_t;
use ffi2::object::*;
use ffi2::pycapsule::PyCapsule_Import;
use ffi2::pyport::Py_hash_t;
use std::ffi::CString;
use std::option::Option;
use std::os::raw::{c_char, c_int, c_uchar};
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
pub static mut PyDateTime_DateType: PyTypeObject;
pub static mut PyDateTime_TimeType: PyTypeObject;
pub static mut PyDateTime_DateTimeType: PyTypeObject;
@ -14,7 +15,6 @@ use ffi2::pycapsule::PyCapsule_Import;
pub static mut PyDateTime_TZInfoType: PyTypeObject;
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PyDateTime_CAPI {
@ -25,12 +25,8 @@ pub struct PyDateTime_CAPI {
pub TZInfoType: *mut PyTypeObject,
pub Date_FromDate: Option<
unsafe extern "C" fn(
year: c_int,
month: c_int,
day: c_int,
cls: *mut PyTypeObject,
) -> *mut PyObject,
unsafe extern "C" fn(year: c_int, month: c_int, day: c_int, cls: *mut PyTypeObject)
-> *mut PyObject,
>,
pub DateTime_FromDateAndTime: Option<
unsafe extern "C" fn(
@ -68,15 +64,14 @@ pub struct PyDateTime_CAPI {
unsafe extern "C" fn(cls: *mut PyTypeObject, args: *mut PyObject, kwargs: *mut PyObject)
-> *mut PyObject,
>,
pub Date_FromTimestamp: Option<
unsafe extern "C" fn(cls: *mut PyTypeObject, args: *mut PyObject) -> *mut PyObject,
>,
pub Date_FromTimestamp:
Option<unsafe extern "C" fn(cls: *mut PyTypeObject, args: *mut PyObject) -> *mut PyObject>,
}
// Type struct wrappers
const _PyDateTime_DATE_DATASIZE : usize = 4;
const _PyDateTime_TIME_DATASIZE : usize = 6;
const _PyDateTime_DATE_DATASIZE: usize = 4;
const _PyDateTime_TIME_DATASIZE: usize = 6;
const _PyDateTime_DATETIME_DATASIZE: usize = 10;
#[repr(C)]
@ -95,7 +90,7 @@ pub struct PyDateTime_Time {
pub hashcode: Py_hash_t,
pub hastzinfo: c_char,
pub data: [c_uchar; _PyDateTime_TIME_DATASIZE],
pub tzinfo: *mut PyObject
pub tzinfo: *mut PyObject,
}
#[repr(C)]
@ -105,7 +100,7 @@ pub struct PyDateTime_DateTime {
pub hashcode: Py_hash_t,
pub hastzinfo: c_char,
pub data: [c_uchar; _PyDateTime_DATETIME_DATASIZE],
pub tzinfo: *mut PyObject
pub tzinfo: *mut PyObject,
}
#[repr(C)]
@ -118,7 +113,6 @@ pub struct PyDateTime_Delta {
pub microseconds: c_int,
}
// C API Capsule
unsafe impl Sync for PyDateTime_CAPI {}
@ -126,7 +120,6 @@ lazy_static! {
pub static ref PyDateTimeAPI: PyDateTime_CAPI = unsafe { PyDateTime_IMPORT() };
}
#[inline(always)]
pub unsafe fn PyDateTime_IMPORT() -> PyDateTime_CAPI {
// PyDateTime_CAPSULE_NAME is a macro in C
@ -136,7 +129,6 @@ pub unsafe fn PyDateTime_IMPORT() -> PyDateTime_CAPI {
*(capsule as *const PyDateTime_CAPI)
}
//
// Type Check macros
//
@ -196,7 +188,7 @@ pub unsafe fn PyTZInfo_CheckExact(op: *mut PyObject) -> c_int {
macro_rules! _access_field {
($obj:expr, $type: ident, $field:tt) => {
(*($obj as *mut $type)).$field
}
};
}
// Accessor functions for PyDateTime_Date
@ -207,7 +199,7 @@ macro_rules! PyDateTime_GET_YEAR {
// without making it a macro in Rust as well, or playing with pointers
($o: expr) => {
(((*$o).data[0] as c_int) << 8) | ((*$o).data[1] as c_int)
}
};
}
#[inline(always)]
@ -224,7 +216,7 @@ pub unsafe fn PyDateTime_DateTime_GET_YEAR(o: *mut PyObject) -> c_int {
macro_rules! PyDateTime_GET_MONTH {
($o: expr) => {
(*$o).data[2] as c_int
}
};
}
#[inline(always)]
@ -241,7 +233,7 @@ pub unsafe fn PyDateTime_DateTime_GET_MONTH(o: *mut PyObject) -> c_int {
macro_rules! PyDateTime_GET_DAY {
($o: expr) => {
(*$o).data[3] as c_int
}
};
}
#[inline(always)]
@ -258,33 +250,33 @@ pub unsafe fn PyDateTime_DateTime_GET_DAY(o: *mut PyObject) -> c_int {
macro_rules! _PyDateTime_GET_HOUR {
($o: expr, $offset:expr) => {
(*$o).data[$offset + 0] as c_int
}
};
}
macro_rules! _PyDateTime_GET_MINUTE {
($o: expr, $offset:expr) => {
(*$o).data[$offset + 1] as c_int
}
};
}
macro_rules! _PyDateTime_GET_SECOND {
($o: expr, $offset:expr) => {
(*$o).data[$offset + 2] as c_int
}
};
}
macro_rules! _PyDateTime_GET_MICROSECOND {
($o: expr, $offset:expr) => {
(((*$o).data[$offset + 3] as c_int) << 16) |
(((*$o).data[$offset + 4] as c_int) << 8) |
((*$o).data[$offset + 5] as c_int)
}
(((*$o).data[$offset + 3] as c_int) << 16)
| (((*$o).data[$offset + 4] as c_int) << 8)
| ((*$o).data[$offset + 5] as c_int)
};
}
macro_rules! _PyDateTime_GET_TZINFO{
macro_rules! _PyDateTime_GET_TZINFO {
($o: expr) => {
(*$o).tzinfo
}
};
}
// Accessor functions for DateTime
@ -339,12 +331,11 @@ pub unsafe fn PyDateTime_TIME_GET_TZINFO(o: *mut PyObject) -> *mut PyObject {
_PyDateTime_GET_TZINFO!(o as *mut PyDateTime_Time)
}
// Accessor functions for PyDateTime_Delta
macro_rules! _access_delta_field {
($obj:expr, $field:tt) => {
_access_field!($obj, PyDateTime_Delta, $field)
}
};
}
#[inline(always)]

View File

@ -1,11 +1,12 @@
use std::os::raw::{c_int, c_char, c_uchar};
use std::ffi::CString;
use std::option::Option;
use ffi3::pyport::Py_hash_t;
use ffi3::object::*;
use ffi3::pycapsule::PyCapsule_Import;
use ffi3::pyport::Py_hash_t;
use std::ffi::CString;
use std::option::Option;
use std::os::raw::{c_char, c_int, c_uchar};
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
pub static mut PyDateTime_DateType: PyTypeObject;
pub static mut PyDateTime_TimeType: PyTypeObject;
pub static mut PyDateTime_DateTimeType: PyTypeObject;
@ -14,7 +15,6 @@ use ffi3::pycapsule::PyCapsule_Import;
pub static mut PyDateTime_TZInfoType: PyTypeObject;
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PyDateTime_CAPI {
@ -27,12 +27,8 @@ pub struct PyDateTime_CAPI {
pub TimeZone_UTC: *mut PyObject,
pub Date_FromDate: Option<
unsafe extern "C" fn(
year: c_int,
month: c_int,
day: c_int,
cls: *mut PyTypeObject,
) -> *mut PyObject,
unsafe extern "C" fn(year: c_int, month: c_int, day: c_int, cls: *mut PyTypeObject)
-> *mut PyObject,
>,
pub DateTime_FromDateAndTime: Option<
unsafe extern "C" fn(
@ -67,16 +63,14 @@ pub struct PyDateTime_CAPI {
) -> *mut PyObject,
>,
#[cfg(Py_3_7)]
pub TimeZone_FromTimeZone: Option<
unsafe extern "C" fn(offset: *mut PyObject, name: *mut PyObject) -> *mut PyObject,
>,
pub TimeZone_FromTimeZone:
Option<unsafe extern "C" fn(offset: *mut PyObject, name: *mut PyObject) -> *mut PyObject>,
pub DateTime_FromTimestamp: Option<
unsafe extern "C" fn(cls: *mut PyTypeObject, args: *mut PyObject, kwargs: *mut PyObject)
-> *mut PyObject,
>,
pub Date_FromTimestamp: Option<
unsafe extern "C" fn(cls: *mut PyTypeObject, args: *mut PyObject) -> *mut PyObject,
>,
pub Date_FromTimestamp:
Option<unsafe extern "C" fn(cls: *mut PyTypeObject, args: *mut PyObject) -> *mut PyObject>,
#[cfg(Py_3_6)]
pub DateTime_FromDateAndTimeAndFold: Option<
unsafe extern "C" fn(
@ -93,7 +87,8 @@ pub struct PyDateTime_CAPI {
) -> *mut PyObject,
>,
#[cfg(Py_3_6)]
pub Time_FromTimeAndFold: Option< unsafe extern "C" fn(
pub Time_FromTimeAndFold: Option<
unsafe extern "C" fn(
hour: c_int,
minute: c_int,
second: c_int,
@ -107,8 +102,8 @@ pub struct PyDateTime_CAPI {
// Type struct wrappers
const _PyDateTime_DATE_DATASIZE : usize = 4;
const _PyDateTime_TIME_DATASIZE : usize = 6;
const _PyDateTime_DATE_DATASIZE: usize = 4;
const _PyDateTime_TIME_DATASIZE: usize = 6;
const _PyDateTime_DATETIME_DATASIZE: usize = 10;
#[repr(C)]
@ -129,7 +124,7 @@ pub struct PyDateTime_Time {
pub data: [c_uchar; _PyDateTime_TIME_DATASIZE],
#[cfg(Py_3_6)]
pub fold: c_uchar,
pub tzinfo: *mut PyObject
pub tzinfo: *mut PyObject,
}
#[repr(C)]
@ -141,7 +136,7 @@ pub struct PyDateTime_DateTime {
pub data: [c_uchar; _PyDateTime_DATETIME_DATASIZE],
#[cfg(Py_3_6)]
pub fold: c_uchar,
pub tzinfo: *mut PyObject
pub tzinfo: *mut PyObject,
}
#[repr(C)]
@ -154,7 +149,6 @@ pub struct PyDateTime_Delta {
pub microseconds: c_int,
}
// C API Capsule
unsafe impl Sync for PyDateTime_CAPI {}
@ -162,7 +156,6 @@ lazy_static! {
pub static ref PyDateTimeAPI: PyDateTime_CAPI = unsafe { PyDateTime_IMPORT() };
}
#[inline(always)]
pub unsafe fn PyDateTime_IMPORT() -> PyDateTime_CAPI {
// PyDateTime_CAPSULE_NAME is a macro in C
@ -172,7 +165,6 @@ pub unsafe fn PyDateTime_IMPORT() -> PyDateTime_CAPI {
*(capsule as *const PyDateTime_CAPI)
}
//
// Type Check macros
//
@ -232,7 +224,7 @@ pub unsafe fn PyTZInfo_CheckExact(op: *mut PyObject) -> c_int {
macro_rules! _access_field {
($obj:expr, $type: ident, $field:tt) => {
(*($obj as *mut $type)).$field
}
};
}
// Accessor functions for PyDateTime_Date
@ -243,7 +235,7 @@ macro_rules! PyDateTime_GET_YEAR {
// without making it a macro in Rust as well, or playing with pointers
($o: expr) => {
(((*$o).data[0] as c_int) << 8) | ((*$o).data[1] as c_int)
}
};
}
#[inline(always)]
@ -260,7 +252,7 @@ pub unsafe fn PyDateTime_DateTime_GET_YEAR(o: *mut PyObject) -> c_int {
macro_rules! PyDateTime_GET_MONTH {
($o: expr) => {
(*$o).data[2] as c_int
}
};
}
#[inline(always)]
@ -277,7 +269,7 @@ pub unsafe fn PyDateTime_DateTime_GET_MONTH(o: *mut PyObject) -> c_int {
macro_rules! PyDateTime_GET_DAY {
($o: expr) => {
(*$o).data[3] as c_int
}
};
}
#[inline(always)]
@ -294,40 +286,40 @@ pub unsafe fn PyDateTime_DateTime_GET_DAY(o: *mut PyObject) -> c_int {
macro_rules! _PyDateTime_GET_HOUR {
($o: expr, $offset:expr) => {
(*$o).data[$offset + 0] as c_int
}
};
}
macro_rules! _PyDateTime_GET_MINUTE {
($o: expr, $offset:expr) => {
(*$o).data[$offset + 1] as c_int
}
};
}
macro_rules! _PyDateTime_GET_SECOND {
($o: expr, $offset:expr) => {
(*$o).data[$offset + 2] as c_int
}
};
}
macro_rules! _PyDateTime_GET_MICROSECOND {
($o: expr, $offset:expr) => {
(((*$o).data[$offset + 3] as c_int) << 16) |
(((*$o).data[$offset + 4] as c_int) << 8) |
((*$o).data[$offset + 5] as c_int)
}
(((*$o).data[$offset + 3] as c_int) << 16)
| (((*$o).data[$offset + 4] as c_int) << 8)
| ((*$o).data[$offset + 5] as c_int)
};
}
#[cfg(Py_3_6)]
macro_rules! _PyDateTime_GET_FOLD{
macro_rules! _PyDateTime_GET_FOLD {
($o: expr) => {
(*$o).fold
}
};
}
macro_rules! _PyDateTime_GET_TZINFO{
macro_rules! _PyDateTime_GET_TZINFO {
($o: expr) => {
(*$o).tzinfo
}
};
}
// Accessor functions for DateTime
@ -394,12 +386,11 @@ pub unsafe fn PyDateTime_TIME_GET_TZINFO(o: *mut PyObject) -> *mut PyObject {
_PyDateTime_GET_TZINFO!(o as *mut PyDateTime_Time)
}
// Accessor functions for PyDateTime_Delta
macro_rules! _access_delta_field {
($obj:expr, $field:tt) => {
_access_field!($obj, PyDateTime_Delta, $field)
}
};
}
#[inline(always)]

View File

@ -118,8 +118,8 @@ mod weakrefobject; // TODO supports PEP-384 only; needs adjustment for Python 3.
mod codecs; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
mod pyerrors; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
mod pystate; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
mod datetime;
mod pystate; // TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
#[cfg(Py_LIMITED_API)]
mod pyarena {}

View File

@ -129,7 +129,8 @@ extern crate spin;
#[doc(hidden)]
pub extern crate mashup;
#[macro_use] extern crate lazy_static;
#[macro_use]
extern crate lazy_static;
#[cfg(not(Py_3))]
mod ffi2;

View File

@ -1,25 +1,33 @@
use err::PyResult;
use ffi::PyDateTimeAPI;
use ffi::{PyDateTime_Check, PyDateTime_DateTimeType};
use ffi::{
PyDateTime_DATE_GET_HOUR, PyDateTime_DATE_GET_MICROSECOND, PyDateTime_DATE_GET_MINUTE,
PyDateTime_DATE_GET_SECOND,
};
use ffi::{
PyDateTime_DELTA_GET_DAYS, PyDateTime_DELTA_GET_MICROSECONDS, PyDateTime_DELTA_GET_SECONDS,
};
use ffi::{
PyDateTime_DateTime_GET_DAY, PyDateTime_DateTime_GET_MONTH, PyDateTime_DateTime_GET_YEAR,
};
use ffi::{PyDateTime_DateType, PyDate_Check};
use ffi::{PyDateTime_Date_GET_DAY, PyDateTime_Date_GET_MONTH, PyDateTime_Date_GET_YEAR};
use ffi::{PyDateTime_DeltaType, PyDelta_Check};
use ffi::{
PyDateTime_TIME_GET_HOUR, PyDateTime_TIME_GET_MICROSECOND, PyDateTime_TIME_GET_MINUTE,
PyDateTime_TIME_GET_SECOND,
};
use ffi::{PyDateTime_TZInfoType, PyTZInfo_Check};
use ffi::{PyDateTime_TimeType, PyTime_Check};
use object::PyObject;
use std::os::raw::c_int;
use ffi::{PyDateTimeAPI};
use ffi::{PyDateTime_DateType, PyDate_Check};
use ffi::{PyDateTime_Date_GET_YEAR, PyDateTime_Date_GET_MONTH, PyDateTime_Date_GET_DAY};
use ffi::{PyDateTime_DateTimeType, PyDateTime_Check};
use ffi::{PyDateTime_DateTime_GET_YEAR, PyDateTime_DateTime_GET_MONTH, PyDateTime_DateTime_GET_DAY};
use ffi::{PyDateTime_TIME_GET_HOUR, PyDateTime_TIME_GET_MINUTE,
PyDateTime_TIME_GET_SECOND, PyDateTime_TIME_GET_MICROSECOND};
use ffi::{PyDateTime_DATE_GET_HOUR, PyDateTime_DATE_GET_MINUTE,
PyDateTime_DATE_GET_SECOND, PyDateTime_DATE_GET_MICROSECOND};
use ffi::{PyDateTime_DeltaType, PyDelta_Check};
use ffi::{PyDateTime_DELTA_GET_DAYS, PyDateTime_DELTA_GET_SECONDS, PyDateTime_DELTA_GET_MICROSECONDS};
use ffi::{PyDateTime_TimeType, PyTime_Check};
use ffi::{PyDateTime_TZInfoType, PyTZInfo_Check};
#[cfg(Py_3_6)]
use ffi::{PyDateTime_DATE_GET_FOLD, PyDateTime_TIME_GET_FOLD};
use python::{Python, ToPyPointer};
use instance::Py;
use python::{Python, ToPyPointer};
// Traits
pub trait PyDateComponentAccess {
@ -34,7 +42,6 @@ pub trait PyDeltaComponentAccess {
fn get_microseconds(&self) -> i32;
}
pub trait PyTimeComponentAccess {
fn get_hour(&self) -> u32;
fn get_minute(&self) -> u32;
@ -44,7 +51,6 @@ pub trait PyTimeComponentAccess {
fn get_fold(&self) -> u8;
}
// datetime.date bindings
pub struct PyDate(PyObject);
pyobject_native_type!(PyDate, PyDateTime_DateType, PyDate_Check);
@ -63,45 +69,43 @@ impl PyDate {
pub fn from_timestamp(py: Python, args: &PyObject) -> PyResult<Py<PyDate>> {
unsafe {
let ptr = PyDateTimeAPI.Date_FromTimestamp.unwrap()
(PyDateTimeAPI.DateType, args.as_ptr());
let ptr =
PyDateTimeAPI.Date_FromTimestamp.unwrap()(PyDateTimeAPI.DateType, args.as_ptr());
Py::from_owned_ptr_or_err(py, ptr)
}
}
}
impl PyDateComponentAccess for PyDate {
fn get_year(&self) -> u32 {
unsafe {
PyDateTime_Date_GET_YEAR(self.as_ptr()) as u32
}
unsafe { PyDateTime_Date_GET_YEAR(self.as_ptr()) as u32 }
}
fn get_month(&self) -> u32 {
unsafe {
PyDateTime_Date_GET_MONTH(self.as_ptr()) as u32
}
unsafe { PyDateTime_Date_GET_MONTH(self.as_ptr()) as u32 }
}
fn get_day(&self) -> u32 {
unsafe {
PyDateTime_Date_GET_DAY(self.as_ptr()) as u32
}
unsafe { PyDateTime_Date_GET_DAY(self.as_ptr()) as u32 }
}
}
// datetime.datetime bindings
pub struct PyDateTime(PyObject);
pyobject_native_type!(PyDateTime, PyDateTime_DateTimeType, PyDateTime_Check);
impl PyDateTime {
pub fn new(py: Python, year: u32, month: u32, day: u32,
hour: u32, minute: u32, second: u32, microsecond: u32,
tzinfo: &PyObject) -> PyResult<Py<PyDateTime>> {
pub fn new(
py: Python,
year: u32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32,
microsecond: u32,
tzinfo: &PyObject,
) -> PyResult<Py<PyDateTime>> {
let y = year as c_int;
let mo = month as c_int;
let d = day as c_int;
@ -112,86 +116,86 @@ impl PyDateTime {
unsafe {
let ptr = PyDateTimeAPI.DateTime_FromDateAndTime.unwrap()(
y, mo, d, h, mi, s, u, tzinfo.as_ptr(),
PyDateTimeAPI.DateTimeType
y,
mo,
d,
h,
mi,
s,
u,
tzinfo.as_ptr(),
PyDateTimeAPI.DateTimeType,
);
Py::from_owned_ptr_or_err(py, ptr)
}
}
pub fn from_timestamp(py: Python, args: &PyObject, kwargs: &PyObject) ->
PyResult<Py<PyDateTime>> {
pub fn from_timestamp(
py: Python,
args: &PyObject,
kwargs: &PyObject,
) -> PyResult<Py<PyDateTime>> {
unsafe {
let ptr = PyDateTimeAPI.DateTime_FromTimestamp.unwrap()
(PyDateTimeAPI.DateTimeType, args.as_ptr(), kwargs.as_ptr());
let ptr = PyDateTimeAPI.DateTime_FromTimestamp.unwrap()(
PyDateTimeAPI.DateTimeType,
args.as_ptr(),
kwargs.as_ptr(),
);
Py::from_owned_ptr_or_err(py, ptr)
}
}
}
impl PyDateComponentAccess for PyDateTime {
fn get_year(&self) -> u32 {
unsafe {
PyDateTime_DateTime_GET_YEAR(self.as_ptr()) as u32
}
unsafe { PyDateTime_DateTime_GET_YEAR(self.as_ptr()) as u32 }
}
fn get_month(&self) -> u32 {
unsafe {
PyDateTime_DateTime_GET_MONTH(self.as_ptr()) as u32
}
unsafe { PyDateTime_DateTime_GET_MONTH(self.as_ptr()) as u32 }
}
fn get_day(&self) -> u32 {
unsafe {
PyDateTime_DateTime_GET_DAY(self.as_ptr()) as u32
}
unsafe { PyDateTime_DateTime_GET_DAY(self.as_ptr()) as u32 }
}
}
impl PyTimeComponentAccess for PyDateTime {
fn get_hour(&self) -> u32 {
unsafe {
PyDateTime_DATE_GET_HOUR(self.as_ptr()) as u32
}
unsafe { PyDateTime_DATE_GET_HOUR(self.as_ptr()) as u32 }
}
fn get_minute(&self) -> u32 {
unsafe {
PyDateTime_DATE_GET_MINUTE(self.as_ptr()) as u32
}
unsafe { PyDateTime_DATE_GET_MINUTE(self.as_ptr()) as u32 }
}
fn get_second(&self) -> u32 {
unsafe {
PyDateTime_DATE_GET_SECOND(self.as_ptr()) as u32
}
unsafe { PyDateTime_DATE_GET_SECOND(self.as_ptr()) as u32 }
}
fn get_microsecond(&self) -> u32 {
unsafe {
PyDateTime_DATE_GET_MICROSECOND(self.as_ptr()) as u32
}
unsafe { PyDateTime_DATE_GET_MICROSECOND(self.as_ptr()) as u32 }
}
#[cfg(Py_3_6)]
fn get_fold(&self) -> u8 {
unsafe {
PyDateTime_DATE_GET_FOLD(self.as_ptr()) as u8
}
unsafe { PyDateTime_DATE_GET_FOLD(self.as_ptr()) as u8 }
}
}
// datetime.time
pub struct PyTime(PyObject);
pyobject_native_type!(PyTime, PyDateTime_TimeType, PyTime_Check);
impl PyTime {
pub fn new(py: Python, hour: u32, minute: u32, second: u32,
microsecond: u32, tzinfo: &PyObject) -> PyResult<Py<PyTime>> {
pub fn new(
py: Python,
hour: u32,
minute: u32,
second: u32,
microsecond: u32,
tzinfo: &PyObject,
) -> PyResult<Py<PyTime>> {
let h = hour as c_int;
let m = minute as c_int;
let s = second as c_int;
@ -199,17 +203,21 @@ impl PyTime {
let tzi = tzinfo.as_ptr();
unsafe {
let ptr = PyDateTimeAPI.Time_FromTime.unwrap()(
h, m, s, u, tzi, PyDateTimeAPI.TimeType
);
let ptr = PyDateTimeAPI.Time_FromTime.unwrap()(h, m, s, u, tzi, PyDateTimeAPI.TimeType);
Py::from_owned_ptr_or_err(py, ptr)
}
}
#[cfg(Py_3_6)]
pub fn new_with_fold(py: Python, hour: u32, minute: u32, second: u32,
microsecond: u32, tzinfo: &PyObject,
fold: bool) -> PyResult<Py<PyTime>> {
pub fn new_with_fold(
py: Python,
hour: u32,
minute: u32,
second: u32,
microsecond: u32,
tzinfo: &PyObject,
fold: bool,
) -> PyResult<Py<PyTime>> {
let h = hour as c_int;
let m = minute as c_int;
let s = second as c_int;
@ -217,70 +225,66 @@ impl PyTime {
let f = fold as c_int;
unsafe {
let ptr = PyDateTimeAPI.Time_FromTimeAndFold.unwrap()
(h, m, s, u, tzinfo.as_ptr(), f, PyDateTimeAPI.TimeType);
let ptr = PyDateTimeAPI.Time_FromTimeAndFold.unwrap()(
h,
m,
s,
u,
tzinfo.as_ptr(),
f,
PyDateTimeAPI.TimeType,
);
Py::from_owned_ptr_or_err(py, ptr)
}
}
}
impl PyTimeComponentAccess for PyTime {
fn get_hour(&self) -> u32 {
unsafe {
PyDateTime_TIME_GET_HOUR(self.as_ptr()) as u32
}
unsafe { PyDateTime_TIME_GET_HOUR(self.as_ptr()) as u32 }
}
fn get_minute(&self) -> u32 {
unsafe {
PyDateTime_TIME_GET_MINUTE(self.as_ptr()) as u32
}
unsafe { PyDateTime_TIME_GET_MINUTE(self.as_ptr()) as u32 }
}
fn get_second(&self) -> u32 {
unsafe {
PyDateTime_TIME_GET_SECOND(self.as_ptr()) as u32
}
unsafe { PyDateTime_TIME_GET_SECOND(self.as_ptr()) as u32 }
}
fn get_microsecond(&self) -> u32 {
unsafe {
PyDateTime_TIME_GET_MICROSECOND(self.as_ptr()) as u32
}
unsafe { PyDateTime_TIME_GET_MICROSECOND(self.as_ptr()) as u32 }
}
#[cfg(Py_3_6)]
fn get_fold(&self) -> u8 {
unsafe {
PyDateTime_TIME_GET_FOLD(self.as_ptr()) as u8
}
unsafe { PyDateTime_TIME_GET_FOLD(self.as_ptr()) as u8 }
}
}
// datetime.tzinfo bindings
pub struct PyTzInfo(PyObject);
pyobject_native_type!(PyTzInfo, PyDateTime_TZInfoType, PyTZInfo_Check);
// datetime.timedelta bindings
pub struct PyDelta(PyObject);
pyobject_native_type!(PyDelta, PyDateTime_DeltaType, PyDelta_Check);
impl PyDelta {
pub fn new(py: Python, days: i32, seconds: i32, microseconds: i32,
normalize: bool) -> PyResult<Py<PyDelta>> {
pub fn new(
py: Python,
days: i32,
seconds: i32,
microseconds: i32,
normalize: bool,
) -> PyResult<Py<PyDelta>> {
let d = days as c_int;
let s = seconds as c_int;
let u = microseconds as c_int;
let n = normalize as c_int;
unsafe {
let ptr = PyDateTimeAPI.Delta_FromDelta.unwrap()(
d, s, u, n, PyDateTimeAPI.DeltaType
);
let ptr = PyDateTimeAPI.Delta_FromDelta.unwrap()(d, s, u, n, PyDateTimeAPI.DeltaType);
Py::from_owned_ptr_or_err(py, ptr)
}
}
@ -288,20 +292,14 @@ impl PyDelta {
impl PyDeltaComponentAccess for PyDelta {
fn get_days(&self) -> i32 {
unsafe {
PyDateTime_DELTA_GET_DAYS(self.as_ptr()) as i32
}
unsafe { PyDateTime_DELTA_GET_DAYS(self.as_ptr()) as i32 }
}
fn get_seconds(&self) -> i32 {
unsafe {
PyDateTime_DELTA_GET_SECONDS(self.as_ptr()) as i32
}
unsafe { PyDateTime_DELTA_GET_SECONDS(self.as_ptr()) as i32 }
}
fn get_microseconds(&self) -> i32 {
unsafe {
PyDateTime_DELTA_GET_MICROSECONDS(self.as_ptr()) as i32
}
unsafe { PyDateTime_DELTA_GET_MICROSECONDS(self.as_ptr()) as i32 }
}
}

View File

@ -5,9 +5,9 @@ mod exc_impl;
pub use self::boolobject::PyBool;
pub use self::bytearray::PyByteArray;
pub use self::datetime::{PyDate, PyTime, PyDateTime, PyDelta, PyTzInfo};
pub use self::datetime::PyDeltaComponentAccess;
pub use self::datetime::{PyDate, PyDateTime, PyDelta, PyTime, PyTzInfo};
pub use self::datetime::{PyDateComponentAccess, PyTimeComponentAccess};
pub use self::datetime::{PyDeltaComponentAccess};
pub use self::dict::PyDict;
pub use self::floatob::PyFloat;
pub use self::iterator::PyIterator;

View File

@ -3,25 +3,25 @@
#[macro_use]
extern crate pyo3;
use pyo3::{Py, Python, PyResult};
use pyo3::{ObjectProtocol, ToPyObject};
use pyo3::prelude::PyDeltaComponentAccess;
use pyo3::prelude::PyModule;
use pyo3::prelude::PyObject;
use pyo3::prelude::{pyfunction, pymodinit};
use pyo3::prelude::{PyObject};
use pyo3::prelude::{PyModule};
use pyo3::prelude::{PyDate, PyTime, PyDateTime, PyDelta, PyTzInfo};
use pyo3::prelude::{PyDate, PyDateTime, PyDelta, PyTime, PyTzInfo};
use pyo3::prelude::{PyDateComponentAccess, PyTimeComponentAccess};
use pyo3::prelude::{PyDeltaComponentAccess};
use pyo3::prelude::{PyTuple, PyDict};
use pyo3::prelude::{PyDict, PyTuple};
use pyo3::{ObjectProtocol, ToPyObject};
use pyo3::{Py, PyResult, Python};
macro_rules! to_pyobject {
($py:expr, $o:ident) => (match $o {
Some(t) => t.to_object($py),
None => $py.None()
})
($py:expr, $o:ident) => {
match $o {
Some(t) => t.to_object($py),
None => $py.None(),
}
};
}
#[pyfunction]
fn make_date(py: Python, year: u32, month: u32, day: u32) -> PyResult<Py<PyDate>> {
PyDate::new(py, year, month, day)
@ -40,32 +40,59 @@ fn date_from_timestamp(py: Python, ts: i64) -> PyResult<Py<PyDate>> {
}
#[pyfunction]
fn make_time(py: Python, hour: u32, minute: u32, second: u32,
microsecond: u32, tzinfo: Option<&PyTzInfo>) -> PyResult<Py<PyTime>> {
fn make_time(
py: Python,
hour: u32,
minute: u32,
second: u32,
microsecond: u32,
tzinfo: Option<&PyTzInfo>,
) -> PyResult<Py<PyTime>> {
let tzi: PyObject = to_pyobject!(py, tzinfo);
PyTime::new(py, hour, minute, second, microsecond, &tzi)
}
#[cfg(Py_3_6)]
#[pyfunction]
fn time_with_fold(py: Python, hour: u32, minute: u32, second: u32,
microsecond: u32, tzinfo: Option<&PyTzInfo>,
fold: bool) -> PyResult<Py<PyTime>> {
fn time_with_fold(
py: Python,
hour: u32,
minute: u32,
second: u32,
microsecond: u32,
tzinfo: Option<&PyTzInfo>,
fold: bool,
) -> PyResult<Py<PyTime>> {
let tzi = to_pyobject!(py, tzinfo);
PyTime::new_with_fold(py, hour, minute, second, microsecond, &tzi, fold)
}
#[pyfunction]
fn get_time_tuple(py: Python, dt: &PyTime) -> Py<PyTuple> {
PyTuple::new(py, &[dt.get_hour(), dt.get_minute(), dt.get_second(),
dt.get_microsecond()])
PyTuple::new(
py,
&[
dt.get_hour(),
dt.get_minute(),
dt.get_second(),
dt.get_microsecond(),
],
)
}
#[cfg(Py_3_6)]
#[pyfunction]
fn get_time_tuple_fold(py: Python, dt: &PyTime) -> Py<PyTuple> {
PyTuple::new(py, &[dt.get_hour(), dt.get_minute(), dt.get_second(),
dt.get_microsecond(), dt.get_fold() as u32])
PyTuple::new(
py,
&[
dt.get_hour(),
dt.get_minute(),
dt.get_second(),
dt.get_microsecond(),
dt.get_fold() as u32,
],
)
}
#[pyfunction]
@ -75,41 +102,85 @@ fn make_delta(py: Python, days: i32, seconds: i32, microseconds: i32) -> PyResul
#[pyfunction]
fn get_delta_tuple(py: Python, delta: &PyDelta) -> Py<PyTuple> {
PyTuple::new(py, &[delta.get_days(), delta.get_seconds(), delta.get_microseconds()])
PyTuple::new(
py,
&[
delta.get_days(),
delta.get_seconds(),
delta.get_microseconds(),
],
)
}
#[pyfunction]
fn make_datetime(py: Python, year: u32, month: u32, day: u32,
hour: u32, minute: u32, second: u32, microsecond: u32,
tzinfo: Option<&PyTzInfo>) -> PyResult<Py<PyDateTime>> {
let tzi : PyObject = match tzinfo {
fn make_datetime(
py: Python,
year: u32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32,
microsecond: u32,
tzinfo: Option<&PyTzInfo>,
) -> PyResult<Py<PyDateTime>> {
let tzi: PyObject = match tzinfo {
Some(t) => t.to_object(py),
None => py.None(),
};
PyDateTime::new(py, year, month, day, hour, minute, second, microsecond, &tzi)
PyDateTime::new(
py,
year,
month,
day,
hour,
minute,
second,
microsecond,
&tzi,
)
}
#[pyfunction]
fn get_datetime_tuple(py: Python, dt: &PyDateTime) -> Py<PyTuple> {
PyTuple::new(py, &[dt.get_year(), dt.get_month(), dt.get_day(),
dt.get_hour(), dt.get_minute(), dt.get_second(),
dt.get_microsecond()])
PyTuple::new(
py,
&[
dt.get_year(),
dt.get_month(),
dt.get_day(),
dt.get_hour(),
dt.get_minute(),
dt.get_second(),
dt.get_microsecond(),
],
)
}
#[cfg(Py_3_6)]
#[pyfunction]
fn get_datetime_tuple_fold(py: Python, dt: &PyDateTime) -> Py<PyTuple> {
PyTuple::new(py, &[dt.get_year(), dt.get_month(), dt.get_day(),
dt.get_hour(), dt.get_minute(), dt.get_second(),
dt.get_microsecond(), dt.get_fold() as u32])
PyTuple::new(
py,
&[
dt.get_year(),
dt.get_month(),
dt.get_day(),
dt.get_hour(),
dt.get_minute(),
dt.get_second(),
dt.get_microsecond(),
dt.get_fold() as u32,
],
)
}
#[pyfunction]
fn datetime_from_timestamp(py: Python, ts: f64, tz: Option<&PyTzInfo>) -> PyResult<Py<PyDateTime>> {
let timestamp : PyObject = ts.to_object(py);
let tzi : PyObject = match tz {
let timestamp: PyObject = ts.to_object(py);
let tzi: PyObject = match tz {
Some(t) => t.to_object(py),
None => py.None()
None => py.None(),
};
let args = PyTuple::new(py, &[timestamp, tzi]);
@ -118,10 +189,6 @@ fn datetime_from_timestamp(py: Python, ts: f64, tz: Option<&PyTzInfo>) -> PyResu
PyDateTime::from_timestamp(py, &args.to_object(py), &kwargs.to_object(py))
}
#[pymodinit]
fn datetime(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_function!(make_date))?;

View File

@ -6,42 +6,40 @@ use pyo3::prelude::*;
use pyo3::ffi::*;
fn _get_subclasses<'p>(py: &'p Python, py_type: &str, args: &str) ->
(&'p PyObjectRef, &'p PyObjectRef, &'p PyObjectRef) {
fn _get_subclasses<'p>(
py: &'p Python,
py_type: &str,
args: &str,
) -> (&'p PyObjectRef, &'p PyObjectRef, &'p PyObjectRef) {
macro_rules! unwrap_py {
($e:expr) => { ($e).map_err(|e| e.print(*py)).unwrap() }
($e:expr) => {
($e).map_err(|e| e.print(*py)).unwrap()
};
};
// Import the class from Python and create some subclasses
let datetime = unwrap_py!(py.import("datetime"));
let locals = PyDict::new(*py);
locals.set_item(py_type, datetime.get(py_type).unwrap())
locals
.set_item(py_type, datetime.get(py_type).unwrap())
.unwrap();
let make_subclass_py =
format!("class Subklass({}):\n pass", py_type);
let make_subclass_py = format!("class Subklass({}):\n pass", py_type);
let make_sub_subclass_py =
"class SubSubklass(Subklass):\n pass";
let make_sub_subclass_py = "class SubSubklass(Subklass):\n pass";
unwrap_py!(py.run(&make_subclass_py, None, Some(&locals)));
unwrap_py!(py.run(&make_sub_subclass_py, None, Some(&locals)));
// Construct an instance of the base class
let obj = unwrap_py!(
py.eval(&format!("{}({})", py_type, args), None, Some(&locals))
);
let obj = unwrap_py!(py.eval(&format!("{}({})", py_type, args), None, Some(&locals)));
// Construct an instance of the subclass
let sub_obj = unwrap_py!(
py.eval(&format!("Subklass({})", args), None, Some(&locals))
);
let sub_obj = unwrap_py!(py.eval(&format!("Subklass({})", args), None, Some(&locals)));
// Construct an instance of the sub-subclass
let sub_sub_obj = unwrap_py!(
py.eval(&format!("SubSubklass({})", args), None, Some(&locals))
);
let sub_sub_obj = unwrap_py!(py.eval(&format!("SubSubklass({})", args), None, Some(&locals)));
(obj, sub_obj, sub_sub_obj)
}
@ -52,7 +50,7 @@ macro_rules! assert_check_exact {
assert!($check_func(($obj).as_ptr()) != 0);
assert!(concat_idents!($check_func, Exact)(($obj).as_ptr()) != 0);
}
}
};
}
macro_rules! assert_check_only {
@ -61,17 +59,14 @@ macro_rules! assert_check_only {
assert!($check_func(($obj).as_ptr()) != 0);
assert!(concat_idents!($check_func, Exact)(($obj).as_ptr()) == 0);
}
}
};
}
#[test]
fn test_date_check() {
let gil = Python::acquire_gil();
let py = gil.python();
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py,
"date", "2018, 1, 1"
);
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "date", "2018, 1, 1");
assert_check_exact!(PyDate_Check, obj);
assert_check_only!(PyDate_Check, sub_obj);
@ -82,9 +77,7 @@ fn test_date_check() {
fn test_time_check() {
let gil = Python::acquire_gil();
let py = gil.python();
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py,
"time", "12, 30, 15"
);
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "time", "12, 30, 15");
assert_check_exact!(PyTime_Check, obj);
assert_check_only!(PyTime_Check, sub_obj);
@ -95,9 +88,7 @@ fn test_time_check() {
fn test_datetime_check() {
let gil = Python::acquire_gil();
let py = gil.python();
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py,
"datetime", "2018, 1, 1, 13, 30, 15"
);
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "datetime", "2018, 1, 1, 13, 30, 15");
assert_check_only!(PyDate_Check, obj);
assert_check_exact!(PyDateTime_Check, obj);
@ -109,9 +100,7 @@ fn test_datetime_check() {
fn test_delta_check() {
let gil = Python::acquire_gil();
let py = gil.python();
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py,
"timedelta", "1, -3"
);
let (obj, sub_obj, sub_sub_obj) = _get_subclasses(&py, "timedelta", "1, -3");
assert_check_exact!(PyDelta_Check, obj);
assert_check_only!(PyDelta_Check, sub_obj);