-
-
Notifications
You must be signed in to change notification settings - Fork 324
String from print() comes as Unicode while from raise Exception() as AnsiString #465
-
I have Unicode file sample.py with the following script inside:
print("Print message: АБВГД ĀČĶ āčķ")
raise Exception("Raising exception: АБВГД ĀČĶ āčķ")
In IO UnicodeIO is set to True.
In OnSendUnicodeData() I simply dump all received messages into a text file. That's how it looks when I run the script:
Some of the data comes in incorrect encoding.
Any ideas how to solve this?
Beta Was this translation helpful? Give feedback.
All reactions
I have not tested but you need to specify a UTF8 encoding in
Script.LoadFromFile(aFileName);
and
TInputOutputStub.Output.SaveToFile('c:\Devel\tmp\PythonSample\sample.out')
Replies: 8 comments 14 replies
-
In python IDLE it works fine:
image
Beta Was this translation helpful? Give feedback.
All reactions
-
When I run your code with Demo01 I works well.
I used
PythonEngine1.ExecStrings( Memo1.Lines, 'c:\temp\sample.py' );
sample.py is utf8 encoded.
Could you post a simple project in a zip file that reproduces the issue?
Beta Was this translation helpful? Give feedback.
All reactions
-
Here.
Beta Was this translation helpful? Give feedback.
All reactions
-
The script should be in the file and the script file name should be passed to the ExecStrings method as a parameter to reproduce the mistake.
Beta Was this translation helpful? Give feedback.
All reactions
-
I have not tested but you need to specify a UTF8 encoding in
Script.LoadFromFile(aFileName);
and
TInputOutputStub.Output.SaveToFile('c:\Devel\tmp\PythonSample\sample.out')
Beta Was this translation helpful? Give feedback.
All reactions
-
When I specify the encoding in LoadFromFile, the following lines disappear from the output (which is really weird), and hence there is no encoding problem, not because the problem is solved, but because there is no line where the problem appeared previously.
I would like to have full information about the exceptions I receive in my scripts.
raise Exception("Raising exception: АБВГД ĀČĶ āčķ")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Beta Was this translation helpful? Give feedback.
All reactions
-
Here it works fine with:
Script.LoadFromFile(aFileName, TEncoding.UTF8);
TInputOutputStub.Output.SaveToFile('c:\users\username\downloads\PythonSample\sample.out', TEncoding.UTF8);
sample,out
Print message: АБВГД ĀČĶ āčķ
Traceback (most recent call last):
File "c:\users\kiria\downloads\PythonSample\sample.py", line 2, in <module>
raise Exception("Raising exception: АБВГД ĀČĶ āčķ")
Exception: Raising exception: АБВГД ĀČĶ āčķ
Unhandled exception caught: EPyException: Exception: Raising exception: АБВГД ĀČĶ āčķ
Beta Was this translation helpful? Give feedback.
All reactions
-
image
Why did this line disappear?
Beta Was this translation helpful? Give feedback.
All reactions
-
This line does not come up in either IDLE or when you run the script with python
python sample.py
The output I am getting is identical to that of IDLE and python.
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes, but if you remove encoding, this line appears and it shows the exact cause of the exception.
For my project this information would be useful.
Beta Was this translation helpful? Give feedback.
All reactions
-
if you remove encoding
the code you run is not the code you indent to run. The code you indent to run produces the output without that line.
Beta Was this translation helpful? Give feedback.
All reactions
-
Did you try running the sample project without specifying the encoding?
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes. And if you inspect Script.Text you will see it is wrong.
Beta Was this translation helpful? Give feedback.
All reactions
-
You get that line in other cases e,g,
sample.py
print "Hi"
sample.out
File "c:\users\kiria\downloads\PythonSample\sample.py", line 1
print "Hi"
^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
Unhandled exception caught: EPySyntaxError: SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? (line 1, offset 1): 'print "Hi"'
Beta Was this translation helpful? Give feedback.
All reactions
-
Why don't you try it?
Beta Was this translation helpful? Give feedback.
All reactions
-
As I understand we receive different results in some cases.
Beta Was this translation helpful? Give feedback.
All reactions
-
Not if you encode your files. Python expects and uses utf8 encoding, In any case this is not a P4D issue.
Beta Was this translation helpful? Give feedback.
All reactions
-
But since you asked:
sample.out
File "c:\users\kiria\downloads\PythonSample\sample.py", line 1
print "Hi АБВГД ĀČĶ āčķ"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
Unhandled exception caught: EPySyntaxError: SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? (line 1, offset 1): 'print "Hi АБВГД ĀČĶ āčķ"'
Beta Was this translation helpful? Give feedback.
All reactions
-
The behavioral is very strange.
I now specify the encoding as you suggested and run the script from zip above and I don't see the "^^^" line:
image
If I add space after the Exception(), then the "^^^" line appears:
image
I believe it's not a P4D issue, thanks for your help!
Beta Was this translation helpful? Give feedback.
All reactions
-
The last thing.
Maybe you will want to specify encoding here in P4D library.
procedure TPythonEngine.ExecFile(const FileName: string; locals,
globals: PPyObject);
var
SL: TStringList;
begin
SL := TStringList.Create;
try
SL.LoadFromFile(FileName);
ExecStrings(SL, locals, globals, FileName);
finally
SL.Free;
end;
end;
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Done.
Beta Was this translation helpful? Give feedback.