|
| 1 | +#define PY_SSIZE_T_CLEAN |
| 2 | +#include <Python.h> |
| 3 | + |
| 4 | +#include <funchook.h> |
| 5 | +#include <logger.h> |
| 6 | +#include <utils.h> |
| 7 | +#include <patch.h> |
| 8 | + |
| 9 | +binaryfunc bytes_concat_origin; |
| 10 | +binaryfunc bytearray_concat_origin; |
| 11 | +binaryfunc bytearray_inplace_concat_origin; |
| 12 | +binaryfunc unicode_concat_origin; |
| 13 | +void (*unicode_append_origin)(PyObject **l, PyObject *r); |
| 14 | + |
| 15 | +PyObject *bytes_concat_new(PyObject *l, PyObject *r) { |
| 16 | + PyObject *result = bytes_concat_origin(l, r); |
| 17 | + |
| 18 | + if (result == NULL) { |
| 19 | + return result; |
| 20 | + } |
| 21 | + |
| 22 | + patch_string_callback("callback_bytes_concat", l, result, r, NULL); |
| 23 | + |
| 24 | + return result; |
| 25 | +} |
| 26 | + |
| 27 | +PyObject *bytearray_concat_new(PyObject *l, PyObject *r) { |
| 28 | + PyObject *result = bytearray_concat_origin(l, r); |
| 29 | + |
| 30 | + if (result == NULL) { |
| 31 | + return result; |
| 32 | + } |
| 33 | + |
| 34 | + patch_string_callback("callback_bytearray_concat", l, result, r, NULL); |
| 35 | + |
| 36 | + return result; |
| 37 | +} |
| 38 | + |
| 39 | +PyObject *bytearray_inplace_concat_new(PyObject *l, PyObject *r) { |
| 40 | + PyObject *result = bytearray_inplace_concat_origin(l, r); |
| 41 | + |
| 42 | + if (result == NULL) { |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + patch_string_callback("callback_bytearray_concat", l, result, r, NULL); |
| 47 | + |
| 48 | + return result; |
| 49 | +} |
| 50 | + |
| 51 | +PyObject *unicode_concat_new(PyObject *l, PyObject *r) { |
| 52 | + PyObject *result = unicode_concat_origin(l, r); |
| 53 | + |
| 54 | + if (result == NULL) { |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + patch_string_callback("callback_unicode_concat", l, result, r, NULL); |
| 59 | + |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | +void unicode_append_new(PyObject **l, PyObject *r) { |
| 64 | + PyObject *origin_l = *l; |
| 65 | + Py_XINCREF(origin_l); |
| 66 | + unicode_append_origin(l, r); |
| 67 | + |
| 68 | + if (*l == NULL) { |
| 69 | + Py_XDECREF(origin_l); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + patch_string_callback("callback_unicode_concat", origin_l, *l, r, NULL); |
| 74 | + Py_XDECREF(origin_l); |
| 75 | +} |
| 76 | + |
| 77 | +int apply_concat_patch(funchook_t *funchook) { |
| 78 | + bytes_concat_origin = PyBytes_Type.tp_as_sequence->sq_concat; |
| 79 | + funchook_prepare_wrapper(funchook, &bytes_concat_origin, bytes_concat_new); |
| 80 | + |
| 81 | + bytearray_concat_origin = PyByteArray_Concat; |
| 82 | + funchook_prepare_wrapper(funchook, &bytearray_concat_origin, bytearray_concat_new); |
| 83 | + |
| 84 | + bytearray_inplace_concat_origin = PyByteArray_Type.tp_as_sequence->sq_inplace_concat; |
| 85 | + funchook_prepare_wrapper(funchook, &bytearray_inplace_concat_origin, bytearray_inplace_concat_new); |
| 86 | + |
| 87 | + unicode_concat_origin = PyUnicode_Concat; |
| 88 | + funchook_prepare_wrapper(funchook, &unicode_concat_origin, unicode_concat_new); |
| 89 | + |
| 90 | + unicode_append_origin = PyUnicode_Append; |
| 91 | + funchook_prepare_wrapper(funchook, &unicode_append_origin, unicode_append_new); |
| 92 | + |
| 93 | + log_debug("------c_patch------------------ concat"); |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
0 commit comments