Deserialize a function from its components.
(self, read_context)
| 1504 | write_context.write_ref(attrs) |
| 1505 | |
| 1506 | def _deserialize_function(self, read_context): |
| 1507 | """Deserialize a function from its components.""" |
| 1508 | |
| 1509 | func_type_id = read_context.read_int8() |
| 1510 | if func_type_id == 0: |
| 1511 | policy = read_context.policy |
| 1512 | _authorize_callable_materialization(policy, types.MethodType) |
| 1513 | self_obj = read_context.read_ref() |
| 1514 | method_name = read_context.read_string() |
| 1515 | if policy is DEFAULT_POLICY: |
| 1516 | return getattr(self_obj, method_name) |
| 1517 | return _resolve_validated_bound_method(policy, self_obj, method_name, is_local=_is_local_receiver(self_obj)) |
| 1518 | |
| 1519 | if func_type_id == 1: |
| 1520 | module = read_context.read_string() |
| 1521 | qualname = read_context.read_string() |
| 1522 | mod = _resolve_validated_module_qualname(read_context.policy, module, qualname) |
| 1523 | return _validate_function_value(read_context.policy, mod, is_local=_is_local_callable(mod)) |
| 1524 | |
| 1525 | module = read_context.read_string() |
| 1526 | qualname = read_context.read_string() |
| 1527 | policy = read_context.policy |
| 1528 | mod = _import_validated_module(policy, module, is_local=_is_local_qualname(module, qualname)) |
| 1529 | _authorize_callable_materialization( |
| 1530 | policy, |
| 1531 | types.FunctionType, |
| 1532 | module=module, |
| 1533 | qualname=qualname, |
| 1534 | is_local=True, |
| 1535 | ) |
| 1536 | name = qualname.rsplit(".")[-1] |
| 1537 | |
| 1538 | marshalled_code = read_context.read_bytes_and_size() |
| 1539 | code = marshal.loads(marshalled_code) |
| 1540 | |
| 1541 | has_defaults = read_context.read_bool() |
| 1542 | defaults = None |
| 1543 | if has_defaults: |
| 1544 | num_defaults = read_context.read_var_uint32() |
| 1545 | _check_non_negative_size(num_defaults, "function default") |
| 1546 | default_values = [] |
| 1547 | for _ in range(num_defaults): |
| 1548 | default_values.append(read_context.read_ref()) |
| 1549 | defaults = tuple(default_values) |
| 1550 | |
| 1551 | has_closure = read_context.read_bool() |
| 1552 | num_freevars = read_context.read_var_uint32() |
| 1553 | _check_non_negative_size(num_freevars, "function closure") |
| 1554 | closure = None |
| 1555 | |
| 1556 | closure_values = [] |
| 1557 | if has_closure: |
| 1558 | for _ in range(num_freevars): |
| 1559 | closure_values.append(read_context.read_ref()) |
| 1560 | |
| 1561 | closure = tuple(types.CellType(value) for value in closure_values) |
| 1562 | |
| 1563 | num_freevars = read_context.read_var_uint32() |