-
Notifications
You must be signed in to change notification settings - Fork 952
Initial datetime bindings #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
2b74cdb
Add initial bindings for datetime.h
pganssle 496b879
Add objects/datetime.rs
pganssle 52a64a9
Add rustapi_module test
pganssle d7b90c1
Add datetime.datetime and switch to PyResult<Py<T>>
pganssle f453629
Add datetime.time
pganssle 8d781cb
Add datetime.timedelta
pganssle ee85940
Add new requirements-dev.txt file
pganssle e8d6a4b
Version qualify PyDateTimeAPI
pganssle 7256bc8
Add PyDate::from_timestamp
pganssle c49bbe4
Add PyDateTime::from_timestamp
pganssle f5e0785
Add time_with_fold
pganssle f47697e
Refactor pyobject_* macros
pganssle d67d2c4
Update rustapi_module to new macro methods
pganssle cca8eb4
Add gitignore to rustapi_module
pganssle 149a13c
Fix how the test rustapi_module is built
pganssle 08e7e0f
Add PyDelta component accessors
pganssle 0b39452
Add accessors to PyDateTime_Date
pganssle ecf3aaa
Add PyDateTime component accessors
pganssle f701bcc
Add PyTime component accessors
pganssle cd6f729
Add type checking FFI bindings
pganssle 996c2ba
Add preliminary Python 2 support to datetime bindings
pganssle 7fc1dae
Add rustapi_module tests to CI
pganssle ee658de
Run rustfmt after datetime changes
pganssle 12c20a7
Move datetime interface to exclusively use functions
pganssle 94bd0d7
Relax constraints on PyDateTimeAPI initialization
pganssle bcc4231
Inline parameter typecasts
pganssle 09ee50d
Add tests for datetime overflow behavior
pganssle bcc27bd
Change datetime constructors to use Rust Option
pganssle f68d093
Unwrap PyDateTime_CAPI function fields
pganssle 003351b
Get rid of unwrap_py! macro
pganssle 3357fab
Remove 'Component' from ComponentAccess Traits
pganssle a271ba9
Use unified implementation for ffi::datetime
pganssle cf3b1d2
Remove leading __ from private cache objects
pganssle 26c5397
Implement Debug for PyObject in Python 2
pganssle b66ab7f
Move cache population logic into PyDateTime_IMPORT
pganssle 078bea4
Move rustapi_module into examples
pganssle 113de1b
Drop setup_requires from rustapi_module
pganssle 5d5689f
Switch Py{Date}{Time} constructor parameters to i32
pganssle a05a78f
Use smallest types for Py{Date}{Time} constructors
pganssle 7053c89
Get rid of #[inline(always)] and replace it with #[inline]
konstin a09ffad
Add convnience function to unwrap Option<&PyObject>
pganssle c7a967c
Silence type complexity warnings in ffi
pganssle c69634e
Remove use_extern_macros, which is now stable
pganssle 265a680
Enable extension-module
pganssle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| .pytest_cache | ||
| .mypy_cache | ||
| .hypothesis | ||
| .tox | ||
|
|
||
| *.py[co] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [package] | ||
| authors = ["PyO3 Authors"] | ||
| name = "rustapi-module" | ||
| version = "0.1.0" | ||
| description = "A Python wrapper for the Rust API for purposes of testing" | ||
|
|
||
| [dependencies] | ||
|
|
||
| [dependencies.pyo3] | ||
| path = "../../" | ||
| features = ["extension-module"] | ||
|
|
||
| [lib] | ||
| name = "rustapi_module" | ||
| crate-type = ["cdylib"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [build-system] | ||
| requires = ["setuptools", "wheel", "setuptools_rust>=0.10.2"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| hypothesis>=3.55 | ||
| pytest>=3.5.0 | ||
| setuptools-rust>=0.10.2 |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import sys | ||
|
|
||
| from setuptools import setup | ||
| from setuptools.command.test import test as TestCommand | ||
| from setuptools_rust import RustExtension | ||
|
|
||
|
|
||
| class PyTest(TestCommand): | ||
| user_options = [] | ||
|
|
||
| def run(self): | ||
| self.run_command("test_rust") | ||
|
|
||
| import subprocess | ||
| errno = subprocess.call(['pytest', 'tests']) | ||
| raise SystemExit(errno) | ||
|
|
||
|
|
||
| def get_py_version_cfgs(): | ||
| # For now each Cfg Py_3_X flag is interpreted as "at least 3.X" | ||
| version = sys.version_info[0:2] | ||
|
|
||
| if version[0] == 2: | ||
| return ['--cfg=Py_2'] | ||
|
|
||
| py3_min = 5 | ||
| out_cfg = [] | ||
| for minor in range(py3_min, version[1]+1): | ||
| out_cfg.append('--cfg=Py_3_%d' % minor) | ||
|
|
||
| return out_cfg | ||
|
|
||
| install_requires = [] | ||
| tests_require = install_requires + ['pytest', 'pytest-benchmark'] | ||
|
|
||
| setup( | ||
| name='rustapi-module', | ||
| version='0.1.0', | ||
| classifiers=[ | ||
| 'License :: OSI Approved :: MIT License', | ||
| 'Development Status :: 3 - Alpha', | ||
| 'Intended Audience :: Developers', | ||
| 'Programming Language :: Python', | ||
| 'Programming Language :: Rust', | ||
| 'Operating System :: POSIX', | ||
| 'Operating System :: MacOS :: MacOS X', | ||
| ], | ||
| packages=['rustapi_module'], | ||
| rust_extensions=[RustExtension('rustapi_module.datetime', 'Cargo.toml', | ||
| rustc_flags=get_py_version_cfgs())], | ||
| install_requires=install_requires, | ||
| tests_require=tests_require, | ||
| include_package_data=True, | ||
| zip_safe=False, | ||
| cmdclass=dict(test=PyTest) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| #![feature(specialization)] | ||
|
|
||
| #[macro_use] | ||
| extern crate pyo3; | ||
|
|
||
| use pyo3::prelude::PyDeltaAccess; | ||
| use pyo3::prelude::PyModule; | ||
| use pyo3::prelude::PyObject; | ||
| use pyo3::prelude::{pyfunction, pymodinit}; | ||
| use pyo3::prelude::{PyDate, PyDateTime, PyDelta, PyTime, PyTzInfo}; | ||
| use pyo3::prelude::{PyDateAccess, PyTimeAccess}; | ||
| use pyo3::prelude::{PyDict, PyTuple}; | ||
| use pyo3::{ObjectProtocol, ToPyObject}; | ||
| use pyo3::{Py, PyResult, Python}; | ||
|
|
||
| #[pyfunction] | ||
| fn make_date(py: Python, year: i32, month: u8, day: u8) -> PyResult<Py<PyDate>> { | ||
| PyDate::new(py, year, month, day) | ||
| } | ||
|
|
||
| #[pyfunction] | ||
| fn get_date_tuple(py: Python, d: &PyDate) -> Py<PyTuple> { | ||
| PyTuple::new( | ||
| py, | ||
| &[d.get_year(), d.get_month() as i32, d.get_day() as i32], | ||
| ) | ||
| } | ||
|
|
||
| #[pyfunction] | ||
| fn date_from_timestamp(py: Python, ts: i64) -> PyResult<Py<PyDate>> { | ||
| let timestamp = ts.to_object(py); | ||
| let args = PyTuple::new(py, &[timestamp]); | ||
| PyDate::from_timestamp(py, &args.to_object(py)) | ||
| } | ||
|
|
||
| #[pyfunction] | ||
| fn make_time( | ||
| py: Python, | ||
| hour: u8, | ||
| minute: u8, | ||
| second: u8, | ||
| microsecond: u32, | ||
| tzinfo: Option<&PyTzInfo>, | ||
| ) -> PyResult<Py<PyTime>> { | ||
| PyTime::new( | ||
| py, | ||
| hour, | ||
| minute, | ||
| second, | ||
| microsecond, | ||
| tzinfo.map(|o| o.to_object(py)).as_ref(), | ||
| ) | ||
| } | ||
|
|
||
| #[cfg(Py_3_6)] | ||
| #[pyfunction] | ||
| fn time_with_fold( | ||
| py: Python, | ||
| hour: u8, | ||
| minute: u8, | ||
| second: u8, | ||
| microsecond: u32, | ||
| tzinfo: Option<&PyTzInfo>, | ||
| fold: bool, | ||
| ) -> PyResult<Py<PyTime>> { | ||
| PyTime::new_with_fold( | ||
| py, | ||
| hour, | ||
| minute, | ||
| second, | ||
| microsecond, | ||
| tzinfo.map(|o| o.to_object(py)).as_ref(), | ||
| fold, | ||
| ) | ||
| } | ||
|
|
||
| #[pyfunction] | ||
| fn get_time_tuple(py: Python, dt: &PyTime) -> Py<PyTuple> { | ||
| PyTuple::new( | ||
| py, | ||
| &[ | ||
| dt.get_hour() as u32, | ||
| dt.get_minute() as u32, | ||
| dt.get_second() as u32, | ||
| 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() as u32, | ||
| dt.get_minute() as u32, | ||
| dt.get_second() as u32, | ||
| dt.get_microsecond(), | ||
| dt.get_fold() as u32, | ||
| ], | ||
| ) | ||
| } | ||
|
|
||
| #[pyfunction] | ||
| fn make_delta(py: Python, days: i32, seconds: i32, microseconds: i32) -> PyResult<Py<PyDelta>> { | ||
| PyDelta::new(py, days, seconds, microseconds, true) | ||
| } | ||
|
|
||
| #[pyfunction] | ||
| fn get_delta_tuple(py: Python, delta: &PyDelta) -> Py<PyTuple> { | ||
| PyTuple::new( | ||
| py, | ||
| &[ | ||
| delta.get_days(), | ||
| delta.get_seconds(), | ||
| delta.get_microseconds(), | ||
| ], | ||
| ) | ||
| } | ||
|
|
||
| #[pyfunction] | ||
| fn make_datetime( | ||
| py: Python, | ||
| year: i32, | ||
| month: u8, | ||
| day: u8, | ||
| hour: u8, | ||
| minute: u8, | ||
| second: u8, | ||
| microsecond: u32, | ||
| tzinfo: Option<&PyTzInfo>, | ||
| ) -> PyResult<Py<PyDateTime>> { | ||
| PyDateTime::new( | ||
| py, | ||
| year, | ||
| month, | ||
| day, | ||
| hour, | ||
| minute, | ||
| second, | ||
| microsecond, | ||
| tzinfo.map(|o| (o.to_object(py))).as_ref(), | ||
| ) | ||
| } | ||
|
|
||
| #[pyfunction] | ||
| fn get_datetime_tuple(py: Python, dt: &PyDateTime) -> Py<PyTuple> { | ||
| PyTuple::new( | ||
| py, | ||
| &[ | ||
| dt.get_year(), | ||
| dt.get_month() as i32, | ||
| dt.get_day() as i32, | ||
| dt.get_hour() as i32, | ||
| dt.get_minute() as i32, | ||
| dt.get_second() as i32, | ||
| dt.get_microsecond() as i32, | ||
| ], | ||
| ) | ||
| } | ||
|
|
||
| #[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() as i32, | ||
| dt.get_day() as i32, | ||
| dt.get_hour() as i32, | ||
| dt.get_minute() as i32, | ||
| dt.get_second() as i32, | ||
| dt.get_microsecond() as i32, | ||
| dt.get_fold() as i32, | ||
| ], | ||
| ) | ||
| } | ||
|
|
||
| #[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 { | ||
| Some(t) => t.to_object(py), | ||
| None => py.None(), | ||
| }; | ||
|
|
||
| let args = PyTuple::new(py, &[timestamp, tzi]); | ||
| let kwargs = PyDict::new(py); | ||
|
|
||
| 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))?; | ||
| m.add_function(wrap_function!(get_date_tuple))?; | ||
| m.add_function(wrap_function!(date_from_timestamp))?; | ||
| m.add_function(wrap_function!(make_time))?; | ||
| m.add_function(wrap_function!(get_time_tuple))?; | ||
| m.add_function(wrap_function!(make_delta))?; | ||
| m.add_function(wrap_function!(get_delta_tuple))?; | ||
| m.add_function(wrap_function!(make_datetime))?; | ||
| m.add_function(wrap_function!(get_datetime_tuple))?; | ||
| m.add_function(wrap_function!(datetime_from_timestamp))?; | ||
|
|
||
| // Python 3.6+ functions | ||
| #[cfg(Py_3_6)] | ||
| { | ||
| m.add_function(wrap_function!(time_with_fold)); | ||
| m.add_function(wrap_function!(get_time_tuple_fold)); | ||
| m.add_function(wrap_function!(get_datetime_tuple_fold)); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Travis is missing hypothesis, I guess it's missing here or up in the .travis.yml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only for the build system.
toxshould install it as part of thedepsbecause it's inrequirements-dev.txt. I'll figure out what's going on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see the problem, it's the
for example in examples/*stuff.