This is an automated email from the ASF dual-hosted git repository. drdub pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/uima-uimacpp.git
commit 21c779925d1a5353d8314ce5f634012ef98b1919 Author: Pablo Duboue <[email protected]> AuthorDate: Wed Dec 28 07:31:24 2022 -0500 Changes for Python 3.9 --- scriptators/python/PythonSample.xml | 2 +- scriptators/python/pythonnator.cpp | 21 +++++++-------------- scriptators/python/sample.py | 14 ++++++-------- scriptators/uima.i | 22 +++------------------- 4 files changed, 17 insertions(+), 42 deletions(-) diff --git a/scriptators/python/PythonSample.xml b/scriptators/python/PythonSample.xml index 6d321b0..b74e04f 100644 --- a/scriptators/python/PythonSample.xml +++ b/scriptators/python/PythonSample.xml @@ -40,7 +40,7 @@ <configurationParameters> <configurationParameter> <name>DebugLevel</name> - <description>Sets the perl annotators debug level</description> + <description>Sets the python annotator debug level</description> <type>Integer</type> <multiValued>false</multiValued> <mandatory>false</mandatory> diff --git a/scriptators/python/pythonnator.cpp b/scriptators/python/pythonnator.cpp index 3dd0bf9..6756f22 100644 --- a/scriptators/python/pythonnator.cpp +++ b/scriptators/python/pythonnator.cpp @@ -30,22 +30,18 @@ // .so file, we do not get the exports from the dependent libraries in // when loading. This flag causes the python .so file to be re-bound // hopefully this can be removed pending a future relesase of UIMA -#define REBIND_PYTHON_SO +#define REBIND_PYTHON_SO #include <dlfcn.h> #endif -#if defined(PYTHON2_3) || defined(PYTHON2_4) #define _PY_BEGIN_BLOCK_THREADS_ \ - PyGILState_STATE __state = PyGILState_Ensure(); + PyEval_RestoreThread(thread_state); #define _PY_END_BLOCK_THREADS_ \ - PyGILState_Release(__state); -#else -#define _PY_BEGIN_BLOCK_THREADS_ -#define _PY_END_BLOCK_THREADS_ -#endif + thread_state = PyEval_SaveThread(); using namespace uima; using namespace std; +using namespace icu; // copied from SwigGenerated code @@ -77,7 +73,7 @@ class Pythonnator : public Annotator { static unsigned int refcount; public: - // We construct a perl interpreter in initialize - it lives for the + // We construct a python interpreter in initialize - it lives for the // life of the annotator - even if reconfigure happens. Reconfigure // and intialize both set dirty so the source code in the source file // and contained in the type system are evaluated. @@ -144,19 +140,15 @@ public: } Py_Initialize(); - PyEval_InitThreads(); thread_state = PyThreadState_Get(); - PyEval_ReleaseLock(); // We will reaquire the lock in a few lines - // after thread_state has been initialized } - PyEval_AcquireLock(); thread_state = Py_NewInterpreter(); if (thread_state == 0) { cerr<< MODULENAME ": unable to create interpreter" << endl; return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT; } - PyEval_ReleaseLock(); + thread_state = PyEval_SaveThread(); _PY_BEGIN_BLOCK_THREADS_ // main_module = PyImport_AddModule("__main__"); @@ -169,6 +161,7 @@ public: user_module = PyImport_ImportModule(srcfile); if (user_module == 0) { cerr << MODULENAME ": could not import python module " << srcfile <<endl; + _PY_END_BLOCK_THREADS_ return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT; } dict = PyModule_GetDict(user_module); diff --git a/scriptators/python/sample.py b/scriptators/python/sample.py index af69306..29887f7 100644 --- a/scriptators/python/sample.py +++ b/scriptators/python/sample.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -32,7 +30,7 @@ def initialize(annotContext): source = ac.extractValue("SourceFile") debug = ac.extractIntegerValue("DebugLevel") if debug > 0: - print source + ": initialize with matchString =" + ac.extractValue("matchString") + print(source + ": initialize with matchString =" + ac.extractValue("matchString")) def typeSystemInit(ts): @@ -40,13 +38,13 @@ def typeSystemInit(ts): global debug global ac if debug > 10: - print source + ": Type sytem init called" + print(source + ": Type sytem init called") global keywordtype keywordtype =ts.getType('com.ibm.uima.examples.keyword') if not keywordtype.isValid(): error = source + ": com.ibm.uima.examples.keyword is NOT found in type system!" ac.logError(error) - raise Exception, error + raise Exception(error) # # the process method is passed two parameters, the CAS and @@ -57,7 +55,7 @@ def process(tcas, rs): global debug global ac if debug > 10: - print source + ": This is a process function" + print(source + ": This is a process function") ac.logMessage("process called") text = tcas.getDocumentText() index = tcas.getIndexRepository() @@ -68,11 +66,11 @@ def process(tcas, rs): index.addFS(fs) annotCount += 1 if debug > 0: - print source + ": created " + str(annotCount) + " annotations" + print(source + ": created " + str(annotCount) + " annotations") if debug > 20: annots = 0 iterator = tcas.getAnnotationIndex(keywordtype).iterator() while iterator.isValid(): annots += 1 iterator.moveToNext() - print source + ": found " + str(annots) + " annotations" + print(source + ": found " + str(annots) + " annotations") diff --git a/scriptators/uima.i b/scriptators/uima.i index 5cc2b23..6d65ca3 100644 --- a/scriptators/uima.i +++ b/scriptators/uima.i @@ -66,22 +66,7 @@ static bool PyUnicodeConvert(PyObject *obj, UnicodeString &rv) { // convert using default codepage static bool PyStringConvert(PyObject *obj, UnicodeString &rv) { - char *src; - #ifdef PY_VERSION_HEX - #if (PY_VERSION_HEX >= 0x02050000) - /* Python version was greater than 2.5 */ - Py_ssize_t len; - PyString_AsStringAndSize(obj, &src, ( Py_ssize_t*)&len); - #else - /* Python version was less than 2.5 */ - int len; - PyString_AsStringAndSize(obj, &src, &len); - #endif - #else - /* Could not determine version */ - PyString_AsStringAndSize(obj, &src, &len); - #endif - rv = UnicodeString((const char *) src, (int32_t) len); + rv = UnicodeString((const char *)PyUnicode_AsUTF8(obj)); return true; } @@ -121,9 +106,8 @@ static bool ConvertUnicodeStringRef(const UnicodeStringRef &ref, } %typemap(in) const UnicodeString & (UnicodeString temp) { - if ( !( (PyUnicode_CheckExact($input) && PyUnicodeConvert($input, temp)) - || (PyString_CheckExact($input) && PyStringConvert($input, temp)))) - SWIG_exception(SWIG_TypeError, "string or unicode string expected or conversion failure"); + if ( !( (PyUnicode_CheckExact($input) && PyUnicodeConvert($input, temp)))) + SWIG_exception(SWIG_TypeError, "unicode string expected or conversion failure"); $1 = &temp; }
