UnicodeEncodeError: 'charmap' codec can't encode character , when trying to get the java object of one of the diagrams

Hello

for i in range(len(allDiagrams)):
    aDiagram=allDiagrams[i]
    print(aDiagram)
    print(aDiagram.get_java_object()) #this line produced the error for one of the diagrams
    print(aDiagram.get_java_object().getName())

The three lines of code worked for most diagrams, until I got this error:

File "L/Python4Capella/sample_scripts/....py", line 39, in <module>
    if _pyease_sys.version_info.major == 2:
  File "E:\.....\MoreTesting\capella\dropins\Python37\Python37\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\uf0e0' in position 178: character maps to <undefined>

I was wondering why would that happen and how to avoid it in case it could cause problems on future scripts.

It seems like your Python is using the default encoding of your system: CP1252 (Windows).
Not sure what the problem is here. Since we use the Java code from Capella to load the model I guess this is not the issue. You could try:

 aDiagram.get_java_object().getName().encode('utf-8')

or

 aDiagram.get_java_object().getName().decode('utf-8')

But I’m not sure it will work…

Hello Yvan,
Thanks for the answer,
The error is produced by the get java object function itself (as commented on the first part of my post, inside the code)

So, i just took your suggestion and applied it to get java object instead of the getName function.
I got this:

print(aDiagram.get_java_object().encode('utf-8')) 
#Result: py4j.Py4JException: Method encode([class java.lang.String]) does not exist
print(aDiagram.get_java_object().decode('utf-8')) 
#Result: py4j.Py4JException: Method decode([class java.lang.String]) does not exist
    

I tried my script on a somewhat complexe project (many diagrams), it produced the error (unicode…);
Trying it on a simple project with only one diagram, did not produce the error, again my script worked fine for MOST diagrams, until it reach the one that cannot accept a “get_java_object” somehow… thus the error.

If you have time to investigate it, when you have time and feel like it, maybe try to run my script on one of your own complexe projects (get the diagrams in “allDiagrams” first). It seems to be a more complicated problem that the usual errors.

It looks like:

aDiagram.get_java_object()

returns a Java String looking at the error message. That’s surprising, it should be a DRepresentationDescriptor. So my guess is EASE is trying to convert the Java String to a Python String. But the Java String contains some Unicode characters not supported by CP1252.

  • check why that Python Diagram is referencing a Java String ?
  • check if the referenced String is properly encoded. Maybe reading the String in the first place was done with a wrong encoding leading to undefined characters.
  • check why this setup is using CP1252 instead of UTF8 and/or how it can be changed if needed ?
1 Like

Interesting,
I am not sure how to check all of these, i shoudl probably go to my capella preferences?
Anyway, here is a piece of info:

org.eclipse.sirius.viewpoint.impl.DRepresentationDescriptorImpl@12a6c08c (uid: _cTI3Yqx...) (documentation: , name: [IS]......)
[IS] .... 
<__main__.Diagram object at 0x0000027EFA670888> # THIS ONE IS THE OBJECT THAT DOES NOT ACCEPT GET JAVA OBJECT FUNCTION.
py4j.Py4JException: An exception was raised by the 

In this quote, you find examples of a Diagram that accepts all three instructions I wrote (first the py ob then the java ob then the name), the FOURTH line is the py ob of a diagram which has that string problem you observed. Indeed the last (fifth line) is the error.

I will try to investigate more later, as it for now I have a more important python 4 capella matter, I will try to write about it in another post in the next 10-30 minutes, if I can’t resolve it myself (still talking about the other matter).

1 Like