Skip to content
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

Modernize the codebase #204

Merged
merged 10 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ python:
- "3.7"
- "3.8"
env:
- RUST_VERSION=1.30.0
- RUST_VERSION=1.32.0
- RUST_VERSION=nightly
matrix:
include:
- os: osx
language: generic
env: RUST_VERSION=1.30.0
env: RUST_VERSION=1.32.0
- os: osx
language: generic
env: RUST_VERSION=nightly
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ exclude = [
"/extensions/**"
]
build = "build.rs"
edition = "2018"

[badges]
travis-ci = { repository = "dgrunwald/rust-cpython" }
Expand All @@ -33,6 +34,7 @@ appveyor = { repository = "dgrunwald/rust-cpython" }
[dependencies]
libc = "0.2"
num-traits = "0.2"
paste = "0.1"

# These features are both optional, but you must pick one to
# indicate which python ffi you are trying to bind to.
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Supported Python versions:
* Python 2.7
* Python 3.3 to 3.8

Requires Rust 1.30.0 or later.
Requires Rust 1.32.0 or later.

# Usage

Expand All @@ -30,8 +30,6 @@ cpython = "0.4"
#### Example program displaying the value of `sys.version`:

```rust
extern crate cpython;

use cpython::{Python, PyDict, PyResult};

fn main() {
Expand Down Expand Up @@ -78,13 +76,11 @@ features = ["extension-module"]

**`src/lib.rs`**
```rust
#[macro_use] extern crate cpython;

use cpython::{PyResult, Python};
use cpython::{PyResult, Python, py_module_initializer, py_fn};

// add bindings to the generated python module
// N.B: names: "rust2py" must be the name of the `.so` or `.pyd` file
py_module_initializer!(rust2py, initrust2py, PyInit_rust2py, |py, m| {
py_module_initializer!(rust2py, |py, m| {
m.add(py, "__doc__", "This module is implemented in Rust.")?;
m.add(py, "sum_as_string", py_fn!(py, sum_as_string_py(a: i64, b:i64)))?;
Ok(())
Expand Down
2 changes: 0 additions & 2 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate cpython;

use cpython::{PyDict, PyResult, Python};

fn main() {
Expand Down
1 change: 1 addition & 0 deletions extensions/hello/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "hello"
version = "0.4.0"
authors = ["Daniel Grunwald <daniel@danielgrunwald.de>"]
edition = "2018"

[lib]
## Python extension modules should be compiled as 'cdylib'
Expand Down
7 changes: 2 additions & 5 deletions extensions/hello/src/hello.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#[macro_use]
extern crate cpython;

use cpython::{PyDict, PyObject, PyResult, PyTuple, Python};
use cpython::{py_fn, py_module_initializer, PyDict, PyObject, PyResult, PyTuple, Python};

// Our module is named 'hello', and can be imported using `import hello`.
// This requires that the output binary file is named `hello.so` (or Windows: `hello.pyd`).
// As the output name cannot be configured in cargo (https://github.com/rust-lang/cargo/issues/1970),
// you'll have to rename the output file.
py_module_initializer!(hello, inithello, PyInit_hello, |py, m| {
py_module_initializer!(hello, |py, m| {
m.add(py, "__doc__", "Module documentation string")?;
m.add(py, "func", py_fn!(py, func(a: &str, b: i32)))?;
m.add(py, "run", py_fn!(py, run(*args, **kwargs)))?;
Expand Down
5 changes: 2 additions & 3 deletions extensions/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ all:
clean:
$(RM) *.so
$(RM) *.out
$(RM) *_expanded.rs
$(RM) -r stamps

stamps:
Expand All @@ -33,10 +32,10 @@ stamps/rust-cpython-$(PY): $(call rwildcard,../../src,*.rs) Makefile | stamps
touch "$@"

%.so: %.rs stamps/rust-cpython-$(PY)
rustc $< -g -L $(TARGETDIR) -L $(TARGETDIR)/deps -o $@
rustc $< --edition 2018 -g -L $(TARGETDIR) -L $(TARGETDIR)/deps --extern cpython=$(TARGETDIR)/libcpython.rlib -o $@

%_expanded.rs: %.rs stamps/rust-cpython-$(PY)
rustc $< -g -L $(TARGETDIR) -L $(TARGETDIR)/deps -Z unstable-options --pretty=expanded -o $@
rustc $< --edition 2018 -g -L $(TARGETDIR) -L $(TARGETDIR)/deps -Z unstable-options --pretty=expanded --extern cpython=$(TARGETDIR)/libcpython.rlib -o $@

hello.out: hello.so
python$(PY) -c "import hello; hello.run(hello.val())" 2>&1 | tee $@
Expand Down
6 changes: 2 additions & 4 deletions extensions/tests/btree.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#![crate_type = "dylib"]

#[macro_use] extern crate cpython;

use cpython::{PyObject, PyResult};
use cpython::{PyObject, PyResult, py_module_initializer, py_class};
use std::{cell, cmp, collections};

py_module_initializer!(btree, initbtree, PyInit_btree, |py, m| {
py_module_initializer!(btree, |py, m| {
m.add(py, "__doc__", "Rust BTreeSet for Python.")?;
m.add_class::<BTreeSet>(py)?;
Ok(())
Expand Down
6 changes: 2 additions & 4 deletions extensions/tests/custom_class.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#![crate_type = "dylib"]

#[macro_use] extern crate cpython;
use cpython::{PyObject, PyResult, py_module_initializer, py_class};

use cpython::{PyObject, PyResult};

py_module_initializer!(custom_class, initcustom_class, PyInit_custom_class, |py, m| {
py_module_initializer!(custom_class, |py, m| {
m.add(py, "__doc__", "Module documentation string")?;
m.add_class::<MyType>(py)?;
Ok(())
Expand Down
6 changes: 2 additions & 4 deletions extensions/tests/hello.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#![crate_type = "dylib"]

#[macro_use] extern crate cpython;
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict, py_module_initializer, py_fn};

use cpython::{PyObject, PyResult, Python, PyTuple, PyDict};

py_module_initializer!(hello, inithello, PyInit_hello, |py, m| {
py_module_initializer!(hello, |py, m| {
m.add(py, "__doc__", "Module documentation string")?;
m.add(py, "run", py_fn!(py, run(*args, **kwargs)))?;
m.add(py, "val", py_fn!(py, val()))?;
Expand Down
1 change: 1 addition & 0 deletions python27-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exclude = [
"/.travis.yml",
]
workspace = ".."
edition = "2018"

[dependencies]
libc = "0.2"
Expand Down
6 changes: 0 additions & 6 deletions python27-sys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,5 @@ For a safe high-level API, see [rust-cpython](https://github.com/dgrunwald/rust-
version = "*"
```

In Rust, import the crate like this:

```rust
extern crate python27_sys as py;
```

Documentation for the python API is available on [https://docs.python.org/2/c-api/].

2 changes: 0 additions & 2 deletions python27-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// THIS FILE IS GENERATED FROM python3-sys/build.rs
// DO NOT MODIFY

extern crate regex;

use regex::Regex;
use std::collections::HashMap;
use std::env;
Expand Down
3 changes: 0 additions & 3 deletions python27-sys/examples/version.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
extern crate libc;
extern crate python27_sys;

unsafe fn get_str<'a>(s: *const libc::c_char) -> &'a str {
let bytes = std::ffi::CStr::from_ptr(s).to_bytes();
std::str::from_utf8(bytes).unwrap()
Expand Down
5 changes: 3 additions & 2 deletions python27-sys/src/boolobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use intobject::PyIntObject;
use libc::{c_int, c_long};
use object::*;

use crate::intobject::PyIntObject;
use crate::object::*;

pub type PyBoolObject = PyIntObject;

Expand Down
5 changes: 3 additions & 2 deletions python27-sys/src/bufferobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::{c_int, c_void};
use object::*;
use pyport::Py_ssize_t;

use crate::object::*;
use crate::pyport::Py_ssize_t;

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down
5 changes: 3 additions & 2 deletions python27-sys/src/bytearrayobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::{c_char, c_int};
use object::*;
use pyport::Py_ssize_t;

use crate::object::*;
use crate::pyport::Py_ssize_t;

/*#[repr(C)]
#[deriving(Copy)]
Expand Down
32 changes: 16 additions & 16 deletions python27-sys/src/bytesobject.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
pub use object::Py_TPFLAGS_STRING_SUBCLASS as Py_TPFLAGS_BYTES_SUBCLASS;
pub use stringobject::PyStringObject as PyBytesObject;
pub use stringobject::PyString_AS_STRING as PyBytes_AS_STRING;
pub use stringobject::PyString_AsString as PyBytes_AsString;
pub use stringobject::PyString_AsStringAndSize as PyBytes_AsStringAndSize;
pub use stringobject::PyString_Check as PyBytes_Check;
pub use stringobject::PyString_CheckExact as PyBytes_CheckExact;
pub use stringobject::PyString_Concat as PyBytes_Concat;
pub use stringobject::PyString_ConcatAndDel as PyBytes_ConcatAndDel;
pub use stringobject::PyString_Format as PyBytes_Format;
pub use stringobject::PyString_FromFormat as PyBytes_FromFormat;
pub use stringobject::PyString_FromString as PyBytes_FromString;
pub use stringobject::PyString_FromStringAndSize as PyBytes_FromStringAndSize;
pub use stringobject::PyString_GET_SIZE as PyBytes_GET_SIZE;
pub use stringobject::PyString_Size as PyBytes_Size;
pub use stringobject::PyString_Type as PyBytes_Type;
pub use crate::object::Py_TPFLAGS_STRING_SUBCLASS as Py_TPFLAGS_BYTES_SUBCLASS;
pub use crate::stringobject::PyStringObject as PyBytesObject;
pub use crate::stringobject::PyString_AS_STRING as PyBytes_AS_STRING;
pub use crate::stringobject::PyString_AsString as PyBytes_AsString;
pub use crate::stringobject::PyString_AsStringAndSize as PyBytes_AsStringAndSize;
pub use crate::stringobject::PyString_Check as PyBytes_Check;
pub use crate::stringobject::PyString_CheckExact as PyBytes_CheckExact;
pub use crate::stringobject::PyString_Concat as PyBytes_Concat;
pub use crate::stringobject::PyString_ConcatAndDel as PyBytes_ConcatAndDel;
pub use crate::stringobject::PyString_Format as PyBytes_Format;
pub use crate::stringobject::PyString_FromFormat as PyBytes_FromFormat;
pub use crate::stringobject::PyString_FromString as PyBytes_FromString;
pub use crate::stringobject::PyString_FromStringAndSize as PyBytes_FromStringAndSize;
pub use crate::stringobject::PyString_GET_SIZE as PyBytes_GET_SIZE;
pub use crate::stringobject::PyString_Size as PyBytes_Size;
pub use crate::stringobject::PyString_Type as PyBytes_Type;
5 changes: 3 additions & 2 deletions python27-sys/src/cellobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::c_int;
use object::*;
use pyport::Py_ssize_t;

use crate::object::*;
use crate::pyport::Py_ssize_t;

#[repr(C)]
#[derive(Copy, Clone)]
Expand Down
11 changes: 6 additions & 5 deletions python27-sys/src/ceval.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use frameobject::PyFrameObject;
use libc::{c_char, c_int, c_void};
use object::PyObject;
use pyport::Py_ssize_t;
use pystate::{PyThreadState, Py_tracefunc};
use pythonrun::PyCompilerFlags;

use crate::frameobject::PyFrameObject;
use crate::object::PyObject;
use crate::pyport::Py_ssize_t;
use crate::pystate::{PyThreadState, Py_tracefunc};
use crate::pythonrun::PyCompilerFlags;

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down
5 changes: 3 additions & 2 deletions python27-sys/src/classobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::c_int;
use object::*;
use pyport::Py_ssize_t;

use crate::object::*;
use crate::pyport::Py_ssize_t;

#[repr(C)]
#[derive(Copy, Clone)]
Expand Down
3 changes: 2 additions & 1 deletion python27-sys/src/cobject.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use libc::{c_char, c_int, c_void};
use object::*;

use crate::object::*;

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down
7 changes: 4 additions & 3 deletions python27-sys/src/code.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::{c_char, c_int, c_void};
use object::*;
use pyport::Py_ssize_t;

use crate::object::*;
use crate::pyport::Py_ssize_t;

#[repr(C)]
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -95,5 +96,5 @@ pub unsafe fn PyCode_Check(op: *mut PyObject) -> c_int {

#[inline]
pub unsafe fn PyCode_GetNumFree(op: *mut PyCodeObject) -> Py_ssize_t {
::tupleobject::PyTuple_GET_SIZE((*op).co_freevars)
crate::tupleobject::PyTuple_GET_SIZE((*op).co_freevars)
}
7 changes: 4 additions & 3 deletions python27-sys/src/compile.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use code::*;
use libc::{c_char, c_int};
use pyarena::PyArena;
use pythonrun::*;

use crate::code::*;
use crate::pyarena::PyArena;
use crate::pythonrun::*;

#[repr(C)]
#[derive(Copy, Clone)]
Expand Down
5 changes: 3 additions & 2 deletions python27-sys/src/complexobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::{c_double, c_int};
use object::*;
use pyport::Py_ssize_t;

use crate::object::*;
use crate::pyport::Py_ssize_t;

#[repr(C)]
#[derive(Copy, Clone)]
Expand Down
7 changes: 4 additions & 3 deletions python27-sys/src/descrobject.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use libc::{c_char, c_int, c_void};
use methodobject::PyMethodDef;
use object::{PyObject, PyTypeObject, Py_TYPE};
use structmember::PyMemberDef;

use crate::methodobject::PyMethodDef;
use crate::object::{PyObject, PyTypeObject, Py_TYPE};
use crate::structmember::PyMemberDef;

pub type getter = unsafe extern "C" fn(slf: *mut PyObject, closure: *mut c_void) -> *mut PyObject;

Expand Down
5 changes: 3 additions & 2 deletions python27-sys/src/dictobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::{c_char, c_int};
use object::*;
use pyport::Py_ssize_t;

use crate::object::*;
use crate::pyport::Py_ssize_t;

//pub enum PyDictObject { /* representation hidden */ }

Expand Down
2 changes: 1 addition & 1 deletion python27-sys/src/enumobject.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use object::PyTypeObject;
use crate::object::PyTypeObject;

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down
5 changes: 3 additions & 2 deletions python27-sys/src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use code::PyCodeObject;
use libc::c_int;
use object::PyObject;

use crate::code::PyCodeObject;
use crate::object::PyObject;

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down
3 changes: 2 additions & 1 deletion python27-sys/src/fileobject.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use libc::{c_char, c_int, size_t, FILE};
use object::*;

use crate::object::*;

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
Expand Down
5 changes: 3 additions & 2 deletions python27-sys/src/floatobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::{c_char, c_double, c_int};
use object::*;
use pyport::Py_ssize_t;

use crate::object::*;
use crate::pyport::Py_ssize_t;

#[repr(C)]
#[derive(Copy, Clone)]
Expand Down
Loading