Commit Graph

37 Commits

Author SHA1 Message Date
Jack S. Hale
4c7697dbe9
Add const T to docstring generation. (#3020)
* Add const T to docstring generation.

* Change order.

* See if existing test triggers for a const type.

* Add tests.

* Fix test.

* Remove experiment.

* Reformat.

* More tests, checks run.

* Adding `test_fmt_desc_` prefix to new test functions.

* Using pytest.mark.parametrize to 1. condense test; 2. exercise all functions even if one fails; 3. be less platform-specific (e.g. C++ float is not necessarily float32).

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
2021-06-08 11:56:45 -07:00
Henry Schreiner
c50f90eca6
style: use Black everywhere (#2594)
* style: use Black everywhere

* style: minor touchup from review
2020-10-16 16:38:13 -04:00
Yannick Jadoul
56784c4f42
Add unchecked_reference::operator() and operator[] to overload resolution of unchecked_mutable_reference (#2514) 2020-10-02 19:07:04 +02:00
Yannick Jadoul
9df13835c8
Stop py::array_t arguments from accepting arrays that do not match the C- or F-contiguity flags (#2484)
* Stop py::array_t arguments from accepting arrays that do not match the C- or F-contiguity flags

* Add trivially-contiguous arrays to the tests
2020-09-15 14:50:51 +02:00
Henry Schreiner
4d9024ec71
tests: cleanup and ci hardening (#2397)
* tests: refactor and cleanup

* refactor: more consistent

* tests: vendor six

* tests: more xfails, nicer system

* tests: simplify to info

* tests: suggestions from @YannickJadoul and @bstaletic

* tests: restore some pypy tests that now pass

* tests: rename info to env

* tests: strict False/True

* tests: drop explicit strict=True again

* tests: reduce minimum PyTest to 3.1
2020-08-16 16:02:12 -04:00
Yannick Jadoul
3e448c0b5e
Enable py::ellipsis on Python 2 (#2360)
* Enable py::ellipsis on Python 2

* Enable py::ellipsis tests on Python 2 and mention `Ellipsis` in the docs
2020-08-04 14:45:55 +02:00
Henry Schreiner
d8c7ee00a6
ci: GHA basic format & pre-commit (#2309) 2020-07-20 13:35:21 -04:00
Sergei Izmailov
22b2504080 Render full numpy numeric names (e.g. numpy.int32) 2020-06-10 13:36:31 +02:00
Pauli Virtanen
c9d32a81f4 numpy: fix refcount leak to dtype singleton (#1860)
PyArray_DescrFromType returns a new reference, not borrowed one
2019-07-27 11:35:32 +02:00
Eric Cousineau
e9ca89f453 numpy: Add test for explicit dtype checks. At present, int64 + uint64 do not exactly match dtype(...).num 2019-07-23 13:17:20 +02:00
Axel Huebl
000aabb2a7 Test: Numpy Scalar Creation (#1530)
I found that the numpy array tests already contained an empty-shaped
array test, but none with data in it.

Following PEP 3118, scalars have an empty shape and ndim 0. This
works already and is now also documented/covered by a test.
2019-06-11 14:00:05 +02:00
Wenzel Jakob
d4b37a284a added py::ellipsis() method for slicing of multidimensional NumPy arrays
This PR adds a new py::ellipsis() method which can be used in
conjunction with NumPy's generalized slicing support. For instance,
the following is now valid (where "a" is a NumPy array):

py::array b = a[py::make_tuple(0, py::ellipsis(), 0)];
2018-08-28 23:22:55 +02:00
Naotoshi Seo
5ef1af138d Fix SEGV to create empty shaped numpy array (#1371)
Fix a segfault when creating a 0-dimension, c-strides array.
2018-05-06 10:59:25 -03:00
Jason Rhinelander
88efb25145 Fixes for numpy 1.14.0 compatibility
- UPDATEIFCOPY is deprecated, replaced with similar (but not identical)
  WRITEBACKIFCOPY; trying to access the flag causes a deprecation
  warning under numpy 1.14, so just check the new flag there.
- Numpy `repr` formatting of floats changed in 1.14.0 to `[1., 2., 3.]`
  instead of the pre-1.14 `[ 1.,  2.,  3.]`.  Updated the tests to
  check for equality with the `repr(...)` value rather than the
  hard-coded (and now version-dependent) string representation.
2018-01-11 11:43:54 -04:00
Jason Rhinelander
391c75447d Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898.  For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.

- test_constants_and_functions: this adds more overload tests with
  overloads with different number of arguments for more comprehensive
  overload_cast testing.  The test style conversion broke the overload
  tests under MSVC 2015, prompting the additional tests while looking
  for a workaround.

- test_eigen: this dropped the unused functions `get_cm_corners` and
  `get_cm_corners_const`--these same tests were duplicates of the same
  things provided (and used) via ReturnTester methods.

- test_opaque_types: this test had a hidden dependence on ExampleMandA
  which is now fixed by using the global UserType which suffices for the
  relevant test.

- test_methods_and_attributes: this required some additions to UserType
  to make it usable as a replacement for the test's previous SimpleType:
  UserType gained a value mutator, and the `value` property is not
  mutable (it was previously readonly).  Some overload tests were also
  added to better test overload_cast (as described above).

- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
  the templated versions with an empty parameter pack expand to the same
  thing.

- test_stl: this was already mostly in the new style; this just tweaks
  things a bit, localizing a class, and adding some missing
  `// test_whatever` comments.

- test_virtual_functions: like `test_stl`, this was mostly in the new
  test style already, but needed some `// test_whatever` comments.
  This commit also moves the inherited virtual example code to the end
  of the file, after the main set of tests (since it is less important
  than the other tests, and rather length); it also got renamed to
  `test_inherited_virtuals` (from `test_inheriting_repeat`) because it
  tests both inherited virtual approaches, not just the repeat approach.
2017-08-05 18:46:22 -04:00
Cris Luengo
30d43c4992 Now shape, size, ndims and itemsize are also signed integers. 2017-05-08 01:50:21 +02:00
Cris Luengo
d400f60c96 Python buffer objects can have negative strides. 2017-05-08 01:50:21 +02:00
uentity
083a0219b5 array: implement array resize 2017-04-29 15:19:45 -04:00
Jason Rhinelander
5749b50239 array: set exception message on failure
When attempting to get a raw array pointer we return nullptr if given a
nullptr, which triggers an error_already_set(), but we haven't set an
exception message, which results in "Unknown internal error".

Callers that want explicit allowing of a nullptr here already handle it
(by clearing the exception after the call).
2017-04-13 09:53:56 -04:00
Jason Rhinelander
773339f131 array-unchecked: add runtime dimension support and array-compatible methods
The extends the previous unchecked support with the ability to
determine the dimensions at runtime.  This incurs a small performance
hit when used (versus the compile-time fixed alternative), but is still considerably
faster than the full checks on every call that happen with
`.at()`/`.mutable_at()`.
2017-03-22 16:15:56 -03:00
Jason Rhinelander
423a49b8be array: add unchecked access via proxy object
This adds bounds-unchecked access to arrays through a `a.unchecked<Type,
Dimensions>()` method.  (For `array_t<T>`, the `Type` template parameter
is omitted).  The mutable version (which requires the array have the
`writeable` flag) is available as `a.mutable_unchecked<...>()`.

Specifying the Dimensions as a template parameter allows storage of an
std::array; having the strides and sizes stored that way (as opposed to
storing a copy of the array's strides/shape pointers) allows the
compiler to make significant optimizations of the shape() method that it
can't make with a pointer; testing with nested loops of the form:

    for (size_t i0 = 0; i0 < r.shape(0); i0++)
        for (size_t i1 = 0; i1 < r.shape(1); i1++)
            ...
                r(i0, i1, ...) += 1;

over a 10 million element array gives around a 25% speedup (versus using
a pointer) for the 1D case, 33% for 2D, and runs more than twice as fast
with a 5D array.
2017-03-22 16:13:59 -03:00
Dean Moldovan
16afbcef46 Improve py::array_t scalar type information (#724)
* Add value_type member alias to py::array_t (resolve #632)

* Use numpy scalar name in py::array_t function signatures (e.g. float32/64 instead of just float)
2017-03-13 19:17:18 +01:00
Jason Rhinelander
c44fe6fda5 array_t overload resolution support
This makes array_t respect overload resolution and noconvert by failing
to load when `convert = false` if the src isn't already an array of the
correct type.
2017-03-06 14:56:22 -05:00
Jason Rhinelander
0861be05da Fix numpy tests for big endian architectures
Fixes some numpy tests failures on ppc64 in big-endian mode due to
little-endian assumptions.

Fixes #694.
2017-02-26 22:59:13 +01:00
Jason Rhinelander
2a75784420 Move requires_numpy, etc. decorators to globals
test_eigen.py and test_numpy_*.py have the same
@pytest.requires_eigen_and_numpy or @pytest.requires_numpy on every
single test; this changes them to use pytest's global `pytestmark = ...`
instead to disable the entire module when numpy and/or eigen aren't
available.
2017-02-24 23:19:50 +01:00
Jason Rhinelander
fd7517037b Change array's writeable exception to a ValueError
Numpy raises ValueError when attempting to modify an array, while
py::array is raising a RuntimeError.  This changes the exception to a
std::domain_error, which gets mapped to the expected ValueError in
python.
2017-02-24 23:19:50 +01:00
Jason Rhinelander
f86dddf7ba array: fix base handling
numpy arrays aren't currently properly setting base: by setting `->base`
directly, the base doesn't follow what numpy expects and documents (that
is, following chained array bases to the root array).

This fixes the behaviour by using numpy's PyArray_SetBaseObject to set
the base instead, and then updates the tests to reflect the fixed
behaviour.
2017-02-24 23:19:50 +01:00
Jason Rhinelander
ee2e5a5086 Make string conversion stricter (#695)
* Make string conversion stricter

The string conversion logic added in PR #624 for all std::basic_strings
was derived from the old std::wstring logic, but that was underused and
turns out to have had a bug in accepting almost anything convertible to
unicode, while the previous std::string logic was much stricter.  This
restores the previous std::string logic by only allowing actual unicode
or string types.

Fixes #685.

* Added missing 'requires numpy' decorator

(I forgot that the change to a global decorator here is in the
not-yet-merged Eigen PR)
2017-02-24 11:33:31 +01:00
Wenzel Jakob
1d1f81b278 WIP: PyPy support (#527)
This commit includes modifications that are needed to get pybind11 to work with PyPy. The full test suite compiles and runs except for a last few functions that are commented out (due to problems in PyPy that were reported on the PyPy bugtracker).

Two somewhat intrusive changes were needed to make it possible: two new tags ``py::buffer_protocol()`` and ``py::metaclass()`` must now be specified to the ``class_`` constructor if the class uses the buffer protocol and/or requires a metaclass (e.g. for static properties).

Note that this is only for the PyPy version based on Python 2.7 for now. When the PyPy 3.x has caught up in terms of cpyext compliance, a PyPy 3.x patch will follow.
2016-12-16 15:00:46 +01:00
Dean Moldovan
bad1740213 Add checks to maintain a consistent Python code style and prevent bugs (#515)
A flake8 configuration is included in setup.cfg and the checks are
executed automatically on Travis:

* Ensures a consistent PEP8 code style
* Does basic linting to prevent possible bugs
2016-11-20 21:21:54 +01:00
Dean Moldovan
4de271027d Improve consistency of array and array_t with regard to other pytypes
* `array_t(const object &)` now throws on error
* `array_t::ensure()` is intended for casters —- old constructor is
  deprecated
* `array` and `array_t` get default constructors (empty array)
* `array` gets a converting constructor
* `py::isinstance<array_T<T>>()` checks the type (but not flags)

There is only one special thing which must remain: `array_t` gets
its own `type_caster` specialization which uses `ensure` instead
of a simple check.
2016-11-17 08:55:42 +01:00
Wenzel Jakob
496feacfd0 pybind11: implicitly convert NumPy integer scalars
The current integer caster was unnecessarily strict and rejected
various kinds of NumPy integer types when calling C++ functions
expecting normal integers. This relaxes the current behavior.
2016-10-28 01:02:46 +02:00
Wenzel Jakob
fac7c09458 NumPy "base" feature: integrated feedback by @aldanor 2016-10-13 10:49:53 +02:00
Wenzel Jakob
369e9b3937 Permit creation of NumPy arrays with a "base" object that owns the data
This patch adds an extra base handle parameter to most ``py::array`` and
``py::array_t<>`` constructors. If specified along with a pointer to
data, the base object will be registered within NumPy, which increases
the base's reference count. This feature is useful to create shallow
copies of C++ or Python arrays while ensuring that the owners of the
underlying can't be garbage collected while referenced by NumPy.

The commit also adds a simple test function involving a ``wrap()``
function that creates shallow copies of various N-D arrays.
2016-10-13 01:03:40 +02:00
Wenzel Jakob
43f6aa6846 added numpy test (minor): check that 'strides' is respected even when creating new arrays
- This actually works with no changes, I just wasn't 100% convinced and
  decided to write a test to see if it's true.
2016-10-12 23:34:13 +02:00
Ivan Smirnov
aca6bcaea5 Add tests for array data access /index methods 2016-09-10 16:42:17 +01:00
Ivan Smirnov
91b3d681ad Expose some dtype/array attributes via NumPy C API 2016-09-10 16:24:00 +01:00