Add custom exceptions
This commit is contained in:
parent
7f2fc476eb
commit
b547ca7158
19
python-tg.c
19
python-tg.c
@ -80,7 +80,6 @@
|
||||
extern PyTypeObject tgl_PeerType;
|
||||
extern PyTypeObject tgl_MsgType;
|
||||
|
||||
|
||||
//#include "interface.h"
|
||||
//#include "auto/constants.h"
|
||||
#include <tgl/tgl.h>
|
||||
@ -93,6 +92,12 @@ extern struct tgl_state *TLS;
|
||||
|
||||
static int python_loaded;
|
||||
|
||||
// TGL Python Exceptions
|
||||
PyObject *TglError;
|
||||
PyObject *PeerError;
|
||||
PyObject *MsgError;
|
||||
|
||||
|
||||
// Python update function callables
|
||||
PyObject *_py_binlog_end;
|
||||
PyObject *_py_diff_end;
|
||||
@ -1183,6 +1188,18 @@ MOD_INIT(tgl)
|
||||
Py_INCREF(&tgl_MsgType);
|
||||
PyModule_AddObject(m, "Msg", (PyObject *)&tgl_MsgType);
|
||||
|
||||
TglError = PyErr_NewException("tgl.Error", NULL, NULL);
|
||||
Py_INCREF(TglError);
|
||||
PyModule_AddObject(m, "TglError", TglError);
|
||||
|
||||
PeerError = PyErr_NewException("tgl.PeerError", NULL, NULL);
|
||||
Py_INCREF(PeerError);
|
||||
PyModule_AddObject(m, "PeerError", PeerError);
|
||||
|
||||
MsgError = PyErr_NewException("tgl.MsgError", NULL, NULL);
|
||||
Py_INCREF(MsgError);
|
||||
PyModule_AddObject(m, "MsgError", MsgError);
|
||||
|
||||
return MOD_SUCCESS_VAL(m);
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,11 @@
|
||||
|
||||
extern struct tgl_state *TLS;
|
||||
|
||||
// TGL Python Exceptions
|
||||
extern PyObject *TglError;
|
||||
extern PyObject *PeerError;
|
||||
extern PyObject *MsgError;
|
||||
|
||||
//
|
||||
// tgl_peer_t wrapper
|
||||
//
|
||||
@ -42,7 +47,7 @@ tgl_Peer_init(tgl_Peer *self, PyObject *args, PyObject *kwds)
|
||||
&peer_id.type,
|
||||
&peer_id.id))
|
||||
{
|
||||
PyErr_Format(PyErr_NewException("tgl.PeerInvalid", NULL, NULL), "Peer must specify type and id");
|
||||
PyErr_Format(PeerError, "Peer must specify type and id");
|
||||
return -1;
|
||||
}
|
||||
self->peer = tgl_peer_get(TLS, peer_id);
|
||||
@ -68,7 +73,7 @@ tgl_Peer_getname (tgl_Peer *self, void *closure)
|
||||
ret = PyUnicode_FromString(self->peer->encr_chat.print_name);
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
|
||||
PyErr_SetString(PeerError, "peer.type_name not supported!");
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@ -87,7 +92,7 @@ tgl_Peer_getuser_id (tgl_Peer *self, void *closure)
|
||||
ret = PyLong_FromLong(self->peer->id.id);
|
||||
break;
|
||||
case TGL_PEER_CHAT:
|
||||
PyErr_SetString(PyExc_TypeError, "peer.type_name == 'chat' has no user_id");
|
||||
PyErr_SetString(PeerError, "peer.type_name == 'chat' has no user_id");
|
||||
Py_RETURN_NONE;
|
||||
|
||||
break;
|
||||
@ -95,7 +100,7 @@ tgl_Peer_getuser_id (tgl_Peer *self, void *closure)
|
||||
ret = PyLong_FromLong(self->peer->encr_chat.user_id);
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
|
||||
PyErr_SetString(PeerError, "peer.type_name not supported!");
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@ -122,11 +127,11 @@ tgl_Peer_getuser_list (tgl_Peer *self, void *closure)
|
||||
break;
|
||||
case TGL_PEER_ENCR_CHAT:
|
||||
case TGL_PEER_USER:
|
||||
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'chat' has user_list");
|
||||
PyErr_SetString(PeerError, "Only peer.type_name == 'chat' has user_list");
|
||||
Py_RETURN_NONE;
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
|
||||
PyErr_SetString(PeerError, "peer.type_name not supported!");
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@ -149,11 +154,11 @@ tgl_Peer_getuser_status(tgl_Peer *self, void *closure)
|
||||
break;
|
||||
case TGL_PEER_CHAT:
|
||||
case TGL_PEER_ENCR_CHAT:
|
||||
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has user_status");
|
||||
PyErr_SetString(PeerError, "Only peer.type_name == 'user' has user_status");
|
||||
Py_RETURN_NONE;
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
|
||||
PyErr_SetString(PeerError, "peer.type_name not supported!");
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@ -172,11 +177,11 @@ tgl_Peer_getphone (tgl_Peer *self, void *closure)
|
||||
break;
|
||||
case TGL_PEER_CHAT:
|
||||
case TGL_PEER_ENCR_CHAT:
|
||||
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has phone");
|
||||
PyErr_SetString(PeerError, "Only peer.type_name == 'user' has phone");
|
||||
Py_RETURN_NONE;
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
|
||||
PyErr_SetString(PeerError, "peer.type_name not supported!");
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@ -195,11 +200,11 @@ tgl_Peer_getusername (tgl_Peer *self, void *closure)
|
||||
break;
|
||||
case TGL_PEER_CHAT:
|
||||
case TGL_PEER_ENCR_CHAT:
|
||||
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has username");
|
||||
PyErr_SetString(PeerError, "Only peer.type_name == 'user' has username");
|
||||
Py_RETURN_NONE;
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
|
||||
PyErr_SetString(PeerError, "peer.type_name not supported!");
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@ -218,11 +223,11 @@ tgl_Peer_getfirst_name (tgl_Peer *self, void *closure)
|
||||
break;
|
||||
case TGL_PEER_CHAT:
|
||||
case TGL_PEER_ENCR_CHAT:
|
||||
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has first_name");
|
||||
PyErr_SetString(PeerError, "Only peer.type_name == 'user' has first_name");
|
||||
Py_RETURN_NONE;
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
|
||||
PyErr_SetString(PeerError, "peer.type_name not supported!");
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@ -241,11 +246,11 @@ tgl_Peer_getlast_name (tgl_Peer *self, void *closure)
|
||||
break;
|
||||
case TGL_PEER_CHAT:
|
||||
case TGL_PEER_ENCR_CHAT:
|
||||
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has last_name");
|
||||
PyErr_SetString(PeerError, "Only peer.type_name == 'user' has last_name");
|
||||
Py_RETURN_NONE;
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
|
||||
PyErr_SetString(PeerError, "peer.type_name not supported!");
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@ -266,11 +271,11 @@ tgl_Peer_getuser (tgl_Peer *self, void *closure)
|
||||
ret = (PyObject*)self;
|
||||
break;
|
||||
case TGL_PEER_CHAT:
|
||||
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'chat' does not have user");
|
||||
PyErr_SetString(PeerError, "Only peer.type_name == 'chat' does not have user");
|
||||
Py_RETURN_NONE;
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
|
||||
PyErr_SetString(PeerError, "peer.type_name not supported!");
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@ -448,7 +453,8 @@ tgl_Msg_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||
static int
|
||||
tgl_Msg_init(tgl_Msg *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
return 0;
|
||||
PyErr_SetString(MsgError, "You cannot instantiate a tgl.Msg object, only the API can send them.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
|
34
tg-test.py
34
tg-test.py
@ -24,45 +24,33 @@ def msg_cb(success, msg):
|
||||
|
||||
HISTORY_QUERY_SIZE = 100
|
||||
|
||||
def history_cb(msg_list, ptype, pid, success, msgs):
|
||||
def history_cb(msg_list, peer, success, msgs):
|
||||
print(len(msgs))
|
||||
msg_list.extend(msgs)
|
||||
print(len(msg_list))
|
||||
if len(msgs) == HISTORY_QUERY_SIZE:
|
||||
tgl.get_history(ptype, pid, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, ptype, pid));
|
||||
tgl.get_history(peer, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, peer));
|
||||
|
||||
|
||||
|
||||
def on_msg_receive(msg):
|
||||
if msg.out and not binlog_done:
|
||||
return;
|
||||
tgl.send_msg(tgl.Peer(97704886), "Test")
|
||||
print("Peers {0}".format(msg.src.id))
|
||||
|
||||
"""
|
||||
def on_msg_receive(msg):
|
||||
if msg["out"] and not binlog_done:
|
||||
return;
|
||||
|
||||
if msg["to"]["id"] == our_id: # direct message
|
||||
ptype = msg["from"]["type"]
|
||||
pid = msg["from"]["id"]
|
||||
if msg.dest.id == our_id: # direct message
|
||||
peer = msg.src
|
||||
else: # chatroom
|
||||
ptype = msg["to"]["type"]
|
||||
pid = msg["to"]["id"]
|
||||
peer = msg.dest
|
||||
|
||||
pp.pprint(msg)
|
||||
|
||||
text = msg["text"]
|
||||
|
||||
if text.startswith("!ping"):
|
||||
if msg.text.startswith("!ping"):
|
||||
print("SENDING PONG")
|
||||
tgl.send_msg(ptype, pid, "PONG!", msg_cb)
|
||||
tgl.send_msg(peer, "PONG!", msg_cb)
|
||||
|
||||
if text.startswith("!loadhistory"):
|
||||
if msg.text.startswith("!loadhistory"):
|
||||
msg_list = []
|
||||
tgl.get_history_ext(ptype, pid, 0, HISTORY_QUERY_SIZE, partial(history_cb, msg_list, ptype, pid));
|
||||
"""
|
||||
tgl.get_history(peer, 0, HISTORY_QUERY_SIZE, partial(history_cb, msg_list, peer));
|
||||
|
||||
|
||||
def on_secret_chat_update(peer, types):
|
||||
return "on_secret_chat_update"
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user