Changeset 13566


Ignore:
Timestamp:
Sep 20, 2019 4:15:14 PM (3 months ago)
Author:
randersson
Message:

#5819 Testing if the exceptions from jpype is now correctly raised using both versions of jpype (in py2 and py3). This commit contains some #... debug information that will be removed later.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/dev-5819/Python/src/pymodelica/compiler_wrappers.py

    r13537 r13566  
    2626from pymodelica.common.core import list_to_string
    2727from .compiler_exceptions import *
     28from sys import version_info
    2829
    2930
     
    4041        Modelica models. A compiler instance can be used multiple times.
    4142        """
     43        if version_info > (3, 0):
     44            self._is_python3 = True
     45            self._java_exception = jpype.JException
     46        else:
     47            self._is_python3 = False
     48            self._java_exception = jpype.JavaException
     49       
    4250        try:
    4351            options = ModelicaCompilerInterface.createOptions()
    44         except jpype.JException as ex:
     52        except self._java_exception as ex:
    4553            self._handle_exception(ex)
    4654           
     
    4856       
    4957        self._compiler = pym._create_compiler(ModelicaCompilerInterface, options)
     58       
    5059       
    5160    def set_options(self, compiler_options):
     
    9099        try:
    91100            self._compiler.setLogger(log_string)
    92         except jpype.JException as ex:
     101        except self._java_exception as ex:
    93102            self._handle_exception(ex)
    94103
     
    129138        try:
    130139            target_obj = self._compiler.createTargetObject(target, version)
    131         except jpype.JException as ex:
     140        except self._java_exception as ex:
    132141            self._handle_exception(ex)
    133142        return target_obj
     
    148157        try:
    149158            option = self._compiler.getBooleanOption(key)
    150         except jpype.JException as ex:
     159        except self._java_exception as ex:
    151160            self._handle_exception(ex)
    152161        return bool(option)
     
    171180        try:
    172181            self._compiler.setBooleanOption(key, value)
    173         except jpype.JException as ex:
     182        except self._java_exception as ex:
    174183            self._handle_exception(ex)
    175184       
     
    189198        try:
    190199            option = self._compiler.getIntegerOption(key)
    191         except jpype.JException as ex:
     200        except self._java_exception as ex:
    192201            self._handle_exception(ex)
    193202        return option
     
    212221        try:
    213222            self._compiler.setIntegerOption(key, value)
    214         except jpype.JException as ex:
     223        except self._java_exception as ex:
    215224            self._handle_exception(ex)
    216225       
     
    230239        try:
    231240            option = self._compiler.getRealOption(key)
    232         except jpype.JException as ex:
     241        except self._java_exception as ex:
    233242            self._handle_exception(ex)
    234243        return option
     
    253262        try:
    254263            self._compiler.setRealOption(key, value)
    255         except jpype.JException as ex:
     264        except self._java_exception as ex:
    256265            self._handle_exception(ex)
    257266                   
     
    271280        try:
    272281            option = self._compiler.getStringOption(key)
    273         except jpype.JException as ex:
     282        except self._java_exception as ex:
    274283            self._handle_exception(ex)
    275284        return str(option)
     
    294303        try:
    295304            self._compiler.setStringOption(key, value)
    296         except jpype.JException as ex:
     305        except self._java_exception as ex:
    297306            self._handle_exception(ex)
    298307       
     
    361370            unit = self._compiler.compileUnit(class_name, file_name, target, version, compile_to)
    362371            self._compiler.closeLogger()
    363         except jpype.JException as ex:
     372        except self._java_exception as ex:
    364373            self._handle_exception(ex)
    365374        from .compiler import CompilerResult
     
    401410            sr = self._compiler.parseModel(model_file_name)
    402411            return sr       
    403         except jpype.JException as ex:
     412        except self._java_exception as ex:
    404413            self._handle_exception(ex)
    405414
     
    439448            ipr = self._compiler.instantiateModel(source_root, model_class_name, target)
    440449            return ipr   
    441         except jpype.JException as ex:
     450        except self._java_exception as ex:
    442451            self._handle_exception(ex)
    443452
     
    477486            fclass = self._compiler.flattenModel(inst_class_decl, target, None)
    478487            return fclass   
    479         except jpype.JException as ex:
     488        except self._java_exception as ex:
    480489            self._handle_exception(ex)
    481490
     
    507516        try:
    508517            self._compiler.generateCode(fclass, target)
    509         except jpype.JException as ex:
     518        except self._java_exception as ex:
    510519            self._handle_exception(ex)
    511520           
     
    516525        or the default JError.
    517526        """
    518         if ex.javaClass() is CompilerException:
     527        def _py_handle_exception(raised_ex, ref_ex):
     528            # Due to differences in jpype between 0.6.2 and 0.7.0
     529            #  we need to compare the raised exception with the reference exception
     530            #  in different ways.
     531            return type(raised_ex) is ref_ex if self._is_python3 else raised_ex.javaClass() is ref_ex
     532
     533        #print("ex.message() = {}".format(ex.message()))
     534        #print("ex.args = {}".format(ex.args))
     535        #print("ex.stacktrace() = {}".format(ex.stacktrace()))
     536        #print("str(ex) = {}".format(str(ex)))
     537        #import sys; sys.exit()
     538        #raise ex
     539        #ex.message=ex.message()
     540        #raise ex doesnt work because they are different in py2 and py3
     541        #raise ex
     542        if _py_handle_exception(ex, CompilerException):
    519543            arraylist = ex.__javaobject__.getProblems()
    520544            itr = arraylist.iterator()
     
    544568            raise CompilerError(errors, warnings)
    545569       
    546         if ex.javaClass() is IllegalCompilerArgumentException:
     570        if _py_handle_exception(ex, IllegalCompilerArgumentException):
    547571            raise IllegalCompilerArgumentError(
    548572                str(ex.__javaobject__.getMessage()))
    549573       
    550         if ex.javaClass() is ModelicaClassNotFoundException:
     574        if _py_handle_exception(ex, ModelicaClassNotFoundException):
    551575            raise ModelicaClassNotFoundError(
    552576                str(ex.__javaobject__.getClassName()))
    553577       
    554         if ex.javaClass() is IllegalLogStringException:
     578        if _py_handle_exception(ex, IllegalLogStringException):
    555579            raise IllegalLogStringError(
    556580                str(ex.__javaobject__.getMessage()))
    557581       
    558         if ex.javaClass() is jpype.java.io.FileNotFoundException:
     582        if _py_handle_exception(ex, jpype.java.io.FileNotFoundException):
    559583            raise IOError(
    560584                '\nMessage: '+ex.message().encode('utf-8')+\
    561585                '\nStacktrace: '+ex.stacktrace().encode('utf-8'))
    562586       
    563         if ex.javaClass() is jpype.java.io.IOException:
     587        if _py_handle_exception(ex, jpype.java.io.IOException):
    564588            raise IOError(
    565                 '\nMessage: '+ex.message().encode('utf-8')+\
    566                 '\nStacktrace: '+ex.stacktrace().encode('utf-8'))
    567        
    568         if ex.javaClass() is jpype.javax.xml.xpath.XPathExpressionException:
     589                '\nMessage: '+str(ex.message().encode('utf-8'))+\
     590                '\nStacktrace: '+str(ex.stacktrace().encode('utf-8')))
     591       
     592        if _py_handle_exception(ex, jpype.javax.xml.xpath.XPathExpressionException):
    569593            raise XPathExpressionError(
    570                 '\nMessage: '+ex.message().encode('utf-8')+\
    571                 '\nStacktrace: '+ex.stacktrace().encode('utf-8'))
    572        
    573         if ex.javaClass() is jpype.javax.xml.parsers.ParserConfigurationException:
     594                '\nMessage: '+str(ex.message().encode('utf-8'))+\
     595                '\nStacktrace: '+str(ex.stacktrace().encode('utf-8')))
     596       
     597        if _py_handle_exception(ex, jpype.javax.xml.parsers.ParserConfigurationException):
    574598            raise ParserConfigurationError(
    575                 '\nMessage: '+ex.message().encode('utf-8')+\
    576                 '\nStacktrace: '+ex.stacktrace().encode('utf-8'))
    577        
    578         if ex.javaClass() is SAXException or \
    579             ex.javaClass() is SAXNotRecognizedException or \
    580             ex.javaClass() is SAXNotSupportedException or \
    581             ex.javaClass() is SAXParseException:
     599                '\nMessage: '+str(ex.message().encode('utf-8'))+\
     600                '\nStacktrace: '+str(ex.stacktrace().encode('utf-8')))
     601       
     602        if _py_handle_exception(ex, SAXException) or \
     603            _py_handle_exception(ex, SAXNotRecognizedException) or \
     604            _py_handle_exception(ex, SAXNotSupportedException) or \
     605            _py_handle_exception(ex, SAXParseException):
    582606            raise SAXError(
    583                 '\nMessage: '+ex.message().encode('utf-8')+\
    584                 '\nStacktrace: '+ex.stacktrace().encode('utf-8'))
     607                '\nMessage: '+str(ex.message().encode('utf-8'))+\
     608                '\nStacktrace: '+str(ex.stacktrace().encode('utf-8')))
    585609   
    586         if ex.javaClass() is UnknownOptionException:
     610        if _py_handle_exception(ex, UnknownOptionException):
    587611            raise UnknownOptionError(
    588                 ex.message().encode('utf-8')+'\nStacktrace: '+\
    589                     ex.stacktrace().encode('utf-8'))
    590 
    591         if ex.javaClass() is InvalidOptionValueException:
     612                str(ex.message().encode('utf-8'))+'\nStacktrace: '+\
     613                    str(ex.stacktrace().encode('utf-8')))
     614
     615        if _py_handle_exception(ex, InvalidOptionValueException):
    592616            raise InvalidOptionValueError(
    593                 ex.message().encode('utf-8')+'\nStacktrace: '+\
    594                     ex.stacktrace().encode('utf-8'))
    595        
    596         if ex.javaClass() is jpype.java.lang.Exception:
     617                str(ex.message().encode('utf-8'))+'\nStacktrace: '+\
     618                    str(ex.stacktrace().encode('utf-8')))
     619       
     620        if _py_handle_exception(ex, jpype.java.lang.Exception):
    597621            raise Exception(
    598                 '\nMessage: '+ex.message().encode('utf-8')+\
    599                 '\nStacktrace: '+ex.stacktrace().encode('utf-8'))
    600        
    601         if ex.javaClass() is jpype.java.lang.NullPointerException:
    602             raise JError(ex.stacktrace().encode('utf-8'))
    603        
    604         if ex.javaClass() is ModelicaCCodeCompilationException or \
    605             ex.javaClass() is OptimicaCCodeCompilationException:
     622                '\nMessage: '+str(ex.message().encode('utf-8'))+\
     623                '\nStacktrace: '+str(ex.stacktrace().encode('utf-8')))
     624       
     625        if _py_handle_exception(ex, jpype.java.lang.NullPointerException):
     626            raise JError(str(ex.stacktrace().encode('utf-8')))
     627       
     628        if _py_handle_exception(ex, ModelicaCCodeCompilationException) or \
     629            _py_handle_exception(ex, OptimicaCCodeCompilationException):
    606630            raise CcodeCompilationError(
    607                 '\nMessage: '+ex.message().encode('utf-8')+\
    608                 '\nStacktrace: '+ex.stacktrace().encode('utf-8'))
    609        
    610         raise JError(ex.stacktrace().encode('utf-8'))
     631                '\nMessage: '+str(ex.message().encode('utf-8'))+\
     632                '\nStacktrace: '+str(ex.stacktrace().encode('utf-8')))
     633       
     634        raise JError(str(ex.stacktrace().encode('utf-8')))
    611635
    612636class OptimicaCompiler(ModelicaCompiler):
     
    625649        try:
    626650            options = OptimicaCompilerInterface.createOptions()
    627         except jpype.JException as ex:
     651        except self._java_exception as ex:
    628652            self._handle_exception(ex)
    629653           
     
    651675        try:
    652676            self._compiler.setBooleanOption(key, value)
    653         except jpype.JException as ex:
    654             self._handle_exception(ex)
     677        except self._java_exception as ex:
     678            self._handle_exception(ex)
Note: See TracChangeset for help on using the changeset viewer.