1. 17 Nov, 2016 3 commits
    • Dean Moldovan's avatar
      Improve consistency of array and array_t with regard to other pytypes · 4de27102
      Dean Moldovan authored
      * `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.
      4de27102
    • Dean Moldovan's avatar
      Add default and converting constructors for all concrete Python types · e18bc02f
      Dean Moldovan authored
      * Deprecate the `py::object::str()` member function since `py::str(obj)`
        is now equivalent and preferred
      
      * Make `py::repr()` a free function
      
      * Make sure obj.cast<T>() works as expected when T is a Python type
      
      `obj.cast<T>()` should be the same as `T(obj)`, i.e. it should convert
      the given object to a different Python type. However, `obj.cast<T>()`
      usually calls `type_caster::load()` which only checks the type without
      doing any actual conversion. That causes a very unexpected `cast_error`.
      This commit makes it so that `obj.cast<T>()` and `T(obj)` are the same
      when T is a Python type.
      
      * Simplify pytypes converting constructor implementation
      
      It's not necessary to maintain a full set of converting constructors
      and assignment operators + const& and &&. A single converting const&
      constructor will work and there is no impact on binary size. On the
      other hand, the conversion functions can be significantly simplified.
      e18bc02f
    • Dean Moldovan's avatar
      Add py::isinstance<T>(obj) for generalized Python type checking · b4498ef4
      Dean Moldovan authored
      Allows checking the Python types before creating an object instead of
      after. For example:
      ```c++
      auto l = list(ptr, true);
      if (l.check())
         // ...
      ```
      The above is replaced with:
      ```c++
      if (isinstance<list>(ptr)) {
          auto l = reinterpret_borrow(ptr);
          // ...
      }
      ```
      
      This deprecates `py::object::check()`. `py::isinstance()` covers the
      same use case, but it can also check for user-defined types:
      ```c++
      class Pet { ... };
      py::class_<Pet>(...);
      
      m.def("is_pet", [](py::object obj) {
          return py::isinstance<Pet>(obj); // works as expected
      });
      ```
      b4498ef4
  2. 16 Nov, 2016 2 commits
  3. 15 Nov, 2016 4 commits
    • Jason Rhinelander's avatar
      2e76daa5
    • Ivan Smirnov's avatar
      Add type casters for nullopt_t, fix none refcount (#499) · 425b4970
      Ivan Smirnov authored
      * Incref returned None in std::optional type caster
      
      * Add type casters for nullopt_t
      
      * Add a test for nullopt_t
      425b4970
    • Alexander Stukowski's avatar
      Provide more control over automatic generation of docstrings (#486) · 9a110e6d
      Alexander Stukowski authored
      Added the docstring_options class, which gives global control over the generation of docstrings and function signatures.
      9a110e6d
    • Jason Rhinelander's avatar
      Fix stl_bind to support movable, non-copyable value types (#490) · 617fbcfc
      Jason Rhinelander authored
      This commit includes the following changes:
      
      * Don't provide make_copy_constructor for non-copyable container
      
      make_copy_constructor currently fails for various stl containers (e.g.
      std::vector, std::unordered_map, std::deque, etc.) when the container's
      value type (e.g. the "T" or the std::pair<K,T> for a map) is
      non-copyable.  This adds an override that, for types that look like
      containers, also requires that the value_type be copyable.
      
      * stl_bind.h: make bind_{vector,map} work for non-copy-constructible types
      
      Most stl_bind modifiers require copying, so if the type isn't copy
      constructible, we provide a read-only interface instead.
      
      In practice, this means that if the type is non-copyable, it will be,
      for all intents and purposes, read-only from the Python side (but
      currently it simply fails to compile with such a container).
      
      It is still possible for the caller to provide an interface manually
      (by defining methods on the returned class_ object), but this isn't
      something stl_bind can handle because the C++ code to construct values
      is going to be highly dependent on the container value_type.
      
      * stl_bind: copy only for arithmetic value types
      
      For non-primitive types, we may well be copying some complex type, when
      returning by reference is more appropriate.  This commit returns by
      internal reference for all but basic arithmetic types.
      
      * Return by reference whenever possible
      
      Only if we definitely can't--i.e. std::vector<bool>--because v[i]
      returns something that isn't a T& do we copy; for everything else, we
      return by reference.
      
      For the map case, we can always return by reference (at least for the
      default stl map/unordered_map).
      617fbcfc
  4. 13 Nov, 2016 2 commits
    • Jason Rhinelander's avatar
      07806558
    • Jason Rhinelander's avatar
      Add cmake option to override tests (#489) · 920e0e34
      Jason Rhinelander authored
      When working on some particular feature, it's nice to be able to disable
      all the tests except for the one I'm working on; this is currently
      possible by editing tests/CMakeLists.txt, and commenting out the tests
      you don't want.
      
      This commit goes a step further by letting you give a list of tests you
      do want when invoking cmake, e.g.:
      
          cmake -DPYBIND11_TEST_OVERRIDE="test_issues.cpp;test_pickling.cpp" ..
      
      changes the build to build just those two tests (and changes the `pytest`
      target to invoke just the two associated tests).
      
      This persists in the build directory until you disable it again by
      running cmake with `-DPYBIND11_TEST_OVERRIDE=`.  It also adds a message
      after the pytest output to remind you that it is in effect:
      
          Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect
      920e0e34
  5. 07 Nov, 2016 1 commit
  6. 06 Nov, 2016 1 commit
    • Jason Rhinelander's avatar
      Don't construct unique_ptr around unowned pointers (#478) · c07ec31e
      Jason Rhinelander authored
      If we need to initialize a holder around an unowned instance, and the
      holder type is non-copyable (i.e. a unique_ptr), we currently construct
      the holder type around the value pointer, but then never actually
      destruct the holder: the holder destructor is called only for the
      instance that actually has `inst->owned = true` set.
      
      This seems no pointer, however, in creating such a holder around an
      unowned instance: we never actually intend to use anything that the
      unique_ptr gives us: and, in fact, do not want the unique_ptr (because
      if it ever actually got destroyed, it would cause destruction of the
      wrapped pointer, despite the fact that that wrapped pointer isn't
      owned).
      
      This commit changes the logic to only create a unique_ptr holder if we
      actually own the instance, and to destruct via the constructed holder
      whenever we have a constructed holder--which will now only be the case
      for owned-unique-holder or shared-holder types.
      
      Other changes include:
      
      * Added test for non-movable holder constructor/destructor counts
      
      The three alive assertions now pass, before #478 they fail with counts
      of 2/2/1 respectively, because of the unique_ptr that we don't want and
      don't destroy (because we don't *want* its destructor to run).
      
      * Return cstats reference; fix ConstructStats doc
      
      Small cleanup to the #478 test code, and fix to the ConstructStats
      documentation (the static method definition should use `reference` not
      `reference_internal`).
      
      * Rename inst->constructed to inst->holder_constructed
      
      This makes it clearer exactly what it's referring to.
      c07ec31e
  7. 04 Nov, 2016 1 commit
    • Jason Rhinelander's avatar
      Add debugging info about .so size to build output (#477) · dc0b4bd2
      Jason Rhinelander authored
      * Add debugging info about so size to build output
      
      This adds a small python script to tools that captures before-and-after
      .so sizes between builds and outputs this in the build output via a
      string such as:
      
      ------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 924696 (decrease of 73680 bytes = 7.38%)
      
      ------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 998376 (increase of 73680 bytes = 7.97%)
      
      ------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 998376 (no change)
      
      Or, if there was no .so during the build, just the .so size by itself:
      
      ------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 998376
      
      This allows you to, for example, build, checkout a different branch,
      rebuild, and easily see exactly the change in the pybind11_tests.so
      size.
      
      It also allows looking at the travis and appveyor build logs to get an
      idea of .so/.dll sizes across different build systems.
      
      * Minor libsize.py script changes
      
      - Use RAII open
      - Remove unused libsize=-1
      - Report change as [+-]xyz bytes = [+-]a.bc%
      dc0b4bd2
  8. 03 Nov, 2016 4 commits
  9. 01 Nov, 2016 2 commits
    • Ivan Smirnov's avatar
      abd3429c
    • Dean Moldovan's avatar
      Make reference(_internal) the default return value policy for properties (#473) · 03f627eb
      Dean Moldovan authored
      * Make reference(_internal) the default return value policy for properties
      
      Before this, all `def_property*` functions used `automatic` as their
      default return value policy. This commit makes it so that:
      
       * Non-static properties use `reference_interal` by default, thus
         matching `def_readonly` and `def_readwrite`.
      
       * Static properties use `reference` by default, thus matching
         `def_readonly_static` and `def_readwrite_static`.
      
      In case `cpp_function` is passed to any `def_property*`, its policy will
      be used instead of any defaults. User-defined arguments in `extras`
      still have top priority and will override both the default policies and
      the ones from `cpp_function`.
      
      Resolves #436.
      
      * Almost always use return_value_policy::move for rvalues
      
      For functions which return rvalues or rvalue references, the only viable
      return value policies are `copy` and `move`. `reference(_internal)` and
      `take_ownership` would take the address of a temporary which is always
      an error.
      
      This commit prevents possible user errors by overriding the bad rvalue
      policies with `move`. Besides `move`, only `copy` is allowed, and only
      if it's explicitly selected by the user.
      
      This is also a necessary safety feature to support the new default
      return value policies for properties: `reference(_internal)`.
      03f627eb
  10. 27 Oct, 2016 2 commits
  11. 25 Oct, 2016 1 commit
    • Jason Rhinelander's avatar
      Prevent overwriting previous declarations · 6873c202
      Jason Rhinelander authored
      Currently pybind11 doesn't check when you define a new object (e.g. a
      class, function, or exception) that overwrites an existing one.  If the
      thing being overwritten is a class, this leads to a segfault (because
      pybind still thinks the type is defined, even though Python no longer
      has the type).  In other cases this is harmless (e.g. replacing a
      function with an exception), but even in that case it's most likely a
      bug.
      
      This code doesn't prevent you from actively doing something harmful,
      like deliberately overwriting a previous definition, but detects
      overwriting with a run-time error if it occurs in the standard
      class/function/exception/def registration interfaces.
      
      All of the additions are in non-template code; the result is actually a
      tiny decrease in .so size compared to master without the new test code
      (977304 to 977272 bytes), and about 4K higher with the new tests.
      6873c202
  12. 23 Oct, 2016 1 commit
  13. 21 Oct, 2016 1 commit
  14. 20 Oct, 2016 5 commits
  15. 14 Oct, 2016 1 commit
  16. 13 Oct, 2016 1 commit
  17. 12 Oct, 2016 3 commits
  18. 11 Oct, 2016 1 commit
  19. 09 Oct, 2016 2 commits
  20. 29 Sep, 2016 1 commit
  21. 27 Sep, 2016 1 commit