1.1 --- a/lib/__builtins__/boolean.py Thu Nov 24 00:29:06 2016 +0100
1.2 +++ b/lib/__builtins__/boolean.py Thu Nov 24 00:58:36 2016 +0100
1.3 @@ -20,16 +20,30 @@
1.4 """
1.5
1.6 class boolean(object):
1.7 +
1.8 + "The type of the True and False objects."
1.9 +
1.10 def __bool__(self):
1.11 +
1.12 "Identity operation."
1.13 +
1.14 return self
1.15 +
1.16 def __str__(self):
1.17 +
1.18 + "Return a string representation."
1.19 +
1.20 return self is True and "True" or "False"
1.21
1.22 + __repr__ = __str__
1.23 +
1.24 False = boolean()
1.25 True = boolean()
1.26
1.27 def bool(obj):
1.28 +
1.29 + "Evaluate 'obj' as a boolean value."
1.30 +
1.31 return obj.__bool__()
1.32
1.33 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/lib/__builtins__/complex.py Thu Nov 24 00:29:06 2016 +0100
2.2 +++ b/lib/__builtins__/complex.py Thu Nov 24 00:58:36 2016 +0100
2.3 @@ -19,7 +19,10 @@
2.4 this program. If not, see <http://www.gnu.org/licenses/>.
2.5 """
2.6
2.7 -class complex(object):
2.8 +class complex:
2.9 +
2.10 + "A complex number representation."
2.11 +
2.12 def __init__(self, real, imag=None):
2.13 self.real = real
2.14 self.imag = imag
2.15 @@ -51,7 +54,15 @@
2.16 def __neg__(self): pass
2.17 def __pos__(self): pass
2.18 def __str__(self): pass
2.19 - def __bool__(self): pass
2.20 +
2.21 + __repr__ = __str__
2.22 +
2.23 + def __bool__(self):
2.24 +
2.25 + "Return a boolean interpretation of the number."
2.26 +
2.27 + return self.real and self.imag
2.28 +
2.29 def conjugate(self): pass
2.30
2.31 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/lib/__builtins__/core.py Thu Nov 24 00:29:06 2016 +0100
3.2 +++ b/lib/__builtins__/core.py Thu Nov 24 00:58:36 2016 +0100
3.3 @@ -19,18 +19,32 @@
3.4 this program. If not, see <http://www.gnu.org/licenses/>.
3.5 """
3.6
3.7 +from native import _get_using
3.8 +
3.9 class object:
3.10
3.11 "The root class of all objects except functions."
3.12
3.13 def __init__(self):
3.14 +
3.15 "No-operation."
3.16 +
3.17 pass
3.18
3.19 def __bool__(self):
3.20 +
3.21 "Objects are true by default."
3.22 +
3.23 return True
3.24
3.25 + def __str__(self):
3.26 +
3.27 + "Return a string representation."
3.28 +
3.29 + return self.__name__
3.30 +
3.31 + __repr__ = __str__
3.32 +
3.33 class function:
3.34
3.35 """
3.36 @@ -48,20 +62,40 @@
3.37 self.__args__ = None
3.38
3.39 def __bool__(self):
3.40 +
3.41 "Functions are true by default."
3.42 +
3.43 return True
3.44
3.45 + def __str__(self):
3.46 +
3.47 + "Return a string representation."
3.48 +
3.49 + return "<function>" # NOTE: Could be made specific.
3.50 +
3.51 + __repr__ = __str__
3.52 +
3.53 class type(object):
3.54
3.55 "The class of all classes."
3.56
3.57 - pass
3.58 + def __str__(self):
3.59 +
3.60 + "Return a string representation."
3.61 +
3.62 + return "<type>"
3.63 +
3.64 + __repr__ = __str__
3.65
3.66 class BaseException(object): pass
3.67 class Exception(BaseException): pass
3.68 class UnboundMethodInvocation(Exception): pass
3.69 class Warning(object): pass
3.70
3.71 -def get_using(callable, instance): pass
3.72 +def get_using(callable, instance):
3.73 +
3.74 + "Return 'callable' bound to 'instance'."
3.75 +
3.76 + return _get_using(callable, instance)
3.77
3.78 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/lib/__builtins__/none.py Thu Nov 24 00:29:06 2016 +0100
4.2 +++ b/lib/__builtins__/none.py Thu Nov 24 00:58:36 2016 +0100
4.3 @@ -35,6 +35,8 @@
4.4
4.5 return "None"
4.6
4.7 + __repr__ = __str__
4.8 +
4.9 None = NoneType()
4.10
4.11 # vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/lib/native.py Thu Nov 24 00:29:06 2016 +0100
5.2 +++ b/lib/native.py Thu Nov 24 00:58:36 2016 +0100
5.3 @@ -63,6 +63,8 @@
5.4
5.5 def _buffer_str(self): pass
5.6
5.7 +def _get_using(callable, instance): pass
5.8 +
5.9 def _object_getattr(obj, name, default): pass
5.10
5.11 def _isinstance(obj, cls): pass
6.1 --- a/templates/native.c Thu Nov 24 00:29:06 2016 +0100
6.2 +++ b/templates/native.c Thu Nov 24 00:58:36 2016 +0100
6.3 @@ -494,6 +494,14 @@
6.4 return __new_str(s);
6.5 }
6.6
6.7 +__attr __fn_native__get_using(__attr __args[])
6.8 +{
6.9 + __attr * const callable = &__args[1];
6.10 + __attr * const instance = &__args[2];
6.11 +
6.12 + return __replace_context(instance->value, *callable);
6.13 +}
6.14 +
6.15 __attr __fn_native__object_getattr(__attr __args[])
6.16 {
6.17 __attr * const obj = &__args[1];
7.1 --- a/templates/native.h Thu Nov 24 00:29:06 2016 +0100
7.2 +++ b/templates/native.h Thu Nov 24 00:58:36 2016 +0100
7.3 @@ -52,6 +52,8 @@
7.4
7.5 __attr __fn_native__buffer_str(__attr __args[]);
7.6
7.7 +__attr __fn_native__get_using(__attr __args[]);
7.8 +
7.9 __attr __fn_native__object_getattr(__attr __args[]);
7.10
7.11 __attr __fn_native__isinstance(__attr __args[]);
8.1 --- a/templates/ops.c Thu Nov 24 00:29:06 2016 +0100
8.2 +++ b/templates/ops.c Thu Nov 24 00:58:36 2016 +0100
8.3 @@ -118,10 +118,14 @@
8.4
8.5 __attr __test_context(__ref context, __attr attr)
8.6 {
8.7 + /* Preserve any existing instance context. */
8.8 +
8.9 if (__is_instance(attr.context))
8.10 return attr;
8.11 - if (__test_common_instance(context, __TYPEPOS(attr.context), __TYPECODE(attr.context)))
8.12 + if (__is_instance(context) && __test_common_instance(context, __TYPEPOS(attr.context), __TYPECODE(attr.context)))
8.13 return __replace_context(context, attr);
8.14 + if (!__is_instance(context) && __test_common_type(context, __TYPEPOS(attr.context), __TYPECODE(attr.context)))
8.15 + return __update_context(context, attr);
8.16
8.17 /* NOTE: An error may be more appropriate. */
8.18
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/tests/methods_unbound.py Thu Nov 24 00:58:36 2016 +0100
9.3 @@ -0,0 +1,15 @@
9.4 +class C:
9.5 + def m(self, x):
9.6 + return x
9.7 +
9.8 +def f(obj, i):
9.9 + if i:
9.10 + return obj.m(i)
9.11 + else:
9.12 + return obj.m
9.13 +
9.14 +c = C()
9.15 +#print f(C, 1) # NOTE: Need to raise and handle error.
9.16 +fn = f(C, 0)
9.17 +print get_using(fn, c)(2) # 2
9.18 +print get_using(f(C, 0), c)(2) # 2