Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
tg
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
tg
Commits
faaea75e
Commit
faaea75e
authored
May 07, 2015
by
Vincent Castellano
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Protect against Py_DECREFing nulls. Add datetime support
parent
66aa288b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
10 deletions
+23
-10
pytest.py
pytest.py
+2
-0
python-tg.c
python-tg.c
+21
-10
No files found.
pytest.py
View file @
faaea75e
...
...
@@ -30,6 +30,8 @@ def on_msg_receive(msg):
ptype
=
msg
[
"to"
][
"type"
]
pid
=
msg
[
"to"
][
"id"
]
pp
.
pprint
(
msg
)
text
=
msg
[
"text"
]
if
text
.
startswith
(
"!ping"
):
...
...
python-tg.c
View file @
faaea75e
...
...
@@ -38,6 +38,9 @@
#include "event-old.h"
#endif
// Python Imports
#include "datetime.h"
//#include "interface.h"
//#include "auto/constants.h"
#include <tgl/tgl.h>
...
...
@@ -47,6 +50,7 @@
extern
int
verbosity
;
extern
struct
tgl_state
*
TLS
;
static
int
python_loaded
;
// Python update function callables
...
...
@@ -61,6 +65,12 @@ PyObject *_py_chat_update;
PyObject
*
get_user
(
tgl_peer_t
*
P
);
PyObject
*
get_peer
(
tgl_peer_id_t
id
,
tgl_peer_t
*
P
);
// Utility functions
PyObject
*
get_datetime
(
long
datetime
)
{
return
PyDateTime_FromTimestamp
(
Py_BuildValue
(
"(O)"
,
PyLong_FromLong
(
datetime
)));
}
void
py_add_string_field
(
PyObject
*
dict
,
char
*
name
,
const
char
*
value
)
{
assert
(
PyDict_Check
(
dict
));
assert
(
name
&&
strlen
(
name
));
...
...
@@ -340,7 +350,7 @@ PyObject* get_message (struct tgl_message *M) {
if
(
tgl_get_peer_type
(
M
->
fwd_from_id
))
{
PyDict_SetItemString
(
msg
,
"fwd_from"
,
get_peer
(
M
->
fwd_from_id
,
tgl_peer_get
(
TLS
,
M
->
fwd_from_id
)));
py_add_num_field
(
msg
,
"fwd_date"
,
M
->
fwd_date
);
PyDict_SetItemString
(
msg
,
"fwd_date"
,
get_datetime
(
M
->
fwd_date
)
);
}
PyDict_SetItemString
(
msg
,
"from"
,
get_peer
(
M
->
from_id
,
tgl_peer_get
(
TLS
,
M
->
from_id
)));
...
...
@@ -348,7 +358,7 @@ PyObject* get_message (struct tgl_message *M) {
PyDict_SetItemString
(
msg
,
"out"
,
(
M
->
out
?
Py_True
:
Py_False
));
PyDict_SetItemString
(
msg
,
"unread"
,
(
M
->
unread
?
Py_True
:
Py_False
));
PyDict_SetItemString
(
msg
,
"service"
,
(
M
->
service
?
Py_True
:
Py_False
));
PyDict_SetItemString
(
msg
,
"date"
,
PyLong_FromLong
(
M
->
date
));
// TODO put this into PyDate object
PyDict_SetItemString
(
msg
,
"date"
,
get_datetime
(
M
->
date
));
if
(
!
M
->
service
)
{
if
(
M
->
message_len
&&
M
->
message
)
{
...
...
@@ -374,7 +384,7 @@ void py_binlog_end (void) {
else
if
(
PyString_Check
(
result
))
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
Py_DECREF
(
result
);
Py_
X
DECREF
(
result
);
}
void
py_diff_end
(
void
)
{
...
...
@@ -389,7 +399,7 @@ void py_diff_end (void) {
else
if
(
PyString_Check
(
result
))
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
Py_DECREF
(
result
);
Py_
X
DECREF
(
result
);
}
void
py_our_id
(
int
id
)
{
...
...
@@ -404,7 +414,7 @@ void py_our_id (int id) {
else
if
(
PyString_Check
(
result
))
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
Py_DECREF
(
result
);
Py_
X
DECREF
(
result
);
}
void
py_new_msg
(
struct
tgl_message
*
M
)
{
...
...
@@ -423,7 +433,7 @@ void py_new_msg (struct tgl_message *M) {
else
if
(
PyString_Check
(
result
))
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
Py_DECREF
(
result
);
Py_
X
DECREF
(
result
);
}
void
py_secret_chat_update
(
struct
tgl_secret_chat
*
C
,
unsigned
flags
)
{
...
...
@@ -443,7 +453,7 @@ void py_secret_chat_update (struct tgl_secret_chat *C, unsigned flags) {
else
if
(
PyString_Check
(
result
))
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
Py_DECREF
(
result
);
Py_
X
DECREF
(
result
);
}
...
...
@@ -464,7 +474,7 @@ void py_user_update (struct tgl_user *U, unsigned flags) {
else
if
(
PyString_Check
(
result
))
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
Py_DECREF
(
result
);
Py_
X
DECREF
(
result
);
}
void
py_chat_update
(
struct
tgl_chat
*
C
,
unsigned
flags
)
{
...
...
@@ -485,7 +495,7 @@ void py_chat_update (struct tgl_chat *C, unsigned flags) {
else
if
(
PyString_Check
(
result
))
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
Py_DECREF
(
result
);
Py_
X
DECREF
(
result
);
}
////extern tgl_peer_t *Peers[];
...
...
@@ -1306,8 +1316,9 @@ void py_init (const char *file) {
exit
(
1
);
}
else
{
python_loaded
=
1
;
PyDateTime_IMPORT
;
pDict
=
PyModule_GetDict
(
pModule
);
// Store callables for python functions
my_python_register
(
pDict
,
"on_binlog_replay_end"
,
_py_binlog_end
);
my_python_register
(
pDict
,
"on_get_difference_end"
,
_py_diff_end
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment