def chaperone_struct(args): if len(args) == 1 and isinstance(args[0], values_struct.W_RootStruct): return args[0] args, prop_keys, prop_vals = unpack_properties(args, "chaperone-struct") if len(args) < 1 or len(args) % 2 != 1: raise SchemeException("chaperone-struct: arity mismatch") struct, args = args[0], args[1:] if not isinstance(struct, values_struct.W_RootStruct): raise SchemeException("chaperone-struct: not given struct") all_false = True count = len(args) / 2 overrides = [None] * count handlers = [None] * count for i in range(count): ovr = args[i * 2] hnd = args[i * 2 + 1] overrides[i] = ovr handlers[i] = hnd if not imp.valid_struct_proc(ovr): raise SchemeException("chaperone-struct: not given valid field accessor") if hnd is not values.w_false: all_false = False if not hnd.iscallable() and hnd is not values.w_false: raise SchemeException("chaperone-struct: supplied hander is not a procedure") if all_false and not prop_keys: return struct return imp.W_ChpStruct(struct, overrides, handlers, prop_keys, prop_vals)
def chaperone_struct(args): args, prop_keys, prop_vals = unpack_properties(args, "chaperone-struct") if len(args) < 1 or len(args) % 2 != 1: raise SchemeException("chaperone-struct: arity mismatch") if len(args) == 1: return args[0] struct, args = args[0], args[1:] if not isinstance(struct, values_struct.W_RootStruct): raise SchemeException("chaperone-struct: not given struct") # Slicing would be nicer overrides = [args[i] for i in range(0, len(args), 2)] handlers = [args[i] for i in range(1, len(args), 2)] for i in overrides: if not imp.valid_struct_proc(i): raise SchemeException("chaperone-struct: not given valid field accessor") for i in handlers: if not i.iscallable(): raise SchemeException("chaperone-struct: supplied hander is not a procedure") return imp.W_ChpStruct(struct, overrides, handlers, prop_keys, prop_vals)
def impersonate_struct(args): if len(args) == 1 and isinstance(args[0], values_struct.W_RootStruct): return args[0] args, prop_keys, prop_vals = unpack_properties(args, "impersonate-struct") if len(args) < 1: raise SchemeException("impersonate-struct: arity mismatch") struct, args = args[0], args[1:] if args and isinstance(args[0], values_struct.W_StructType): args = args[1:] if len(args) % 2 != 0: raise SchemeException("impersonate-struct: arity mismatch") if not isinstance(struct, values_struct.W_RootStruct): raise SchemeException("impersonate-struct: not given struct") struct_type = struct.struct_type() assert isinstance(struct_type, values_struct.W_StructType) # Consider storing immutables in an easier form in the structs implementation immutables = struct_type.immutables all_false = True count = len(args) / 2 overrides = [None] * count handlers = [None] * count for i in range(count): ovr = args[i * 2] hnd = args[i * 2 + 1] overrides[i] = ovr handlers[i] = hnd if not imp.valid_struct_proc(ovr): raise SchemeException( "impersonate-struct: not given valid field accessor") elif (isinstance(ovr, values_struct.W_StructFieldMutator) and ovr.field in immutables): raise SchemeException( "impersonate-struct: cannot impersonate immutable field") elif (isinstance(ovr, values_struct.W_StructFieldAccessor) and ovr.field in immutables): raise SchemeException( "impersonate-struct: cannot impersonate immutable field") if hnd is not values.w_false: all_false = False if not hnd.iscallable() and hnd is not values.w_false: raise SchemeException( "impersonate-struct: supplied hander is not a procedure") if all_false and not prop_keys: return struct return imp.W_ImpStruct(struct, overrides, handlers, prop_keys, prop_vals)
def impersonate_struct(args): if len(args) == 1 and isinstance(args[0], values_struct.W_RootStruct): return args[0] args, prop_keys, prop_vals = unpack_properties(args, "impersonate-struct") if len(args) < 1: raise SchemeException("impersonate-struct: arity mismatch") struct, args = args[0], args[1:] if args and isinstance(args[0], values_struct.W_StructType): args = args[1:] if len(args) % 2 != 0: raise SchemeException("impersonate-struct: arity mismatch") base = imp.get_base_object(struct) if not isinstance(base, values_struct.W_RootStruct): raise SchemeException("impersonate-struct: not given struct") struct_type = base.struct_type() assert isinstance(struct_type, values_struct.W_StructType) # Consider storing immutables in an easier form in the structs implementation immutables = struct_type.immutables all_false = True count = len(args) / 2 overrides = [None] * count handlers = [None] * count for i in range(count): ovr = args[i * 2] hnd = args[i * 2 + 1] overrides[i] = ovr handlers[i] = hnd if not imp.valid_struct_proc(ovr): raise SchemeException("impersonate-struct: not given valid field accessor") elif (isinstance(ovr, values_struct.W_StructFieldMutator) and ovr.field in immutables): raise SchemeException("impersonate-struct: cannot impersonate immutable field") elif (isinstance(ovr, values_struct.W_StructFieldAccessor) and ovr.field in immutables): raise SchemeException("impersonate-struct: cannot impersonate immutable field") if hnd is not values.w_false: all_false = False if not hnd.iscallable() and hnd is not values.w_false: raise SchemeException("impersonate-struct: supplied hander is not a procedure") if all_false and not prop_keys: return struct return imp.make_struct_proxy(imp.W_ImpStruct, struct, overrides, handlers, prop_keys, prop_vals)
def chaperone_struct(args): from pycket.prims.struct_structinfo import struct_info if len(args) == 1 and isinstance(args[0], values_struct.W_RootStruct): return args[0] args, prop_keys, prop_vals = unpack_properties(args, "chaperone-struct") if len(args) < 1: raise SchemeException("chaperone-struct: arity mismatch") struct, args = args[0], args[1:] if args and isinstance(args[0], values_struct.W_StructType): args = args[1:] if len(args) % 2 != 0: raise SchemeException("chaperone-struct: arity mismatch") base = imp.get_base_object(struct) if not isinstance(base, values_struct.W_RootStruct): raise SchemeException("chaperone-struct: not given struct") all_false = True count = len(args) / 2 overrides = [None] * count handlers = [None] * count for i in range(count): ovr = args[i * 2] hnd = args[i * 2 + 1] overrides[i] = ovr handlers[i] = hnd if not imp.valid_struct_proc(ovr) and ovr is not struct_info: raise SchemeException( "chaperone-struct: not given valid field accessor") if hnd is not values.w_false: all_false = False if not hnd.iscallable() and hnd is not values.w_false: raise SchemeException( "chaperone-struct: supplied hander is not a procedure") if all_false and not prop_keys: return struct return imp.make_struct_proxy(imp.W_ChpStruct, struct, overrides, handlers, prop_keys, prop_vals)
def chaperone_struct(args): from pycket.prims.struct_structinfo import struct_info if len(args) == 1 and isinstance(args[0], values_struct.W_RootStruct): return args[0] args, prop_keys, prop_vals = unpack_properties(args, "chaperone-struct") if len(args) < 1: raise SchemeException("chaperone-struct: arity mismatch") struct, args = args[0], args[1:] if args and isinstance(args[0], values_struct.W_StructType): args = args[1:] if len(args) % 2 != 0: raise SchemeException("chaperone-struct: arity mismatch") base = imp.get_base_object(struct) if not isinstance(base, values_struct.W_RootStruct): raise SchemeException("chaperone-struct: not given struct") all_false = True count = len(args) / 2 overrides = [None] * count handlers = [None] * count for i in range(count): ovr = args[i * 2] hnd = args[i * 2 + 1] overrides[i] = ovr handlers[i] = hnd if not imp.valid_struct_proc(ovr) and ovr is not struct_info: raise SchemeException("chaperone-struct: not given valid field accessor") if hnd is not values.w_false: all_false = False if not hnd.iscallable() and hnd is not values.w_false: raise SchemeException("chaperone-struct: supplied hander is not a procedure") if all_false and not prop_keys: return struct return imp.make_struct_proxy(imp.W_ChpStruct, struct, overrides, handlers, prop_keys, prop_vals)
def chaperone_struct(args): if len(args) == 1 and isinstance(args[0], values_struct.W_RootStruct): return args[0] args, prop_keys, prop_vals = unpack_properties(args, "chaperone-struct") if len(args) < 1 or len(args) % 2 != 1: raise SchemeException("chaperone-struct: arity mismatch") struct, args = args[0], args[1:] if not isinstance(struct, values_struct.W_RootStruct): raise SchemeException("chaperone-struct: not given struct") all_false = True count = len(args) / 2 overrides = [None] * count handlers = [None] * count for i in range(count): ovr = args[i * 2] hnd = args[i * 2 + 1] overrides[i] = ovr handlers[i] = hnd if not imp.valid_struct_proc(ovr): raise SchemeException( "chaperone-struct: not given valid field accessor") if hnd is not values.w_false: all_false = False if not hnd.iscallable() and hnd is not values.w_false: raise SchemeException( "chaperone-struct: supplied hander is not a procedure") if all_false and not prop_keys: return struct return imp.W_ChpStruct(struct, overrides, handlers, prop_keys, prop_vals)
def impersonate_struct(args): args, prop_keys, prop_vals = unpack_properties(args, "impersonate-struct") if len(args) < 1 or len(args) % 2 != 1: raise SchemeException("impersonate-struct: arity mismatch") if len(args) == 1: return args[0] struct, args = args[0], args[1:] if not isinstance(struct, values_struct.W_RootStruct): raise SchemeException("impersonate-struct: not given struct") struct_type = struct.struct_type() assert isinstance(struct_type, values_struct.W_StructType) # Consider storing immutables in an easier form in the structs implementation immutables = struct_type.immutables # Slicing would be nicer overrides = [args[i] for i in range(0, len(args), 2)] handlers = [args[i] for i in range(1, len(args), 2)] for i in overrides: if not imp.valid_struct_proc(i): raise SchemeException("impersonate-struct: not given valid field accessor") elif (isinstance(i, values_struct.W_StructFieldMutator) and i.field.value in immutables): raise SchemeException("impersonate-struct: cannot impersonate immutable field") elif (isinstance(i, values_struct.W_StructFieldAccessor) and i.field.value in immutables): raise SchemeException("impersonate-struct: cannot impersonate immutable field") for i in handlers: if not i.iscallable(): raise SchemeException("impersonate-struct: supplied hander is not a procedure") return imp.W_ImpStruct(struct, overrides, handlers, prop_keys, prop_vals)