Skip to content

Commit b547ca7

Browse files
committed
Add custom exceptions
1 parent 7f2fc47 commit b547ca7

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

‎python-tg.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
extern PyTypeObject tgl_PeerType;
8181
extern PyTypeObject tgl_MsgType;
8282

83-
8483
//#include "interface.h"
8584
//#include "auto/constants.h"
8685
#include <tgl/tgl.h>
@@ -93,6 +92,12 @@ extern struct tgl_state *TLS;
9392

9493
static int python_loaded;
9594

95+
// TGL Python Exceptions
96+
PyObject *TglError;
97+
PyObject *PeerError;
98+
PyObject *MsgError;
99+
100+
96101
// Python update function callables
97102
PyObject *_py_binlog_end;
98103
PyObject *_py_diff_end;
@@ -1182,6 +1187,18 @@ MOD_INIT(tgl)
11821187

11831188
Py_INCREF(&tgl_MsgType);
11841189
PyModule_AddObject(m, "Msg", (PyObject *)&tgl_MsgType);
1190+
1191+
TglError = PyErr_NewException("tgl.Error", NULL, NULL);
1192+
Py_INCREF(TglError);
1193+
PyModule_AddObject(m, "TglError", TglError);
1194+
1195+
PeerError = PyErr_NewException("tgl.PeerError", NULL, NULL);
1196+
Py_INCREF(PeerError);
1197+
PyModule_AddObject(m, "PeerError", PeerError);
1198+
1199+
MsgError = PyErr_NewException("tgl.MsgError", NULL, NULL);
1200+
Py_INCREF(MsgError);
1201+
PyModule_AddObject(m, "MsgError", MsgError);
11851202

11861203
return MOD_SUCCESS_VAL(m);
11871204
}

‎python-types.c

+25-19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616
extern struct tgl_state *TLS;
1717

18+
// TGL Python Exceptions
19+
extern PyObject *TglError;
20+
extern PyObject *PeerError;
21+
extern PyObject *MsgError;
22+
1823
//
1924
// tgl_peer_t wrapper
2025
//
@@ -42,7 +47,7 @@ tgl_Peer_init(tgl_Peer *self, PyObject *args, PyObject *kwds)
4247
&peer_id.type,
4348
&peer_id.id))
4449
{
45-
PyErr_Format(PyErr_NewException("tgl.PeerInvalid", NULL, NULL), "Peer must specify type and id");
50+
PyErr_Format(PeerError, "Peer must specify type and id");
4651
return -1;
4752
}
4853
self->peer = tgl_peer_get(TLS, peer_id);
@@ -68,7 +73,7 @@ tgl_Peer_getname (tgl_Peer *self, void *closure)
6873
ret = PyUnicode_FromString(self->peer->encr_chat.print_name);
6974
break;
7075
default:
71-
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
76+
PyErr_SetString(PeerError, "peer.type_name not supported!");
7277
Py_RETURN_NONE;
7378
}
7479

@@ -87,15 +92,15 @@ tgl_Peer_getuser_id (tgl_Peer *self, void *closure)
8792
ret = PyLong_FromLong(self->peer->id.id);
8893
break;
8994
case TGL_PEER_CHAT:
90-
PyErr_SetString(PyExc_TypeError, "peer.type_name == 'chat' has no user_id");
95+
PyErr_SetString(PeerError, "peer.type_name == 'chat' has no user_id");
9196
Py_RETURN_NONE;
9297

9398
break;
9499
case TGL_PEER_ENCR_CHAT:
95100
ret = PyLong_FromLong(self->peer->encr_chat.user_id);
96101
break;
97102
default:
98-
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
103+
PyErr_SetString(PeerError, "peer.type_name not supported!");
99104
Py_RETURN_NONE;
100105
}
101106

@@ -122,11 +127,11 @@ tgl_Peer_getuser_list (tgl_Peer *self, void *closure)
122127
break;
123128
case TGL_PEER_ENCR_CHAT:
124129
case TGL_PEER_USER:
125-
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'chat' has user_list");
130+
PyErr_SetString(PeerError, "Only peer.type_name == 'chat' has user_list");
126131
Py_RETURN_NONE;
127132
break;
128133
default:
129-
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
134+
PyErr_SetString(PeerError, "peer.type_name not supported!");
130135
Py_RETURN_NONE;
131136
}
132137

@@ -149,11 +154,11 @@ tgl_Peer_getuser_status(tgl_Peer *self, void *closure)
149154
break;
150155
case TGL_PEER_CHAT:
151156
case TGL_PEER_ENCR_CHAT:
152-
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has user_status");
157+
PyErr_SetString(PeerError, "Only peer.type_name == 'user' has user_status");
153158
Py_RETURN_NONE;
154159
break;
155160
default:
156-
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
161+
PyErr_SetString(PeerError, "peer.type_name not supported!");
157162
Py_RETURN_NONE;
158163
}
159164

@@ -172,11 +177,11 @@ tgl_Peer_getphone (tgl_Peer *self, void *closure)
172177
break;
173178
case TGL_PEER_CHAT:
174179
case TGL_PEER_ENCR_CHAT:
175-
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has phone");
180+
PyErr_SetString(PeerError, "Only peer.type_name == 'user' has phone");
176181
Py_RETURN_NONE;
177182
break;
178183
default:
179-
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
184+
PyErr_SetString(PeerError, "peer.type_name not supported!");
180185
Py_RETURN_NONE;
181186
}
182187

@@ -195,11 +200,11 @@ tgl_Peer_getusername (tgl_Peer *self, void *closure)
195200
break;
196201
case TGL_PEER_CHAT:
197202
case TGL_PEER_ENCR_CHAT:
198-
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has username");
203+
PyErr_SetString(PeerError, "Only peer.type_name == 'user' has username");
199204
Py_RETURN_NONE;
200205
break;
201206
default:
202-
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
207+
PyErr_SetString(PeerError, "peer.type_name not supported!");
203208
Py_RETURN_NONE;
204209
}
205210

@@ -218,11 +223,11 @@ tgl_Peer_getfirst_name (tgl_Peer *self, void *closure)
218223
break;
219224
case TGL_PEER_CHAT:
220225
case TGL_PEER_ENCR_CHAT:
221-
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has first_name");
226+
PyErr_SetString(PeerError, "Only peer.type_name == 'user' has first_name");
222227
Py_RETURN_NONE;
223228
break;
224229
default:
225-
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
230+
PyErr_SetString(PeerError, "peer.type_name not supported!");
226231
Py_RETURN_NONE;
227232
}
228233

@@ -241,11 +246,11 @@ tgl_Peer_getlast_name (tgl_Peer *self, void *closure)
241246
break;
242247
case TGL_PEER_CHAT:
243248
case TGL_PEER_ENCR_CHAT:
244-
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has last_name");
249+
PyErr_SetString(PeerError, "Only peer.type_name == 'user' has last_name");
245250
Py_RETURN_NONE;
246251
break;
247252
default:
248-
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
253+
PyErr_SetString(PeerError, "peer.type_name not supported!");
249254
Py_RETURN_NONE;
250255
}
251256

@@ -266,11 +271,11 @@ tgl_Peer_getuser (tgl_Peer *self, void *closure)
266271
ret = (PyObject*)self;
267272
break;
268273
case TGL_PEER_CHAT:
269-
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'chat' does not have user");
274+
PyErr_SetString(PeerError, "Only peer.type_name == 'chat' does not have user");
270275
Py_RETURN_NONE;
271276
break;
272277
default:
273-
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
278+
PyErr_SetString(PeerError, "peer.type_name not supported!");
274279
Py_RETURN_NONE;
275280
}
276281

@@ -448,7 +453,8 @@ tgl_Msg_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
448453
static int
449454
tgl_Msg_init(tgl_Msg *self, PyObject *args, PyObject *kwds)
450455
{
451-
return 0;
456+
PyErr_SetString(MsgError, "You cannot instantiate a tgl.Msg object, only the API can send them.");
457+
return -1;
452458
}
453459

454460
static PyObject *

‎tg-test.py

+11-23
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,33 @@ def msg_cb(success, msg):
2424

2525
HISTORY_QUERY_SIZE = 100
2626

27-
def history_cb(msg_list, ptype, pid, success, msgs):
27+
def history_cb(msg_list, peer, success, msgs):
2828
print(len(msgs))
2929
msg_list.extend(msgs)
3030
print(len(msg_list))
3131
if len(msgs) == HISTORY_QUERY_SIZE:
32-
tgl.get_history(ptype, pid, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, ptype, pid));
32+
tgl.get_history(peer, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, peer));
3333

3434

3535

3636
def on_msg_receive(msg):
3737
if msg.out and not binlog_done:
38-
return;
39-
tgl.send_msg(tgl.Peer(97704886), "Test")
40-
print("Peers {0}".format(msg.src.id))
41-
42-
"""
43-
def on_msg_receive(msg):
44-
if msg["out"] and not binlog_done:
4538
return;
4639

47-
if msg["to"]["id"] == our_id: # direct message
48-
ptype = msg["from"]["type"]
49-
pid = msg["from"]["id"]
40+
if msg.dest.id == our_id: # direct message
41+
peer = msg.src
5042
else: # chatroom
51-
ptype = msg["to"]["type"]
52-
pid = msg["to"]["id"]
53-
54-
pp.pprint(msg)
43+
peer = msg.dest
5544

56-
text = msg["text"]
57-
58-
if text.startswith("!ping"):
45+
if msg.text.startswith("!ping"):
5946
print("SENDING PONG")
60-
tgl.send_msg(ptype, pid, "PONG!", msg_cb)
47+
tgl.send_msg(peer, "PONG!", msg_cb)
6148

62-
if text.startswith("!loadhistory"):
49+
if msg.text.startswith("!loadhistory"):
6350
msg_list = []
64-
tgl.get_history_ext(ptype, pid, 0, HISTORY_QUERY_SIZE, partial(history_cb, msg_list, ptype, pid));
65-
"""
51+
tgl.get_history(peer, 0, HISTORY_QUERY_SIZE, partial(history_cb, msg_list, peer));
52+
53+
6654
def on_secret_chat_update(peer, types):
6755
return "on_secret_chat_update"
6856

0 commit comments

Comments
 (0)