Skip to content

Commit b710d39

Browse files
committed
Python2 support
1 parent 733083c commit b710d39

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ num-complex = "0.2"
1616
num-traits = "0.2.6"
1717
ndarray = "0.12"
1818
pyo3 = "0.5.0-alpha.2"
19+
20+
[features]
21+
# In default setting, python version is automatically detected
22+
default = []
23+
# Use this feature when building python2 binding.
24+
python2 = ["pyo3/python2"]
25+
# Use this feature when building python3 binding.
26+
python3 = ["pyo3/python3"]
27+

examples/simple-extension/setup.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ def run(self):
1414
raise SystemExit(errno)
1515

1616

17+
def get_cfg_flags():
18+
version = sys.version_info[0:2]
19+
if version[0] == 2:
20+
return ['--cfg=Py_2']
21+
else:
22+
return ['--cfg=Py_3']
23+
1724
setup_requires = ['setuptools-rust>=0.6.0']
1825
install_requires = ['numpy']
1926
test_requires = install_requires + ['pytest']
@@ -22,7 +29,9 @@ def run(self):
2229
name='rust_ext',
2330
version='0.1.0',
2431
description='Example of python-extension using rust-numpy',
25-
rust_extensions=[RustExtension('rust_ext.rust_ext', './Cargo.toml')],
32+
rust_extensions=[RustExtension(
33+
'rust_ext.rust_ext', './Cargo.toml', rustc_flags=get_cfg_flags()
34+
)],
2635
install_requires=install_requires,
2736
setup_requires=setup_requires,
2837
test_requires=test_requires,

examples/simple-extension/tests/test_ext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def test_axpy():
77
y = np.array([3.0, 3.0, 3.0])
88
z = axpy(3.0, x, y)
99
np.testing.assert_array_almost_equal(z, np.array([6.0, 9.0, 12.0]))
10-
x = np.array([*x, 4.0])
11-
y = np.array([*y, 3.0])
10+
x = np.array([1.0, 2.0, 3.0, 4.0])
11+
y = np.array([3.0, 3.0, 3.0, 3.0])
1212
z = axpy(3.0, x, y)
1313
np.testing.assert_array_almost_equal(z, np.array([6.0, 9.0, 12.0, 15.0]))
1414

examples/simple-extension/tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[tox]
2-
envlist = py35,
2+
envlist = py27,
3+
py35,
34
py36,
45
py37,
56
minversion = 2.9.0

src/npyffi/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@
1212
use pyo3::ffi;
1313
use std::ffi::CString;
1414
use std::os::raw::c_void;
15-
use std::ptr::null_mut;
1615

1716
fn get_numpy_api(module: &str, capsule: &str) -> *const *const c_void {
1817
let module = CString::new(module).unwrap();
1918
let capsule = CString::new(capsule).unwrap();
19+
#[cfg(not(Py_3))]
20+
unsafe fn get_capsule(capsule: *mut ffi::PyObject) -> *const *const c_void {
21+
ffi::PyCObject_AsVoidPtr(capsule) as *const *const c_void
22+
}
23+
#[cfg(Py_3)]
24+
unsafe fn get_capsule(capsule: *mut ffi::PyObject) -> *const *const c_void {
25+
use std::ptr::null_mut;
26+
ffi::PyCapsule_GetPointer(capsule, null_mut()) as *const *const c_void
27+
}
2028
unsafe {
2129
assert_ne!(
2230
ffi::Py_IsInitialized(),
@@ -27,7 +35,8 @@ Please make sure that you get gil, by `let gil = Python::acquire_gil();`"
2735
let numpy = ffi::PyImport_ImportModule(module.as_ptr());
2836
assert!(!numpy.is_null(), "Failed to import numpy module");
2937
let capsule = ffi::PyObject_GetAttrString(numpy as *mut ffi::PyObject, capsule.as_ptr());
30-
ffi::PyCapsule_GetPointer(capsule, null_mut()) as *const *const c_void
38+
assert!(!capsule.is_null(), "Failed to import numpy module");
39+
get_capsule(capsule)
3140
}
3241
}
3342

0 commit comments

Comments
 (0)