Exemple #1
0
def do_from(t):
    mod = t.items[0]
    names = t.items[1]
    mod = mod.as_string()

    if names.type == 'largs':
        # it really shouldn't be largs -- need to fix the parser.
        if names.val == '*':
            names = names.as_string()
    elif names.type == 'name':
        names = Token(names.pos, 'tuple', None, [names.as_string()])
    elif names.type == 'tuple':
        names = Token(names.pos, 'tuple', None,
                      [i.as_string() for i in names.items])
    else:
        tokenize.u_error('SyntaxError', D.code, t.pos)

    v = do(
        Token(t.pos, 'call', None, [
            Token(t.pos, 'name', '__import__'),
            mod,
            names,
        ]))

    g = do(Token(t.pos, 'name', '__dict__'))
    code(UPDATE, g, v)
    free_tmp(g)
    free_tmp(v)
Exemple #2
0
def do_class(tok):
    items = tok.items
    parent = None
    if items[0].type == 'name':
        name = items[0]
        parent = Token(tok.pos, 'name', 'object')
    else:
        name = items[0].items[0]
        parent = items[0].items[1]

    kls = get_tmp()
    code(CLASS, kls)
    un_tmp(kls)
    ts = do_string(name.as_string())
    code(GSET, ts, kls)
    free_tmp(ts)  #REG

    free_tmp(
        do(
            Token(tok.pos, 'call', None, [
                Token(tok.pos, 'name', 'setmeta'),
                Token(tok.pos, 'reg', kls), parent
            ])))
    free_reg(kls)  #REG

    # define a function for the class body, and call it.
    rf, t = deffnc(tok, [], 'end')

    D.begin()
    setpos(tok.pos)
    # Run the class body.
    free_tmp(do(items[1]))  #REG
    # merge the local variables to the class:

    # we must refetch the kls object, because after begin() the kls reg is
    # invalidated.
    ts = do_string(name.as_string())
    kls = get_tmp()
    code(GGET, kls, ts)
    un_tmp(kls)
    free_tmp(ts)  #REG
    for val in D.vars:
        val_name = Token(tok.pos, 'name', val)
        ts = do_string(val_name.as_string())
        code(SET, kls, ts, get_reg(val))
        free_tmp(ts)  #REG
    free_reg(kls)
    D.end()

    tag(t, 'end')

    tmp, lparams, dparams = get_tmps(3)
    _do_none(lparams)
    _do_none(dparams)
    code(CALL, tmp, rf, lparams)
    free_tmp(rf)
    free_tmp(tmp)
    free_tmp(lparams)
    free_tmp(dparams)