From d85bd77116b2cf7e7565a0905b53ff0fa2a5b50e Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Tue, 10 Jan 2023 18:50:57 +0900 Subject: Drop support for python2. Signed-off-by: Michele Calgaro --- dcoppython/shell/gen_marshal_code.py | 20 +++--- dcoppython/shell/importedmodules.cpp | 4 +- dcoppython/shell/marshal_funcs.data | 130 +++++++++++++---------------------- dcoppython/shell/pcop.cpp | 36 ++++++---- 4 files changed, 82 insertions(+), 108 deletions(-) (limited to 'dcoppython/shell') diff --git a/dcoppython/shell/gen_marshal_code.py b/dcoppython/shell/gen_marshal_code.py index b6ae516f..5d947102 100644 --- a/dcoppython/shell/gen_marshal_code.py +++ b/dcoppython/shell/gen_marshal_code.py @@ -33,13 +33,13 @@ class DictMaker: def handle_string_marsh(self, attribute): """Handle marshalling of string item from the dictionary.""" - return ["if (%s && !PyString_Check(%s)) return false;" % (attribute, attribute), - "if (%s) { qobj.%s(TQString(PyString_AsString(%s)));" % (attribute, set_method(attribute), attribute), + return ["if (%s && !PyBytes_Check(%s)) return false;" % (attribute, attribute), + "if (%s) { qobj.%s(TQString::fromUtf8(PyBytes_AS_STRING(%s)));" % (attribute, set_method(attribute), attribute), "PyDict_DelItemString(dict,(char*)\"%s\"); } " % (attribute)] def handle_string_demarsh(self, attribute): """Handle demarshalling of string items into the dictionary.""" - return ["PyObject *%s = PyString_FromString(qobj.%s().utf8().data() );" % (attribute ,attribute), + return ["PyObject *%s = PyBytes_FromString(qobj.%s().utf8().data() );" % (attribute ,attribute), "PyDict_SetItemString(dict, (char*)\"%s\", %s);" % (attribute, attribute) ] @@ -141,7 +141,7 @@ class DocType: MARSHAL, DEMARSHAL, TOPYOBJ, FROMPYOBJ = 0,1,2,3 if len(sys.argv)!=4: - print "Use: gen_marshal_code.py " + print("Use: gen_marshal_code.py ") raise RuntimeError nowt, in_name, code_name, doc_xml_name = tuple(sys.argv) @@ -267,10 +267,10 @@ out_file.writelines([x + '\n' for x in gen_code_comments]) out_file.writelines([x + '\n' for x in code]) out_file.close() -xml_file = file(doc_xml_name,"w") -print >>xml_file, '' -print >>xml_file, '' -print >>xml_file, "" -[ [xml_file.write(x+"\n") for x in doc.xml()] for doc in doc_types.values() ] # silly one-liner -print >>xml_file, "" +xml_file = open(doc_xml_name,"w") +print('', file=xml_file) +print('', file=xml_file) +print("", file=xml_file) +[ [xml_file.write(x+"\n") for x in doc.xml()] for doc in list(doc_types.values()) ] # silly one-liner +print("", file=xml_file) xml_file.close() diff --git a/dcoppython/shell/importedmodules.cpp b/dcoppython/shell/importedmodules.cpp index 4b2a23bb..cc352954 100644 --- a/dcoppython/shell/importedmodules.cpp +++ b/dcoppython/shell/importedmodules.cpp @@ -40,8 +40,8 @@ namespace PythonDCOP { return 0; PyObject* args = PyTuple_New( 2 ); - PyTuple_SetItem( args, 0, PyString_FromString( appname ) ); - PyTuple_SetItem( args, 1, PyString_FromString( objname ) ); + PyTuple_SetItem( args, 0, PyBytes_FromString( appname ) ); + PyTuple_SetItem( args, 1, PyBytes_FromString( objname ) ); return PyObject_CallObject( cl, args ); } diff --git a/dcoppython/shell/marshal_funcs.data b/dcoppython/shell/marshal_funcs.data index 6cf4d34f..f8c00050 100644 --- a/dcoppython/shell/marshal_funcs.data +++ b/dcoppython/shell/marshal_funcs.data @@ -46,7 +46,7 @@ type: bool } %% to_pyobj { - return PyInt_FromLong(val ? 1 : 0); + return PyLong_FromLong(val ? 1 : 0); } %% marshal { @@ -69,9 +69,9 @@ type:int %doc as int i %% marshal { - if (!PyInt_Check(obj)) return false; + if (!PyLong_Check(obj)) return false; if (str) { - (*str) << (TQ_INT32)PyInt_AsLong(obj); + (*str) << (TQ_INT32)PyLong_AsLong(obj); } return true; } @@ -79,7 +79,7 @@ type:int { TQ_INT32 i; (*str) >> i; - return PyInt_FromLong( (long)i ); + return PyLong_FromLong( (long)i ); } %% @@ -87,9 +87,9 @@ type:uint %doc as int i %% marshal { - if (!PyInt_Check(obj)) return false; + if (!PyLong_Check(obj)) return false; if (str) { - (*str) << (TQ_INT32)PyInt_AsLong(obj); + (*str) << (TQ_INT32)PyLong_AsLong(obj); } return true; } @@ -97,7 +97,7 @@ type:uint { TQ_INT32 i; (*str) >> i; - return PyInt_FromLong( (long)i ); + return PyLong_FromLong( (long)i ); } %% @@ -124,17 +124,17 @@ type:uchar %doc as int c %% marshal { - if (PyString_Check(obj) && PyString_Size(obj)==1) { + if (PyBytes_Check(obj) && PyBytes_Size(obj)==1) { if (str) { - char *c = PyString_AsString(obj); + char *c = PyBytes_AS_STRING(obj); (*str) << (*c); } return true; } - if (PyInt_Check(obj)) { + if (PyLong_Check(obj)) { if (str) { - long l = PyInt_AsLong(obj); + long l = PyLong_AsLong(obj); TQ_UINT8 c = (TQ_UINT8)(l & 0xff); (*str) << c; } @@ -147,7 +147,7 @@ type:uchar { TQ_UINT8 c; (*str) >> c; - return PyString_FromStringAndSize((const char *)(&c),1); + return PyBytes_FromStringAndSize((const char *)(&c),1); } %% @@ -155,9 +155,9 @@ type:char %doc as int c %% marshal { - if (PyInt_Check(obj)) { + if (PyLong_Check(obj)) { if (str) { - long l = PyInt_AsLong(obj); + long l = PyLong_AsLong(obj); TQ_INT8 c = (TQ_INT8)(l & 0xff); (*str) << c; } @@ -170,7 +170,7 @@ type:char { TQ_INT8 c; (*str) >> c; - return PyInt_FromLong((long)c); + return PyLong_FromLong((long)c); } %% @@ -178,37 +178,29 @@ type:char type:TQByteArray %% marshal { - PyBufferProcs *pb = obj->ob_type->tp_as_buffer; - - if ( pb && pb->bf_getreadbuffer && pb->bf_getsegcount ) + Py_buffer view; + if (PyObject_GetBuffer(obj, &view, PyBUF_WRITABLE|PyBUF_SIMPLE)) { - // Get the number of buffer segments - int seg_count = (pb->bf_getsegcount)(obj, 0); - - if ( seg_count != 1 ) - // Can't handle more (or less) than 1 buffer segment - // at the moment - return false; - - // Get buffer size and data - void *data; - int size; - - if ( (size = (pb->bf_getreadbuffer)(obj, 0, &data)) < 0 ) - return false; + return false; + } - if (str) { - TQByteArray a; - a.setRawData( (const char*)data, size ); - (*str) << a; - a.resetRawData( (const char*)data, size ); - } + int size = view.len; + if (size < 0) + { + PyBuffer_Release(&view); + return false; + } - return true; + if (str) + { + TQByteArray a; + a.setRawData((const char*)view.buf, size); + (*str) << a; + a.resetRawData((const char*)view.buf, size); } - else - // obj does not implement the buffer interface - return false; + + PyBuffer_Release(&view); + return true; } %% demarshal { @@ -219,25 +211,8 @@ type:TQByteArray uint size = a.size(); char *data = a.data(); - // Create a new buffer object and copy the data. - // Don't use PyBuffer_FromMemory() and the likes since - // that wouldn't give correct allocation and deallocation. - - PyObject *buffer_obj = PyBuffer_New( size ); - - if ( !buffer_obj ) - return NULL; - - PyBufferProcs *pb = buffer_obj->ob_type->tp_as_buffer; - - void *buffer_data; - - (pb->bf_getwritebuffer)( buffer_obj, 0, &buffer_data ); - - for ( uint i = 0; i < size; i++ ) - ((char*)buffer_data)[i] = data[i]; - - return buffer_obj; + // Demarshal to a writable buffer object + return PyBytes_FromStringAndSize(data, size); } %% @@ -245,9 +220,9 @@ type:TQString %doc as str s %% marshal { - if (!PyString_Check(obj)) return false; + if (!PyBytes_Check(obj)) return false; if (str) { - TQString s( PyString_AsString(obj) ); + TQString s = TQString::fromUtf8(PyBytes_AS_STRING(obj)); (*str) << s; } return true; @@ -256,7 +231,7 @@ type:TQString { TQString s; (*str) >> s; - return PyString_FromString( s.utf8().data() ); + return PyBytes_FromString( s.utf8().data() ); } %% @@ -264,9 +239,9 @@ type:TQCString %doc as str s %% marshal { - if (!PyString_Check(obj)) return false; + if (!PyBytes_Check(obj)) return false; if (str) { - TQCString s( PyString_AsString(obj) ); + TQCString s( PyBytes_AS_STRING(obj) ); (*str) << s; } return true; @@ -275,7 +250,7 @@ type:TQCString { TQCString s; (*str) >> s; - return PyString_FromString( s.data() ); + return PyBytes_FromString( s.data() ); } %% @@ -473,13 +448,6 @@ type:TQDateTime return dt; } - if (PyInt_Check(obj)) { - *ok=true; - TQDateTime dt; - dt.setTime_t( (uint)PyInt_AsLong(obj) ); - return dt; - } - PyObject *date_tuple, *time_tuple; if (PyArg_ParseTuple(obj, (char*)"OO", &date_tuple, &time_tuple)) { TQDateTime dt; @@ -507,13 +475,13 @@ type:KURL %% from_pyobj { *ok=false; - if (!PyString_Check(obj)) return KURL(); + if (!PyBytes_Check(obj)) return KURL(); *ok=true; - return KURL( TQString(PyString_AsString(obj)) ); + return KURL( TQString::fromUtf8(PyBytes_AS_STRING(obj)) ); } %% to_pyobj { - return PyString_FromString( val.prettyURL().utf8().data() ); + return PyBytes_FromString( val.prettyURL().utf8().data() ); } %% marshal %defaultcode @@ -524,14 +492,14 @@ type:KURL type:DCOPRef %% from_pyobj { - if (PyInstance_Check(obj) && + if (PyType_Check(obj) && PyObject_HasAttrString(obj, (char*)"appname") && PyObject_HasAttrString(obj, (char*)"name")) { PyObject *appname = PyObject_GetAttrString(obj, (char*)"appname"); PyObject *name = PyObject_GetAttrString(obj, (char*)"name"); - if (PyString_Check(appname) && PyString_Check(name)) { - char *c_appname = PyString_AsString(appname); - char *c_name = PyString_AsString(name); + if (PyBytes_Check(appname) && PyBytes_Check(name)) { + char *c_appname = PyBytes_AS_STRING(appname); + char *c_name = PyBytes_AS_STRING(name); DCOPRef ref; ref.setRef(TQCString(c_appname), TQCString(c_name) ); Py_DECREF(appname); diff --git a/dcoppython/shell/pcop.cpp b/dcoppython/shell/pcop.cpp index 19557a14..01b45c7f 100644 --- a/dcoppython/shell/pcop.cpp +++ b/dcoppython/shell/pcop.cpp @@ -188,7 +188,7 @@ namespace PythonDCOP { it.current(); ++it, ++c) { PyObject *tuple = PyTuple_New(2); PyList_SetItem(result, c, tuple); - PyTuple_SetItem(tuple, 0, PyString_FromString(it.currentKey() ) ); + PyTuple_SetItem(tuple, 0, PyBytes_FromString(it.currentKey() ) ); PyTuple_SetItem(tuple, 1, it.current()->pythonMethod() ); } return result; @@ -593,7 +593,7 @@ namespace PythonDCOP { QCStringList::ConstIterator end = apps.end(); unsigned int i = 0; for (; it != end; ++it, i++ ) - PyList_SetItem( l, i, PyString_FromString( (*it).data() ) ); + PyList_SetItem( l, i, PyBytes_FromString( (*it).data() ) ); return l; } @@ -621,7 +621,7 @@ namespace PythonDCOP { int add_pid = 1; if (PyArg_ParseTuple(args, (char*)"s|i", &appid, &add_pid)) { TQCString actual_appid = Client::instance()->dcop()->registerAs(TQCString(appid), add_pid!=0); - return PyString_FromString(actual_appid.data()); + return PyBytes_FromString(actual_appid.data()); } return NULL; } @@ -632,7 +632,7 @@ namespace PythonDCOP { if (PyArg_ParseTuple(args, (char*)"O|s", &py_dcop_object, &objid)) { Py_INCREF(py_dcop_object); PCOPObject *obj = objid ? new PCOPObject(py_dcop_object, objid) : new PCOPObject(py_dcop_object); - return PyCObject_FromVoidPtr( (void*)obj, delete_dcop_object ); + return PyCapsule_New( (void*)obj, NULL, (PyCapsule_Destructor)delete_dcop_object ); } return NULL; } @@ -646,7 +646,7 @@ namespace PythonDCOP { PyObject *c_obj; PyObject *method_list; if (PyArg_ParseTuple(args, (char*)"OO", &c_obj, &method_list) && - PyCObject_Check(c_obj) && + PyCapsule_CheckExact(c_obj) && PyList_Check(method_list)) { // extract each tuple from the list, aborting if any is invalid @@ -662,7 +662,7 @@ namespace PythonDCOP { meth_list.insert(method_signature, py_method); } - PCOPObject *obj = (PCOPObject*)PyCObject_AsVoidPtr(c_obj); + PCOPObject *obj = (PCOPObject*)PyCapsule_GetPointer(c_obj, NULL); if (obj) { if (!obj->setMethodList(meth_list)) return NULL; } @@ -675,8 +675,8 @@ namespace PythonDCOP { PyObject *get_method_list(PyObject */*self*/, PyObject *args) { PyObject *c_obj; if (PyArg_ParseTuple(args, (char*)"O", &c_obj)) { - if (PyCObject_Check(c_obj)) { - PCOPObject *obj = (PCOPObject*)PyCObject_AsVoidPtr(c_obj); + if (PyCapsule_CheckExact(c_obj)) { + PCOPObject *obj = (PCOPObject*)PyCapsule_GetPointer(c_obj, NULL); return obj->methodList(); } } @@ -736,7 +736,7 @@ namespace PythonDCOP { for(QCStringList::ConstIterator it = qt_list.begin(); it!=qt_list.end(); ++it,c++) - PyList_SetItem(l, c, PyString_FromString( (*it).data() ) ); + PyList_SetItem(l, c, PyBytes_FromString( (*it).data() ) ); return l; } @@ -757,14 +757,20 @@ PyMethodDef PCOPMethods[] = { { NULL, NULL, 0, NULL } /* Sentinel */ }; +static struct PyModuleDef pcopmodule = { + PyModuleDef_HEAD_INIT, + "pcop", + NULL, + -1, + PCOPMethods +}; + extern "C" { - - void initpcop() - { - (void) Py_InitModule( (char*)"pcop", PCOPMethods ); - } - + PyMODINIT_FUNC PyInit_pcop(void) + { + return PyModule_Create(&pcopmodule); + } } -- cgit v1.2.3