Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

String from print() comes as Unicode while from raise Exception() as AnsiString#465

Answeredbypyscripter
MKostitsyn asked this question inQ&A
Discussion options

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:

image

Some of the data comes in incorrect encoding.

Any ideas how to solve this?

You must be logged in to vote

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

Comment options

In python IDLE it works fine:
image

You must be logged in to vote
0 replies
Comment options

When I run your code with Demo01 I works well.

image

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?

You must be logged in to vote
0 replies
Comment options

PythonSample.zip

Here.

You must be logged in to vote
0 replies
Comment options

image

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.

You must be logged in to vote
0 replies
Comment options

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')

You must be logged in to vote
5 replies
@MKostitsyn
Comment options

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: АБВГД ĀČĶ āčķ")    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@pyscripter
Comment options

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: АБВГД ĀČĶ āčķ
@MKostitsyn
Comment options

image
Why did this line disappear?

@pyscripter
Comment options

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.

@MKostitsyn
Comment options

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.

Answer selected bypyscripter
Comment options

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.

You must be logged in to vote
2 replies
@MKostitsyn
Comment options

Did you try running the sample project without specifying the encoding?

@pyscripter
Comment options

Yes. And if you inspect Script.Text you will see it is wrong.

Comment options

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"'
You must be logged in to vote
6 replies
@pyscripter
Comment options

Why don't you try it?

@MKostitsyn
Comment options

As I understand we receive different results in some cases.

@pyscripter
Comment options

Not if you encode your files. Python expects and uses utf8 encoding, In any case this is not a P4D issue.

@pyscripter
Comment options

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 АБВГД ĀČĶ āčķ"'
@MKostitsyn
Comment options

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!

Comment options

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;
You must be logged in to vote
1 reply
@pyscripter
Comment options

Done.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@MKostitsyn@pyscripter

[8]ページ先頭

©2009-2025 Movatter.jp