Skip to content

str() failure when C# ToString() return unicode character #666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
EcmaXp opened this issue May 5, 2018 · 6 comments
Closed

str() failure when C# ToString() return unicode character #666

EcmaXp opened this issue May 5, 2018 · 6 comments

Comments

@EcmaXp
Copy link

EcmaXp commented May 5, 2018

Environment

  • Pythonnet version: 2.4.0.dev0 (master; #19d854c)
  • Python version: 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
  • Operating System: Windows 10 Pro 64 Bit (1803; 17134.5)

Details

  • Describe what you were trying to get done.
  1. Just call str python built-in function with C# class
  2. Raise UnicodeDecodeError if value is 안녕
  3. Return 안녕PADD if value is `안녕PADDINGX"

It seems like failure str convert with ToString() function only.
(Another method, nor read value does not problem)

namespace Hello
{
    public class World
    {
        public string value = "안녕";

        public string GetString()
        {
            return value;
        }

        public override string ToString()
        {
            return value;
        }
    }
}
import clr
clr.AddReference("hello")
from Hello import World
world = World()
print(1, str(world.value))
print(2, str(world.GetString()))
print(3, str(world))
  • If there was a crash, please include the traceback here.
1 안녕
2 안녕
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    str(hello)
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 0-1: unexpected end of data
@yagweb
Copy link
Contributor

yagweb commented May 8, 2018

It's a bug of the PyString_FromString method in runtime.cs, refer to #662

@EcmaXp
Copy link
Author

EcmaXp commented May 10, 2018

https://github.com/EcmaXp/execlib/blob/master/gen_lib/PyExcelLibrary/Excel.cs#L14-L33

Because, PyString_FromStringAndSize python c api accept the bytes length, not string length.

@den-run-ai
Copy link
Contributor

@EcmaXp pull requests are welcome!

@EcmaXp
Copy link
Author

EcmaXp commented May 10, 2018

@denfromufa I can't build this repo, how can i build?

@den-run-ai
Copy link
Contributor

@EcmaXp have a look at wiki about building pythonnet and @yagweb pull request, feel free to add a unit test for it:

https://github.com/pythonnet/pythonnet/wiki
#670

@filmor
Copy link
Member

filmor commented Aug 29, 2018

The fix for this has been merged to master, so I'll close this. Feel free to reopen if the version in the master branch doesn't solve the issue.

@filmor filmor closed this as completed Aug 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants