Esempio n. 1
0
def format__Unicode_ANY(space, w_unicode, w_format_spec):
    if not space.isinstance_w(w_format_spec, space.w_unicode):
        w_format_spec = space.call_function(space.w_unicode, w_format_spec)
    from pypy.objspace.std.unicodetype import unicode_from_object
    w_unicode = unicode_from_object(space, w_unicode)
    spec = space.unicode_w(w_format_spec)
    formatter = newformat.unicode_formatter(space, spec)
    return formatter.format_string(space.unicode_w(w_unicode))
Esempio n. 2
0
 def descr__format__(self, space, w_format_spec):
     if not space.isinstance_w(w_format_spec, space.w_unicode):
         w_format_spec = space.call_function(space.w_unicode, w_format_spec)
     spec = space.unicode_w(w_format_spec)
     formatter = newformat.unicode_formatter(space, spec)
     self2 = unicode_from_object(space, self)
     assert isinstance(self2, W_UnicodeObject)
     return formatter.format_string(self2._value)
Esempio n. 3
0
 def descr__format__(self, space, w_format_spec):
     if not space.isinstance_w(w_format_spec, space.w_unicode):
         w_format_spec = space.call_function(space.w_unicode, w_format_spec)
     spec = space.unicode_w(w_format_spec)
     formatter = newformat.unicode_formatter(space, spec)
     self2 = unicode_from_object(space, self)
     assert isinstance(self2, W_UnicodeObject)
     return formatter.format_string(self2._value)
Esempio n. 4
0
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 _int_to_base method, which
    # accepts an int base rather than a str spec
    formatter = newformat.unicode_formatter(space, '')
    value = space.int_w(w_index)
    return space.newtext(formatter._int_to_base(base, value))