Exemple #1
0
/* Socket connect method.
 *
 * Example:
 * s.connect('10.10.1.1', 7000) #Connects to 10.10.1.1:7000
 */
tp_obj kolibri_connect(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    tp_obj  remote_addr_obj = TP_OBJ();
    __u32  remote_addr;
    __u32  remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
    __u32  local_port  = tp_get(tp, self, tp_string("local_port")).number.val;
    __u32  socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
    int  s = -1; /* Socket descriptor */


    if (remote_addr_obj.type == TP_NUMBER)
        remote_addr = remote_addr_obj.number.val;
    else if (remote_addr_obj.type == TP_STRING)
        inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);

    if (socktype == SOCK_STREAM)
        s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 1);
    else if (socktype == SOCK_DGRAM)
        s = __menuet__open_UDP_socket(local_port, remote_port, remote_addr);
    if (s >= 0)
    {
        tp_set(tp, self, tp_string("socket"), tp_number(s));
        return tp_True;
    }
    else
        return tp_False;
}
Exemple #2
0
/* Socket listen method.
 * 
 * Example:
 * s.listen('10.10.1.1', 5000)
 */
tp_obj kolibri_listen(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    tp_obj remote_addr_obj = TP_OBJ();
    __u32  remote_addr;
    __u32  remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
    __u32  local_port  = tp_get(tp, self, tp_string("local_port")).number.val;
    __u32  socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
    int  s = -1; /* Socket descriptor */

    if (socktype != SOCK_STREAM)
        tp_raise(tp_None, "IOError: attempt to listen on non-TCP socket", tp_None);

    if (remote_addr_obj.type == TP_NUMBER)
        remote_addr = remote_addr_obj.number.val;
    else if (remote_addr_obj.type == TP_STRING)
        inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);

    if ((s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 0)) >= 0)
    {
        tp_set(tp, self, tp_string("socket"), tp_number(s));
        return tp_True;
    }
    else
        return tp_False;
}
Exemple #3
0
/* utility functions */
Uint32 pygame_list_to_color(TP,tp_obj clr,SDL_Surface *s) {
    int r,g,b;
    r = tp_get(tp,clr,tp_number(0)).number.val;
    g = tp_get(tp,clr,tp_number(1)).number.val;
    b = tp_get(tp,clr,tp_number(2)).number.val;
    return SDL_MapRGB(s->format,r,g,b);
}
Exemple #4
0
/**
   \fn  asObjectPointer
*/
void *TinyParams::asObjectPointer(void)
{
	preamble(TP_DICT);
	tp_obj cdata = tp_get(tp, obj, tp_string("cdata"));
	return cdata.data.val;

}
static tp_obj zzpy__pyDFMenu_help(TP)
{
	PythonEngine *engine = (PythonEngine*)tp_get(tp, tp->builtins, tp_string("userdata")).data.val;

	engine->callEventHandlers(IScriptEngine::Information, NULL, -1, "addItem(str)\n");

	return tp_None;
};
Exemple #6
0
/* Function: tp_iter
 * Iterate through a list or dict.
 *
 * If self is a list/string/dictionary, this will iterate over the
 * elements/characters/keys respectively, if k is an increasing index
 * starting with 0 up to the length of the object-1.
 *
 * In the case of a list of string, the returned items will correspond to the
 * item at index k. For a dictionary, no guarantees are made about the order.
 * You also cannot call the function with a specific k to get a specific
 * item -- it is only meant for iterating through all items, calling this
 * function len(self) times. Use <tp_get> to retrieve a specific item, and
 * <tp_len> to get the length.
 *
 * Parameters:
 * self - The object over which to iterate.
 * k - You must pass 0 on the first call, then increase it by 1 after each call,
 *     and don't call the function with k >= len(self).
 *
 * Returns:
 * The first (k = 0) or next (k = 1 .. len(self)-1) item in the iteration.
 */
tp_obj tp_iter(TP,tp_obj self, tp_obj k) {
    int type = self.type;
    if (type == TP_LIST || type == TP_STRING) { return tp_get(tp,self,k); }
    if (type == TP_DICT && k.type == TP_NUMBER) {
        return self.dict.val->items[_tp_dict_next(tp,self.dict.val)].key;
    }
    tp_raise(tp_None,tp_string("(tp_iter) TypeError: iteration over non-sequence"));
}
Exemple #7
0
tp_obj tp_iter(TP,tp_obj self, tp_obj k) {
    int type = obj_type(self);
    if (type == TP_LIST || type == TP_STRING) { return tp_get(tp,self,k); }
    if (type == TP_DICT && obj_type(k) == TP_NUMBER) {
        return tp_dict_val(self)->items[_tp_dict_next(tp, tp_dict_val(self))].key;
    }
    tp_raise(None,"tp_iter(%s,%s)",STR(self),STR(k));
}
tp_obj zzpy__pyDFMenu_get(tp_vm *vm)
{
  tp_obj self = tp_getraw(vm);
  IScriptEngine *engine = (IScriptEngine*)tp_get(vm, vm->builtins, tp_string("userdata")).data.val;
  IEditor *editor = engine->editor();
  TinyParams pm(vm);
  ADM_scriptDFMenuHelper *me=(ADM_scriptDFMenuHelper *)pm.asThis(&self, ADM_PYID_DF_INTEGER);
  char const *key = pm.asString();
  if (!strcmp(key, "index"))
  {
     if(!me) pm.raise("pyDFMenu:No this!");
     return tp_number(me->index());
  }
  if (!strcmp(key, "addItem"))
  {
     return tp_method(vm, self, zzpy_addItem);
  }
  return tp_get(vm, self, tp_string(key));
}
Exemple #9
0
tp_obj tp_iter(TP,tp_obj self, tp_obj k) {
    int type = self.type;
    if (type == TP_LIST || type == TP_STRING) {
        return tp_get(tp,self,k);
    }
    if (type == TP_DICT && k.type == TP_NUMBER) {
        return self.dict.val->items[_tp_dict_next(tp,self.dict.val)].key;
    }
    tp_raise(tp_None,"tp_iter(%s,%s)",TP_CSTR(self),TP_CSTR(k));
}
Exemple #10
0
void *TinyParams::asThis(tp_obj *self, int id)
{
	tp_obj cdata = tp_get(tp, *self, tp_string("cdata"));

	if (cdata.data.magic != id)
	{
		raise("Bad class : Expected %d, got %d\n", id, cdata.data.magic);
		\
	}

	return cdata.data.val;
}
Exemple #11
0
/* Function: tp_iget
 * Failsafe attribute lookup.
 *
 * This is like <tp_get>, except it will return false if the attribute lookup
 * failed. Otherwise, it will return true, and the object will be returned
 * over the reference parameter r.
 */
int tp_iget(TP,tp_obj *r, tp_obj self, tp_obj k) {
    if (self.type == TP_DICT) {
        int n = _tp_dict_find(tp,self.dict.val,k);
        if (n == -1) { return 0; }
        *r = self.dict.val->items[n].val;
        tp_grey(tp,*r);
        return 1;
    }
    if (self.type == TP_LIST && !self.list.val->len) { return 0; }
    *r = tp_get(tp,self,k); tp_grey(tp,*r);
    return 1;
}
Exemple #12
0
int tp_iget(TP,tp_obj *r, tp_obj self, tp_obj k) {
    if (obj_type(self) == TP_DICT) {
        int n = _tp_dict_find(tp, tp_dict_val(self),k);
        if (n == -1) { 
            return 0; 
        }
        *r = tp_dict_val(self)->items[n].val;
        tp_grey(tp,*r);
        return 1;
    }
    if (obj_type(self) == TP_LIST && !tp_list_val(self)->len) { return 0; }
    *r = tp_get(tp,self,k); tp_grey(tp,*r);
    return 1;
}
Exemple #13
0
/* Socket close method.
 *
 * Example: 
 * s.close() # s must be a socket object created by socket.
 *
 * Raises exception if socket was not opened. Otherwise returns True.
 */
static tp_obj kolibri_close_socket(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    __u32  socktype;
    __u32  s;

    GET_SOCKET_DESCRIPTOR(self, s);

    socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
    GET_SOCKET_DESCRIPTOR(self, s);
    if (socktype == SOCK_STREAM)
        __menuet__close_TCP_socket(s);
    else if (socktype == SOCK_DGRAM)
        __menuet__close_UDP_socket(s);
    return tp_True;
}
tp_obj zzpy__pyDFMenu_set(tp_vm *vm)
{
  tp_obj self = tp_getraw(vm);
  IScriptEngine *engine = (IScriptEngine*)tp_get(vm, vm->builtins, tp_string("userdata")).data.val;
  IEditor *editor = engine->editor();
  TinyParams pm(vm);
  ADM_scriptDFMenuHelper *me = (ADM_scriptDFMenuHelper *)pm.asThis(&self, ADM_PYID_DF_INTEGER);
  char const *key = pm.asString();
  if (!strcmp(key, "index"))
  {
     if(!me) pm.raise("pyDFMenu:No this!");
     int val = pm.asInt();
     me->setIndex(val);
     return tp_None;
  }
  return tp_None;
}
Exemple #15
0
/* Socket send method.
 *
 * Example:
 * data="<html><head><title>Preved!!!</title></head><body>Example.</body></html>"
 * s.send(data)
 * or:
 * s.send(data, 20) # Send just 20 bytes
 */
static tp_obj kolibri_send(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    tp_obj data_obj = TP_TYPE(TP_STRING);
    __u32  datalen = TP_DEFAULT(tp_False).number.val;
    __u32  socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
    __u32  s;
    int result;

    GET_SOCKET_DESCRIPTOR(self, s);

    if (datalen < 0 || datalen > data_obj.string.len)
        datalen = data_obj.string.len;
    if (socktype == SOCK_STREAM)
        result = __menuet__write_TCP_socket(s, datalen, (void *)data_obj.string.val);
    else if (socktype == SOCK_DGRAM)
        result = __menuet__write_UDP_socket(s, datalen, (void *)data_obj.string.val);
    return tp_number(!(result != 0));
}
Exemple #16
0
/* Function: tp_get
 * Attribute lookup.
 * 
 * This returns the result of using self[k] in actual code. It works for
 * dictionaries (including classes and instantiated objects), lists and strings.
 *
 * As a special case, if self is a list, self[None] will return the first
 * element in the list and subsequently remove it from the list.
 */
tp_obj tp_get(TP,tp_obj self, tp_obj k) {
    int type = self.type;
    tp_obj r;
    if (type == TP_DICT) {
        TP_META_BEGIN(self,"__get__");
            return tp_call(tp,meta,tp_params_v(tp,1,k));
        TP_META_END;
        if (self.dict.dtype && _tp_lookup(tp,self,k,&r)) { return r; }
        return _tp_dict_get(tp,self.dict.val,k,"tp_get");
    } else if (type == TP_LIST) {
        if (k.type == TP_NUMBER) {
            int l = tp_len(tp,self).number.val;
            int n = k.number.val;
            n = (n<0?l+n:n);
            return _tp_list_get(tp,self.list.val,n,"tp_get");
        } else if (k.type == TP_STRING) {
            if (tp_cmp(tp,tp_string("append"),k) == 0) {
                return tp_method(tp,self,tp_append);
            } else if (tp_cmp(tp,tp_string("pop"),k) == 0) {
                return tp_method(tp,self,tp_pop);
            } else if (tp_cmp(tp,tp_string("index"),k) == 0) {
                return tp_method(tp,self,tp_index);
            } else if (tp_cmp(tp,tp_string("sort"),k) == 0) {
                return tp_method(tp,self,tp_sort);
            } else if (tp_cmp(tp,tp_string("extend"),k) == 0) {
                return tp_method(tp,self,tp_extend);
            } else if (tp_cmp(tp,tp_string("*"),k) == 0) {
                tp_params_v(tp,1,self);
                r = tp_copy(tp);
                self.list.val->len=0;
                return r;
            }
        } else if (k.type == TP_NONE) {
            return _tp_list_pop(tp,self.list.val,0,"tp_get");
        }
    } else if (type == TP_STRING) {
        if (k.type == TP_NUMBER) {
            int l = self.string.len;
            int n = k.number.val;
            n = (n<0?l+n:n);
            if (n >= 0 && n < l) { return tp_string_n(tp->chars[(unsigned char)self.string.val[n]],1); }
        } else if (k.type == TP_STRING) {
            if (tp_cmp(tp,tp_string("join"),k) == 0) {
                return tp_method(tp,self,tp_join);
            } else if (tp_cmp(tp,tp_string("split"),k) == 0) {
                return tp_method(tp,self,tp_split);
            } else if (tp_cmp(tp,tp_string("index"),k) == 0) {
                return tp_method(tp,self,tp_str_index);
            } else if (tp_cmp(tp,tp_string("strip"),k) == 0) {
                return tp_method(tp,self,tp_strip);
            } else if (tp_cmp(tp,tp_string("replace"),k) == 0) {
                return tp_method(tp,self,tp_replace);
            }
        }
    }

    if (k.type == TP_LIST) {
        int a,b,l;
        tp_obj tmp;
        l = tp_len(tp,self).number.val;
        tmp = tp_get(tp,k,tp_number(0));
        if (tmp.type == TP_NUMBER) { a = tmp.number.val; }
        else if(tmp.type == TP_NONE) { a = 0; }
        else { tp_raise(tp_None,tp_string("(tp_get) TypeError: indices must be numbers")); }
        tmp = tp_get(tp,k,tp_number(1));
        if (tmp.type == TP_NUMBER) { b = tmp.number.val; }
        else if(tmp.type == TP_NONE) { b = l; }
        else { tp_raise(tp_None,tp_string("(tp_get) TypeError: indices must be numbers")); }
        a = _tp_max(0,(a<0?l+a:a)); b = _tp_min(l,(b<0?l+b:b));
        if (type == TP_LIST) {
            return tp_list_n(tp,b-a,&self.list.val->items[a]);
        } else if (type == TP_STRING) {
/*            tp_obj r = tp_string_t(tp,b-a);
            char *ptr = r.string.info->s;
            memcpy(ptr,self.string.val+a,b-a); ptr[b-a]=0;
            return tp_track(tp,r);*/
            return tp_string_slice(tp,self,a,b);
        }
    }

    tp_raise(tp_None,tp_string("(tp_get) TypeError: ?"));
}
Exemple #17
0
tp_obj tp_get(tp_vm *tp, tp_obj self, tp_obj k) {
    int type = obj_type(self);
    tp_obj r;
    if (type == TP_DICT) {
        return _tp_dict_get(tp, tp_dict_val(self), k, "tp_get");
    } else if (type == TP_LIST) {
        if (obj_type(k) == TP_NUMBER) {
            int l = tp_number_val(tp_len(tp,self));
            int n = tp_number_val(k);
            n = (n<0?l+n:n);
            return _tp_list_get(tp,tp_list_val(self),n,"tp_get");
        } else if (obj_type(k) == TP_STRING) {
            if (strcmp("append",STR(k)) == 0) {
                return tp_method(tp,self,tp_append);
            } else if (strcmp("pop",STR(k)) == 0) {
                return tp_method(tp,self,tp_pop);
            } else if (strcmp("index",STR(k)) == 0) {
                return tp_method(tp,self,tp_index);
            } else if (strcmp("sort",STR(k)) == 0) {
                return tp_method(tp,self,tp_sort);
            } else if (strcmp("extend",STR(k)) == 0) {
                return tp_method(tp,self,tp_extend);
            } else if (strcmp("*",STR(k)) == 0) {
                tp_params_v(tp,1,self); 
                r = tp_copy(tp);
                tp_list_val(self)->len=0;
                return r;
            }
        } else if (obj_type(k) == TP_NONE) {
            return _tp_list_pop(tp,tp_list_val(self),0,"tp_get");
        }
    } else if (type == TP_STRING) {
        if (obj_type(k) == TP_NUMBER) {
            int l = tp_str_len(self);
            int n = tp_number_val(k);
            n = (n<0?l+n:n);
            if (n >= 0 && n < l) { 
                return tp_string_n(tp, tp->chars[(unsigned char)tp_str_val(self)[n]],1);
            }
        } else if (obj_type(k) == TP_STRING) {
            if (strcmp("join",STR(k)) == 0) {
                return tp_method(tp,self,tp_join);
            } else if (strcmp("split",STR(k)) == 0) {
                return tp_method(tp,self,tp_split);
            } else if (strcmp("index",STR(k)) == 0) {
                return tp_method(tp,self,tp_str_index);
            } else if (strcmp("strip",STR(k)) == 0) {
                return tp_method(tp,self,tp_strip);
            } else if (strcmp("replace",STR(k)) == 0) {
                return tp_method(tp,self,tp_replace);
            }
        }
    } else if (type == TP_DATA) {
        return tp_data_meta(self)->get(tp,self,k);
    }

    if (obj_type(k) == TP_LIST) {
        int a,b,l;
        tp_obj tmp;
        l = tp_number_val(tp_len(tp,self));
        tmp = tp_get(tp, k, tp_number(tp, 0));
        if (obj_type(tmp) == TP_NUMBER) { 
            a = tp_number_val(tmp); 
        } else if (obj_type(tmp) == TP_NONE) { 
            a = 0; 
        } else { 
            tp_raise(None,"%s is not a number",STR(tmp)); 
        }
        tmp = tp_get(tp, k, tp_number(tp, 1));
        if (obj_type(tmp) == TP_NUMBER) {
            b = tp_number_val(tmp);
        } else if (obj_type(tmp) == TP_NONE) {
            b = l;
        } else {
            tp_raise(None,"%s is not a number",STR(tmp));
        }
        a = _tp_max(0,(a<0?l+a:a));
        b = _tp_min(l,(b<0?l+b:b));
        if (type == TP_LIST) {
            return tp_list_n(tp,b-a, &tp_list_val(self)->items[a]);
        } else if (type == TP_STRING) {
            tp_obj r = tp_string_t(tp, b-a);
            char *ptr = tp_str_val(r);
            memcpy(ptr,tp_str_val(self)+a,b-a);
            ptr[b-a]=0;
            return tp_track(tp,r);
        }
    }

    tp_raise(None,"tp_get(%s,%s)",STR(self),STR(k));
}
Exemple #18
0
tp_obj tp_get(TP,tp_obj self, tp_obj k) {
    int type = self.type;
    tp_obj r;

    if (type == TP_DICT) {
        return _tp_dict_get(tp,self.dict.val,k,"tp_get");
    } else if (type == TP_LIST) {
        if (k.type == TP_NUMBER) {
            int l = tp_len(tp,self).number.val;
            int n = k.number.val;
            n = (n<0?l+n:n);
            return _tp_list_get(tp,self.list.val,n,"tp_get");
        } else if (k.type == TP_STRING) {
            if (strcmp("append",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_append);
            } else if (strcmp("pop",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_pop);
            } else if (strcmp("index",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_index);
            } else if (strcmp("sort",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_sort);
            } else if (strcmp("extend",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_extend);
            } else if (strcmp("*",TP_CSTR(k)) == 0) {
                tp_params_v(tp,1,self);
                r = tp_copy(tp);
                self.list.val->len=0;
                return r;
            }
        } else if (k.type == TP_NONE) {
            return _tp_list_pop(tp,self.list.val,0,"tp_get");
        }
    } else if (type == TP_STRING) {
        if (k.type == TP_NUMBER) {
            int l = self.string.len;
            int n = k.number.val;
            n = (n<0?l+n:n);
            if (n >= 0 && n < l) {
                return tp_string_n(tp->chars[(unsigned char)self.string.val[n]],1);
            }
        } else if (k.type == TP_STRING) {
            if (strcmp("join",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_join);
            } else if (strcmp("split",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_split);
            } else if (strcmp("index",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_str_index);
            } else if (strcmp("strip",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_strip);
            } else if (strcmp("replace",TP_CSTR(k)) == 0) {
                return tp_method(tp,self,tp_replace);
            }
        }
    }

    if (k.type == TP_LIST) {
        int a,b,l;
        tp_obj tmp;
        l = tp_len(tp,self).number.val;
        tmp = tp_get(tp,k,tp_number(0));
        if (tmp.type == TP_NUMBER) {
            a = tmp.number.val;
        }
        else if(tmp.type == TP_NONE) {
            a = 0;
        }
        else {
            tp_raise(tp_None,"%s is not a number",TP_CSTR(tmp));
        }
        tmp = tp_get(tp,k,tp_number(1));
        if (tmp.type == TP_NUMBER) {
            b = tmp.number.val;
        }
        else if(tmp.type == TP_NONE) {
            b = l;
        }
        else {
            tp_raise(tp_None,"%s is not a number",TP_CSTR(tmp));
        }
        a = _tp_max(0,(a<0?l+a:a));
        b = _tp_min(l,(b<0?l+b:b));
        if (type == TP_LIST) {
            return tp_list_n(tp,b-a,&self.list.val->items[a]);
        } else if (type == TP_STRING) {
            tp_obj r = tp_string_t(tp,b-a);
            char *ptr = r.string.info->s;
            memcpy(ptr,self.string.val+a,b-a);
            ptr[b-a]=0;
            return tp_track(tp,r);
        }
    }

    tp_raise(tp_None,"tp_get(%s,%s)",TP_CSTR(self),TP_CSTR(k));
}
Exemple #19
0
/* Function: tp_class
 * Creates a new base class.
 *
 * Parameters:
 * none
 *
 * Returns:
 * A new, empty class (derived from tinypy's builtin "object" class).
 */
tp_obj tp_class(TP) {
    tp_obj klass = tp_dict(tp);
    klass.dict.val->meta = tp_get(tp,tp->builtins,tp_string("object")); 
    return klass;
}