static RawObject memberSetter()

in ext/Objects/typeobject.cpp [1105:1290]


static RawObject memberSetter(Thread* thread, PyMemberDef* member) {
  HandleScope scope(thread);
  Runtime* runtime = thread->runtime();
  if (member->flags & READONLY) {
    Object name(&scope, runtime->newStrFromCStr(member->name));
    Function setter(
        &scope, thread->invokeFunction1(ID(builtins),
                                        ID(_new_member_set_readonly), name));
    return *setter;
  }

  Int offset(&scope, runtime->newInt(member->offset));
  switch (member->type) {
    case T_BOOL:
      return thread->invokeFunction1(ID(builtins), ID(_new_member_set_bool),
                                     offset);
    case T_BYTE: {
      Int num_bytes(&scope, runtime->newInt(sizeof(char)));
      Int min_value(&scope, runtime->newInt(std::numeric_limits<char>::min()));
      Int max_value(&scope, runtime->newInt(std::numeric_limits<char>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("char"));
      Function setter(&scope,
                      thread->invokeFunction5(
                          ID(builtins), ID(_new_member_set_integral), offset,
                          num_bytes, min_value, max_value, primitive_type));
      return *setter;
    }
    case T_UBYTE: {
      Int num_bytes(&scope, runtime->newInt(sizeof(unsigned char)));
      Int max_value(&scope, runtime->newIntFromUnsigned(
                                std::numeric_limits<unsigned char>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("unsigned char"));
      Function setter(&scope,
                      thread->invokeFunction4(
                          ID(builtins), ID(_new_member_set_integral_unsigned),
                          offset, num_bytes, max_value, primitive_type));
      return *setter;
    }
    case T_SHORT: {
      Int num_bytes(&scope, runtime->newInt(sizeof(short)));
      Int min_value(&scope, runtime->newInt(std::numeric_limits<short>::min()));
      Int max_value(&scope, runtime->newInt(std::numeric_limits<short>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("short"));
      Function setter(&scope,
                      thread->invokeFunction5(
                          ID(builtins), ID(_new_member_set_integral), offset,
                          num_bytes, min_value, max_value, primitive_type));
      return *setter;
    }
    case T_USHORT: {
      Int num_bytes(&scope, runtime->newInt(sizeof(unsigned short)));
      Int max_value(&scope, runtime->newIntFromUnsigned(
                                std::numeric_limits<unsigned short>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("unsigned short"));
      Function setter(&scope,
                      thread->invokeFunction4(
                          ID(builtins), ID(_new_member_set_integral_unsigned),
                          offset, num_bytes, max_value, primitive_type));
      return *setter;
    }
    case T_INT: {
      Int num_bytes(&scope, runtime->newInt(sizeof(int)));
      Int min_value(&scope, runtime->newInt(std::numeric_limits<int>::min()));
      Int max_value(&scope, runtime->newInt(std::numeric_limits<int>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("int"));
      Function setter(&scope,
                      thread->invokeFunction5(
                          ID(builtins), ID(_new_member_set_integral), offset,
                          num_bytes, min_value, max_value, primitive_type));
      return *setter;
    }
    case T_UINT: {
      Int num_bytes(&scope, runtime->newInt(sizeof(unsigned int)));
      Int max_value(&scope, runtime->newIntFromUnsigned(
                                std::numeric_limits<unsigned int>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("unsigned int"));
      Function setter(&scope,
                      thread->invokeFunction4(
                          ID(builtins), ID(_new_member_set_integral_unsigned),
                          offset, num_bytes, max_value, primitive_type));
      return *setter;
    }
    case T_LONG: {
      Int num_bytes(&scope, runtime->newInt(sizeof(long)));
      Int min_value(&scope, runtime->newInt(std::numeric_limits<long>::min()));
      Int max_value(&scope, runtime->newInt(std::numeric_limits<long>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("long"));
      Function setter(&scope,
                      thread->invokeFunction5(
                          ID(builtins), ID(_new_member_set_integral), offset,
                          num_bytes, min_value, max_value, primitive_type));
      return *setter;
    }
    case T_ULONG: {
      Int num_bytes(&scope, runtime->newInt(sizeof(unsigned long)));
      Int max_value(&scope, runtime->newIntFromUnsigned(
                                std::numeric_limits<unsigned long>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("unsigned long"));
      Function setter(&scope,
                      thread->invokeFunction4(
                          ID(builtins), ID(_new_member_set_integral_unsigned),
                          offset, num_bytes, max_value, primitive_type));
      return *setter;
    }
    case T_PYSSIZET: {
      Int num_bytes(&scope, runtime->newInt(sizeof(Py_ssize_t)));
      Int min_value(&scope, SmallInt::fromWord(0));
      Int max_value(&scope,
                    runtime->newInt(std::numeric_limits<Py_ssize_t>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("Py_ssize_t"));
      Function setter(&scope,
                      thread->invokeFunction5(
                          ID(builtins), ID(_new_member_set_integral), offset,
                          num_bytes, min_value, max_value, primitive_type));
      return *setter;
    }
    case T_FLOAT: {
      return thread->invokeFunction1(ID(builtins), ID(_new_member_set_float),
                                     offset);
    }
    case T_DOUBLE: {
      return thread->invokeFunction1(ID(builtins), ID(_new_member_set_double),
                                     offset);
    }
    case T_STRING: {
      Object name(&scope, runtime->newStrFromCStr(member->name));
      Function setter(&scope, thread->invokeFunction1(
                                  ID(builtins),
                                  ID(_new_member_set_readonly_strings), name));
      return *setter;
    }
    case T_STRING_INPLACE: {
      Object name(&scope, runtime->newStrFromCStr(member->name));
      Function setter(&scope, thread->invokeFunction1(
                                  ID(builtins),
                                  ID(_new_member_set_readonly_strings), name));
      return *setter;
    }
    case T_CHAR: {
      Function setter(
          &scope, thread->invokeFunction1(ID(builtins),
                                          ID(_new_member_set_char), offset));
      return *setter;
    }
    case T_OBJECT: {
      Function setter(&scope,
                      thread->invokeFunction1(
                          ID(builtins), ID(_new_member_set_pyobject), offset));
      return *setter;
    }
    case T_OBJECT_EX: {
      Function setter(&scope,
                      thread->invokeFunction1(
                          ID(builtins), ID(_new_member_set_pyobject), offset));
      return *setter;
    }
    case T_LONGLONG: {
      Int num_bytes(&scope, runtime->newInt(sizeof(long long)));
      Int min_value(&scope,
                    runtime->newInt(std::numeric_limits<long long>::min()));
      Int max_value(&scope,
                    runtime->newInt(std::numeric_limits<long long>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("long long"));
      Function setter(&scope,
                      thread->invokeFunction5(
                          ID(builtins), ID(_new_member_set_integral), offset,
                          num_bytes, min_value, max_value, primitive_type));
      return *setter;
    }
    case T_ULONGLONG: {
      Int num_bytes(&scope, runtime->newInt(sizeof(unsigned long long)));
      Int max_value(&scope,
                    runtime->newIntFromUnsigned(
                        std::numeric_limits<unsigned long long>::max()));
      Str primitive_type(&scope, runtime->newStrFromCStr("unsigned long long"));
      Function setter(&scope,
                      thread->invokeFunction4(
                          ID(builtins), ID(_new_member_set_integral_unsigned),
                          offset, num_bytes, max_value, primitive_type));
      return *setter;
    }
    default:
      return thread->raiseWithFmt(LayoutId::kSystemError,
                                  "bad member name type");
  }
}