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 csuu(obj): if not isinstance(obj, values_struct.W_RootStruct): return obj typ = obj.struct_type() fields = typ.total_field_cnt acc = [make_accessor(typ.accessor, i) for i in range(fields)] mut = [make_mutator(typ.mutator, i) for i in range(fields)] h1 = [accessor_handler] * fields h2 = [mutator_handler] * fields return imp.make_struct_proxy(imp.W_ChpStruct, obj, acc + mut, h1 + h2, [], [])
def csuu(obj): if not isinstance(obj, values_struct.W_RootStruct): return obj typ = obj.struct_type() fields = typ.total_field_count acc = [make_accessor(typ.accessor, i) for i in range(fields)] mut = [make_mutator(typ.mutator, i) for i in range(fields)] h1 = [accessor_handler] * fields h2 = [mutator_handler] * fields return imp.make_struct_proxy(imp.W_ChpStruct, obj, acc + mut, h1 + h2, [], [])
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 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)