Ejemplo n.º 1
0
def wrapchar(space, c):
    from pypy.objspace.std.stringobject import W_StringObject
    from pypy.objspace.std.ropeobject import rope, W_RopeObject
    if space.config.objspace.std.withprebuiltchar:
        if space.config.objspace.std.withrope:
            return W_RopeObject.PREBUILT[ord(c)]
        return W_StringObject.PREBUILT[ord(c)]
    else:
        if space.config.objspace.std.withrope:
            return W_RopeObject(rope.LiteralStringNode(c))
        return W_StringObject(c)
Ejemplo n.º 2
0
def descr__new__(space, w_stringtype, w_object=''):
    # NB. the default value of w_object is really a *wrapped* empty string:
    #     there is gateway magic at work
    from pypy.objspace.std.stringobject import W_StringObject
    w_obj = space.str(w_object)
    if space.is_w(w_stringtype, space.w_str):
        return w_obj  # XXX might be reworked when space.str() typechecks
    value = space.str_w(w_obj)
    if space.config.objspace.std.withrope:
        from pypy.objspace.std.ropeobject import rope, W_RopeObject
        w_obj = space.allocate_instance(W_RopeObject, w_stringtype)
        W_RopeObject.__init__(w_obj, rope.LiteralStringNode(value))
        return w_obj
    else:
        w_obj = space.allocate_instance(W_StringObject, w_stringtype)
        W_StringObject.__init__(w_obj, value)
        return w_obj
Ejemplo n.º 3
0
def wrapstr(space, s):
    from pypy.objspace.std.stringobject import W_StringObject
    from pypy.objspace.std.ropeobject import rope, W_RopeObject
    if space.config.objspace.std.sharesmallstr:
        if space.config.objspace.std.withprebuiltchar:
            # share characters and empty string
            if len(s) <= 1:
                if len(s) == 0:
                    if space.config.objspace.std.withrope:
                        return W_RopeObject.EMPTY
                    return W_StringObject.EMPTY
                else:
                    s = s[0]  # annotator hint: a single char
                    return wrapchar(space, s)
        else:
            # only share the empty string
            if len(s) == 0:
                if space.config.objspace.std.withrope:
                    return W_RopeObject.EMPTY
                return W_StringObject.EMPTY
    if space.config.objspace.std.withrope:
        return W_RopeObject(rope.LiteralStringNode(s))
    return W_StringObject(s)