Commit Graph

479 Commits

Author SHA1 Message Date
David Hewitt 7b3dd68bfb Fix support for MSRV
Co-authored-by: Alex Gaynor <alex.gaynor@gmail.com>
2020-10-12 15:46:40 +01:00
kngwyu e7092fe630 Make PyCFunction more backward-compatible 2020-10-09 01:35:55 +09:00
kngwyu b42886a38a Change PyCFunction to take &'static str as a function name 2020-10-08 17:26:55 +09:00
dvermd df984ec8df
Keyword only arguments (#1209)
* allow keyword arguments without default value

* allow keyword-only arguments
2020-10-01 13:34:54 +09:00
David Hewitt 151af7a0b7
Merge pull request #1115 from davidhewitt/std-py-err
Implement std::error::Error for PyErr
2020-09-10 21:17:11 +01:00
David Hewitt b9e95dc7c9 Implement std::error::Error for PyErr 2020-09-10 19:04:24 +01:00
Sebastian Pütz 22881a3c2f Change add_function, ensure static docstrings.
Change add_function to take `&PyCFunction` instead of a wrapper
fn and ensure that dostrings of functions are `&'static str`.
2020-09-09 12:37:37 +02:00
Sebastian Pütz be877d133f Add constructor for PyCFunction. 2020-09-08 14:26:08 +02:00
Sebastian Pütz 2e8010b5df Add native Function native types.
Add bindings for PyCFunction, PyFunction, PyClassMethod and
PyStaticMethod.
2020-09-08 09:51:31 +02:00
Sebastian Pütz 64b06ea9ec Change `add_submodule()` to take `&PyModule`.
The C-exported wrapper generated through `#[pymodule]` is only
required for the top-level module.
2020-09-05 15:54:57 +02:00
Sebastian Pütz e65b849ab6 Doc fixes, changelog and rename. 2020-09-05 10:20:22 +02:00
Sebastian Pütz 795c054511 Possible to pass PyModule as first arg.
This commit makes it possible to access the module of a function
by passing the `need_module` argument to the pyfn and pyfunction
macros.
2020-09-04 09:01:21 +02:00
Sebastian Pütz 3214249010 Make python function wrapper creation fallible.
Wrapping a function can fail if we can't get the module name.

Based on suggestion by @kngwyu
2020-09-03 15:48:32 +02:00
Sebastian Pütz 5bbca1a052 Set the module of `#[pyfunction]`s.
Previously neither the module nor the name of the module of
pyfunctions were registered. This commit passes the module and
its name when creating a new pyfunction.

PyModule::add_function and PyModule::add_module have been added and are
set to replace `add_wrapped` in a future release. `add_wrapped` is kept
for compatibility reasons during the transition.

Depending on whether a `PyModule` or `Python` is the argument for the
Python function-wrapper, the module will be registered with the function.
2020-09-03 14:49:28 +02:00
Yuji Kanagawa 729f2f42e7
Merge pull request #1065 from sebpuetz/union
FromPyObject derivation for structs and enums
2020-08-31 21:28:49 +09:00
Sebastian Pütz 0f32f886b8 More FromPyObject derive suggestions by @davidhewitt 2020-08-30 19:16:59 +02:00
David Hewitt 82cb815afa Use IntoPy<PyObject> for PyModule::add 2020-08-30 16:24:20 +01:00
Sebastian Pütz 7a9f4a1633 FromPyObject derive suggestions by @kngwyu 2020-08-30 15:33:50 +02:00
Sebastian Pütz a8c5379eff Add compile fail tests for FromPyObject derives + some fixes.
Fix some error messages and accidental passes.
2020-08-30 12:54:13 +02:00
Sebastian Pütz 7781bb78de Specify item key and attr name as arguments. 2020-08-30 10:22:01 +02:00
Sebastian Pütz 60fe4925f5 '#[derive(FromPyObject)]` changes suggested by @davidwhewitt. 2020-08-30 10:22:01 +02:00
Sebastian Pütz 7168309464 Derive FromPyObject 2020-08-30 10:22:01 +02:00
David Hewitt 42296ea5c2 Update UI tests for Rust 1.46 2020-08-29 09:22:00 +01:00
Mario 608aea726c
Allow other Result types in `#[pyfunction]` (#1118)
* Added a couple basic tests

* Implemented suggested change

* Fixed type inference

* cargo fmt

* Finished tests and removed warnings

* Include in CHANGELOG.md

* Moved test into separate file

* &'static str and function rename

* Mention in the book
2020-08-29 08:25:20 +01:00
kngwyu 8cbfdd52b8 Simplify test cases where both __*__ and __r*__ are defined 2020-08-20 14:53:47 +09:00
kngwyu 71a7a76227 More tests for RHS 2020-08-20 14:33:47 +09:00
kngwyu f086f48499 Left-hand operands are fellback to RH ones for type mismatching 2020-08-20 14:33:47 +09:00
David Hewitt 565e36d989
Merge pull request #1098 from davidhewitt/py-as-into-ref
Py::as_ref and Py::into_ref (remove AsPyRef)
2020-08-11 20:44:53 +01:00
Yuji Kanagawa c4d9ab227e
Merge branch 'master' into iterator-example 2020-08-10 17:11:00 +09:00
kngwyu 093dda375f Improve lifetime insertions for #[pyproto] 2020-08-10 15:18:16 +09:00
David Hewitt 77ed6d6d69 Remove AsPyRef for just Py::as_ref 2020-08-09 22:47:54 +01:00
David Hewitt ffd6e562ab
Merge pull request #1090 from davidhewitt/unsendable-test-tidy
Change unsendable test to use Rust thread
2020-08-08 18:21:38 +01:00
David Hewitt ad76a8a5ce Change unsendable test to use Rust thread 2020-08-08 17:45:41 +01:00
David Hewitt e72b682038 Remove redundant iter test 2020-08-07 13:49:52 +01:00
David Hewitt 1f37dbc1a7 Various fixes to edge cases with GILGuard 2020-08-06 14:32:59 +01:00
Manuel Vázquez Acosta f2ba3e6da7
#1064: Comparisons with __eq__ should not raise TypeError (#1072)
* Add (failing) tests for issue #1064

* Return NotImplemented when richcmp doesn't match the expected type.

* Fix tests that expect TypeError when richcmp returns NotImplemented.

- The python code 'class Other: pass; c2 {} Other()' was raising a NameError:
  c2 not found

- eq and ne never raise a TypeError, so I split the those cases.

* Return NotImplemented for number-like binary operations.

* Add dummy impl PyNumberProtocol for the test struct.

* Rework tests of NotImplemented.

* Make py_ternary_num_func return NotImplemented when type mismatches.

* Return NotImplement for type mismatches in binary inplace operators.

* Reduce boilerplate with `extract_or_return_not_implemented!`

* Extract common definition 'Other' into a function.

* Test explicitly for NotImplemented in the __ipow__ test.

* Add entry in CHANGELOG for PR #1072.

* Add the section 'Emulating numeric types' to the guide.

* Ensure we're returning NotImplemented in tests.

* Simplify the tests: only test we return NotImplemented.

Our previous test were rather indirect: were relying that Python
behaves correctly when we return NotImplemented.

Now we only test that calling a pyclass dunder method returns NotImplemented
when the argument doesn't match the type signature.  This is the expected
behavior.

* Remove reverse operators in tests of NotImplemented

The won't be used because of #844.

* Apply suggestions from code review

Co-authored-by: Yuji Kanagawa <yuji.kngw.80s.revive@gmail.com>

* Add a note about #844 below the list of reflected operations.

Co-authored-by: Yuji Kanagawa <yuji.kngw.80s.revive@gmail.com>
2020-08-05 22:53:16 +09:00
kngwyu f5f2e84f4b Enable &Self in #[pymethods] again 2020-07-28 20:55:38 +09:00
Yuji Kanagawa 4563e00a67 Add testcase for unsendable, dict, weakref pyclass. 2020-07-21 10:42:36 +02:00
Sebastian Puetz e75f768ea8 Move the ThreadChecker field in front of dict and weakref.
Offsets for dict and weakref are calculated from the end of the
PyCell struct. When using the non-dummy ThreadChecker, the offsets
were invalid since the `ThreadCheckerImpl` is not zero-sized.
2020-07-20 19:37:38 +02:00
David Hewitt 4ed9748b45 Rename exceptions to PyException etc; reintroduce deprecated ones 2020-07-18 06:02:57 +01:00
David Hewitt a7e0c6bfa7 Make exceptions proper native types. 2020-07-18 01:57:39 +01:00
kngwyu 3ae1caa55a Skip panic_unsendable test if -Cpanic=abort is given 2020-06-30 17:58:31 +09:00
kngwyu d76fe7835a Introduce #[pyclass(unsendable)] 2020-06-30 12:30:17 +09:00
David Hewitt c3e993e5a6 Apply suggestions from code review
Co-authored-by: Yuji Kanagawa <yuji.kngw.80s.revive@gmail.com>
2020-06-27 14:56:15 +01:00
David Hewitt e140b729fc Allow #[getter] and #[setter] functions to take PyRef 2020-06-27 14:55:22 +01:00
kngwyu a78a832431 Refactor around the nightly feature 2020-06-26 19:29:37 +09:00
scalexm f49478619f Re-enable recursive class attributes
Use some kind of two-stage initialization as described in #975, by
being very cautious about when to allow the GIL to be released.
2020-06-23 22:40:29 +02:00
David Hewitt a9c7e12be0 Allow skipping the return type completely for `#[pyproto]` methods returning `()`. 2020-06-23 11:51:02 +01:00
David Hewitt c7a4b4770f Refactor `#[pyproto]` Result types 2020-06-23 11:08:36 +01:00
kngwyu b70ee9a5ad Use subclass correctly in tp_new 2020-06-22 01:38:13 +09:00