def PyNumber_ToBase(space, w_obj, base): """Returns the integer n converted to base as a string with a base marker of '0b', '0o', or '0x' if applicable. When base is not 2, 8, 10, or 16, the format is 'x#num' where x is the base. If n is not an int object, it is converted with PyNumber_Index() first. """ base = widen(base) if not (base == 2 or base == 8 or base == 10 or base == 16): # In Python3.7 this becomes a SystemError. Before that, CPython would # assert in debug or segfault in release. bpo 38643 raise oefmt(space.w_ValueError, "PyNumber_ToBase: base must be 2, 8, 10 or 16") w_index = space.index(w_obj) # A slight hack to call the internal _*_to_base method, which # accepts an int base rather than a str spec formatter = newformat.str_formatter(space, '') try: value = space.int_w(w_index) except OperationError as e: if not e.match(space, space.w_OverflowError): raise value = space.bigint_w(w_index) return space.newtext(formatter._long_to_base(base, value)) return space.newtext(formatter._int_to_base(base, value))
def descr__format__(self, space, w_format_spec): if not space.isinstance_w(w_format_spec, space.w_str): w_format_spec = space.str(w_format_spec) spec = space.str_w(w_format_spec) formatter = newformat.str_formatter(space, spec) return formatter.format_string(self._value)
def format__String_ANY(space, w_string, w_format_spec): if not space.isinstance_w(w_format_spec, space.w_str): w_format_spec = space.str(w_format_spec) spec = space.str_w(w_format_spec) formatter = newformat.str_formatter(space, spec) return formatter.format_string(w_string._value)
def descr__format__(self, space, w_format_spec): if not space.isinstance_w(w_format_spec, space.w_bytes): w_format_spec = space.str(w_format_spec) spec = space.bytes_w(w_format_spec) formatter = newformat.str_formatter(space, spec) return formatter.format_string(self)