-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlibpyafipws.c
283 lines (245 loc) · 8.79 KB
/
libpyafipws.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* This file is part of PyAfipWs dynamical-link shared library
* Copyright (C) 2013 Mariano Reingart <reingart@gmail.com>
*
* PyAfipWs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PyAfipWs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PyAfipWs. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#include <frameobject.h>
#include "libpyafipws.h"
#ifdef WIN32
#include "Shlwapi.h"
/* sys.fronzenddlhandle emulation (set by DllMain) */
HMODULE dllhandle;
#endif
/* Start-up the python interpreter */
CONSTRUCTOR static void initialize(void) {
//Py_SetProgramName("libpyafipws");
#ifdef WIN32
char buf[2000], *b;
unsigned long ok;
PyObject *pSysPath, *pName;
MessageBox(NULL, "Py_Initialize...", "LibPyAfipWs Initialize", 0);
Py_Initialize();
PyRun_SimpleString("import sys, os");
PyRun_SimpleString("sys.stdout = open('stdout.txt', 'w')");
PyRun_SimpleString("sys.stderr = open('stderr.txt', 'w')");
/* on windows, add the base path of the .DLL */
ok = GetModuleFileName(dllhandle, buf, sizeof(buf));
MessageBox(NULL, buf, "LibPyAfipWs Initialize (module name)", 0);
ok = PathRemoveFileSpec(buf);
MessageBox(NULL, buf, "LibPyAfipWs Initialize (module path)", 0);
pSysPath = PySys_GetObject("path");
pName = PyString_FromString(buf);
if (PyList_Insert(pSysPath, 0, pName))
MessageBox(NULL, "PyList_Insert", "LibPyAfipWs Initialize", 0);
Py_XDECREF(pName); /* note that pSysPath is a Borrowed reference! */
MessageBox(NULL, "done!", "LibPyAfipWs Initialize", 0);
#else
Py_Initialize();
puts(Py_GetPath());
/* on linux, add the current directory so python can find the modules */
PyRun_SimpleString("import sys, os");
PyRun_SimpleString("sys.path.append(os.curdir)");
/* preliminary fix, it could not work on some cases and there could be
some security concerns. It should add the base path of the .so */
#endif
}
/* Tear down the python interpreter */
DESTRUCTOR static void finalize(void) {
MessageBox(NULL, "Py_Finalize...", "LibPyAfipWs Finalize", 0);
Py_Finalize();
MessageBox(NULL, "done!", "LibPyAfipWs Finalize", 0);
}
#ifdef WIN32
/* Windows DLL Hook */
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) {
BOOL ret = TRUE;
switch (dwReason) {
case DLL_PROCESS_ATTACH: {
dllhandle = (HINSTANCE) hInstance;
initialize();
break;
}
case DLL_PROCESS_DETACH: {
finalize();
break;
}
}
return ret;
}
#endif
/* test function to check if python & stdlib is installed ok */
EXPORT BSTR STDCALL test() {
PyObject *pret, *pdict;
BSTR ret = NULL;
MessageBox(NULL, "iniciando pruebas...", "LibPyAfipWs Test!", 0);
pdict = PyDict_New();
PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
pret = PyRun_String("from time import time,ctime", Py_single_input, pdict, pdict);
Py_XDECREF(pret);
pret = PyRun_String("'Today is %s' % ctime(time())", Py_eval_input, pdict, pdict);
if (pret == NULL) {
ret = format_ex();
} else {
ret = cstr(PyObject_Str(pret));
}
MessageBox(NULL, (char*)ret, "LibPyAfipWs Test!", 0);
Py_XDECREF(pdict);
Py_XDECREF(pret);
return ret;
}
/* cstr: utility function to convert a python string to c (dyn. allocated) */
BSTR cstr(void *pStr) {
BSTR ret;
char *str;
size_t len;
/* get the string val/size, remember to copy '\0' termination character */
len = PyString_Size((PyObject*) pStr) + 1;
str = PyString_AsString((PyObject*) pStr);
#ifdef WIN32
/* on windows, returns a automation string */
ret = SysAllocStringByteLen(str, len);
#else
/* allocate memory for the c string */
ret = (char *) malloc(len);
if (ret) {
/* copy the py string to c (note that it may have \0 characters */
strncpy(ret, str, len);
}
#endif
return ret;
}
#define FMT "%s: %s - File %s, line %d, in %s"
/* format exception: simplified PyErr_PrintEx (to not write to stdout) */
BSTR format_ex(void) {
char buf[2000];
BSTR ret;
char *ex, *v, *filename="<internal>", *name="<C>";
int lineno=-1;
size_t len;
PyObject *exception, *value, *tb;
PyTracebackObject *tb1;
/* PyErr_PrintEx (pythonrun.c) */
PyErr_Fetch(&exception, &value, &tb);
if (exception == NULL) return NULL;
PyErr_NormalizeException(&exception, &value, &tb);
if (exception == NULL) return NULL;
/* PyErr_Display (pythonrun.c) */
ex = PyExceptionClass_Name(exception);
if (ex == NULL){
ex = "<no exception class name>";
}
if (value == NULL) {
v = "<no exception value>";
} else {
v = PyString_AsString(PyObject_Str(value));
}
/* PyTracebackObject seems defined at frameobject.h, it should be included
to avoid "error: dereferencing pointer to incomplete type"
tb is NULL if the failure is in the c-api (for example in PyImport_Import)
*/
tb1 = (PyTracebackObject *)tb;
/* tb_printinternal (traceback.c) */
if (tb1) {
filename = PyString_AsString(tb1->tb_frame->f_code->co_filename);
lineno = tb1->tb_lineno;
name = PyString_AsString(tb1->tb_frame->f_code->co_name);
}
/* tb_displayline (traceback.c) */
PyOS_snprintf(buf, sizeof(buf), FMT, ex, v, filename, lineno, name);
Py_XDECREF(exception);
Py_XDECREF(value);
Py_XDECREF(tb);
len = strlen(buf);
#ifdef WIN32
/* on windows, returns a automation string */
ret = SysAllocStringByteLen(buf, len);
#else
/* allocate memory for the c string */
ret = (char *) malloc(len);
if (ret) {
/* copy the py string to c (note that it may have \0 characters */
strncpy(ret, buf, len);
}
#endif
return ret;
}
/* CreateObject: import the module, instantiate the object and return the ref */
EXPORT void * STDCALL PYAFIPWS_CreateObject(char *module, char *name) {
PyObject *pName, *pModule, *pClass, *pObject=NULL;
pName = PyString_FromString(module);
pModule = PyImport_Import(pName);
Py_DECREF(pName);
//fprintf(stderr, "imported!\n");
if (pModule != NULL) {
pClass = PyObject_GetAttrString(pModule, name);
if (pClass && PyCallable_Check(pClass)) {
//fprintf(stderr, "pfunc!!!\n");
pObject = PyObject_CallObject(pClass, NULL);
//fprintf(stderr, "call!!!\n");
Py_XDECREF(pClass);
}
Py_DECREF(pModule);
return (void *) pObject;
} else {
return NULL;
}
}
/* DestroyObject: decrement the reference to the module */
EXPORT void STDCALL PYAFIPWS_DestroyObject(void * object) {
Py_DECREF((PyObject *) object);
}
/* Get: generic method to get an attribute of an object (returns a string) */
EXPORT BSTR STDCALL PYAFIPWS_Get(void * object, char * name) {
PyObject *pValue;
BSTR ret=NULL;
pValue = PyObject_GetAttrString((PyObject *) object, name);
if (pValue) {
ret = cstr(pValue);
Py_DECREF(pValue);
} else {
PyErr_Print();
//fprintf(stderr,"GetAttr to %s failed\n", name);
}
return ret;
}
/* Set: generic method to set an attribute of an object (string value) */
EXPORT bool STDCALL PYAFIPWS_Set(void * object, char * name, char * value) {
PyObject *pValue;
int ret;
bool ok=false;
pValue = PyString_FromString(value);
ret = PyObject_SetAttrString((PyObject *) object, name, pValue);
if (pValue) {
Py_DECREF(pValue);
}
if (ret == -1) {
PyErr_Print();
//fprintf(stderr,"GetAttr to %s failed\n", name);
ok = false;
} else {
ok = true;
}
return ok;
}
/* deallocation function for libpyafipws string values */
EXPORT void STDCALL PYAFIPWS_Free(BSTR psz) {
if (psz != (BSTR) NULL)
#ifdef WIN32
SysFreeString(psz);
#else
free(psz);
#endif
}