Skip to content

add to_object to the vtable and fix numpy "linking" #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 16, 2019
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ifneq ($(SANITIZE_ADDRESS),0)
OPTLEVEL := 0
CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -static-libasan
LDFLAGS += -fsanitize=address -static-libasan
ASAN_OPTIONS=malloc_context_size=50
endif

SANITIZE_UNDEFINED ?= 0
Expand Down Expand Up @@ -89,7 +90,7 @@ TEST_INCLUDE := -I tests -I $(GTEST_DIR)/include
TESTRUNNER := tests/run

ALL_SOURCES := $(SOURCES) $(EXAMPLE_SOURCES) $(TEST_SOURCES)
ALL_HEADERS := include/$(LIBRARY)/**.h
ALL_HEADERS := include/libpy/**.h

ALL_FLAGS := 'CFLAGS=$(CFLAGS) CXXFLAGS=$(CXXFLAGS) LDFLAGS=$(LDFLAGS)'

Expand Down Expand Up @@ -130,6 +131,8 @@ example-%: examples/%
.PHONY: test
test: $(TESTRUNNER)
@GTEST_OUTPUT=$(GTEST_OUTPUT) \
ASAN_OPTIONS=$(ASAN_OPTIONS) \
LSAN_OPTIONS=$(LSAN_OPTIONS) \
LD_LIBRARY_PATH=. \
LSAN_OPTIONS=$(LSAN_OPTIONS) \
$< --gtest_filter=$(GTEST_FILTER)
Expand All @@ -139,7 +142,8 @@ gdbtest: $(TESTRUNNER)
@LD_LIBRARY_PATH=. GTEST_BREAK_ON_FAILURE=$(GTEST_BREAK) gdb -ex run $<

tests/%.o: tests/%.cc .compiler_flags
$(CXX) $(CXXFLAGS) $(INCLUDE) $(TEST_INCLUDE) -MD -fPIC -c $< -o $@
$(CXX) $(CXXFLAGS) $(INCLUDE) $(TEST_INCLUDE) -DLIBPY_COMPILING_FOR_TESTS \
-MD -fPIC -c $< -o $@

$(TESTRUNNER): gtest.a $(TEST_OBJECTS) $(SONAME)
$(CXX) -o $@ $(TEST_OBJECTS) gtest.a $(TEST_INCLUDE) \
Expand Down
5 changes: 0 additions & 5 deletions etc/format-all

This file was deleted.

32 changes: 31 additions & 1 deletion include/libpy/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <typeinfo>

#include "libpy/demangle.h"
#include "libpy/exception.h"
#include "libpy/to_object.h"

namespace py {
namespace detail {
Expand All @@ -29,6 +31,7 @@ struct any_vtable_impl {
void (*destruct)(void* addr);
bool (*ne)(const void* lhs, const void* rhs);
bool (*eq)(const void* lhs, const void* rhs);
py::scoped_ref<> (*to_object)(const void* addr);
py::util::demangled_cstring (*type_name)();
};

Expand Down Expand Up @@ -64,6 +67,18 @@ constexpr any_vtable_impl any_vtable_instance = {
[](const void* lhs, const void* rhs) -> bool {
return *static_cast<const T*>(lhs) == *static_cast<const T*>(rhs);
},
[](const void* addr) -> scoped_ref<> {
if constexpr (py::has_to_object<T>) {
return to_object(*static_cast<const T*>(addr));
}
else {
static_cast<void>(addr);
throw py::exception(PyExc_TypeError,
"cannot convert values of type ",
py::util::type_name<T>().get(),
" into Python object");
}
},
[]() { return py::util::type_name<T>(); },
};
} // namespace detail
Expand Down Expand Up @@ -165,10 +180,14 @@ class any_vtable {
return m_impl->ne(lhs, rhs);
}

inline bool eq(const void* lhs, const void* rhs) {
inline bool eq(const void* lhs, const void* rhs) const {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this just an oversight? I wonder if we could set up a linter of some sort to suggest obvious missing consts like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this just means we don't have a test for this, idk if a linter is the correct tool. These qualifications aren't "obvious" or not, they are part of the API

return m_impl->eq(lhs, rhs);
}

inline scoped_ref<> to_object(const void* addr) const {
return m_impl->to_object(addr);
}

inline py::util::demangled_cstring type_name() const {
return m_impl->type_name();
}
Expand Down Expand Up @@ -422,4 +441,15 @@ template<typename T>
any_cref make_any_cref(T& ob) {
return {&ob, any_vtable::make<T>()};
}

namespace dispatch {
/** Convert an any_ref into a Python object.
*/
template<>
struct to_object<py::any_ref> {
static PyObject* f(const py::any_ref& ref) {
return ref.vtable().to_object(ref.addr()).escape();
}
};
} // namespace dispatch
} // namespace py
3 changes: 1 addition & 2 deletions include/libpy/call_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include <tuple>

#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>

#include "libpy/numpy_utils.h"
#include "libpy/scoped_ref.h"
#include "libpy/to_object.h"

Expand Down
Loading