From 96cee301ec9eeb8794e2e0572d89e04f6e7a5d6f Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Tue, 17 Sep 2019 13:52:23 -0300 Subject: [PATCH] Refactoring python object. --- Makefile.in | 24 ++++++++++++++++++++++++ configure.ac | 4 +++- py3270.cbp | 4 +++- src/include/py3270.h | 28 +++++++++++++++++++++++++++- src/module/init.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/module/init.cc | 109 ------------------------------------------------------------------------------------------------------------- src/terminal/init.cc | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++--------------- testprograms/sample.py | 2 +- 8 files changed, 233 insertions(+), 128 deletions(-) create mode 100644 src/module/init.c delete mode 100644 src/module/init.cc diff --git a/Makefile.in b/Makefile.in index 0ee460a..d093460 100644 --- a/Makefile.in +++ b/Makefile.in @@ -29,6 +29,7 @@ LIBNAME=tn3270 SOURCES= \ + $(wildcard src/module/*.c) \ $(wildcard src/module/*.cc) \ $(wildcard src/module/@OSNAME@/*.cc) \ $(wildcard src/terminal/*.cc) @@ -36,6 +37,7 @@ SOURCES= \ #---[ Tools ]---------------------------------------------------------------------------- CXX=@CXX@ +CC=@CC@ LD=@CXX@ LN_S=@LN_S@ MKDIR=@MKDIR_P@ @@ -91,6 +93,14 @@ CFLAGS= \ -DBUILD_DATE=`date +%Y%m%d` \ @IPC3270_CFLAGS@ +CXXFLAGS= \ + @CXXFLAGS@ \ + @PYTHON_CFLAGS@ \ + -g \ + -Isrc/include \ + -DBUILD_DATE=`date +%Y%m%d` \ + @IPC3270_CFLAGS@ + LDFLAGS= \ @LDFLAGS@ @@ -109,6 +119,20 @@ $(OBJDBG)/%.o: \ @$(MKDIR) $(dir $@) @$(CXX) \ + $(CXXFLAGS) \ + -Wall -Wextra -fstack-check \ + -DDEBUG=1 \ + -o $@ \ + -c $< + +$(OBJDBG)/%.o: \ + %.c \ + $(DEPENDS) + + @echo $< ... + @$(MKDIR) $(dir $@) + + @$(CC) \ $(CFLAGS) \ -Wall -Wextra -fstack-check \ -DDEBUG=1 \ diff --git a/configure.ac b/configure.ac index 353705c..a099f2e 100644 --- a/configure.ac +++ b/configure.ac @@ -99,6 +99,7 @@ case "$host" in app_cv_osname="windows" CFLAGS="$CFLAGS -pthread -D_WIN32_WINNT=0x0600" + CXXFLAGS="$CXXFLAGS -pthread -D_WIN32_WINNT=0x0600" LIBS="$LIBS -lws2_32 -lwtsapi32 -lcomdlg32" DLLEXT=".dll" @@ -127,6 +128,7 @@ dnl AC_SUBST(SONAME,py3270.dll) *) CFLAGS="$CFLAGS -pthread" + CXXFLAGS="$CXXFLAGS -pthread" LDFLAGS="$LDFLAGS -pthread" app_cv_osname="linux" LOGDIR="/var/log" @@ -164,7 +166,7 @@ AS_IF([test x"AS_VAR_GET(CACHEVAR)" = xyes], AS_VAR_POPDEF([CACHEVAR])dnl ])dnl AX_CHECK_COMPILE_FLAGS -AX_CHECK_COMPILE_FLAG([-std=gnu++11], [CFLAGS="$CFLAGS -std=gnu++11"]) +AX_CHECK_COMPILE_FLAG([-std=gnu++11], [CXXFLAGS="$CXXFLAGS -std=gnu++11"]) dnl --------------------------------------------------------------------------- dnl Check for IPC3270 diff --git a/py3270.cbp b/py3270.cbp index 72dd957..f6726cd 100644 --- a/py3270.cbp +++ b/py3270.cbp @@ -45,7 +45,9 @@ - + + diff --git a/src/include/py3270.h b/src/include/py3270.h index 5b46e13..4065530 100644 --- a/src/include/py3270.h +++ b/src/include/py3270.h @@ -35,7 +35,6 @@ #include #include - #include #if defined(_WIN32) @@ -60,13 +59,38 @@ #endif +#ifdef __cplusplus + #include + #include + #include + #include + + using std::exception; + using std::runtime_error; + using TN3270::Host; extern "C" { +#else + + typedef void Host; + +#endif + + typedef struct { + PyObject_HEAD + Host *host; + } pySession; + DLL_PRIVATE PyObject * py3270_get_module_version(PyObject *self, PyObject *args); DLL_PRIVATE PyObject * py3270_get_module_revision(PyObject *self, PyObject *args); + DLL_PRIVATE PyObject * py3270_session_new(PyTypeObject *type, PyObject *args, PyObject *kwds); +// DLL_PRIVATE int py3270_session_init(pySession *self, PyObject *args, PyObject *kwds); + DLL_PRIVATE void py3270_session_dealloc(pySession * self); + + /* DLL_PRIVATE PyObject * py3270_alloc(PyTypeObject *type, PyObject *args, PyObject *kwds); @@ -99,6 +123,8 @@ DLL_PRIVATE PyObject * terminal_wait_for_string_at(PyObject *self, PyObject *args); */ +#ifdef __cplusplus } +#endif #endif // PY3270_H_INCLUDED diff --git a/src/module/init.c b/src/module/init.c new file mode 100644 index 0000000..36ba405 --- /dev/null +++ b/src/module/init.c @@ -0,0 +1,124 @@ +/* + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a + * aplicativos mainframe. Registro no INPI sob o nome G3270. + * + * Copyright (C) <2008> + * + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela + * Free Software Foundation. + * + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para + * obter mais detalhes. + * + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin + * St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Este programa está nomeado como - e possui - linhas de código. + * + * Contatos: + * + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) + * + * + * + */ + + #include + +/*---[ Globals ]------------------------------------------------------------------------------------*/ + +static PyMethodDef methods[] = { + + { + "version", + py3270_get_module_version, + METH_NOARGS, + "Get " PACKAGE_NAME " version" + }, + + { + "revision", + py3270_get_module_revision, + METH_NOARGS, + "Get " PACKAGE_NAME " revision" + + }, + + { + NULL, + NULL, + 0, + NULL + } + +}; + +static struct PyModuleDef definition = { + PyModuleDef_HEAD_INIT, + .m_name = "tn3270", // name of module + .m_doc = PACKAGE_DESCRIPTION, // module documentation, may be NUL + .m_size = -1, // size of per-interpreter state of the module or -1 if the module keeps state in global variables. + .m_methods = methods // Module methods +}; + +static PyTypeObject SessionType = { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "tn3270.Session", + .tp_doc = "TN3270 Session Object", + .tp_basicsize = sizeof(pySession), + .tp_itemsize = 0, + .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_dealloc = py3270_session_dealloc, + .tp_new = py3270_session_new, +}; + +/*---[ Implement ]----------------------------------------------------------------------------------*/ + +PyMODINIT_FUNC PyInit_tn3270(void) +{ + // + // Initialize module. + // + if (PyType_Ready(&SessionType) < 0) + return NULL; + + Py_Initialize(); + + PyObject *module = PyModule_Create(&definition); + + if(!module) + return NULL; + + /* + // + // Create exception object + // + PyObject * except = PyErr_NewException("tn3270.Error", NULL, NULL); + + Py_XINCREF(except); + if (PyModule_AddObject(module, "error", except) < 0) { + Py_XDECREF(except); + Py_CLEAR(except); + Py_DECREF(module); + return NULL; + } + */ + + // + // Create custom type + // + Py_INCREF(&SessionType); + if (PyModule_AddObject(module, "Session", (PyObject *) &SessionType) < 0) { + Py_DECREF(&SessionType); + Py_DECREF(module); + return NULL; + } + + return module; +} diff --git a/src/module/init.cc b/src/module/init.cc deleted file mode 100644 index 03463da..0000000 --- a/src/module/init.cc +++ /dev/null @@ -1,109 +0,0 @@ -/* - * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 - * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a - * aplicativos mainframe. Registro no INPI sob o nome G3270. - * - * Copyright (C) <2008> - * - * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob - * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela - * Free Software Foundation. - * - * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER - * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO - * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para - * obter mais detalhes. - * - * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este - * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin - * St, Fifth Floor, Boston, MA 02110-1301 USA - * - * Este programa está nomeado como - e possui - linhas de código. - * - * Contatos: - * - * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) - * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) - * - * - * - */ - - #include - -/*---[ Implement ]----------------------------------------------------------------------------------*/ - -PyMODINIT_FUNC PyInit_tn3270(void) -{ - static PyMethodDef methods[] = { - - { - "version", - py3270_get_module_version, - METH_NOARGS, - "Get " PACKAGE_NAME " version" - }, - - { - "revision", - py3270_get_module_revision, - METH_NOARGS, - "Get " PACKAGE_NAME " revision" - - }, - - { - NULL, - NULL, - 0, - NULL - } - - }; - - static struct PyModuleDef definition = { - PyModuleDef_HEAD_INIT, - "tn3270", // name of module - PACKAGE_DESCRIPTION, // module documentation, may be NUL - -1, // size of per-interpreter state of the module or -1 if the module keeps state in global variables. - methods // Module methods - }; - - Py_Initialize(); - - PyObject *module = PyModule_Create(&definition); - - if(module) { - - PyObject * except = PyErr_NewException("tn3270.error", NULL, NULL); - - Py_XINCREF(except); - if (PyModule_AddObject(module, "error", except) < 0) { - Py_XDECREF(except); - Py_CLEAR(except); - Py_DECREF(module); - return NULL; - } - - } - - - /* - - m = PyModule_Create(&spammodule); - if (m == NULL) - return NULL; - - SpamError = PyErr_NewException("spam.error", NULL, NULL); - Py_XINCREF(SpamError); - if (PyModule_AddObject(m, "error", SpamError) < 0) { - Py_XDECREF(SpamError); - Py_CLEAR(SpamError); - Py_DECREF(m); - return NULL; - } - - */ - - return module; -} diff --git a/src/terminal/init.cc b/src/terminal/init.cc index 7abb2f8..6a82fab 100644 --- a/src/terminal/init.cc +++ b/src/terminal/init.cc @@ -38,6 +38,57 @@ /*---[ Implement ]----------------------------------------------------------------------------------*/ +PyObject * py3270_session_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { + + const char *id = ""; + + if (!PyArg_ParseTuple(args, "s", &id)) + id = ""; + + pySession * session = (pySession *) type->tp_alloc(type,0); + + printf("---[%s]---\n",id); + + if(session) { + + try { + + session->host = new TN3270::Host(id); + + printf("---[%s=%p]---\n",id,session->host); + return (PyObject *) session; + + } catch(const exception &e) { + + printf("---[%s]---\n",e.what()); + PyErr_SetString(PyExc_RuntimeError, e.what()); + + } catch( ... ) { + + printf("---[%s]---\n","???"); + PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module"); + + } + + } + + type->tp_free(session); + + return NULL; + +} + +void py3270_session_dealloc(pySession * self) { + + if(self->host) { + delete self->host; + } + + Py_TYPE(self)->tp_free((PyObject *) self); + +} + + /* PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { @@ -70,19 +121,4 @@ PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { } -int terminal_init(pw3270_TerminalObject *self, PyObject *args, PyObject *kwds) { - - return 0; - -} - -void terminal_dealloc(pw3270_TerminalObject * self) { - - trace("%s",__FUNCTION__); - - delete self->session; - - self->ob_type->tp_free((PyObject*)self); - -} */ diff --git a/testprograms/sample.py b/testprograms/sample.py index f9582bc..2b43763 100644 --- a/testprograms/sample.py +++ b/testprograms/sample.py @@ -8,7 +8,7 @@ print("Teste extensão pw3270") print("Using TN3270 Version " + tn3270.version()) print(tn3270.revision()) -#term = py3270.Terminal("") +session = tn3270.Session(":a") #print "Using pw3270 version " + term.Version() + " revision " + term.Revision() -- libgit2 0.21.2