Commit Graph

262 Commits

Author SHA1 Message Date
Daniel Grunwald 04081bc8de __contains__: if extraction fails with TypeError, return False instead. 2016-05-07 23:31:46 +02:00
Daniel Grunwald 9eafaebfa2 py_class!: add __contains__ and __reversed__ 2016-05-07 23:19:26 +02:00
Daniel Grunwald 5886720c90 py_class!: add __setitem__ and __delitem__ 2016-05-07 23:01:45 +02:00
Daniel Grunwald 806f212dc4 py_class!: Make the body to be parsed the first argument. 2016-05-07 20:29:29 +02:00
Daniel Grunwald 20c2dcdba7 Simplify py_class_impl.py 2016-05-07 19:49:07 +02:00
Daniel Grunwald 3de859373c py_class!: add __getitem__ 2016-05-07 00:04:18 +02:00
Daniel Grunwald 713a5c53d3 py_class!: add __length_hint__, and map __len__ to both sq_length and mp_length. 2016-05-06 23:16:06 +02:00
Daniel Grunwald 318d9a9ef3 py_class!: add __call__ slot 2016-05-06 22:32:46 +02:00
Daniel Grunwald 78f673893d Make CallbackConverter output type an associated type. 2016-05-06 22:11:56 +02:00
Daniel Grunwald d21143fa39 py_class!: add __bool__ slot 2016-05-06 22:05:12 +02:00
Daniel Grunwald c06838a38e py_class!: add `__hash__` slot 2016-05-06 21:41:05 +02:00
Daniel Grunwald 933e0ed11d Add documentation and tests for string conversion special methods. 2016-05-06 21:24:32 +02:00
Daniel Grunwald 3d99a4ac3c Fix build with stable rust. 2016-05-05 07:48:29 +02:00
Daniel Grunwald 9ab3e9ea4e char::REPLACEMENT_CHARACTER isn't stable yet 2016-05-05 07:42:04 +02:00
Daniel Grunwald be0c1573aa py_class!: add support for __str__, __repr__, __unicode__ and __format__ 2016-05-05 07:31:14 +02:00
Daniel Grunwald b37fa220df On Python 2.7, PyString now corresponds to `basestring` instead of `str`.
`<str as ToPyObject>::ObjectType` now always is `PyString`
  (previously the type differed between Python 2.7 and 3.x)

`PyStringData` allows zero-copy access to Python's underlying string
representation.
2016-05-05 07:26:52 +02:00
Daniel Grunwald d68e664327 Add support for __iter__ and __next__ slots. 2016-04-30 23:41:18 +02:00
Daniel Grunwald 0445560241 Fix not(nightly) build 2016-04-17 23:42:23 +02:00
Daniel Grunwald 9aec3f33f1 We only need num-traits, not the full num crate. 2016-04-17 23:35:17 +02:00
Daniel Grunwald fffcf57834 Add support for __len__ special method. 2016-04-17 23:26:33 +02:00
Daniel Grunwald a14e3d2b77 Share macro code between tp_new and other class methods. 2016-04-17 18:04:58 +02:00
Daniel Grunwald fb5e4a1521 Generate repetive portions of py_class_impl! macro 2016-04-17 15:53:48 +02:00
Daniel Grunwald b721868c25 Document pitfalls with __traverse__ and RefCell. 2016-04-17 14:03:51 +02:00
Daniel Grunwald 4a01ed31fb Add documentation for __traverse__ and __clear__ 2016-04-14 19:44:08 +02:00
Daniel Grunwald cd88d28973 Add __clear__() special method.
Together with __traverse__, this can be used integrate Python classes
defined in Rust with the Python garbage collector.
2016-04-14 18:41:29 +02:00
Daniel Grunwald 2e977a9c21 gc integration: __traverse__ method 2016-04-14 18:41:29 +02:00
Daniel Grunwald 271a44475d panic::recover -> panic::catch_unwind 2016-04-14 18:41:29 +02:00
Daniel Grunwald cdf2dc8ff7 py_class!: add support for static class members
Fix bug that caused macro expansion to fail when a class had more than
one member.
2016-03-18 21:39:11 +01:00
Daniel Grunwald 3826b54a30 py_class!: Add support for class methods. 2016-03-16 22:18:48 +01:00
Daniel Grunwald f89f957dd4 Add support for @staticmethod. 2016-03-16 21:49:45 +01:00
Daniel Grunwald 80a4ad1466 Fix bug in py_class! when declaring an instance method with parameter list 2016-03-16 21:36:18 +01:00
Daniel Grunwald e608d5eabe Fix build with stable Rust. 2016-03-12 20:20:41 +01:00
Daniel Grunwald b43fa55e01 Delete old 'rustobject' code 2016-03-12 19:09:10 +01:00
Daniel Grunwald 337a90c2fb Reserve special method names
Some of these might work via the normal "instance method" functionality,
but we'll need testcases to be sure.
2016-03-12 19:08:51 +01:00
Daniel Grunwald 0bd9e3ebd2 Adjust extension test cases. 2016-03-12 18:14:21 +01:00
Daniel Grunwald cc81a01077 py_class!: add support for instance methods 2016-03-12 17:58:50 +01:00
Daniel Grunwald ab83b4f4a1 #14; Convert Rust panic to Python SystemError 2016-03-12 01:31:06 +01:00
Daniel Grunwald 309182cfe8 py_class!: Add support for "def __new__(cls)". 2016-03-12 00:06:39 +01:00
Daniel Grunwald 547879ef0f Remove rustobject (PyRustObject, TypeBuilder).
Add new py_class!() macro that generates a static type.
2016-03-09 01:07:50 +01:00
Daniel Grunwald 9b278ba56b Implement second form of py_fn!() (inline function definition) 2016-03-07 23:48:44 +01:00
Daniel Grunwald 3af6e17841 WIP: big macro refactoring
* Introduce py_argparse_parse_plist!() to allow a more flexible
  param list syntax.
* py_fn!() syntax changed
* Remove py_method!() / py_class_method!() macros.
  These are of limited use when building dynamic types,
  and not useful for py_class!() static types.
2016-03-07 23:22:44 +01:00
Daniel Grunwald be682d848f WIP: generalize py_argparse!() macro 2016-03-06 13:33:57 +01:00
Daniel Grunwald 162e20d9e4 Fix build with rust-stable: we can't mark functions as public if we
nest the impl block in fn init().
2016-03-06 11:02:48 +01:00
Daniel Grunwald 96ee652737 TypeBuilder: take &mut self instead of by-value self
py_class!() now can be used to define simple types.
2016-03-06 10:29:28 +01:00
Daniel Grunwald 061e46bde8 Rename PyRustTypeBuilder -> TypeBuilder 2016-03-06 06:33:16 +01:00
Daniel Grunwald acc5712536 Don't flatten the rustobject module + started implementing the py_class!() macro. 2016-03-06 06:29:43 +01:00
Daniel Grunwald d705620502 Add support for Rust stable.
All functions that depend on PyObject having the same memory layout
as *mut ffi::PyObject should now be hidden behind #[cfg(feature="nightly")].
2016-03-06 01:08:40 +01:00
Daniel Grunwald ad7f43efc7 Remove #![feature(optin_builtin_traits)] usage 2016-03-06 00:22:16 +01:00
Daniel Grunwald 7c90651157 Make use of nightly const_fn + shared features optional. 2016-03-06 00:03:17 +01:00
Daniel Grunwald 53353d374b Remove dependency on interpolate_idents.
We now use the generic <DUMMY> hack to avoid duplicate extern "C" symbols.
See rust-lang/rust#26201.

py_module_initializer!() calls now need to manually concatenate the module
name with the prefixes "init" and "PyInit_".
2016-03-05 23:20:53 +01:00
Daniel Grunwald a4f946096c Add back accidentally removed #[macro_use] 2016-03-05 17:44:30 +01:00
Daniel Grunwald 067f262876 Some documentation updates 2016-03-05 17:41:04 +01:00
Daniel Grunwald 5223cf4730 Fix build 2016-03-05 15:57:48 +01:00
Daniel Grunwald e5f77018f1 Allow using py_fn!() as argument to PyRustTypeBuilder::set_new() 2016-03-05 15:16:11 +01:00
Daniel Grunwald d22fbb31ce Refactor py_fn / py_method / py_class_method macros 2016-03-05 15:05:41 +01:00
Daniel Grunwald c01a1e8306 Use std::ptr::Shared in PyObject to enable the null pointer optimization. 2016-03-05 02:01:03 +01:00
Daniel Grunwald b617af7c5b Add PyTypeBuilder::set_new(). 2016-03-05 01:16:54 +01:00
Daniel Grunwald 249be0da3a Use #[cfg] on expressions to avoid some code duplication 2016-03-05 00:13:38 +01:00
Daniel Grunwald 109c704ad7 Fix "reexport of private crate" warning. 2016-03-04 21:35:52 +01:00
Joshua Miller 7092bd4362 remove duplicate word in docstring 2016-01-28 21:13:39 -06:00
Daniel Grunwald 168a060f4b Make PreparedString public. 2015-12-20 13:59:03 +01:00
Daniel Grunwald c1682b68ac Add some tests for py_fn!() 2015-11-07 16:52:20 +01:00
Daniel Grunwald 32086a0ef5 Add back PyIterator, PyListIterator etc. 2015-10-29 02:36:50 +01:00
Daniel Grunwald 0a270a0583 Put Python argument at beginning of argument list.
Closes #33.
2015-10-26 23:52:18 +01:00
Daniel Grunwald 44611991c3 Remove 'p lifetime from PyObject (#15)
Since the `Python` token no longer is a part of `PyObject`,
lots of methods now require the token as additional argument.

This [breaking-change] breaks everything!
2015-10-25 17:55:29 +01:00
Daniel Grunwald 2b7d28f7fd Fix build with python 2.7 2015-09-20 19:43:24 +02:00
Daniel Grunwald 5bab92cbcc Add back PyTuple::as_slice() 2015-09-20 19:40:18 +02:00
Daniel Grunwald 080ca17621 Use concrete return types for str() and repr() 2015-09-20 19:39:59 +02:00
Daniel Grunwald b21a23b304 In python 3, use PyUnicode_AsUTF8AndSize for extracting UTF-8 from unicode objects. 2015-09-20 16:21:00 +02:00
Daniel Grunwald 65395a11b1 Merge branch 'python-eval' of https://github.com/murarth/rust-cpython 2015-09-19 16:14:42 +02:00
Daniel Grunwald d87a4596ef Dict tests.
(squashed pull request #31 without PyMapping)
2015-09-18 21:47:10 +02:00
Daniel Grunwald d00d59959a PySequence: change some more return types to PyResult<'p, PyObject<'p>> 2015-09-13 19:37:49 +02:00
Ewan Higgs 65967c2401 Use err::error_on_minusone to clean the code up a bit. 2015-09-08 12:54:38 +02:00
Ewan Higgs dbcdbedc59 Fix up get_item so it returns PyResult (w/o lifetime elision).
Make concat and repeate return PyObject. Also uncomment tests of concat and
repeat.
2015-09-07 23:03:22 +02:00
Ewan Higgs 809e9ab227 Use signed size to allow negative indexing. Add more tests. 2015-09-04 00:52:16 +02:00
Ewan Higgs f90d427454 Tidy up docs, make length and size the same. Test del_item. 2015-08-26 21:45:46 +02:00
Ewan Higgs a63aa43536 Use cast_into instead of from_object. More PySequence tests. 2015-08-26 21:13:13 +02:00
Ewan Higgs 636cfee28d Add tests for sequence. Make list and tuple use private PyObject again.
Add from_object that converts from PyObject into PyList and PyTuple.
Maybe it should be a macro as part of the newtype thing?
2015-08-26 21:13:13 +02:00
Ewan Higgs ff69760b99 Make ctors public for various object types. 2015-08-26 21:13:13 +02:00
Daniel Grunwald 160b23eff0 Remove unnecessary PyList -> PyObject -> PyList roundtrip 2015-08-26 20:19:58 +02:00
Ewan Higgs 173a6147f4 Add tests for list.rs. Still missing tests for extract. 2015-08-26 15:49:43 +02:00
Daniel Grunwald fa5c866cd6 Fix compiler error
Closes #27
2015-08-21 21:27:11 +02:00
Daniel Grunwald 6316ddc28b Fix some build errors 2015-08-20 21:14:47 +02:00
Daniel Grunwald a23b5b5910 Add parameter extraction support to `py_fn!` and `py_method!`.
These macros now support specifying an argument list:
`py_fn!(myfn(myarg: i32))`
will expect `myfn` to be a function with the signature:
`fn myfn<'p>(py: Python<'p>, myarg: i32) -> PyResult<'p, _>`

It can called from python as `myfn(1)`
or using keyword arguments: `myfn(myarg=1)`.

If no parameter list is specified (`py_fn!(myfn)`), the expected signature
now includes the keyword arguments:
`fn run<'p>(py: Python<'p>, args: &PyTuple<'p>, kwargs: Option<&PyDict<'p>>)`

Due to the additional `kwargs` argument, this is a [breaking-change].
2015-08-03 00:06:15 +02:00
Murarth 376ce21f2d Implement `eval` and `run` to execute Python code 2015-08-02 14:30:14 -07:00
Daniel Grunwald 072f4d24eb Initial version of `argparse!` 2015-08-02 21:56:03 +02:00
Murarth 74cdb841c6 Fix function and method name fields 2015-08-02 09:32:52 -07:00
gentoo90 a785ba87a9 Add an example of calling a python function from rust 2015-07-27 21:56:59 +03:00
Daniel Grunwald a884ee945c Change PyDict::items() to return `Vec<(PyObject, PyObject)>` instead of `PyList`.
This makes it possible iterate through a PyDict in Rust without
having to unpack tuples.

The PyList version of the method is renamed to `items_list()`.

[breaking-change]
2015-07-23 22:21:11 +02:00
Daniel Grunwald 63d5f2cd1a Rename PyErr::new() to PyErr::from_instance() and add use the name PyErr::new() for a more useful function.
Allows simplifying the common
`PyErr::new_lazy_init(py.get_type::<exc::ValueError>(), Some(msg.to_py_object(py).into_object()));`
to
`PyErr:🆕:<exc::ValueError, _>(py, msg)`

[breaking-change]
2015-07-23 22:21:11 +02:00
Daniel Grunwald 45d03bf8bb Replace FromPyObject with the more powerful ExtractPyObject.
This is a [breaking-change] and makes the trait more difficult to implement.
The usage through PyObject::extract() is unchanged.

This change allows extracting a `&str` through a temporary `Cow<str>`
without having to copy the string data from python to rust
(at least in cases where the python string is UTF-8 encoded).

This is preparation in hope I'll be able to make py_fn!()
automatically extract the function arguments.
2015-07-23 22:09:20 +02:00
Daniel Grunwald 1ad27b977e Merge pull request #20 from traff/master
Implement ToPyObject for HashMap->dict conversion.
2015-07-18 17:51:08 +02:00
Daniel Grunwald aac888b1b1 Fix python 2.7 build 2015-07-18 13:44:39 +02:00
Daniel Grunwald 1c4d71d712 Implement ToPyObject for all PythonObjects. 2015-07-18 13:40:26 +02:00
Dmitry Trofimov 2cfe522157 -Remove unsafe code;
-Panic if can't add item to dict;
-Test added;
2015-07-17 23:47:30 +02:00
Dmitry Trofimov 144d14f8c4 Implement ToPyObject for HashMap->dict conversion. 2015-07-17 22:07:37 +02:00
Daniel Grunwald f6718a271b Merge pull request #18 from traff/master
A method for accessing dictionary items.
2015-07-13 20:30:08 +02:00
Dmitry Trofimov d495eaaa4a Use err::cast_from_owned_ptr_or_panic, because PyDict_Items creates a new reference to list and can fail. 2015-07-09 00:51:12 +02:00
Daniel Grunwald 04ea39d26f Adjust to breaking change rust-lang/rust#26728 2015-07-08 22:47:56 +02:00
Dmitry Trofimov 79abfb99e8 A method for accessing dictionary items. 2015-07-08 01:26:50 +02:00
Daniel Grunwald cdca6f04e8 Fix python 3 build on windows. 2015-06-28 21:45:53 +02:00
Daniel Grunwald 76e38e1a54 Add python 3 support to PyRustTypeBuilder. 2015-06-28 04:12:51 +02:00
Daniel Grunwald 9f2244bbcf Remove unused imports and variables. 2015-06-27 23:49:53 +02:00
Daniel Grunwald 3b02ef5b99 Improve documentation. 2015-06-27 22:48:40 +02:00
Daniel Grunwald 6b496b4772 Prefer taking generic arguments by value. 2015-06-27 22:48:40 +02:00
Daniel Grunwald 1423162ad4 Call PyEval_InitThreads() in module initializer to ensure it's safe
to spawn rust threads in the extension module and acquire the GIL from
those threads.
2015-06-27 22:48:40 +02:00
Daniel Grunwald 280cdab7a2 Remove ToPythonPointer from public API 2015-06-27 22:48:40 +02:00
Daniel Grunwald e6b5312c81 mem::min_align_of -> mem::align_of
See rust#25646
2015-06-27 15:42:10 +02:00
Daniel Grunwald 5414cfb89c Add py_class_method!() 2015-06-26 03:31:35 +02:00
Daniel Grunwald 4980053e95 Add py_method!() macro for creating method descriptors. 2015-06-25 23:58:57 +02:00
Daniel Grunwald 5c74f55862 Change `py_func!(py, f)` to `py_fn(f)`. 2015-06-25 00:02:56 +02:00
Daniel Grunwald 6e1fc35177 Split rustobject module. 2015-06-22 00:35:01 +02:00
Daniel Grunwald ba0643c11c Add support for creating instances of PyRustObjects that involve inheritance.
Prevent panics from unwinding into C code.
2015-06-20 16:02:09 +02:00
Daniel Grunwald 7c9b96ee90 PyRustObject: implement tp_dealloc by dropping the rust object 2015-06-20 00:30:40 +02:00
Daniel Grunwald fa32b52392 Remove unnecessary std::mem::copy_lifetime call. 2015-06-19 22:47:21 +02:00
Daniel Grunwald 2459d8f0c8 Fix #[cfg] condition for i64 on windows 2015-05-29 10:26:28 +02:00
Daniel Grunwald 73e4ce3f8b #11: Allow defining python extension types
Highly incomplete initial version.
2015-05-27 21:19:32 +02:00
Yoichi Imai 92a30e2c33 Added tests for i64. 2015-05-27 00:17:42 +09:00
Yoichi Imai e237bdd57f Converting function for i64 on 32bit/non-windows platform. 2015-05-26 23:48:01 +09:00
Yoichi Imai 8387932793 Generalize pylong_as_u64. 2015-05-26 23:44:29 +09:00
Daniel Grunwald 0b712d0679 Add methods for PyDict 2015-05-24 22:45:43 +02:00
Daniel Grunwald 8c8779e3fb py_module_initializer! for python 3 2015-05-24 20:06:08 +02:00
Daniel Grunwald db47455904 Add IntoIterator impl for PyTuple and PyList. 2015-05-24 18:20:35 +02:00
Daniel Grunwald d70479d257 Remove lifetime 's from `FromPyObject`.
This allows implementing `FromPyObject` for `Vec<T>`.
It also means that `obj.extract::<Cow<str>>()` won't be supported, but we
have `PyString::extract()` for that now.
2015-05-24 17:12:12 +02:00
Daniel Grunwald a106cabc4a Initial support for python 3. 2015-05-23 23:52:38 +02:00
James Salter b1eca56ec3 Python 3 build support
* Add python3-sys to rust-cpython as an optional feature, and
  make python27-sys also optional, but still the default
* Parametrise python27-sys/build.rs so that it is python
  version independent, and clone it into python3-sys/build.rs.
  Hopefully this can continue to be maintained as an identical
  file.
* python27-sys and python3-sys gain features for explicitly
  selecting a python version to link to. for python27-sys,
  there's currently only python27; for python3-sys there's
  python 3.4 and 3.5.
* explicitly tell travis to use nightlies (seems to have
  started trying to use 1.0.0)
2015-05-23 15:23:08 +01:00
Daniel Grunwald d4bdcc5cbd Fix warning: unnecessary 'unsafe' block 2015-05-17 21:43:12 +02:00
Daniel Grunwald 6a0f951e94 Use try!() instead of unwrap() 2015-05-17 21:42:34 +02:00
James Salter b3e20e900b fix issues in bb13ec, support utf16, python fom PATH
* fix ucs4 build broken by bb13ec
* add utf16 decoding to unicode.from_py_object for
  narrow unicode builds
* change unicode narrow/wide cfg flag to be
  Py_UNICODE_SIZE_4 not Py_UNICODE_WIDE, which doesn't
  appear in sysconfig
* support framework builds on os x
* python27-sys exports compilation flags as cargo vars,
  and rust-python resurrects them as cfg flags
* travis runs against local python27-sys
* rust-cpython depends on git python27-sys, because
  the one on cargo is now incompatible with it (since bb13ec)
2015-05-17 13:20:58 +01:00
James Salter c1acf04e0e Fix refs to std::num::cast -> num::traits::cast
This function was removed from the rust stdlib in
https://github.com/rust-lang/rust/commit/eeb948.

Updated to refer to the 'num' crate instead.
2015-05-09 17:12:26 +01:00
Daniel Grunwald c07b0468e8 MIT license headers 2015-04-19 05:22:03 +02:00
Daniel Grunwald b3fe39c3c4 call() 2015-04-19 05:01:19 +02:00
Daniel Grunwald fbd60f11f4 Simplify ToPyObject: directly return the object without PyResult
to_py_object should always succeed (modulo out-of-memory)
2015-04-19 01:07:14 +02:00
Daniel Grunwald 1e5605036e Update documentation 2015-04-19 00:39:04 +02:00
Daniel Grunwald 5d3d03bcac Remove dead code 2015-04-18 22:25:03 +02:00
Daniel Grunwald 5a303789b9 Don't export cstr! macro 2015-04-18 22:20:19 +02:00
Daniel Grunwald f8e24a870c Add support for exposing rust functions to python 2015-04-18 20:17:25 +02:00
Daniel Grunwald 60360fa03f Fix py_module_initializer 2015-04-18 01:52:48 +02:00
Daniel Grunwald 2a91b00abe Fix segfault due to POST_DROP_USIZE and update to latest nightly 2015-04-17 22:13:15 +02:00
Daniel Grunwald dd20e00c77 Update for rust beta. 2015-03-09 14:31:20 +01:00
Daniel Grunwald 913f3ea2e5 Adjust to rust nightly 2015-03-08 15:29:44 +01:00
Daniel Grunwald f423acee6d Update to current rust nightly 2015-01-12 03:17:29 +01:00
Daniel Grunwald 5566faf235 py_module_initializer macro 2015-01-12 03:00:34 +01:00
Daniel Grunwald 55ea6cc85b Add PyList, PyInt, PyLong and PyFloat. 2015-01-11 18:56:59 +01:00
Daniel Grunwald 284f7a7931 Fix build 2015-01-11 05:52:11 +01:00
Daniel Grunwald 6d9fd93df2 Go back to 1.0-alpha due to rust issue 21510 2015-01-11 05:41:55 +01:00
Daniel Grunwald d8a1fcd869 update for rust nightly 2015-01-11 04:21:05 +01:00
Daniel Grunwald 706d7c1b78 Port to new rust nighly and add CStr. 2015-01-08 09:20:05 +01:00
Daniel Grunwald fd2ae9ce60 delete pyptr 2015-01-08 09:19:35 +01:00
Daniel Grunwald b3be3d35de Change API for owned python pointers:
replace PyPtr<'p, PyObject<'p>> with PyObject<'p>

Borrowed python pointers stay &PyObject<'p>.
Note: this means the internal representation of borrowed python pointers
changes from "*mut PyObject" to "&*mut PyObject".
2015-01-07 01:40:48 +01:00