Commit 96cee301ec9eeb8794e2e0572d89e04f6e7a5d6f

Authored by Perry Werneck
1 parent 36238ae4
Exists in master

Refactoring python object.

@@ -29,6 +29,7 @@ @@ -29,6 +29,7 @@
29 LIBNAME=tn3270 29 LIBNAME=tn3270
30 30
31 SOURCES= \ 31 SOURCES= \
  32 + $(wildcard src/module/*.c) \
32 $(wildcard src/module/*.cc) \ 33 $(wildcard src/module/*.cc) \
33 $(wildcard src/module/@OSNAME@/*.cc) \ 34 $(wildcard src/module/@OSNAME@/*.cc) \
34 $(wildcard src/terminal/*.cc) 35 $(wildcard src/terminal/*.cc)
@@ -36,6 +37,7 @@ SOURCES= \ @@ -36,6 +37,7 @@ SOURCES= \
36 #---[ Tools ]---------------------------------------------------------------------------- 37 #---[ Tools ]----------------------------------------------------------------------------
37 38
38 CXX=@CXX@ 39 CXX=@CXX@
  40 +CC=@CC@
39 LD=@CXX@ 41 LD=@CXX@
40 LN_S=@LN_S@ 42 LN_S=@LN_S@
41 MKDIR=@MKDIR_P@ 43 MKDIR=@MKDIR_P@
@@ -91,6 +93,14 @@ CFLAGS= \ @@ -91,6 +93,14 @@ CFLAGS= \
91 -DBUILD_DATE=`date +%Y%m%d` \ 93 -DBUILD_DATE=`date +%Y%m%d` \
92 @IPC3270_CFLAGS@ 94 @IPC3270_CFLAGS@
93 95
  96 +CXXFLAGS= \
  97 + @CXXFLAGS@ \
  98 + @PYTHON_CFLAGS@ \
  99 + -g \
  100 + -Isrc/include \
  101 + -DBUILD_DATE=`date +%Y%m%d` \
  102 + @IPC3270_CFLAGS@
  103 +
94 LDFLAGS= \ 104 LDFLAGS= \
95 @LDFLAGS@ 105 @LDFLAGS@
96 106
@@ -109,6 +119,20 @@ $(OBJDBG)/%.o: \ @@ -109,6 +119,20 @@ $(OBJDBG)/%.o: \
109 @$(MKDIR) $(dir $@) 119 @$(MKDIR) $(dir $@)
110 120
111 @$(CXX) \ 121 @$(CXX) \
  122 + $(CXXFLAGS) \
  123 + -Wall -Wextra -fstack-check \
  124 + -DDEBUG=1 \
  125 + -o $@ \
  126 + -c $<
  127 +
  128 +$(OBJDBG)/%.o: \
  129 + %.c \
  130 + $(DEPENDS)
  131 +
  132 + @echo $< ...
  133 + @$(MKDIR) $(dir $@)
  134 +
  135 + @$(CC) \
112 $(CFLAGS) \ 136 $(CFLAGS) \
113 -Wall -Wextra -fstack-check \ 137 -Wall -Wextra -fstack-check \
114 -DDEBUG=1 \ 138 -DDEBUG=1 \
@@ -99,6 +99,7 @@ case &quot;$host&quot; in @@ -99,6 +99,7 @@ case &quot;$host&quot; in
99 app_cv_osname="windows" 99 app_cv_osname="windows"
100 100
101 CFLAGS="$CFLAGS -pthread -D_WIN32_WINNT=0x0600" 101 CFLAGS="$CFLAGS -pthread -D_WIN32_WINNT=0x0600"
  102 + CXXFLAGS="$CXXFLAGS -pthread -D_WIN32_WINNT=0x0600"
102 LIBS="$LIBS -lws2_32 -lwtsapi32 -lcomdlg32" 103 LIBS="$LIBS -lws2_32 -lwtsapi32 -lcomdlg32"
103 DLLEXT=".dll" 104 DLLEXT=".dll"
104 105
@@ -127,6 +128,7 @@ dnl AC_SUBST(SONAME,py3270.dll) @@ -127,6 +128,7 @@ dnl AC_SUBST(SONAME,py3270.dll)
127 128
128 *) 129 *)
129 CFLAGS="$CFLAGS -pthread" 130 CFLAGS="$CFLAGS -pthread"
  131 + CXXFLAGS="$CXXFLAGS -pthread"
130 LDFLAGS="$LDFLAGS -pthread" 132 LDFLAGS="$LDFLAGS -pthread"
131 app_cv_osname="linux" 133 app_cv_osname="linux"
132 LOGDIR="/var/log" 134 LOGDIR="/var/log"
@@ -164,7 +166,7 @@ AS_IF([test x&quot;AS_VAR_GET(CACHEVAR)&quot; = xyes], @@ -164,7 +166,7 @@ AS_IF([test x&quot;AS_VAR_GET(CACHEVAR)&quot; = xyes],
164 AS_VAR_POPDEF([CACHEVAR])dnl 166 AS_VAR_POPDEF([CACHEVAR])dnl
165 ])dnl AX_CHECK_COMPILE_FLAGS 167 ])dnl AX_CHECK_COMPILE_FLAGS
166 168
167 -AX_CHECK_COMPILE_FLAG([-std=gnu++11], [CFLAGS="$CFLAGS -std=gnu++11"]) 169 +AX_CHECK_COMPILE_FLAG([-std=gnu++11], [CXXFLAGS="$CXXFLAGS -std=gnu++11"])
168 170
169 dnl --------------------------------------------------------------------------- 171 dnl ---------------------------------------------------------------------------
170 dnl Check for IPC3270 172 dnl Check for IPC3270
@@ -45,7 +45,9 @@ @@ -45,7 +45,9 @@
45 <Unit filename="src/include/config.h" /> 45 <Unit filename="src/include/config.h" />
46 <Unit filename="src/include/config.h.in" /> 46 <Unit filename="src/include/config.h.in" />
47 <Unit filename="src/include/py3270.h" /> 47 <Unit filename="src/include/py3270.h" />
48 - <Unit filename="src/module/init.cc" /> 48 + <Unit filename="src/module/init.c">
  49 + <Option compilerVar="CC" />
  50 + </Unit>
49 <Unit filename="src/module/properties.cc" /> 51 <Unit filename="src/module/properties.cc" />
50 <Unit filename="src/terminal/actions.cc" /> 52 <Unit filename="src/terminal/actions.cc" />
51 <Unit filename="src/terminal/get.cc" /> 53 <Unit filename="src/terminal/get.cc" />
src/include/py3270.h
@@ -35,7 +35,6 @@ @@ -35,7 +35,6 @@
35 #include <Python.h> 35 #include <Python.h>
36 36
37 #include <config.h> 37 #include <config.h>
38 - #include <lib3270/ipc.h>  
39 38
40 #if defined(_WIN32) 39 #if defined(_WIN32)
41 40
@@ -60,13 +59,38 @@ @@ -60,13 +59,38 @@
60 59
61 #endif 60 #endif
62 61
  62 +#ifdef __cplusplus
  63 +
63 #include <functional> 64 #include <functional>
  65 + #include <exception>
  66 + #include <stdexcept>
  67 + #include <lib3270/ipc.h>
  68 +
  69 + using std::exception;
  70 + using std::runtime_error;
  71 + using TN3270::Host;
64 72
65 extern "C" { 73 extern "C" {
66 74
  75 +#else
  76 +
  77 + typedef void Host;
  78 +
  79 +#endif
  80 +
  81 + typedef struct {
  82 + PyObject_HEAD
  83 + Host *host;
  84 + } pySession;
  85 +
67 DLL_PRIVATE PyObject * py3270_get_module_version(PyObject *self, PyObject *args); 86 DLL_PRIVATE PyObject * py3270_get_module_version(PyObject *self, PyObject *args);
68 DLL_PRIVATE PyObject * py3270_get_module_revision(PyObject *self, PyObject *args); 87 DLL_PRIVATE PyObject * py3270_get_module_revision(PyObject *self, PyObject *args);
69 88
  89 + DLL_PRIVATE PyObject * py3270_session_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
  90 +// DLL_PRIVATE int py3270_session_init(pySession *self, PyObject *args, PyObject *kwds);
  91 + DLL_PRIVATE void py3270_session_dealloc(pySession * self);
  92 +
  93 +
70 /* 94 /*
71 95
72 DLL_PRIVATE PyObject * py3270_alloc(PyTypeObject *type, PyObject *args, PyObject *kwds); 96 DLL_PRIVATE PyObject * py3270_alloc(PyTypeObject *type, PyObject *args, PyObject *kwds);
@@ -99,6 +123,8 @@ @@ -99,6 +123,8 @@
99 DLL_PRIVATE PyObject * terminal_wait_for_string_at(PyObject *self, PyObject *args); 123 DLL_PRIVATE PyObject * terminal_wait_for_string_at(PyObject *self, PyObject *args);
100 */ 124 */
101 125
  126 +#ifdef __cplusplus
102 } 127 }
  128 +#endif
103 129
104 #endif // PY3270_H_INCLUDED 130 #endif // PY3270_H_INCLUDED
src/module/init.c 0 → 100644
@@ -0,0 +1,124 @@ @@ -0,0 +1,124 @@
  1 +/*
  2 + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
  3 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
  4 + * aplicativos mainframe. Registro no INPI sob o nome G3270.
  5 + *
  6 + * Copyright (C) <2008> <Banco do Brasil S.A.>
  7 + *
  8 + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
  9 + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
  10 + * Free Software Foundation.
  11 + *
  12 + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
  13 + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
  14 + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
  15 + * obter mais detalhes.
  16 + *
  17 + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
  18 + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
  19 + * St, Fifth Floor, Boston, MA 02110-1301 USA
  20 + *
  21 + * Este programa está nomeado como - e possui - linhas de código.
  22 + *
  23 + * Contatos:
  24 + *
  25 + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
  26 + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
  27 + *
  28 + * <https://docs.python.org/3/extending/extending.html>
  29 + *
  30 + */
  31 +
  32 + #include <py3270.h>
  33 +
  34 +/*---[ Globals ]------------------------------------------------------------------------------------*/
  35 +
  36 +static PyMethodDef methods[] = {
  37 +
  38 + {
  39 + "version",
  40 + py3270_get_module_version,
  41 + METH_NOARGS,
  42 + "Get " PACKAGE_NAME " version"
  43 + },
  44 +
  45 + {
  46 + "revision",
  47 + py3270_get_module_revision,
  48 + METH_NOARGS,
  49 + "Get " PACKAGE_NAME " revision"
  50 +
  51 + },
  52 +
  53 + {
  54 + NULL,
  55 + NULL,
  56 + 0,
  57 + NULL
  58 + }
  59 +
  60 +};
  61 +
  62 +static struct PyModuleDef definition = {
  63 + PyModuleDef_HEAD_INIT,
  64 + .m_name = "tn3270", // name of module
  65 + .m_doc = PACKAGE_DESCRIPTION, // module documentation, may be NUL
  66 + .m_size = -1, // size of per-interpreter state of the module or -1 if the module keeps state in global variables.
  67 + .m_methods = methods // Module methods
  68 +};
  69 +
  70 +static PyTypeObject SessionType = {
  71 + PyVarObject_HEAD_INIT(NULL, 0)
  72 + .tp_name = "tn3270.Session",
  73 + .tp_doc = "TN3270 Session Object",
  74 + .tp_basicsize = sizeof(pySession),
  75 + .tp_itemsize = 0,
  76 + .tp_flags = Py_TPFLAGS_DEFAULT,
  77 + .tp_dealloc = py3270_session_dealloc,
  78 + .tp_new = py3270_session_new,
  79 +};
  80 +
  81 +/*---[ Implement ]----------------------------------------------------------------------------------*/
  82 +
  83 +PyMODINIT_FUNC PyInit_tn3270(void)
  84 +{
  85 + //
  86 + // Initialize module.
  87 + //
  88 + if (PyType_Ready(&SessionType) < 0)
  89 + return NULL;
  90 +
  91 + Py_Initialize();
  92 +
  93 + PyObject *module = PyModule_Create(&definition);
  94 +
  95 + if(!module)
  96 + return NULL;
  97 +
  98 + /*
  99 + //
  100 + // Create exception object
  101 + //
  102 + PyObject * except = PyErr_NewException("tn3270.Error", NULL, NULL);
  103 +
  104 + Py_XINCREF(except);
  105 + if (PyModule_AddObject(module, "error", except) < 0) {
  106 + Py_XDECREF(except);
  107 + Py_CLEAR(except);
  108 + Py_DECREF(module);
  109 + return NULL;
  110 + }
  111 + */
  112 +
  113 + //
  114 + // Create custom type
  115 + //
  116 + Py_INCREF(&SessionType);
  117 + if (PyModule_AddObject(module, "Session", (PyObject *) &SessionType) < 0) {
  118 + Py_DECREF(&SessionType);
  119 + Py_DECREF(module);
  120 + return NULL;
  121 + }
  122 +
  123 + return module;
  124 +}
src/module/init.cc
@@ -1,109 +0,0 @@ @@ -1,109 +0,0 @@
1 -/*  
2 - * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270  
3 - * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a  
4 - * aplicativos mainframe. Registro no INPI sob o nome G3270.  
5 - *  
6 - * Copyright (C) <2008> <Banco do Brasil S.A.>  
7 - *  
8 - * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob  
9 - * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela  
10 - * Free Software Foundation.  
11 - *  
12 - * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER  
13 - * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO  
14 - * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para  
15 - * obter mais detalhes.  
16 - *  
17 - * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este  
18 - * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin  
19 - * St, Fifth Floor, Boston, MA 02110-1301 USA  
20 - *  
21 - * Este programa está nomeado como - e possui - linhas de código.  
22 - *  
23 - * Contatos:  
24 - *  
25 - * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)  
26 - * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)  
27 - *  
28 - * <https://docs.python.org/3/extending/extending.html>  
29 - *  
30 - */  
31 -  
32 - #include <py3270.h>  
33 -  
34 -/*---[ Implement ]----------------------------------------------------------------------------------*/  
35 -  
36 -PyMODINIT_FUNC PyInit_tn3270(void)  
37 -{  
38 - static PyMethodDef methods[] = {  
39 -  
40 - {  
41 - "version",  
42 - py3270_get_module_version,  
43 - METH_NOARGS,  
44 - "Get " PACKAGE_NAME " version"  
45 - },  
46 -  
47 - {  
48 - "revision",  
49 - py3270_get_module_revision,  
50 - METH_NOARGS,  
51 - "Get " PACKAGE_NAME " revision"  
52 -  
53 - },  
54 -  
55 - {  
56 - NULL,  
57 - NULL,  
58 - 0,  
59 - NULL  
60 - }  
61 -  
62 - };  
63 -  
64 - static struct PyModuleDef definition = {  
65 - PyModuleDef_HEAD_INIT,  
66 - "tn3270", // name of module  
67 - PACKAGE_DESCRIPTION, // module documentation, may be NUL  
68 - -1, // size of per-interpreter state of the module or -1 if the module keeps state in global variables.  
69 - methods // Module methods  
70 - };  
71 -  
72 - Py_Initialize();  
73 -  
74 - PyObject *module = PyModule_Create(&definition);  
75 -  
76 - if(module) {  
77 -  
78 - PyObject * except = PyErr_NewException("tn3270.error", NULL, NULL);  
79 -  
80 - Py_XINCREF(except);  
81 - if (PyModule_AddObject(module, "error", except) < 0) {  
82 - Py_XDECREF(except);  
83 - Py_CLEAR(except);  
84 - Py_DECREF(module);  
85 - return NULL;  
86 - }  
87 -  
88 - }  
89 -  
90 -  
91 - /*  
92 -  
93 - m = PyModule_Create(&spammodule);  
94 - if (m == NULL)  
95 - return NULL;  
96 -  
97 - SpamError = PyErr_NewException("spam.error", NULL, NULL);  
98 - Py_XINCREF(SpamError);  
99 - if (PyModule_AddObject(m, "error", SpamError) < 0) {  
100 - Py_XDECREF(SpamError);  
101 - Py_CLEAR(SpamError);  
102 - Py_DECREF(m);  
103 - return NULL;  
104 - }  
105 -  
106 - */  
107 -  
108 - return module;  
109 -}  
src/terminal/init.cc
@@ -38,6 +38,57 @@ @@ -38,6 +38,57 @@
38 38
39 /*---[ Implement ]----------------------------------------------------------------------------------*/ 39 /*---[ Implement ]----------------------------------------------------------------------------------*/
40 40
  41 +PyObject * py3270_session_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
  42 +
  43 + const char *id = "";
  44 +
  45 + if (!PyArg_ParseTuple(args, "s", &id))
  46 + id = "";
  47 +
  48 + pySession * session = (pySession *) type->tp_alloc(type,0);
  49 +
  50 + printf("---[%s]---\n",id);
  51 +
  52 + if(session) {
  53 +
  54 + try {
  55 +
  56 + session->host = new TN3270::Host(id);
  57 +
  58 + printf("---[%s=%p]---\n",id,session->host);
  59 + return (PyObject *) session;
  60 +
  61 + } catch(const exception &e) {
  62 +
  63 + printf("---[%s]---\n",e.what());
  64 + PyErr_SetString(PyExc_RuntimeError, e.what());
  65 +
  66 + } catch( ... ) {
  67 +
  68 + printf("---[%s]---\n","???");
  69 + PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module");
  70 +
  71 + }
  72 +
  73 + }
  74 +
  75 + type->tp_free(session);
  76 +
  77 + return NULL;
  78 +
  79 +}
  80 +
  81 +void py3270_session_dealloc(pySession * self) {
  82 +
  83 + if(self->host) {
  84 + delete self->host;
  85 + }
  86 +
  87 + Py_TYPE(self)->tp_free((PyObject *) self);
  88 +
  89 +}
  90 +
  91 +
41 /* 92 /*
42 PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { 93 PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
43 94
@@ -70,19 +121,4 @@ PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { @@ -70,19 +121,4 @@ PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
70 } 121 }
71 122
72 123
73 -int terminal_init(pw3270_TerminalObject *self, PyObject *args, PyObject *kwds) {  
74 -  
75 - return 0;  
76 -  
77 -}  
78 -  
79 -void terminal_dealloc(pw3270_TerminalObject * self) {  
80 -  
81 - trace("%s",__FUNCTION__);  
82 -  
83 - delete self->session;  
84 -  
85 - self->ob_type->tp_free((PyObject*)self);  
86 -  
87 -}  
88 */ 124 */
testprograms/sample.py
@@ -8,7 +8,7 @@ print(&quot;Teste extensão pw3270&quot;) @@ -8,7 +8,7 @@ print(&quot;Teste extensão pw3270&quot;)
8 print("Using TN3270 Version " + tn3270.version()) 8 print("Using TN3270 Version " + tn3270.version())
9 print(tn3270.revision()) 9 print(tn3270.revision())
10 10
11 -#term = py3270.Terminal("") 11 +session = tn3270.Session(":a")
12 12
13 #print "Using pw3270 version " + term.Version() + " revision " + term.Revision() 13 #print "Using pw3270 version " + term.Version() + " revision " + term.Revision()
14 14