# HG changeset patch # User Paul Boddie # Date 1479945516 -3600 # Node ID c21393bbe00228a8b8ddc787081b0070bfd38eab # Parent f3ea117e51dbb49f7798f3ef5ed6132566777ad8 Added get_using support. Also added various representation methods. diff -r f3ea117e51db -r c21393bbe002 lib/__builtins__/boolean.py --- a/lib/__builtins__/boolean.py Thu Nov 24 00:29:06 2016 +0100 +++ b/lib/__builtins__/boolean.py Thu Nov 24 00:58:36 2016 +0100 @@ -20,16 +20,30 @@ """ class boolean(object): + + "The type of the True and False objects." + def __bool__(self): + "Identity operation." + return self + def __str__(self): + + "Return a string representation." + return self is True and "True" or "False" + __repr__ = __str__ + False = boolean() True = boolean() def bool(obj): + + "Evaluate 'obj' as a boolean value." + return obj.__bool__() # vim: tabstop=4 expandtab shiftwidth=4 diff -r f3ea117e51db -r c21393bbe002 lib/__builtins__/complex.py --- a/lib/__builtins__/complex.py Thu Nov 24 00:29:06 2016 +0100 +++ b/lib/__builtins__/complex.py Thu Nov 24 00:58:36 2016 +0100 @@ -19,7 +19,10 @@ this program. If not, see . """ -class complex(object): +class complex: + + "A complex number representation." + def __init__(self, real, imag=None): self.real = real self.imag = imag @@ -51,7 +54,15 @@ def __neg__(self): pass def __pos__(self): pass def __str__(self): pass - def __bool__(self): pass + + __repr__ = __str__ + + def __bool__(self): + + "Return a boolean interpretation of the number." + + return self.real and self.imag + def conjugate(self): pass # vim: tabstop=4 expandtab shiftwidth=4 diff -r f3ea117e51db -r c21393bbe002 lib/__builtins__/core.py --- a/lib/__builtins__/core.py Thu Nov 24 00:29:06 2016 +0100 +++ b/lib/__builtins__/core.py Thu Nov 24 00:58:36 2016 +0100 @@ -19,18 +19,32 @@ this program. If not, see . """ +from native import _get_using + class object: "The root class of all objects except functions." def __init__(self): + "No-operation." + pass def __bool__(self): + "Objects are true by default." + return True + def __str__(self): + + "Return a string representation." + + return self.__name__ + + __repr__ = __str__ + class function: """ @@ -48,20 +62,40 @@ self.__args__ = None def __bool__(self): + "Functions are true by default." + return True + def __str__(self): + + "Return a string representation." + + return "" # NOTE: Could be made specific. + + __repr__ = __str__ + class type(object): "The class of all classes." - pass + def __str__(self): + + "Return a string representation." + + return "" + + __repr__ = __str__ class BaseException(object): pass class Exception(BaseException): pass class UnboundMethodInvocation(Exception): pass class Warning(object): pass -def get_using(callable, instance): pass +def get_using(callable, instance): + + "Return 'callable' bound to 'instance'." + + return _get_using(callable, instance) # vim: tabstop=4 expandtab shiftwidth=4 diff -r f3ea117e51db -r c21393bbe002 lib/__builtins__/none.py --- a/lib/__builtins__/none.py Thu Nov 24 00:29:06 2016 +0100 +++ b/lib/__builtins__/none.py Thu Nov 24 00:58:36 2016 +0100 @@ -35,6 +35,8 @@ return "None" + __repr__ = __str__ + None = NoneType() # vim: tabstop=4 expandtab shiftwidth=4 diff -r f3ea117e51db -r c21393bbe002 lib/native.py --- a/lib/native.py Thu Nov 24 00:29:06 2016 +0100 +++ b/lib/native.py Thu Nov 24 00:58:36 2016 +0100 @@ -63,6 +63,8 @@ def _buffer_str(self): pass +def _get_using(callable, instance): pass + def _object_getattr(obj, name, default): pass def _isinstance(obj, cls): pass diff -r f3ea117e51db -r c21393bbe002 templates/native.c --- a/templates/native.c Thu Nov 24 00:29:06 2016 +0100 +++ b/templates/native.c Thu Nov 24 00:58:36 2016 +0100 @@ -494,6 +494,14 @@ return __new_str(s); } +__attr __fn_native__get_using(__attr __args[]) +{ + __attr * const callable = &__args[1]; + __attr * const instance = &__args[2]; + + return __replace_context(instance->value, *callable); +} + __attr __fn_native__object_getattr(__attr __args[]) { __attr * const obj = &__args[1]; diff -r f3ea117e51db -r c21393bbe002 templates/native.h --- a/templates/native.h Thu Nov 24 00:29:06 2016 +0100 +++ b/templates/native.h Thu Nov 24 00:58:36 2016 +0100 @@ -52,6 +52,8 @@ __attr __fn_native__buffer_str(__attr __args[]); +__attr __fn_native__get_using(__attr __args[]); + __attr __fn_native__object_getattr(__attr __args[]); __attr __fn_native__isinstance(__attr __args[]); diff -r f3ea117e51db -r c21393bbe002 templates/ops.c --- a/templates/ops.c Thu Nov 24 00:29:06 2016 +0100 +++ b/templates/ops.c Thu Nov 24 00:58:36 2016 +0100 @@ -118,10 +118,14 @@ __attr __test_context(__ref context, __attr attr) { + /* Preserve any existing instance context. */ + if (__is_instance(attr.context)) return attr; - if (__test_common_instance(context, __TYPEPOS(attr.context), __TYPECODE(attr.context))) + if (__is_instance(context) && __test_common_instance(context, __TYPEPOS(attr.context), __TYPECODE(attr.context))) return __replace_context(context, attr); + if (!__is_instance(context) && __test_common_type(context, __TYPEPOS(attr.context), __TYPECODE(attr.context))) + return __update_context(context, attr); /* NOTE: An error may be more appropriate. */ diff -r f3ea117e51db -r c21393bbe002 tests/methods_unbound.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/methods_unbound.py Thu Nov 24 00:58:36 2016 +0100 @@ -0,0 +1,15 @@ +class C: + def m(self, x): + return x + +def f(obj, i): + if i: + return obj.m(i) + else: + return obj.m + +c = C() +#print f(C, 1) # NOTE: Need to raise and handle error. +fn = f(C, 0) +print get_using(fn, c)(2) # 2 +print get_using(f(C, 0), c)(2) # 2