Skip to content

Commit 034e13d

Browse files
committed
Rename to py::prepend and add docs mention
1 parent a331f96 commit 034e13d

File tree

6 files changed

+18
-12
lines changed

6 files changed

+18
-12
lines changed

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ v2.3.0 (Not yet released)
1414
precomputed at compile time (this was previously only available in C++14 mode
1515
for non-MSVC compilers).
1616
`#934 <https://github.com/pybind/pybind11/pull/934>`_.
17+
* Added ``py::prepend()``, allowing a function to be placed at the beginning of the
18+
overload chain `#1131 <https://github.com/pybind/pybind11/pull/1131>`_.
1719

1820
v2.2.1 (September 14, 2017)
1921
-----------------------------------------------------

docs/classes.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,9 @@ Attempting to bind ``Pet::set`` will cause an error since the compiler does not
364364
know which method the user intended to select. We can disambiguate by casting
365365
them to function pointers. Binding multiple functions to the same Python name
366366
automatically creates a chain of function overloads that will be tried in
367-
sequence.
367+
the order they were defined. If the ``py::prepend()`` tag is added to the
368+
definition, the function will be placed at the begining of the overload sequence
369+
instead.
368370

369371
.. code-block:: cpp
370372

include/pybind11/attr.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct module_local { const bool value; constexpr module_local(bool v = true) :
7171
struct arithmetic { };
7272

7373
/// Annotation to mark a function as top for the overload chain
74-
struct priority_overload { };
74+
struct prepend { };
7575

7676
/** \rst
7777
A call policy which places one or more guard variables (``Ts...``) around the function call.
@@ -138,7 +138,7 @@ struct function_record {
138138
function_record()
139139
: is_constructor(false), is_new_style_constructor(false), is_stateless(false),
140140
is_operator(false), has_args(false), has_kwargs(false), is_method(false),
141-
is_priority_overload(false) { }
141+
is_prepended(false) { }
142142

143143
/// Function name
144144
char *name = nullptr; /* why no C++ strings? They generate heavier code.. */
@@ -185,8 +185,8 @@ struct function_record {
185185
/// True if this is a method
186186
bool is_method : 1;
187187

188-
/// True if this function takes priority when defined in overload resolution
189-
bool is_priority_overload : 1;
188+
/// True if this function is to be inserted at the beginning of the overload resolution chain
189+
bool is_prepended : 1;
190190

191191
/// Number of arguments (including py::args and/or py::kwargs, if present)
192192
std::uint16_t nargs;
@@ -434,9 +434,9 @@ struct process_attribute<module_local> : process_attribute_default<module_local>
434434
static void init(const module_local &l, type_record *r) { r->module_local = l.value; }
435435
};
436436

437-
/// Process an 'priority_overload' attribute, putting this at the top of the overload chain
438-
template <> struct process_attribute<priority_overload> : process_attribute_default<priority_overload> {
439-
static void init(const priority_overload &, function_record *r) { r->is_priority_overload = true; }
437+
/// Process an 'prepend' attribute, putting this at the top of the overload chain
438+
template <> struct process_attribute<prepend> : process_attribute_default<prepend> {
439+
static void init(const prepend &, function_record *r) { r->is_prepended = true; }
440440
};
441441

442442
/// Process an 'arithmetic' attribute for enums (does nothing here)

include/pybind11/pybind11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ class cpp_function : public function {
341341
#endif
342342
);
343343

344-
if (rec->is_priority_overload) {
344+
if (rec->is_prepended) {
345345
// Beginning of chain
346346
chain_start = rec;
347347
rec->next = chain;

tests/test_methods_and_attributes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ TEST_SUBMODULE(methods_and_attributes, m) {
335335
py::class_<OverloadOrder>(m, "OverloadOrder")
336336
.def(py::init([](std::string) { return new OverloadOrder{1};}))
337337
.def(py::init([](std::string) { return new OverloadOrder{2};}))
338-
.def(py::init([](int) { return new OverloadOrder{1};}))
339-
.def(py::init([](int) { return new OverloadOrder{2};}), py::priority_overload())
338+
.def(py::init([](int) { return new OverloadOrder{3};}))
339+
.def(py::init([](int) { return new OverloadOrder{4};}), py::prepend())
340340
.def_readonly("value", &OverloadOrder::value);
341341

342342
#if !defined(PYPY_VERSION)

tests/test_methods_and_attributes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,10 @@ def test_custom_caster_destruction():
477477

478478

479479
def test_overload_order_normal():
480+
'Check to see if the normal overload order (first defined) works'
480481
assert m.OverloadOrder("this").value == 1
481482

482483

483484
def test_overload_order_priority():
484-
assert m.OverloadOrder(0).value == 2
485+
'Check to see if the prepend overload order works'
486+
assert m.OverloadOrder(0).value == 4

0 commit comments

Comments
 (0)