Commit 7fbd26b0407a75c0845193053024fadb0c02e43b

Authored by Perry Werneck
1 parent 1bf62f40

Carga/Descarga da lib3270 já funciona pelo objeto python.

src/python/private.h
... ... @@ -44,5 +44,6 @@
44 44  
45 45 } pw3270_TerminalObject;
46 46  
  47 + extern PyObject * terminalError;
47 48  
48 49 #endif // PRIVATE_H_INCLUDED
... ...
src/python/py3270.cc
... ... @@ -36,6 +36,10 @@
36 36  
37 37 #include "private.h"
38 38  
  39 +/*---[ Globals ]------------------------------------------------------------------------------------*/
  40 +
  41 + PyObject * terminalError = NULL;
  42 +
39 43 /*---[ Implement ]----------------------------------------------------------------------------------*/
40 44  
41 45 static PyObject * get_revision(PyObject *self, PyObject *args) {
... ... @@ -44,6 +48,111 @@ static PyObject * get_revision(PyObject *self, PyObject *args) {
44 48  
45 49 }
46 50  
  51 +static PyObject * terminal_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
  52 +
  53 + pw3270_TerminalObject *self = (pw3270_TerminalObject *) type->tp_alloc(type, 0);
  54 +
  55 + trace("%s: self=%p",__FUNCTION__,self);
  56 +
  57 + self->session = NULL;
  58 +
  59 + return (PyObject *)self;
  60 +}
  61 +
  62 +
  63 +static int terminal_init(pw3270_TerminalObject *self, PyObject *args, PyObject *kwds) {
  64 +
  65 + const char *id = "";
  66 +
  67 + if (!PyArg_ParseTuple(args, "s", &id)) {
  68 + id = "";
  69 + }
  70 +
  71 + trace("%s(%s)",__FUNCTION__,id);
  72 +
  73 + try {
  74 +
  75 + self->session = PW3270_NAMESPACE::session::create(id);
  76 +
  77 + } catch(std::exception &e) {
  78 +
  79 + trace("%s failed: %s",__FUNCTION__,e.what());
  80 + PyErr_SetString(terminalError, e.what());
  81 +
  82 + }
  83 +
  84 +
  85 + return 0;
  86 +
  87 +}
  88 +
  89 +static void terminal_dealloc(pw3270_TerminalObject * self) {
  90 +
  91 + trace("%s",__FUNCTION__);
  92 +
  93 + delete self->session;
  94 +
  95 + self->ob_type->tp_free((PyObject*)self);
  96 +
  97 +}
  98 +
  99 +static PyMethodDef terminal_methods[] = {
  100 +
  101 + {NULL} // Sentinel
  102 +
  103 +};
  104 +
  105 +/*
  106 +static PyMemberDef terminal_members[] = {
  107 +
  108 + { NULL } // Sentinel
  109 +
  110 +};
  111 +*/
  112 +
  113 +static PyTypeObject pw3270_TerminalType = {
  114 + PyObject_HEAD_INIT(NULL)
  115 + 0, /*ob_size*/
  116 + "py3270.terminal", /*tp_name*/
  117 + sizeof(pw3270_TerminalObject), /*tp_basicsize*/
  118 + 0, /*tp_itemsize*/
  119 + (destructor) terminal_dealloc, /*tp_dealloc*/
  120 + 0, /*tp_print*/
  121 + 0, /*tp_getattr*/
  122 + 0, /*tp_setattr*/
  123 + 0, /*tp_compare*/
  124 + 0, /*tp_repr*/
  125 + 0, /*tp_as_number*/
  126 + 0, /*tp_as_sequence*/
  127 + 0, /*tp_as_mapping*/
  128 + 0, /*tp_hash */
  129 + 0, /*tp_call*/
  130 + 0, /*tp_str*/
  131 + 0, /*tp_getattro*/
  132 + 0, /*tp_setattro*/
  133 + 0, /*tp_as_buffer*/
  134 + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  135 + "3270 terminal object", /* tp_doc */
  136 + 0, /* tp_traverse */
  137 + 0, /* tp_clear */
  138 + 0, /* tp_richcompare */
  139 + 0, /* tp_weaklistoffset */
  140 + 0, /* tp_iter */
  141 + 0, /* tp_iternext */
  142 + terminal_methods, /* tp_methods */
  143 + 0, // terminal_members, /* tp_members */
  144 + 0, /* tp_getset */
  145 + 0, /* tp_base */
  146 + 0, /* tp_dict */
  147 + 0, /* tp_descr_get */
  148 + 0, /* tp_descr_set */
  149 + 0, /* tp_dictoffset */
  150 + (initproc) terminal_init, /* tp_init */
  151 + 0, /* tp_alloc */
  152 + terminal_new, /* tp_new */
  153 +
  154 +};
  155 +
47 156 static PyMethodDef MyMethods[] = {
48 157  
49 158 { "revision", get_revision, METH_VARARGS, "Get module revision." },
... ... @@ -54,15 +163,24 @@ static PyMethodDef MyMethods[] = {
54 163  
55 164 PyMODINIT_FUNC initpy3270(void) {
56 165  
  166 + // Cria o módulo
  167 +
57 168 PyObject *m = Py_InitModule("py3270", MyMethods);
58 169  
59 170 if (m == NULL)
60 171 return;
61 172  
62   -/*
63   - SpamError = PyErr_NewException("spam.error", NULL, NULL);
64   - Py_INCREF(SpamError);
65   - PyModule_AddObject(m, "error", SpamError);
66   -*/
  173 + // Adiciona objeto para tratamento de erros.
  174 + terminalError = PyErr_NewException((char *) "py3270.error", NULL, NULL);
  175 +
  176 + (void) Py_INCREF(terminalError);
  177 + PyModule_AddObject(m, "error", terminalError);
  178 +
  179 + // Adiciona terminal
  180 + if(PyType_Ready(&pw3270_TerminalType) < 0)
  181 + return
  182 +
  183 + (void) Py_INCREF(&pw3270_TerminalType);
  184 + PyModule_AddObject(m, "terminal", (PyObject *)&pw3270_TerminalType);
67 185  
68 186 }
... ...
src/python/sample.py
... ... @@ -7,4 +7,6 @@ print &quot;Teste extensão pw3270&quot;
7 7  
8 8 print py3270.revision()
9 9  
  10 +term = py3270.terminal("")
  11 +
10 12  
... ...