Skip to content

Commit 12dcb7e

Browse files
committed
add example
1 parent c3682be commit 12dcb7e

File tree

6 files changed

+74
-9
lines changed

6 files changed

+74
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.pyc
22
*.pyo
3+
*.so
34
/dist
45
/build
56
/*.egg-info

README.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ setup.py
1515
1616
setup(name='hello-rust',
1717
version='1.0',
18-
rust_extensions=[RustExtension('hello_rust._hello_rust', 'extensions/Cargo.toml')],
18+
rust_extensions=[RustExtension('hello_rust._helloworld', 'extensions/Cargo.toml')],
1919
packages=['hello_rust'],
2020
# rust extensions are not zip safe, just like C-extensions.
2121
zip_safe=False
@@ -28,20 +28,20 @@ You can use same commands as for c-extensions. For example::
2828
running develop
2929
running egg_info
3030
writing hello-rust.egg-info/PKG-INFO
31-
writing top-level names to hello-rust.egg-info/top_level.txt
32-
writing dependency_links to hello-rust.egg-info/dependency_links.txt
33-
reading manifest file 'hello-rust.egg-info/SOURCES.txt'
34-
writing manifest file 'hello-rust.egg-info/SOURCES.txt'
31+
writing top-level names to hello_rust.egg-info/top_level.txt
32+
writing dependency_links to hello_rust.egg-info/dependency_links.txt
33+
reading manifest file 'hello_rust.egg-info/SOURCES.txt'
34+
writing manifest file 'hello_rust.egg-info/SOURCES.txt'
3535
running build_ext
3636
running build_rust
3737
cargo build --manifest-path extensions/Cargo.toml --features python27-sys
3838
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
3939

40-
Creating /.../lib/python2.7/site-packages/hello-rust.egg-link (link to .)
40+
Creating /.../lib/python2.7/site-packages/hello_rust.egg-link (link to .)
4141

42-
Installed hello-rust
43-
Processing dependencies for hello-rust==1.0
44-
Finished processing dependencies for hello-rust==1.0
42+
Installed hello_rust
43+
Processing dependencies for hello_rust==1.0
44+
Finished processing dependencies for hello_rust==1.0
4545

4646

4747
Or you can use commands like `bdist_wheel` or `bdist_egg`

example/extensions/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
3+
name = "hello-world"
4+
version = "0.0.1"
5+
authors = ["James Salter <[email protected]>"]
6+
7+
[features]
8+
python27-sys = ["cpython/python27-sys"]
9+
python3-sys = ["cpython/python3-sys"]
10+
11+
[dependencies.cpython]
12+
git = "https://github.com/dgrunwald/rust-cpython.git"
13+
rev = "e644f24"
14+
default-features = false
15+
16+
[lib]
17+
name = "helloworld"
18+
crate-type = ["cdylib"]

example/extensions/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![crate_type = "dylib"]
2+
3+
#[macro_use] extern crate cpython;
4+
5+
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict};
6+
7+
py_module_initializer!(_helloworld, init_helloworld, PyInit_helloworld, |py, m| {
8+
try!(m.add(py, "__doc__", "Module documentation string"));
9+
try!(m.add(py, "run", py_fn!(py, run(*args, **kwargs))));
10+
try!(m.add(py, "val", py_fn!(py, val())));
11+
Ok(())
12+
});
13+
14+
fn run(py: Python, args: &PyTuple, kwargs: Option<&PyDict>) -> PyResult<PyObject> {
15+
println!("Rust says: Hello Python!");
16+
for arg in args.iter(py) {
17+
println!("Rust got {}", arg);
18+
}
19+
if let Some(kwargs) = kwargs {
20+
for (key, val) in kwargs.items(py) {
21+
println!("{} = {}", key, val);
22+
}
23+
}
24+
Ok(py.None())
25+
}
26+
27+
fn val(_: Python) -> PyResult<i32> {
28+
Ok(42)
29+
}
30+

example/hello_rust/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from . import _helloworld
2+
3+
4+
def hello():
5+
_helloworld.run(_helloworld.val())

example/setup.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from setuptools import setup
2+
from setuptools_rust import RustExtension
3+
4+
5+
setup(name='hello-rust',
6+
version='1.0',
7+
rust_extensions=[RustExtension('hello_rust._helloworld', 'extensions/Cargo.toml')],
8+
packages=['hello_rust'],
9+
# rust extensions are not zip safe, just like C-extensions.
10+
zip_safe=False
11+
)

0 commit comments

Comments
 (0)