1. 25 May, 2017 7 commits
    • Bruce Merry's avatar
      Fix invalid memory access in vector insert method · eee4f4fc
      Bruce Merry authored
      The stl_bind.h wrapper for `Vector.insert` neglected to do a bounds
      check.
      eee4f4fc
    • Bruce Merry's avatar
      Fix quadratic-time behaviour of vector extend · 6a0f6c40
      Bruce Merry authored
      The "extend" method for vectors defined in stl_bind.h used `reserve` to
      allocate space for the extra growth. While this can sometimes make a
      constant-factor improvement in performance, it can also cause
      construction of a vector by repeated extension to take quadratic rather
      than linear time, as memory is reallocated in small increments rather
      than on an exponential schedule. For example, this Python code would
      take time proportional to the square of the trip count:
      
      ```python
      a = VectorInt([1, 2, 3])
      b = VectorInt()
      for i in range(100000):
          b.extend(a)
      ```
      
      This commit removes the `reserve` call. The alternative would be to try
      to add some smarter heuristics, but the standard library may well have
      its own heuristics (the iterators are random access iterators, so it can
      easily determine the number of items being added) and trying to add more
      heuristics on top of that seems like a bad idea.
      6a0f6c40
    • Jason Rhinelander's avatar
      Force MSVC to compile in utf-8 mode · 8dc63ba9
      Jason Rhinelander authored
      MSVC by default uses the local codepage, which fails when it sees the
      utf-8 in test_python_types.cpp.  This adds the /utf-8 flag to the test
      suite compilation to force it to interpret source code as utf-8.
      
      Fixes #869
      8dc63ba9
    • Jason Rhinelander's avatar
      vectorize: pass-through of non-vectorizable args · f3ce00ea
      Jason Rhinelander authored
      This extends py::vectorize to automatically pass through
      non-vectorizable arguments.  This removes the need for the documented
      "explicitly exclude an argument" workaround.
      
      Vectorization now applies to arithmetic, std::complex, and POD types,
      passed as plain value or by const lvalue reference (previously only
      pass-by-value types were supported).  Non-const lvalue references and
      any other types are passed through as-is.
      
      Functions with rvalue reference arguments (whether vectorizable or not)
      are explicitly prohibited: an rvalue reference is inherently not
      something that can be passed multiple times and is thus unsuitable to
      being in a vectorized function.
      
      The vectorize returned value is also now more sensitive to inputs:
      previously it would return by value when all inputs are of size 1; this
      is now amended to having all inputs of size 1 *and* 0 dimensions.  Thus
      if you pass in, for example, [[1]], you get back a 1x1, 2D array, while
      previously you got back just the resulting single value.
      
      Vectorization of member function specializations is now also supported
      via `py::vectorize(&Class::method)`; this required passthrough support
      for the initial object pointer on the wrapping function pointer.
      f3ce00ea
    • Jason Rhinelander's avatar
      array_t: make c_style/f_style work for array creation · 41f8da4a
      Jason Rhinelander authored
      Currently if you construct an `array_t<T, array::f_style>` with a shape
      but not strides you get a C-style array; the only way to get F-style
      strides was to calculate the strides manually.  This commit fixes that
      by adding logic to use f_style strides when the flag is set.
      
      This also simplifies the existing c_style stride logic.
      41f8da4a
    • Jason Rhinelander's avatar
      Add and use detail::remove_reference_t · 129a7256
      Jason Rhinelander authored
      Adds `remove_reference_t` and converts various `typename
      std::remove_reference<...>::type` to using it.
      129a7256
    • Jason Rhinelander's avatar
      Add PYBIND11_EXPAND_SIDE_EFFECTS macro · 926e2cf3
      Jason Rhinelander authored
      This allows calling of functions (typically void) over a parameter
      pack, replacing usage such as:
      
          bool unused[] = { (voidfunc(param_pack_arg), false)..., false };
          (void) unused;
      
      with a much cleaner:
      
          PYBIND11_EXPAND_SIDE_EFFECTS(voidfunc(param_pack_arg));
      926e2cf3
  2. 24 May, 2017 2 commits
    • Jason Rhinelander's avatar
      Allow py::arg().none(false) argument attribute · 4e1e4a58
      Jason Rhinelander authored
      This attribute lets you disable (or explicitly enable) passing None to
      an argument that otherwise would allow it by accepting
      a value by raw pointer or shared_ptr.
      4e1e4a58
    • Jason Rhinelander's avatar
      Add movable cast support to type casters · 813d7e86
      Jason Rhinelander authored
      This commit allows type_casters to allow their local values to be moved
      away, rather than copied, when the type caster instance itself is an rvalue.
      
      This only applies (automatically) to type casters using
      PYBIND11_TYPE_CASTER; the generic type type casters don't own their own
      pointer, and various value casters (e.g. std::string, std::pair,
      arithmetic types) already cast to an rvalue (i.e. they return by value).
      
      This updates various calling code to attempt to get a movable value
      whenever the value is itself coming from a type caster about to be
      destroyed: for example, when constructing an std::pair or various stl.h
      containers.  For types that don't support value moving, the cast_op
      falls back to an lvalue cast.
      
      There wasn't an obvious place to add the tests, so I added them to
      test_copy_move_policies, but also renamed it to drop the _policies as it
      now tests more than just policies.
      813d7e86
  3. 22 May, 2017 3 commits
    • Bruce Merry's avatar
      Support pointers to member functions in def_buffer. · fe0cf8b7
      Bruce Merry authored
      Closes #857, by adding overloads to def_buffer that match pointers to
      member functions and wrap them in lambdas.
      fe0cf8b7
    • Jason Rhinelander's avatar
      Style cleanup of javadoc-style comments · 37b2383a
      Jason Rhinelander authored
      This changes javadoc-style documenting comments from:
      
          /** Text starts here
           * and continues here
           */
      
      to:
      
          /**
           * Test starts here
           * and continues here
           */
      
      which looks a little better, and also matches the javadoc-recommended
      way of writing documenting comments.
      37b2383a
    • Jason Rhinelander's avatar
      Use dynamic cast for shared_from_this holder init · b8ac4383
      Jason Rhinelander authored
      Using a dynamic_cast instead of a static_cast is needed to safely cast
      from a base to a derived type.  The previous static_pointer_cast isn't
      safe, however, when downcasting (and fails to compile when downcasting
      with virtual inheritance).
      
      Switching this to always use a dynamic_pointer_cast shouldn't incur any
      additional overhead when a static_pointer_cast is safe (i.e. when
      upcasting, or self-casting): compilers don't need RTTI checks in those
      cases.
      b8ac4383
  4. 21 May, 2017 3 commits
    • Jason Rhinelander's avatar
      Define both __div__ and __truediv__ for Python 2 · 35998a03
      Jason Rhinelander authored
      Python 2 requires both `__div__` and `__truediv__` (and variants) for
      compatibility with both regular Python 2 and Python 2 under `from
      __future__ import division`.  Without both, division fails in one or the
      other case.
      35998a03
    • Jason Rhinelander's avatar
      Minor operators.h style cleanups · 1ac51a02
      Jason Rhinelander authored
      - realign \ at end of macro lines
      - use 'using A = B;' rather than 'typedef B A;'
      - use conditional_t
      1ac51a02
    • Jason Rhinelander's avatar
      Fix /= operator under Python 3 · acad05cb
      Jason Rhinelander authored
      The Python method for /= was set as `__idiv__`, which should be
      `__itruediv__` under Python 3.
      
      This wasn't totally broken in that without it defined, Python constructs
      a new object by calling __truediv__.  The operator tests, however,
      didn't actually test the /= operator: when I added it, I saw an extra
      construction, leading to the problem.  This commit also includes tests
      for the previously untested *= operator, and adds some element-wise
      vector multiplication and division operators.
      acad05cb
  5. 20 May, 2017 1 commit
  6. 18 May, 2017 1 commit
  7. 16 May, 2017 1 commit
  8. 11 May, 2017 1 commit
  9. 10 May, 2017 5 commits
    • Dean Moldovan's avatar
      Improve constructor resolution in variant_caster · 94d0a9f7
      Dean Moldovan authored
      Currently, `py::int_(1).cast<variant<double, int>>()` fills the `double`
      slot of the variant. This commit switches the loader to a 2-pass scheme
      in order to correctly fill the `int` slot.
      94d0a9f7
    • Jason Rhinelander's avatar
      Defer None loading to second pass · 93e3eac6
      Jason Rhinelander authored
      Many of our `is_none()` checks in type caster loading return true, but
      this should really be considered a deferral so that, for example, an
      overload with a `py::none` argument would win over one that takes
      `py::none` as a null option.
      
      This keeps None-accepting for the `!convert` pass only for std::optional
      and void casters.  (The `char` caster already deferred None; this just
      extends that behaviour to other casters).
      93e3eac6
    • Jason Rhinelander's avatar
      Fix gcc 7 warning · 7fb01ecd
      Jason Rhinelander authored
      Under gcc 7 with -std=c++11, compilation results in several of the
      following warnings:
      
          In file included from /home/jagerman/src/pybind11/tests/test_sequences_and_iterators.cpp:13:0:
          /home/jagerman/src/pybind11/include/pybind11/operators.h: In function ‘pybind11::detail::op_<(pybind11::detail::op_id)0, (pybind11::detail::op_type)0, pybind11::detail::self_t, pybind11::detail::self_t> pybind11::detail::operator+(const pybind11::detail::self_t&, const pybind11::detail::self_t&)’:
          /home/jagerman/src/pybind11/include/pybind11/operators.h:78:76: warning: inline declaration of ‘pybind11::detail::op_<(pybind11::detail::op_id)0, (pybind11::detail::op_type)0, pybind11::detail::self_t, pybind11::detail::self_t> pybind11::detail::operator+(const pybind11::detail::self_t&, const pybind11::detail::self_t&)’ follows declaration with attribute noinline [-Wattributes]
           inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) {         \
                                                                                      ^
          /home/jagerman/src/pybind11/include/pybind11/operators.h:109:1: note: in expansion of macro ‘PYBIND11_BINARY_OPERATOR’
           PYBIND11_BINARY_OPERATOR(add,       radd,         operator+,    l + r)
           ^~~~~~~~~~~~~~~~~~~~~~~~
          In file included from /home/jagerman/src/pybind11/include/pybind11/cast.h:15:0,
                           from /home/jagerman/src/pybind11/include/pybind11/attr.h:13,
                           from /home/jagerman/src/pybind11/include/pybind11/pybind11.h:36,
                           from /home/jagerman/src/pybind11/tests/pybind11_tests.h:2,
                           from /home/jagerman/src/pybind11/tests/test_sequences_and_iterators.cpp:11:
          /home/jagerman/src/pybind11/include/pybind11/descr.h:116:36: note: previous definition of ‘pybind11::detail::descr pybind11::detail::operator+(pybind11::detail::descr&&, pybind11::detail::descr&&)’ was here
               PYBIND11_NOINLINE descr friend operator+(descr &&d1, descr &&d2) {
                                              ^~~~~~~~
      
      This appears to be happening because gcc is considering implicit
      construction of `descr` in some places using addition of two
      `descr`-compatible arguments in the `descr.h` c++11 fallback code.
      There's no particular reason that this operator needs to be a friend
      function: this commit changes it to an rvalue-context member function
      operator, which avoids the warning.
      7fb01ecd
    • Bruce Merry's avatar
      Allow std::complex field with PYBIND11_NUMPY_DTYPE (#831) · b82c0f0a
      Bruce Merry authored
      This exposed a few underlying issues:
      
      1. is_pod_struct was too strict to allow this. I've relaxed it to
      require only trivially copyable and standard layout, rather than POD
      (which additionally requires a trivial constructor, which std::complex
      violates).
      
      2. format_descriptor<std::complex<T>>::format() returned numpy format
      strings instead of PEP3118 format strings, but register_dtype
      feeds format codes of its fields to _dtype_from_pep3118. I've changed it
      to return PEP3118 format codes. format_descriptor is a public type, so
      this may be considered an incompatible change.
      
      3. register_structured_dtype tried to be smart about whether to mark
      fields as unaligned (with ^). However, it's examining the C++ alignment,
      rather than what numpy (or possibly PEP3118) thinks the alignment should
      be. For complex values those are different. I've made it mark all fields
      as ^ unconditionally, which should always be safe even if they are
      aligned, because we explicitly mark the padding.
      b82c0f0a
    • Bruce Merry's avatar
      Support arrays inside PYBIND11_NUMPY_DTYPE (#832) · 8e0d832c
      Bruce Merry authored
      Resolves #800.
      
      Both C++ arrays and std::array are supported, including mixtures like
      std::array<int, 2>[4]. In a multi-dimensional array of char, the last
      dimension is used to construct a numpy string type.
      8e0d832c
  10. 09 May, 2017 6 commits
    • Dean Moldovan's avatar
      Fix std::nullptr_t caster (#840) · 78f1dcf9
      Dean Moldovan authored
      * Fix compilation error with std::nullptr_t
      
      * Enable conversion from None to std::nullptr_t and std::nullopt_t
      
      Fixes #839.
      78f1dcf9
    • Jason Rhinelander's avatar
      Make PYBIND11_CPP_STANDARD work under MSVC · 77710ff0
      Jason Rhinelander authored
      Under MSVC we were ignoring PYBIND11_CPP_STANDARD and simply not
      passing any standard (which makes MSVC default to its C++14 mode).
      
      MSVC 2015u3 added the `/std:c++14` and `/std:c++latest` flags; the
      latter, under MSVC 2017, enables some C++17 features (such as
      `std::optional` and `std::variant`), so it is something we need to
      start supporting under MSVC.
      
      This makes the PYBIND11_CPP_STANDARD cmake variable work under MSVC,
      defaulting it to /std:c++14 (matching the default -std=c++14 for
      non-MSVC).
      
      It also adds a new appveyor test running under MSVC 2017 with
      /std:c++latest, which runs (and passes) the
      `std::optional`/`std::variant` tests.
      
      Also updated the documentation to clarify the c++ flags and add show
      MSVC flag examples.
      77710ff0
    • Jason Rhinelander's avatar
      Update PYBIND11_CPP1{4,7} macros for MSVC · ca0e82b7
      Jason Rhinelander authored
      The PYBIND11_CPP14 macro started out as a guard for the compile-time
      path code in `descr.h`, but has since come to mean other things.  This
      means that while the `descr.h` check has just checked the
      `PYBIND11_CPP14` macro, various other places now check `PYBIND11_CPP14
      || _MSC_VER`.  This reverses that by now setting the CPP14 macro when
      MSVC is trying to support C++14, but disabling the `descr.h` C++14 code
      (which still fails under MSVC 2017).
      
      The CPP17 macro also gets enabled when MSVC 2017 is compiling with
      /std:c++latest (the default is /std:c++14), which enables
      `std::optional` and `std::variant` support under MSVC.
      ca0e82b7
    • Jason Rhinelander's avatar
      d15b217f
    • Jason Rhinelander's avatar
      attr.h: Fix header copy-and-paste typo · 616e3d8f
      Jason Rhinelander authored
      616e3d8f
    • Jason Rhinelander's avatar
      gcc 7 disable warning · 88ebc49b
      Jason Rhinelander authored
      GCC 7 generates (when compiling in C++11/14 mode) warnings such as:
      
          mangled name for ‘pybind11::class_<type_, options>&
          pybind11::class_<type_, options>::def(const char*, Func&&, const Extra&
          ...) [with Func = int (test_exc_sp::C::*)(int) noexcept; Extra = {};
          type_ = test_exc_sp::C; options = {}]’ will change in C++17 because the
          exception specification is part of a function type [-Wnoexcept-type]
      
      There's nothing we can actually do in the code to avoid this, so just
      disable the warning.
      88ebc49b
  11. 08 May, 2017 1 commit
  12. 07 May, 2017 7 commits
  13. 02 May, 2017 1 commit
  14. 29 Apr, 2017 1 commit