Commit 1d1f81b2 authored by Wenzel Jakob's avatar Wenzel Jakob Committed by GitHub
Browse files

WIP: PyPy support (#527)

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.
parent c79e435e
......@@ -125,10 +125,19 @@ def test_property_rvalue_policy():
instance = TestPropRVP()
o = instance.rvalue
assert o.value == 1
def test_property_rvalue_policy_static():
"""When returning an rvalue, the return value policy is automatically changed from
`reference(_internal)` to `move`. The following would not work otherwise.
"""
from pybind11_tests import TestPropRVP
o = TestPropRVP.static_rvalue
assert o.value == 1
# https://bitbucket.org/pypy/pypy/issues/2447
@pytest.unsupported_on_pypy
def test_dynamic_attributes():
from pybind11_tests import DynamicClass, CppDerivedDynamicClass
......
......@@ -10,7 +10,6 @@
#include "pybind11_tests.h"
struct Base1 {
Base1(int i) : i(i) { }
int foo() { return i; }
......
def test_multiple_inheritance_cpp():
from pybind11_tests import MIType
......
import pytest
import gc
with pytest.suppress(ImportError):
import numpy as np
......@@ -220,7 +219,7 @@ def test_numpy_view(capture):
ac_view_2 = ac.numpy_view()
assert np.all(ac_view_1 == np.array([1, 2], dtype=np.int32))
del ac
gc.collect()
pytest.gc_collect()
assert capture == """
ArrayClass()
ArrayClass::numpy_view()
......@@ -233,12 +232,14 @@ def test_numpy_view(capture):
with capture:
del ac_view_1
del ac_view_2
gc.collect()
pytest.gc_collect()
pytest.gc_collect()
assert capture == """
~ArrayClass()
"""
@pytest.unsupported_on_pypy
@pytest.requires_numpy
def test_cast_numpy_int64_to_uint64():
from pybind11_tests.array import function_taking_uint64
......
def test_operator_overloading():
from pybind11_tests import Vector2, Vector, ConstructorStats
......
import pytest
try:
import cPickle as pickle # Use cPickle on Python 2.7
except ImportError:
......@@ -18,6 +20,7 @@ def test_roundtrip():
assert p2.extra2() == p.extra2()
@pytest.unsupported_on_pypy
def test_roundtrip_with_dict():
from pybind11_tests import PickleableWithDict
......
......@@ -185,7 +185,7 @@ struct MoveOutContainer {
test_initializer python_types([](py::module &m) {
/* No constructor is explicitly defined below. An exception is raised when
trying to construct it directly from Python */
py::class_<ExamplePythonTypes>(m, "ExamplePythonTypes", "Example 2 documentation")
py::class_<ExamplePythonTypes>(m, "ExamplePythonTypes", "Example 2 documentation", py::metaclass())
.def("get_dict", &ExamplePythonTypes::get_dict, "Return a Python dictionary")
.def("get_dict_2", &ExamplePythonTypes::get_dict_2, "Return a C++ dictionary")
.def("get_list", &ExamplePythonTypes::get_list, "Return a Python list")
......@@ -212,8 +212,7 @@ test_initializer python_types([](py::module &m) {
.def("test_print", &ExamplePythonTypes::test_print, "test the print function")
.def_static("new_instance", &ExamplePythonTypes::new_instance, "Return an instance")
.def_readwrite_static("value", &ExamplePythonTypes::value, "Static value member")
.def_readonly_static("value2", &ExamplePythonTypes::value2, "Static value member (readonly)")
;
.def_readonly_static("value2", &ExamplePythonTypes::value2, "Static value member (readonly)");
m.def("test_print_function", []() {
py::print("Hello, World!");
......
......@@ -132,8 +132,12 @@ def test_instance(capture):
assert cstats.alive() == 0
def test_docs(doc):
# PyPy does not seem to propagate the tp_docs field at the moment
def test_class_docs(doc):
assert doc(ExamplePythonTypes) == "Example 2 documentation"
def test_method_docs(doc):
assert doc(ExamplePythonTypes.get_dict) == """
get_dict(self: m.ExamplePythonTypes) -> dict
......
......@@ -206,6 +206,9 @@ def test_inheriting_repeat():
assert obj.say_everything() == "BT -7"
# PyPy: Reference count > 1 causes call with noncopyable instance
# to fail in ncv1.print_nc()
@pytest.unsupported_on_pypy
@pytest.mark.skipif(not hasattr(pybind11_tests, 'NCVirt'),
reason="NCVirt test broken on ICPC")
def test_move_support():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment