Skip to content

Commit a76a7fd

Browse files
committed
Merge branch to reduce complexity and improve performance of mutlimethods
2 parents 0e447d7 + 0c8fe29 commit a76a7fd

19 files changed

+190
-375
lines changed

cffi/etg.rst

-7
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,6 @@ It provides the following functions:
251251
the implementation. If ``owner`` is ``None`` the reference is leaked and
252252
``obj`` will never be deleted.
253253

254-
``wrapper_lib.LD(expression)``
255-
LD stands for "lazy default." This can be used in the default values for
256-
function parameters to delay the evaluation of an expression until the whole
257-
module has been initialized. This must be used for any default value that
258-
references a wrapped variable or type. For example:
259-
``def some_func(param=wrapper_lib.LD('Size(10, 10)'):``
260-
261254
``wrapper_lib.check_exception()``
262255
Checks for an exception set in C++ code. This should follow most calls to C++
263256
code, though potentially following cleanup code for the call.

cffi/test/test_bindgen.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,7 @@ int take_opaque_object(OpaqueType *obj)
705705
{
706706
return obj->i;
707707
}
708+
709+
void take_broken_mapped_type(BrokenMappedType b)
710+
{
711+
}

cffi/test/test_bindgen.h

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ struct IntWrapper
5050
int i;
5151
};
5252

53+
struct BrokenMappedType
54+
{
55+
};
56+
57+
void take_broken_mapped_type(BrokenMappedType b);
58+
5359
class VMethClass
5460
{
5561
public:

cffi/test/test_bindgen.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,19 @@ def create_generators(cls):
699699
return isinstance(py_obj, numbers.Number)""",
700700
placeHolder='0'))
701701

702+
module.addItem(MappedTypeDef_cffi(
703+
name='BrokenMappedType', cType='int',
704+
py2c="raise TypeError('Message')",
705+
c2py="",
706+
c2cpp="return new BrokenMappedType();",
707+
cpp2c="return 0;",
708+
instanceCheck="return True",
709+
placeHolder='0'))
710+
711+
module.addItem(FunctionDef(
712+
type='void', name='take_broken_mapped_type',
713+
items=ArgsString('(BrokenMappedType b)')))
714+
702715
module.addItem(FunctionDef(
703716
type='int', argsString='(string *str)', name='std_string_len',
704717
items=[ParamDef(name='str', type='string *')],
@@ -1132,7 +1145,7 @@ def test_pure_virtual_method_private_overriding(self):
11321145
assert o.pvmeth(20) == 10
11331146

11341147
assert o.call_pvmeth() == 16
1135-
with pytest.raises(TypeError):
1148+
with pytest.raises(NotImplementedError):
11361149
o.pvmeth()
11371150

11381151
def test_subclass_virtual_method_direct_call(self):
@@ -2041,3 +2054,12 @@ def test_docstrings(self):
20412054

20422055
def test_typedefs(self):
20432056
obj = self.mod.CtorsAlias(10)
2057+
2058+
def test_conversion_failure(self):
2059+
try:
2060+
self.mod.take_broken_mapped_type(object())
2061+
except TypeError as e:
2062+
assert e.message == 'Message'
2063+
else:
2064+
# No exception raised
2065+
assert False

cffi/wrapper_lib/__init__.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@
44
forget_ptr, take_ownership, give_ownership, keep_reference, MappedBase,
55
instancecheck, convert_to_type, init_wrapper, hassubclass, CastData)
66
from multimethod import (
7-
Multimethod, StaticMultimethod, ClassMultimethod, MMTypeCheckMeta,
8-
check_args_types)
9-
from lazy_defaults import LD, eval_func_defaults
7+
MMTypeCheckMeta, MMTypeError, MMInternalError, check_arg_type,
8+
check_args_types, raise_mm_arg_failure)
109
from annotations import create_array_type, allocate_cstring, allocate_cunicode
11-
from deprecated import deprecated
10+
from deprecated import deprecated_msg
1211
from exceptions import register_exception, check_exception
1312
from refcounting import adjust_refcount, get_refcounted_handle
1413
from abstract import (
1514
abstract_class, concrete_subclass, purevirtual_abstract_class)
1615
from voidptr import VoidPtrABC
1716
from defaults import default_arg_indicator
1817

19-
def eval_class_attrs(cls):
20-
for attr in cls.__dict__.itervalues():
21-
eval_func_defaults(attr)
22-
if isinstance(attr, (Multimethod, VirtualMethod)):
23-
attr.finalize()
24-
2518
classname_registry = {}
2619

2720
def register_classname(name, cls):

cffi/wrapper_lib/cppwrapper.py

-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import collections
33

44
from _ffi import ffi, clib
5-
from multimethod import Multimethod
65

76

87
class WrapperType(type):
@@ -175,18 +174,6 @@ def __set__(self, obj, value):
175174
for i in self.indices:
176175
obj._vdata.set_vflag(obj, i)
177176

178-
def finalize(self):
179-
if hasattr(self.func, 'finalize'):
180-
self.func.finalize()
181-
182-
def overload(self, *args, **kwargs):
183-
if not isinstance(self.func, Multimethod):
184-
raise TypeError('overload may only used on virtual multimethods')
185-
def closure(func):
186-
self.func.overload(*args, **kwargs)(func)
187-
return self
188-
return closure
189-
190177
def __repr__(self):
191178
return '<VirtualMethod%s: %s>' % (self.indices,
192179
repr(getattr(self, 'func', None)))

cffi/wrapper_lib/deprecated.py

+2-26
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
import warnings
2-
import functools
32

4-
def deprecated(arg):
5-
if isinstance(arg, str):
6-
return deprecated_method(arg)
7-
else:
8-
return deprecated_func(arg)
3+
def deprecated_msg(message):
4+
warnings.warn(message, category=DeprecationWarning, stacklevel=3)
95

10-
11-
def deprecated_func(func, message="{0}() is deprecated"):
12-
@functools.wraps(func)
13-
def wrapper(*args, **kwargs):
14-
warnings.warn(message.format(func.__name__),
15-
category=DeprecationWarning,
16-
stacklevel=3)
17-
18-
return func(*args, **kwargs)
19-
wrapper._wrapped_func = func
20-
return wrapper
21-
22-
def deprecated_method(cls_name):
23-
def closure(func):
24-
if func.__name__ == '__init__':
25-
message = cls_name + " constructor is deprecated"
26-
else:
27-
message = cls_name + ".{0}() is deprecated"
28-
return deprecated_func(func, message)
29-
return closure

cffi/wrapper_lib/lazy_defaults.py

-13
This file was deleted.

0 commit comments

Comments
 (0)