Skip to content

Commit

Permalink
Fix #10: Windows support.
Browse files Browse the repository at this point in the history
We keep the #[link] attributes in #[cfg_attr(windows)] so that we don't require a nightly Rust build on non-Windows platforms.
This can be simplified once RFC 1717 is available in a stable rust version.

This commit also increases the minimum Rust version to 1.13.
  • Loading branch information
dgrunwald committed Dec 17, 2016
1 parent 278c1ae commit f6ed2bb
Show file tree
Hide file tree
Showing 104 changed files with 223 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ python:
- "3.4"
- "3.5"
env:
- RUST_VERSION=1.7.0
- RUST_VERSION=1.13.0
- RUST_VERSION=nightly
sudo: false
install:
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Supported Python versions:
* Python 3.5

Supported Rust version:
* Rust 1.7.0 or later
* Rust 1.13.0 or later
* On Windows, we require rustc 1.15.0-nightly

# Usage

Expand All @@ -35,21 +36,23 @@ Example program displaying the value of `sys.version`:
```rust
extern crate cpython;

use cpython::Python;
use cpython::ObjectProtocol; //for call method
use cpython::{Python, PyDict, PyResult};

fn main() {
let gil = Python::acquire_gil();
let py = gil.python();
hello(gil.python()).unwrap();
}

let sys = py.import("sys").unwrap();
let version: String = sys.get(py, "version").unwrap().extract(py).unwrap();
fn hello(py: Python) -> PyResult<()> {
let sys = py.import("sys")?;
let version: String = sys.get(py, "version")?.extract(py)?;

let os = py.import("os").unwrap();
let getenv = os.get(py, "getenv").unwrap();
let user: String = getenv.call(py, ("USER",), None).unwrap().extract(py).unwrap();
let locals = PyDict::new(py);
locals.set_item(py, "os", py.import("os")?)?;
let user: String = py.eval("os.getenv('USER') or os.getenv('USERNAME')", None, Some(&locals))?.extract(py)?;

println!("Hello {}, I'm Python {}", user, version);
Ok(())
}
```

Expand Down
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ install:
build_script:
- set PATH=C:\Python27-x64;%PATH%&& cargo build --verbose --features python27-sys --no-default-features
- set PATH=C:\Python34-x64;%PATH%&& cargo build --verbose --features python3-sys --no-default-features
test: false
#test_script:
# - cargo test --verbose
test_script:
- set PATH=C:\Python27-x64;%PATH%&& cargo test --verbose --features python27-sys --no-default-features
- set PATH=C:\Python34-x64;%PATH%&& cargo test --verbose --features python3-sys --no-default-features
18 changes: 10 additions & 8 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
extern crate cpython;

use cpython::Python;
use cpython::ObjectProtocol; //for call method
use cpython::{Python, PyDict, PyResult};

fn main() {
let gil = Python::acquire_gil();
let py = gil.python();
hello(gil.python()).unwrap();
}

let sys = py.import("sys").unwrap();
let version: String = sys.get(py, "version").unwrap().extract(py).unwrap();
fn hello(py: Python) -> PyResult<()> {
let sys = py.import("sys")?;
let version: String = sys.get(py, "version")?.extract(py)?;

let os = py.import("os").unwrap();
let getenv = os.get(py, "getenv").unwrap();
let user: String = getenv.call(py, ("USER",), None).unwrap().extract(py).unwrap();
let locals = PyDict::new(py);
locals.set_item(py, "os", py.import("os")?)?;
let user: String = py.eval("os.getenv('USER') or os.getenv('USERNAME')", None, Some(&locals))?.extract(py)?;

println!("Hello {}, I'm Python {}", user, version);
Ok(())
}
2 changes: 1 addition & 1 deletion python27-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ fn get_interpreter_version(line: &str) -> Result<PythonVersion, String> {
#[cfg(target_os="windows")]
fn get_rustc_link_lib(version: &PythonVersion, _: bool) -> Result<String, String> {
// Py_ENABLE_SHARED doesn't seem to be present on windows.
Ok(format!("cargo:rustc-link-lib=python{}{}", version.major,
Ok(format!("cargo:rustc-link-lib=pythonXY:python{}{}", version.major,
match version.minor {
Some(minor) => minor.to_string(),
None => "".to_owned()
Expand Down
3 changes: 1 addition & 2 deletions python27-sys/src/boolobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use intobject::PyIntObject;

pub type PyBoolObject = PyIntObject;


extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyBool_Type: PyTypeObject;
static mut _Py_ZeroStruct: PyIntObject;
static mut _Py_TrueStruct: PyIntObject;
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/bufferobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use libc::{c_void, c_int};
use object::*;
use pyport::Py_ssize_t;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyBuffer_Type: PyTypeObject;
}

Expand All @@ -14,7 +14,7 @@ pub unsafe fn PyBuffer_Check(op : *mut PyObject) -> c_int {

pub const Py_END_OF_BUFFER: Py_ssize_t = -1;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyBuffer_FromObject(base: *mut PyObject, offset: Py_ssize_t,
size: Py_ssize_t) -> *mut PyObject;
pub fn PyBuffer_FromReadWriteObject(base: *mut PyObject,
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/bytearrayobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct PyByteArrayObject {
pub ob_bytes: *mut c_char,
}*/

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyByteArray_Type: PyTypeObject;
pub static mut PyByteArrayIter_Type: PyTypeObject;
}
Expand All @@ -31,7 +31,7 @@ pub unsafe fn PyByteArray_CheckExact(op : *mut PyObject) -> c_int {
(Py_TYPE(op) == u) as c_int
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyByteArray_FromObject(o: *mut PyObject) -> *mut PyObject;
pub fn PyByteArray_Concat(a: *mut PyObject, b: *mut PyObject)
-> *mut PyObject;
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/cellobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct PyCellObject {
pub ob_ref: *mut PyObject
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyCell_Type: PyTypeObject;
}

Expand All @@ -23,7 +23,7 @@ pub unsafe fn PyCell_Check(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == &mut PyCell_Type) as c_int
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyCell_New(obj: *mut PyObject) -> *mut PyObject;
pub fn PyCell_Get(op: *mut PyObject) -> *mut PyObject;
pub fn PyCell_Set(op: *mut PyObject, obj: *mut PyObject) -> c_int;
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/ceval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use frameobject::PyFrameObject;
use pystate::{PyThreadState, Py_tracefunc};
use pythonrun::PyCompilerFlags;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyEval_CallObjectWithKeywords(callable: *mut PyObject,
args: *mut PyObject,
kwds: *mut PyObject)
Expand Down Expand Up @@ -53,7 +53,7 @@ extern "C" {
}

#[cfg(py_sys_config="WITH_THREAD")]
extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyEval_ThreadsInitialized() -> c_int;
pub fn PyEval_InitThreads();
pub fn PyEval_AcquireLock();
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/classobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct PyMethodObject {
pub im_weakreflist: *mut PyObject,
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyClass_Type: PyTypeObject;
pub static mut PyInstance_Type: PyTypeObject;
pub static mut PyMethod_Type: PyTypeObject;
Expand All @@ -73,7 +73,7 @@ pub unsafe fn PyMethod_Check(op : *mut PyObject) -> c_int {
(Py_TYPE(op) == u) as c_int
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyClass_New(arg1: *mut PyObject, arg2: *mut PyObject,
arg3: *mut PyObject) -> *mut PyObject;
pub fn PyInstance_New(arg1: *mut PyObject, arg2: *mut PyObject,
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/cobject.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc::{c_void, c_char, c_int};
use object::*;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyCObject_Type: PyTypeObject;
}

Expand All @@ -10,7 +10,7 @@ pub unsafe fn PyCObject_Check(op : *mut PyObject) -> c_int {
(Py_TYPE(op) == &mut PyCObject_Type) as c_int
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyCObject_FromVoidPtr(cobj: *mut c_void,
destruct:
Option<unsafe extern "C" fn
Expand Down
2 changes: 1 addition & 1 deletion python27-sys/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub const CO_FUTURE_UNICODE_LITERALS : c_int = 0x20000;

pub const CO_MAXBLOCKS : usize = 20;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyCode_Type: PyTypeObject;

pub fn PyCode_New(arg1: c_int, arg2: c_int,
Expand Down
2 changes: 1 addition & 1 deletion python27-sys/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub const FUTURE_WITH_STATEMENT : &'static str = "with_statement";
pub const FUTURE_PRINT_FUNCTION : &'static str = "print_function";
pub const FUTURE_UNICODE_LITERALS : &'static str = "unicode_literals";

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyNode_Compile(arg1: *mut Struct__node,
arg2: *const c_char) -> *mut PyCodeObject;
pub fn PyAST_Compile(arg1: *mut Struct__mod, arg2: *const c_char,
Expand Down
6 changes: 3 additions & 3 deletions python27-sys/src/complexobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Py_complex {
pub imag: c_double
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn _Py_c_sum(left: Py_complex, right: Py_complex) -> Py_complex;
pub fn _Py_c_diff(left: Py_complex, right: Py_complex) -> Py_complex;
pub fn _Py_c_neg(complex: Py_complex) -> Py_complex;
Expand All @@ -31,7 +31,7 @@ pub struct PyComplexObject {
pub cval: Py_complex
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyComplex_Type: PyTypeObject;
}

Expand All @@ -46,7 +46,7 @@ pub unsafe fn PyComplex_CheckExact(op : *mut PyObject) -> c_int {
(Py_TYPE(op) == u) as c_int
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyComplex_FromCComplex(v: Py_complex) -> *mut PyObject;
pub fn PyComplex_FromDoubles(real: c_double,
imag: c_double) -> *mut PyObject;
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/descrobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Clone for wrapperbase {

pub const PyWrapperFlag_KEYWORDS : c_int = 1;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyWrapperDescr_Type: PyTypeObject;
pub static mut PyDictProxy_Type: PyTypeObject;
pub static mut PyGetSetDescr_Type: PyTypeObject;
Expand All @@ -78,7 +78,7 @@ pub unsafe fn PyDescr_IsData(d: *mut PyObject) -> c_int {
(*Py_TYPE(d)).tp_descr_set.is_some() as c_int
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
//pub fn PyDictProxy_New(arg1: *mut PyObject) -> *mut PyObject;
// PyDictProxy_New is also defined in dictobject.h
pub fn PyWrapper_New(arg1: *mut PyObject, arg2: *mut PyObject)
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/dictobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use object::*;

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

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyDict_Type: PyTypeObject;
pub static mut PyDictIterKey_Type: PyTypeObject;
pub static mut PyDictIterValue_Type: PyTypeObject;
Expand All @@ -25,7 +25,7 @@ pub unsafe fn PyDict_CheckExact(op : *mut PyObject) -> c_int {
(Py_TYPE(op) == u) as c_int
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyDict_New() -> *mut PyObject;
pub fn PyDictProxy_New(dict: *mut PyObject) -> *mut PyObject;
pub fn PyDict_Clear(mp: *mut PyObject);
Expand Down
2 changes: 1 addition & 1 deletion python27-sys/src/enumobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use object::PyTypeObject;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyEnum_Type: PyTypeObject;
pub static mut PyReversed_Type: PyTypeObject;
}
Expand Down
2 changes: 1 addition & 1 deletion python27-sys/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use libc::c_int;
use object::PyObject;
use code::PyCodeObject;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyEval_EvalCode(arg1: *mut PyCodeObject, arg2: *mut PyObject,
arg3: *mut PyObject) -> *mut PyObject;
pub fn PyEval_EvalCodeEx(co: *mut PyCodeObject, globals: *mut PyObject,
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/fileobject.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc::{c_char, c_int, size_t, FILE};
use object::*;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyFile_Type: PyTypeObject;
}

Expand All @@ -18,7 +18,7 @@ pub unsafe fn PyFile_CheckExact(op : *mut PyObject) -> c_int {

pub const PY_STDIOTEXTMODE : &'static str = "b";

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyFile_FromString(arg1: *mut c_char,
arg2: *mut c_char) -> *mut PyObject;
pub fn PyFile_SetBufSize(arg1: *mut PyObject, arg2: c_int);
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/floatobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct PyFloatObject {
pub ob_fval: c_double
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyFloat_Type: PyTypeObject;
}

Expand All @@ -31,7 +31,7 @@ pub unsafe fn PyFloat_CheckExact(op : *mut PyObject) -> c_int {

pub const PyFloat_STR_PRECISION : c_int = 12;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyFloat_FromString(str: *mut PyObject,
pend: *mut *mut c_char)
-> *mut PyObject;
Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/frameobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct PyFrameObject {
pub f_localsplus: [*mut PyObject; 1] /* locals+stack, dynamically sized */
}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyFrame_Type: PyTypeObject;
}

Expand All @@ -66,7 +66,7 @@ pub unsafe fn PyFrame_Check(op: *mut PyObject) -> c_int {
// ((*f).f_builtins != (*(*(*f).f_tstate).interp).builtins) as c_int
//}

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyFrame_New(tstate: *mut PyThreadState, code: *mut PyCodeObject,
globals: *mut PyObject, locals: *mut PyObject) -> *mut PyFrameObject;

Expand Down
4 changes: 2 additions & 2 deletions python27-sys/src/funcobject.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libc::c_int;
use object::*;

extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub static mut PyFunction_Type: PyTypeObject;
}

Expand All @@ -12,7 +12,7 @@ pub unsafe fn PyFunction_Check(op : *mut PyObject) -> c_int {
}


extern "C" {
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
pub fn PyFunction_New(code: *mut PyObject, globals: *mut PyObject)
-> *mut PyObject;
pub fn PyFunction_GetCode(f: *mut PyObject) -> *mut PyObject;
Expand Down
Loading

0 comments on commit f6ed2bb

Please sign in to comment.