1. 23 Feb, 2017 2 commits
    • 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 01 Feb, 2017 1 commit
    • Jason Rhinelander's avatar
      Add warning about binding multiple modules (#635) · 0558a9a7
      Jason Rhinelander authored
      Issue #633 suggests people might be tempted to copy the test scripts
      self-binding code, but that's a bad idea for pretty much anything other
      than a test suite with self-contained test code.
      
      This commit adds a comment as such with a reference to the
      documentation that tells people how to do it instead.
      0558a9a7
  7. 31 Jan, 2017 5 commits
    • Jason Rhinelander's avatar
      Minor fixes (#613) · 12494525
      Jason Rhinelander authored
      * Minor doc syntax fix
      
      The numpy documentation had a bad :file: reference (was using double
      backticks instead of single backticks).
      
      * Changed long-outdated "example" -> "tests" wording
      
      The ConstructorStats internal docs still had "from example import", and
      the main testing cpp file still used "example" in the module
      description.
      12494525
    • Jason Rhinelander's avatar
      Add support for positional args with args/kwargs · 2686da83
      Jason Rhinelander authored
      This commit rewrites the function dispatcher code to support mixing
      regular arguments with py::args/py::kwargs arguments.  It also
      simplifies the argument loader noticeably as it no longer has to worry
      about args/kwargs: all of that is now sorted out in the dispatcher,
      which now simply appends a tuple/dict if the function takes
      py::args/py::kwargs, then passes all the arguments in a vector.
      
      When the argument loader hit a py::args or py::kwargs, it doesn't do
      anything special: it just calls the appropriate type_caster just like it
      does for any other argument (thus removing the previous special cases
      for args/kwargs).
      
      Switching to passing arguments in a single std::vector instead of a pair
      of tuples also makes things simpler, both in the dispatch and the
      argument_loader: since this argument list is strictly pybind-internal
      (i.e. it never goes to Python) we have no particular reason to use a
      Python tuple here.
      
      Some (intentional) restrictions:
      - you may not bind a function that has args/kwargs somewhere other than
        the end (this somewhat matches Python, and keeps the dispatch code a
        little cleaner by being able to not worry about where to inject the
        args/kwargs in the argument list).
      - If you specify an argument both positionally and via a keyword
        argument, you get a TypeError alerting you to this (as you do in
        Python).
      2686da83
    • Dean Moldovan's avatar
      Improve custom holder support (#607) · ec009a7c
      Dean Moldovan authored
      * Abstract away some holder functionality (resolve #585)
      
      Custom holder types which don't have `.get()` can select the correct
      function to call by specializing `holder_traits`.
      
      * Add support for move-only holders (fix #605)
      ec009a7c
    • Jason Rhinelander's avatar
      Numpy: better compilation errors, long double support (#619) · f7f5bc8e
      Jason Rhinelander authored
      * Clarify PYBIND11_NUMPY_DTYPE documentation
      
      The current documentation and example reads as though
      PYBIND11_NUMPY_DTYPE is a declarative macro along the same lines as
      PYBIND11_DECLARE_HOLDER_TYPE, but it isn't.  The changes the
      documentation and docs example to make it clear that you need to "call"
      the macro.
      
      * Add satisfies_{all,any,none}_of<T, Preds>
      
      `satisfies_all_of<T, Pred1, Pred2, Pred3>` is a nice legibility-enhanced
      shortcut for `is_all<Pred1<T>, Pred2<T>, Pred3<T>>`.
      
      * Give better error message for non-POD dtype attempts
      
      If you try to use a non-POD data type, you get difficult-to-interpret
      compilation errors (about ::name() not being a member of an internal
      pybind11 struct, among others), for which isn't at all obvious what the
      problem is.
      
      This adds a static_assert for such cases.
      
      It also changes the base case from an empty struct to the is_pod_struct
      case by no longer using `enable_if<is_pod_struct>` but instead using a
      static_assert: thus specializations avoid the base class, POD types
      work, and non-POD types (and unimplemented POD types like std::array)
      get a more informative static_assert failure.
      
      * Prefix macros with PYBIND11_
      
      numpy.h uses unprefixed macros, which seems undesirable.  This prefixes
      them with PYBIND11_ to match all the other macros in numpy.h (and
      elsewhere).
      
      * Add long double support
      
      This adds long double and std::complex<long double> support for numpy
      arrays.
      
      This allows some simplification of the code used to generate format
      descriptors; the new code uses fewer macros, instead putting the code as
      different templated options; the template conditions end up simpler with
      this because we are now supporting all basic C++ arithmetic types (and
      so can use is_arithmetic instead of is_integral + multiple
      different specializations).
      
      In addition to testing that it is indeed working in the test script, it
      also adds various offset and size calculations there, which
      fixes the test failures under x86 compilations.
      f7f5bc8e
    • Pim Schellart's avatar
  8. 04 Jan, 2017 2 commits
  9. 26 Dec, 2016 2 commits
  10. 19 Dec, 2016 4 commits
  11. 16 Dec, 2016 1 commit
    • Wenzel Jakob's avatar
      WIP: PyPy support (#527) · 1d1f81b2
      Wenzel Jakob authored
      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.
      1d1f81b2
  12. 15 Dec, 2016 1 commit
  13. 14 Dec, 2016 3 commits
    • Jason Rhinelander's avatar
      Change all_of_t/any_of_t to all_of/any_of, add none_of · fa5d05e1
      Jason Rhinelander authored
      This replaces the current `all_of_t<Pred, Ts...>` with `all_of<Ts...>`,
      with previous use of `all_of_t<Pred, Ts...>` becoming
      `all_of<Pred<Ts>...>` (and similarly for `any_of_t`).  It also adds a
      `none_of<Ts...>`, a shortcut for `negation<any_of<Ts...>>`.
      
      This allows `all_of` and `any_of` to be used a bit more flexible, e.g.
      in cases where several predicates need to be tested for the same type
      instead of the same predicate for multiple types.
      
      This commit replaces the implementation with a more efficient version
      for non-MSVC.  For MSVC, this changes the workaround to use the
      built-in, recursive std::conjunction/std::disjunction instead.
      
      This also removes the `count_t` since `any_of_t` and `all_of_t` were the
      only things using it.
      
      This commit also rearranges some of the future std imports to use actual
      `std` implementations for C++14/17 features when under the appropriate
      compiler mode, as we were already doing for a few things (like
      index_sequence).  Most of these aren't saving much (the implementation
      for enable_if_t, for example, is trivial), but I think it makes the
      intention of the code instantly clear.  It also enables MSVC's native
      std::index_sequence support.
      fa5d05e1
    • Jason Rhinelander's avatar
      Support binding noexcept function/methods in C++17 · 6e036e78
      Jason Rhinelander authored
      When compiling in C++17 mode the noexcept specifier is part of the
      function type.  This causes a failure in pybind11 because, by omitting
      a noexcept specifier when deducing function return and argument types,
      we are implicitly making `noexcept(false)` part of the type.
      
      This means that functions with `noexcept` fail to match the function
      templates in cpp_function (and other places), and we get compilation
      failure (we end up trying to fit it into the lambda function version,
      which fails since a function pointer has no `operator()`).
      
      We can, however, deduce the true/false `B` in noexcept(B), so we don't
      need to add a whole other set of overloads, but need to deduce the extra
      argument when under C++17.  That will *not* work under pre-C++17,
      however.
      
      This commit adds two macros to fix the problem: under C++17 (with the
      appropriate feature macro set) they provide an extra `bool NoExceptions`
      template argument and provide the `noexcept(NoExceptions)` deduced
      specifier.  Under pre-C++17 they expand to nothing.
      
      This is needed to compile pybind11 with gcc7 under -std=c++17.
      6e036e78
    • Jason Rhinelander's avatar
      Fix test compilation when both optional's exist · 79de508e
      Jason Rhinelander authored
      gcc 7 has both std::experimental::optional and std::optional, but this
      breaks the test compilation as we are trying to use the same `opt_int`
      type alias for both.
      79de508e
  14. 13 Dec, 2016 1 commit
  15. 12 Dec, 2016 2 commits
    • Dean Moldovan's avatar
      76e993a3
    • Jason Rhinelander's avatar
      Adds automatic casting on assignment of non-pyobject types (#551) · 3f1ff3f4
      Jason Rhinelander authored
      This adds automatic casting when assigning to python types like dict,
      list, and attributes.  Instead of:
      
          dict["key"] = py::cast(val);
          m.attr("foo") = py::cast(true);
          list.append(py::cast(42));
      
      you can now simply write:
      
          dict["key"] = val;
          m.attr("foo") = true;
          list.append(42);
      
      Casts needing extra parameters (e.g. for a non-default rvp) still
      require the py::cast() call. set::add() is also supported.
      
      All usage is channeled through a SFINAE implementation which either just returns or casts. 
      
      Combined non-converting handle and autocasting template methods via a
      helper method that either just returns (handle) or casts (C++ type).
      3f1ff3f4
  16. 08 Dec, 2016 1 commit
  17. 07 Dec, 2016 2 commits
    • Jason Rhinelander's avatar
      std::valarray support for stl.h (#545) · ae185b7f
      Jason Rhinelander authored
      * Added ternary support with descr args
      
      Current the `_<bool>(a, b)` ternary support only works for `char[]` `a`
      and `b`; this commit allows it to work for `descr` `a` and `b` arguments
      as well.
      
      * Add support for std::valarray to stl.h
      
      This abstracts the std::array into a `array_caster` which can then be
      used with either std::array or std::valarray, the main difference being
      that std::valarray is resizable.  (It also lets the array_caster be
      potentially used for other std::array-like interfaces, much as the
      list_caster and map_caster currently provide).
      
      * Small stl.h cleanups
      
      - Remove redundant `type` typedefs
      - make internal list_caster methods private
      ae185b7f
    • Dean Moldovan's avatar
  18. 03 Dec, 2016 1 commit
  19. 25 Nov, 2016 1 commit
  20. 22 Nov, 2016 3 commits
  21. 20 Nov, 2016 1 commit