* Load a protocol decoder module into the embedded Python interpreter. * * @param module_name The module name to be loaded. * * @return SRD_OK upon success, a (negative) error code otherwise. * * @since 0.1.0 */
| 727 | * @since 0.1.0 |
| 728 | */ |
| 729 | SRD_API int srd_decoder_load(const char *module_name) |
| 730 | { |
| 731 | PyObject *py_basedec; |
| 732 | struct srd_decoder *d; |
| 733 | long apiver; |
| 734 | int is_subclass; |
| 735 | const char *fail_txt = NULL; |
| 736 | PyGILState_STATE gstate; |
| 737 | |
| 738 | if (!srd_check_init()) |
| 739 | return SRD_ERR; |
| 740 | |
| 741 | if (!module_name) |
| 742 | return SRD_ERR_ARG; |
| 743 | |
| 744 | gstate = PyGILState_Ensure(); |
| 745 | |
| 746 | if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) { |
| 747 | /* Module was already imported. */ |
| 748 | PyGILState_Release(gstate); |
| 749 | return SRD_OK; |
| 750 | } |
| 751 | |
| 752 | d = malloc(sizeof(struct srd_decoder)); |
| 753 | if (d == NULL){ |
| 754 | srd_err("%s,ERROR:failed to alloc memory.", __func__); |
| 755 | goto err_out; |
| 756 | } |
| 757 | memset(d, 0, sizeof(struct srd_decoder)); |
| 758 | |
| 759 | fail_txt = NULL; |
| 760 | |
| 761 | //Load module from python script file,module_name is a sub directory |
| 762 | d->py_mod = py_import_by_name(module_name); |
| 763 | if (!d->py_mod) { |
| 764 | fail_txt = "import by name failed"; |
| 765 | goto except_out; |
| 766 | } |
| 767 | |
| 768 | if (!mod_sigrokdecode) { |
| 769 | srd_err("sigrokdecode module not loaded."); |
| 770 | fail_txt = "sigrokdecode(3) not loaded"; |
| 771 | goto err_out; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | Get the 'Decoder' class as Python object. |
| 776 | Here, Decoder is python class type |
| 777 | */ |
| 778 | d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"); |
| 779 | if (!d->py_dec) { |
| 780 | fail_txt = "no 'Decoder' attribute in imported module"; |
| 781 | goto except_out; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | Here, Decoder is c class type |
| 786 | */ |
no test coverage detected