1. 21 Mar, 2017 1 commit
    • Dean Moldovan's avatar
      Throw an exception when attempting to load an incompatible holder · cd3d1fc7
      Dean Moldovan authored
      Instead of a segfault. Fixes #751.
      
      This covers the case of loading a custom holder from a default-holder
      instance. Attempting to load one custom holder from a different custom
      holder (i.e. not `std::unique_ptr`) yields undefined behavior, just as
      #588 established for inheritance.
      cd3d1fc7
  2. 17 Mar, 2017 2 commits
    • Jason Rhinelander's avatar
      Fail to compile with MI via class_ ctor parameters · b961626c
      Jason Rhinelander authored
      We can't support this for classes from imported modules (which is the
      primary purpose of a ctor argument base class) because we *have* to
      have both parent and derived to properly extract a multiple-inheritance
      base class pointer from a derived class pointer.
      
      We could support this for actual `class_<Base, ...> instances, but since
      in that case the `Base` is already present in the code, it seems more
      consistent to simply always require MI to go via template options.
      b961626c
    • Jason Rhinelander's avatar
      Eigen: don't require conformability on length-1 dimensions · efa8726f
      Jason Rhinelander authored
      Fixes #738
      
      The current check for conformability fails when given a 2D, 1xN or Nx1
      input to a row-major or column-major, respectively, Eigen::Ref, leading
      to a copy-required state in the type_caster, but this later failed
      because the copy was also non-conformable because it had the same shape
      and strides (because a 1xN or Nx1 is both F and C contiguous).
      
      In such cases we can safely ignore the stride on the "1" dimension since
      it'll never be used: only the "N" dimension stride needs to match the
      Eigen::Ref stride, which both fixes the non-conformable copy problem,
      but also avoids a copy entirely as long as the "N" dimension has a
      compatible stride.
      efa8726f
  3. 16 Mar, 2017 1 commit
  4. 15 Mar, 2017 1 commit
  5. 14 Mar, 2017 2 commits
  6. 13 Mar, 2017 1 commit
  7. 12 Mar, 2017 1 commit
    • Jason Rhinelander's avatar
      Fix for floating point durations · e5456c22
      Jason Rhinelander authored
      The duration calculation was using %, but that's only supported on
      duration objects when the arithmetic type supports %, and hence fails
      for floats.  Fixed by subtracting off the calculated values instead.
      e5456c22
  8. 10 Mar, 2017 1 commit
    • Dean Moldovan's avatar
      Minor pytest maintenance (#702) · d47febcb
      Dean Moldovan authored
      * Add `pytest.ini` config file and set default options there instead of
        in `CMakeLists.txt` (command line arguments).
      
      * Change all output capture from `capfd` (filedescriptors) to `capsys`
        (Python's `sys.stdout` and `sys.stderr`). This avoids capturing
        low-level C errors, e.g. from the debug build of Python.
      
      * Set pytest minimum version to 3.0 to make it easier to use new
        features. Removed conditional use of `excinfo.match()`.
      
      * Clean up some leftover function-level `@pytest.requires_numpy`.
      d47febcb
  9. 08 Mar, 2017 1 commit
    • Jason Rhinelander's avatar
      Fix extra docstring newlines under `options.disable_function_signatures()` · 10d13048
      Jason Rhinelander authored
      When using pybind::options to disable function signatures, user-defined
      docstrings only get appended if they exist, but newlines were getting
      appended unconditionally, so the docstring could end up with blank lines
      (depending on which overloads, in particular, provided docstrings).
      
      This commit suppresses the empty lines by only adding newlines for
      overloads when needed.
      10d13048
  10. 06 Mar, 2017 1 commit
  11. 03 Mar, 2017 1 commit
  12. 28 Feb, 2017 1 commit
  13. 27 Feb, 2017 1 commit
  14. 26 Feb, 2017 6 commits
  15. 24 Feb, 2017 9 commits
    • Jason Rhinelander's avatar
      Move requires_numpy, etc. decorators to globals · 2a757844
      Jason Rhinelander authored
      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.
      2a757844
    • Jason Rhinelander's avatar
      Eigen<->numpy referencing support · 17d0283e
      Jason Rhinelander authored
      This commit largely rewrites the Eigen dense matrix support to avoid
      copying in many cases: Eigen arguments can now reference numpy data, and
      numpy objects can now reference Eigen data (given compatible types).
      
      Eigen::Ref<...> arguments now also make use of the new `convert`
      argument use (added in PR #634) to avoid conversion, allowing
      `py::arg().noconvert()` to be used when binding a function to prohibit
      copying when invoking the function.  Respecting `convert` also means
      Eigen overloads that avoid copying will be preferred during overload
      resolution to ones that require copying.
      
      This commit also rewrites the Eigen documentation and test suite to
      explain and test the new capabilities.
      17d0283e
    • Jason Rhinelander's avatar
      Change array's writeable exception to a ValueError · fd751703
      Jason Rhinelander authored
      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.
      fd751703
    • Jason Rhinelander's avatar
      array: fix base handling · f86dddf7
      Jason Rhinelander authored
      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.
      f86dddf7
    • Jason Rhinelander's avatar
      Eigen: fix partially-fixed matrix conversion · d9d224f2
      Jason Rhinelander authored
      Currently when we do a conversion between a numpy array and an Eigen
      Vector, we allow the conversion only if the Eigen type is a
      compile-time vector (i.e. at least one dimension is fixed at 1 at
      compile time), or if the type is dynamic on *both* dimensions.
      
      This means we can run into cases where MatrixXd allow things that
      conforming, compile-time sizes does not: for example,
      `Matrix<double,4,Dynamic>` is currently not allowed, even when assigning
      from a 4-element vector, but it *is* allowed for a
      `Matrix<double,Dynamic,Dynamic>`.
      
      This commit also reverts the current behaviour of using the matrix's
      storage order to determine the structure when the Matrix is fully
      dynamic (i.e. in both dimensions).  Currently we assign to an eigen row
      if the storage order is row-major, and column otherwise: this seems
      wrong (the storage order has nothing to do with the shape!).  While
      numpy doesn't distinguish between a row/column vector, Eigen does, but
      it makes more sense to consistently choose one than to produce
      something with a different shape based on the intended storage layout.
      d9d224f2
    • Jason Rhinelander's avatar
      Workaround style check false positive · a04410bd
      Jason Rhinelander authored
      a04410bd
    • Jason Rhinelander's avatar
      Show kwargs in failed method invocation · 231e1678
      Jason Rhinelander authored
      With the previous commit, output can be very confusing because you only
      see positional arguments in the "invoked with" line, but you can have a
      failure from kwargs as well (in particular, when a value is invalidly
      specified via both via positional and kwargs).  This commits adds
      kwargs to the output, and updates the associated tests to match.
      231e1678
    • Jason Rhinelander's avatar
      Independent tests (#665) · 60d0e0db
      Jason Rhinelander authored
      * Make tests buildable independently
      
      This makes "tests" buildable as a separate project that uses
      find_package(pybind11 CONFIG) when invoked independently.
      
      This also moves the WERROR option into tests/CMakeLists.txt, as that's
      the only place it is used.
      
      * Use Eigen 3.3.1's cmake target, if available
      
      This changes the eigen finding code to attempt to use Eigen's
      system-installed Eigen3Config first.  In Eigen 3.3.1, it exports a cmake
      Eigen3::Eigen target to get dependencies from (rather than setting the
      include path directly).
      
      If it fails, we fall back to the trying to load allowing modules (i.e.
      allowing our tools/FindEigen3.cmake).  If we either fallback, or the
      eigen version is older than 3.3.1 (or , we still set the include
      directory manually; otherwise, for CONFIG + new Eigen, we get it via
      the target.
      
      This is also needed to allow 'tests' to be built independently, when
      the find_package(Eigen3) is going to find via the system-installed
      Eigen3Config.cmake.
      
      * Add a install-then-build test, using clang on linux
      
      This tests that `make install` to the actual system, followed by a build
      of the tests (without the main pybind11 repository available) works as
      expected.
      
      To also expand the testing variety a bit, it also builds using
      clang-3.9 instead of gcc.
      
      * Don't try loading Eigen3Config in cmake < 3.0
      
      It could FATAL_ERROR as the newer cmake includes a cmake 3.0 required
      line.
      
      If doing an independent, out-of-tree "tests" build, the regular
      find_package(Eigen3) is likely to fail with the same error, but I think
      we can just let that be: if you want a recent Eigen with proper cmake
      loading support *and* want to do an independent tests build, you'll
      need at least cmake 3.0.
      60d0e0db
    • Jason Rhinelander's avatar
      Make string conversion stricter (#695) · ee2e5a50
      Jason Rhinelander authored
      * 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)
      ee2e5a50
  16. 23 Feb, 2017 3 commits
    • Dean Moldovan's avatar
      Enable static properties (py::metaclass) by default · dd01665e
      Dean Moldovan authored
      Now that only one shared metaclass is ever allocated, it's extremely
      cheap to enable it for all pybind11 types.
      
      * Deprecate the default py::metaclass() since it's not needed anymore.
      * Allow users to specify a custom metaclass via py::metaclass(handle).
      dd01665e
    • Dean Moldovan's avatar
      Make all classes with the same instance size derive from a common base · 08cbe8df
      Dean Moldovan authored
      In order to fully satisfy Python's inheritance type layout requirements,
      all types should have a common 'solid' base. A solid base is one which
      has the same instance size as the derived type (not counting the space
      required for the optional `dict_ptr` and `weakrefs_ptr`). Thus, `object`
      does not qualify as a solid base for pybind11 types and this can lead to
      issues with multiple inheritance.
      
      To get around this, new base types are created: one per unique instance
      size. There is going to be very few of these bases. They ensure Python's
      MRO checks will pass when multiple bases are involved.
      08cbe8df
    • Dean Moldovan's avatar
      Reimplement static properties by extending PyProperty_Type · c91f8bd6
      Dean Moldovan authored
      Instead of creating a new unique metaclass for each type, the builtin
      `property` type is subclassed to support static properties. The new
      setter/getters always pass types instead of instances in their `self`
      argument. A metaclass is still required to support this behavior, but
      it doesn't store any data anymore, so a new one doesn't need to be
      created for each class. There is now only one common metaclass which
      is shared by all pybind11 types.
      c91f8bd6
  17. 22 Feb, 2017 1 commit
    • Lunderberg's avatar
      Fixed compilation error when binding function accepting some forms of std::function (#689) · c7fcde7c
      Lunderberg authored
      * Fixed compilation error when defining function accepting some forms of std::function.
      
      The compilation error happens only when the functional.h header is
      present, and the build is done in debug mode, with NDEBUG being
      undefined.  In addition, the std::function must accept an abstract
      base class by reference.
      
      The compilation error occurred in cast.h, when trying to construct a
      std::tuple<AbstractBase>, rather than a std::tuple<AbstractBase&>.
      This was caused by functional.h using std::move rather than
      std::forward, changing the signature of the function being used.
      
      This commit contains the fix, along with a test that exhibits the
      issue when compiled in debug mode without the fix applied.
      
      * Moved new std::function tests into test_callbacks, added callback_with_movable test.
      c7fcde7c
  18. 14 Feb, 2017 2 commits
    • Jason Rhinelander's avatar
      Unicode fixes and docs (#624) · 11a337f1
      Jason Rhinelander authored
      * Propagate unicode conversion failure
      
      If returning a std::string with invalid utf-8 data, we currently fail
      with an uninformative TypeError instead of propagating the
      UnicodeDecodeError that Python sets on failure.
      
      * Add support for u16/u32strings and literals
      
      This adds support for wchar{16,32}_t character literals and the
      associated std::u{16,32}string types.  It also folds the
      character/string conversion into a single type_caster template, since
      the type casters for string and wstring were mostly the same anyway.
      
      * Added too-long and too-big character conversion errors
      
      With this commit, when casting to a single character, as opposed to a
      C-style string, we make sure the input wasn't a multi-character string
      or a single character with codepoint too large for the character type.
      
      This also changes the character cast op to CharT instead of CharT& (we
      need to be able to return a temporary decoded char value, but also
      because there's little gained by bothering with an lvalue return here).
      
      Finally it changes the char caster to 'has-a-string-caster' instead of
      'is-a-string-caster' because, with the cast_op change above, there's
      nothing at all gained from inheritance.  This also lets us remove the
      `success` from the string caster (which was only there for the char
      caster) into the char caster itself.  (I also renamed it to 'none' and
      inverted its value to better reflect its purpose).  The None -> nullptr
      loading also now takes place only under a `convert = true` load pass.
      Although it's unlikely that a function taking a char also has overloads
      that can take a None, it seems marginally more correct to treat it as a
      conversion.
      
      This commit simplifies the size assumptions about character sizes with
      static_asserts to back them up.
      11a337f1
    • Jason Rhinelander's avatar
      Overhaul LTO flag detection · 1bee6e7d
      Jason Rhinelander authored
      Clang on linux currently fails to run cmake:
      
          $ CC=clang CXX=clang++ cmake ..
          ...
          -- Configuring done
          CMake Error at tools/pybind11Tools.cmake:135 (target_compile_options):
            Error evaluating generator expression:
      
              $<:-flto>
      
            Expression did not evaluate to a known generator expression
          Call Stack (most recent call first):
            tests/CMakeLists.txt:68 (pybind11_add_module)
      
      But investigating this led to various other -flto detection problems;
      this commit thus overhauls LTO flag detection:
      
      - -flto needs to be passed to the linker as well
      - Also compile with -fno-fat-lto-objects under GCC
      - Pass the equivalent flags to MSVC
      - Enable LTO flags for via generator expressions (for non-debug builds
        only), so that multi-config builds (like on Windows) still work
        properly.  This seems reasonable, however, even on single-config
        builds (and simplifies the cmake code a bit).
      - clang's lto linker plugins don't accept '-Os', so replace it with
        '-O3' when doing a MINSIZEREL build
      - Enable trying ThinLTO by default for test suite (only affects clang)
      - Match Clang$ rather than ^Clang$ because, for cmake with 3.0+
        policies in effect, the compiler ID will be AppleClang on macOS.
      1bee6e7d
  19. 08 Feb, 2017 2 commits
    • Matthew Woehlke's avatar
      Fix path to libsize.py (#658) · 5e92b3e6
      Matthew Woehlke authored
      Use PROJECT_SOURCE_DIR instead of CMAKE_SOURCE_DIR as the base of the
      path to libsize.py. This fixes an error if pybind11 is being built
      directly within another project.
      5e92b3e6
    • Jason Rhinelander's avatar
      Fix debugging output for nameless py::arg_v annotations (#648) · 1eaacd19
      Jason Rhinelander authored
      * Fix debugging output for nameless py::arg annotations
      
      This fixes a couple bugs with nameless py::arg() (introduced in #634)
      annotations:
      
      - the argument name was being used in debug mode without checking that
        it exists (which would result in the std::string construction throwing
        an exception for being invoked with a nullptr)
      - the error output says "keyword arguments", but py::arg_v() can now
        also be used for positional argument defaults.
      - the debugging output "in function named 'blah'" was overly verbose:
        changed it to just "in function 'blah'".
      
      * Fix missing space in debug test string
      
      * Moved tests from issues to methods_and_attributes
      1eaacd19
  20. 04 Feb, 2017 2 commits
    • Jason Rhinelander's avatar
      Prefer non-converting argument overloads · e550589b
      Jason Rhinelander authored
      This changes the function dispatching code for overloaded functions into
      a two-pass procedure where we first try all overloads with
      `convert=false` for all arguments.  If no function calls succeeds in the
      first pass, we then try a second pass where we allow arguments to have
      `convert=true` (unless, of course, the argument was explicitly specified
      with `py::arg().noconvert()`).
      
      For non-overloaded methods, the two-pass procedure is skipped (we just
      make the overload-allowed call).  The second pass is also skipped if it
      would result in the same thing (i.e. where all arguments are
      `.noconvert()` arguments).
      e550589b
    • Jason Rhinelander's avatar
      Add support for non-converting arguments · abc29cad
      Jason Rhinelander authored
      This adds support for controlling the `convert` flag of arguments
      through the py::arg annotation.  This then allows arguments to be
      flagged as non-converting, which the type_caster is able to use to
      request different behaviour.
      
      Currently, AFAICS `convert` is only used for type converters of regular
      pybind11-registered types; all of the other core type_casters ignore it.
      We can, however, repurpose it to control internal conversion of
      converters like Eigen and `array`: most usefully to give callers a way
      to disable the conversion that would otherwise occur when a
      `Eigen::Ref<const Eigen::Matrix>` argument is passed a numpy array that
      requires conversion (either because it has an incompatible stride or the
      wrong dtype).
      
      Specifying a noconvert looks like one of these:
      
          m.def("f1", &f, "a"_a.noconvert() = "default"); // Named, default, noconvert
          m.def("f2", &f, "a"_a.noconvert()); // Named, no default, no converting
          m.def("f3", &f, py::arg().noconvert()); // Unnamed, no default, no converting
      
      (The last part--being able to declare a py::arg without a name--is new:
      previous py::arg() only accepted named keyword arguments).
      
      Such an non-convert argument is then passed `convert = false` by the
      type caster when loading the argument.  Whether this has an effect is up
      to the type caster itself, but as mentioned above, this would be
      extremely helpful for the Eigen support to give a nicer way to specify
      a "no-copy" mode than the custom wrapper in the current PR, and
      moreover isn't an Eigen-specific hack.
      abc29cad