项目作者: novocaine

项目描述 :
Distutils helpers for rust Python extensions
高级语言: Python
项目地址: git://github.com/novocaine/rust-python-ext.git
创建时间: 2015-05-31T12:16:55Z
项目社区:https://github.com/novocaine/rust-python-ext

开源协议:MIT License

下载


rust-python-ext

Build Status

Setuptools helpers for rust Python extensions.

Compile and distribute Python extensions written in rust as easily as if they were written in C.

Well, maybe easier - it’s rust.

Example

setup.py

  1. from setuptools import setup
  2. from rust_ext import build_rust_cmdclass, install_lib_including_rust
  3. setup(name='hello-rust',
  4. version='1.0',
  5. cmdclass={
  6. # This enables 'setup.py build_rust', and makes it run
  7. # 'cargo extensions/cargo.toml' before building your package.
  8. 'build_rust': build_rust_cmdclass('extensions/cargo.toml'),
  9. # This causes your rust binary to be automatically installed
  10. # with the package when install_lib runs (including when you
  11. # run 'setup.py install'.
  12. 'install_lib': install_lib_including_rust
  13. },
  14. packages=['hello_rust'],
  15. # rust extensions are not zip safe, just like C-extensions.
  16. zip_safe=False
  17. )

You can optionally pass additional arguments to cargo through buildrustcmdclass - see
https://github.com/novocaine/rust-python-ext/blob/master/rust_ext/__init
.py.

Result:

  1. example git:(master) python setup.py install
  2. .. yada yada yada ..
  3. running build_rust
  4. cargo build --manifest-path extensions/cargo.toml --release
  5. Updating registry `https://github.com/rust-lang/crates.io-index`
  6. Updating git repository `https://github.com/alexcrichton/pkg-config-rs.git`
  7. Downloading regex v0.1.38
  8. Compiling pkg-config v0.3.5 (https://github.com/alexcrichton/pkg-config-rs.git#42f1704b)
  9. Compiling regex-syntax v0.1.2
  10. Compiling rustc-serialize v0.3.15
  11. Compiling memchr v0.1.3
  12. Compiling aho-corasick v0.2.1
  13. Compiling regex v0.1.38
  14. Compiling python27-sys v0.0.6 (file:///Users/jsalter/dev/rust-cpython/python27-sys)
  15. Compiling num v0.1.25
  16. Compiling cpython v0.0.1 (file:///Users/jsalter/dev/rust-cpython)
  17. Compiling hello-world v0.0.1 (file:///Users/jsalter/Documents/dev/rust-ext/example/extensions)
  18. .. yada yada yada ..
  19. Installed /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/hello_rust-1.0-py2.7.egg
  20. Processing dependencies for hello-rust==1.0
  21. Finished processing dependencies for hello-rust==1.0
  22. example git:(master) python
  23. Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
  24. [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
  25. Type "help", "copyright", "credits" or "license" for more information
  26. >>> import hello_rust
  27. >>> hello_rust.hello()
  28. Rust says: Hello Python!

Getting Started

Install:

  1. pip install https://github.com/novocaine/rust-python-ext/zipball/master

Note that I didn’t ever upload this extension to pypi - the version that’s
there has been put there has been uploaded by someone else and is old.

Compile the example:

  1. cd example
  2. python setup.py install
  3. python -c 'import hello_rust; hello_rust.hello()'

If you are not pretty confident with Python C extensions already, it is recommended that you base your project off the code in the example directory. This gives you a sensible layout and something that is already compiling.

Notes

  • Supports Python 2.7 and Python 3.6 on Linux and OS X (tested by travis CI)

  • Unlike distutils, rust-python-ext delegates all rust build decisions to cargo.
    So you can’t pass compiler args to the compiler from setup.py. This is by design. Cargo’s awesome - use that.
    You can however pass args to cargo which might then influence what it does.

  • If you want to access the python C API from rust, use https://github.com/dgrunwald/rust-cpython. The example dir contains a project that shows how this is done.

  • If you don’t explicitly pass ext_name to build_rust_cmdclass, your
    extensions will be be named according to your lib’s name in cargo.toml,
    with the lib prefix stripped out so that it looks like a regular Python
    module as per the c-ext convention. If you want it to start with lib or be
    named something else, pass a value to ext_name.

  • This should interop just fine with other C-exts or cython being in the package, although I haven’t tested it.
    The cmdclass approach is minimally invasive and is how, I believe, the setuptools god intends things to be. There is no monkey-patching or hacking of distutils internals.

  • As per the above, you don’t have to use the supplied cmdclass helper for install_lib if you don’t want to, it just means that install will automatically trigger build_rust.

  • You can use setup.py develop to put your module’s code on PYTHONPATH
    without installing it, as you can with other extensions. This automatically
    enables —inplace.

TODO

  • Windows
  • An example using CFFI and/or ctypes
  • clean command