Skip to content

Commit f6ed2bb

Browse files
committed
Fix #10: Windows support.
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.
1 parent 278c1ae commit f6ed2bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+223
-217
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ python:
55
- "3.4"
66
- "3.5"
77
env:
8-
- RUST_VERSION=1.7.0
8+
- RUST_VERSION=1.13.0
99
- RUST_VERSION=nightly
1010
sudo: false
1111
install:

README.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Supported Python versions:
1919
* Python 3.5
2020

2121
Supported Rust version:
22-
* Rust 1.7.0 or later
22+
* Rust 1.13.0 or later
23+
* On Windows, we require rustc 1.15.0-nightly
2324

2425
# Usage
2526

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

38-
use cpython::Python;
39-
use cpython::ObjectProtocol; //for call method
39+
use cpython::{Python, PyDict, PyResult};
4040

4141
fn main() {
4242
let gil = Python::acquire_gil();
43-
let py = gil.python();
43+
hello(gil.python()).unwrap();
44+
}
4445

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

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

5254
println!("Hello {}, I'm Python {}", user, version);
55+
Ok(())
5356
}
5457
```
5558

appveyor.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ install:
2222
build_script:
2323
- set PATH=C:\Python27-x64;%PATH%&& cargo build --verbose --features python27-sys --no-default-features
2424
- set PATH=C:\Python34-x64;%PATH%&& cargo build --verbose --features python3-sys --no-default-features
25-
test: false
26-
#test_script:
27-
# - cargo test --verbose
25+
test_script:
26+
- set PATH=C:\Python27-x64;%PATH%&& cargo test --verbose --features python27-sys --no-default-features
27+
- set PATH=C:\Python34-x64;%PATH%&& cargo test --verbose --features python3-sys --no-default-features

examples/hello.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
extern crate cpython;
22

3-
use cpython::Python;
4-
use cpython::ObjectProtocol; //for call method
3+
use cpython::{Python, PyDict, PyResult};
54

65
fn main() {
76
let gil = Python::acquire_gil();
8-
let py = gil.python();
7+
hello(gil.python()).unwrap();
8+
}
99

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

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

1718
println!("Hello {}, I'm Python {}", user, version);
19+
Ok(())
1820
}

python27-sys/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn get_interpreter_version(line: &str) -> Result<PythonVersion, String> {
226226
#[cfg(target_os="windows")]
227227
fn get_rustc_link_lib(version: &PythonVersion, _: bool) -> Result<String, String> {
228228
// Py_ENABLE_SHARED doesn't seem to be present on windows.
229-
Ok(format!("cargo:rustc-link-lib=python{}{}", version.major,
229+
Ok(format!("cargo:rustc-link-lib=pythonXY:python{}{}", version.major,
230230
match version.minor {
231231
Some(minor) => minor.to_string(),
232232
None => "".to_owned()

python27-sys/src/boolobject.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use intobject::PyIntObject;
44

55
pub type PyBoolObject = PyIntObject;
66

7-
8-
extern "C" {
7+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
98
pub static mut PyBool_Type: PyTypeObject;
109
static mut _Py_ZeroStruct: PyIntObject;
1110
static mut _Py_TrueStruct: PyIntObject;

python27-sys/src/bufferobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use libc::{c_void, c_int};
22
use object::*;
33
use pyport::Py_ssize_t;
44

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

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

1515
pub const Py_END_OF_BUFFER: Py_ssize_t = -1;
1616

17-
extern "C" {
17+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
1818
pub fn PyBuffer_FromObject(base: *mut PyObject, offset: Py_ssize_t,
1919
size: Py_ssize_t) -> *mut PyObject;
2020
pub fn PyBuffer_FromReadWriteObject(base: *mut PyObject,

python27-sys/src/bytearrayobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct PyByteArrayObject {
1717
pub ob_bytes: *mut c_char,
1818
}*/
1919

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

34-
extern "C" {
34+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
3535
pub fn PyByteArray_FromObject(o: *mut PyObject) -> *mut PyObject;
3636
pub fn PyByteArray_Concat(a: *mut PyObject, b: *mut PyObject)
3737
-> *mut PyObject;

python27-sys/src/cellobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct PyCellObject {
1414
pub ob_ref: *mut PyObject
1515
}
1616

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

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

26-
extern "C" {
26+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
2727
pub fn PyCell_New(obj: *mut PyObject) -> *mut PyObject;
2828
pub fn PyCell_Get(op: *mut PyObject) -> *mut PyObject;
2929
pub fn PyCell_Set(op: *mut PyObject, obj: *mut PyObject) -> c_int;

python27-sys/src/ceval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use frameobject::PyFrameObject;
55
use pystate::{PyThreadState, Py_tracefunc};
66
use pythonrun::PyCompilerFlags;
77

8-
extern "C" {
8+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
99
pub fn PyEval_CallObjectWithKeywords(callable: *mut PyObject,
1010
args: *mut PyObject,
1111
kwds: *mut PyObject)
@@ -53,7 +53,7 @@ extern "C" {
5353
}
5454

5555
#[cfg(py_sys_config="WITH_THREAD")]
56-
extern "C" {
56+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
5757
pub fn PyEval_ThreadsInitialized() -> c_int;
5858
pub fn PyEval_InitThreads();
5959
pub fn PyEval_AcquireLock();

python27-sys/src/classobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub struct PyMethodObject {
4949
pub im_weakreflist: *mut PyObject,
5050
}
5151

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

76-
extern "C" {
76+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
7777
pub fn PyClass_New(arg1: *mut PyObject, arg2: *mut PyObject,
7878
arg3: *mut PyObject) -> *mut PyObject;
7979
pub fn PyInstance_New(arg1: *mut PyObject, arg2: *mut PyObject,

python27-sys/src/cobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use libc::{c_void, c_char, c_int};
22
use object::*;
33

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

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

13-
extern "C" {
13+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
1414
pub fn PyCObject_FromVoidPtr(cobj: *mut c_void,
1515
destruct:
1616
Option<unsafe extern "C" fn

python27-sys/src/code.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub const CO_FUTURE_UNICODE_LITERALS : c_int = 0x20000;
5151

5252
pub const CO_MAXBLOCKS : usize = 20;
5353

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

5757
pub fn PyCode_New(arg1: c_int, arg2: c_int,

python27-sys/src/compile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub const FUTURE_WITH_STATEMENT : &'static str = "with_statement";
1818
pub const FUTURE_PRINT_FUNCTION : &'static str = "print_function";
1919
pub const FUTURE_UNICODE_LITERALS : &'static str = "unicode_literals";
2020

21-
extern "C" {
21+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
2222
pub fn PyNode_Compile(arg1: *mut Struct__node,
2323
arg2: *const c_char) -> *mut PyCodeObject;
2424
pub fn PyAST_Compile(arg1: *mut Struct__mod, arg2: *const c_char,

python27-sys/src/complexobject.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct Py_complex {
99
pub imag: c_double
1010
}
1111

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

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

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

49-
extern "C" {
49+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
5050
pub fn PyComplex_FromCComplex(v: Py_complex) -> *mut PyObject;
5151
pub fn PyComplex_FromDoubles(real: c_double,
5252
imag: c_double) -> *mut PyObject;

python27-sys/src/descrobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl Clone for wrapperbase {
5353

5454
pub const PyWrapperFlag_KEYWORDS : c_int = 1;
5555

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

81-
extern "C" {
81+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
8282
//pub fn PyDictProxy_New(arg1: *mut PyObject) -> *mut PyObject;
8383
// PyDictProxy_New is also defined in dictobject.h
8484
pub fn PyWrapper_New(arg1: *mut PyObject, arg2: *mut PyObject)

python27-sys/src/dictobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use object::*;
44

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

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

28-
extern "C" {
28+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
2929
pub fn PyDict_New() -> *mut PyObject;
3030
pub fn PyDictProxy_New(dict: *mut PyObject) -> *mut PyObject;
3131
pub fn PyDict_Clear(mp: *mut PyObject);

python27-sys/src/enumobject.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use object::PyTypeObject;
22

3-
extern "C" {
3+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
44
pub static mut PyEnum_Type: PyTypeObject;
55
pub static mut PyReversed_Type: PyTypeObject;
66
}

python27-sys/src/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use libc::c_int;
22
use object::PyObject;
33
use code::PyCodeObject;
44

5-
extern "C" {
5+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
66
pub fn PyEval_EvalCode(arg1: *mut PyCodeObject, arg2: *mut PyObject,
77
arg3: *mut PyObject) -> *mut PyObject;
88
pub fn PyEval_EvalCodeEx(co: *mut PyCodeObject, globals: *mut PyObject,

python27-sys/src/fileobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use libc::{c_char, c_int, size_t, FILE};
22
use object::*;
33

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

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

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

21-
extern "C" {
21+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
2222
pub fn PyFile_FromString(arg1: *mut c_char,
2323
arg2: *mut c_char) -> *mut PyObject;
2424
pub fn PyFile_SetBufSize(arg1: *mut PyObject, arg2: c_int);

python27-sys/src/floatobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct PyFloatObject {
1414
pub ob_fval: c_double
1515
}
1616

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

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

3232
pub const PyFloat_STR_PRECISION : c_int = 12;
3333

34-
extern "C" {
34+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
3535
pub fn PyFloat_FromString(str: *mut PyObject,
3636
pend: *mut *mut c_char)
3737
-> *mut PyObject;

python27-sys/src/frameobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub struct PyFrameObject {
5252
pub f_localsplus: [*mut PyObject; 1] /* locals+stack, dynamically sized */
5353
}
5454

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

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

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

python27-sys/src/funcobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use libc::c_int;
22
use object::*;
33

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

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

1414

15-
extern "C" {
15+
#[cfg_attr(windows, link(name="pythonXY"))] extern "C" {
1616
pub fn PyFunction_New(code: *mut PyObject, globals: *mut PyObject)
1717
-> *mut PyObject;
1818
pub fn PyFunction_GetCode(f: *mut PyObject) -> *mut PyObject;

0 commit comments

Comments
 (0)