Skip to content

fixed bug of method PyString_FromString #670

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

Merged
merged 15 commits into from
Aug 24, 2018

Conversation

yagweb
Copy link
Contributor

@yagweb yagweb commented May 10, 2018

What does this implement/fix? Explain your changes.

The second parameter of PyString_FromStringAndSize should be filled with the length of the bytes array, not the the length of the raw string.

This method is a very basic method,sufficient tests should be applied.

Does this close any currently open issues?

#666
A unit test method need be added.

Any other comments?

No.

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

@den-run-ai
Copy link
Contributor

@yagweb failing in python 2.7:

https://travis-ci.org/pythonnet/pythonnet/jobs/377211034

runtime.cs(1213,56): error CS1503: Argument 1: cannot convert from 'System.IntPtr' to 'string'

@codecov
Copy link

codecov bot commented May 11, 2018

Codecov Report

Merging #670 into master will decrease coverage by 0.15%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #670      +/-   ##
==========================================
- Coverage    77.4%   77.24%   -0.16%     
==========================================
  Files          63       63              
  Lines        5589     5590       +1     
  Branches      892      892              
==========================================
- Hits         4326     4318       -8     
- Misses        970      980      +10     
+ Partials      293      292       -1
Flag Coverage Δ
#setup_linux 69.42% <ø> (ø) ⬆️
#setup_windows 76.42% <100%> (-0.16%) ⬇️
Impacted Files Coverage Δ
src/runtime/runtime.cs 91.4% <100%> (+0.03%) ⬆️
src/runtime/CustomMarshaler.cs 57.53% <0%> (-12.33%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fb84cd2...050f8cc. Read the comment docs.

@den-run-ai
Copy link
Contributor

@yagweb need a fix for appveyor pip update:

microsoft/vscode-python#1107

@den-run-ai
Copy link
Contributor

@yagweb this PR still needs a unit test for the failing case before it can be merged. All tests are passing!

@den-run-ai
Copy link
Contributor

@yagweb will this PR fix these 3 disabled tests?

[Ignore("Ambiguous behavior between PY2/PY3. Needs remapping")]

[Ignore("Ambiguous behavior between PY2/PY3. Needs remapping")]

public void TestUnicode()

@yagweb
Copy link
Contributor Author

yagweb commented May 31, 2018

@denfromufa No.

I also found another bug of PyString/PyAnsiString, the ToString method of a object constructed using PyString(string) don't work with non-ASCII strings.

I will add all these into the checklist and dig deeper when I have time.

Maybe they are the cause of #662.

@den-run-ai
Copy link
Contributor

@yagweb i added the unit test based on reported bug by @EcmaXp in #666 but the issue does not look resolved in Python 2.7, works on all Python 3+ versions:

Travis CI:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xec in position 0: ordinal not in range(128)
https://travis-ci.org/pythonnet/pythonnet/jobs/387536002#L2111

Appveyor CI:

assert test_unicode_str == text_type(world)
\x1b[1m\x1b[31mE       AssertionError: assert '\uc548\ub155' == '??'\x1b[0m
\x1b[1m\x1b[31mE         - \uc548\ub155\x1b[0m
E         + ??
src\tests\test_conversion.py:544: AssertionError

https://ci.appveyor.com/project/pythonnet/pythonnet/build/master-1182/job/nvwn4ek3eskteg3m#L220

What do you think? Maybe the test should be modified for Python 2.7?

@filmor
Copy link
Member

filmor commented Jun 4, 2018

I don't think this can work on Py2 without breaking backwards compatibility, as we identify System.String with str while it would need to be unicode.

@den-run-ai
Copy link
Contributor

@filmor agreed, so then skip the last test on PY2?

@filmor
Copy link
Member

filmor commented Jun 4, 2018

I think so, yes. Put a comment there for posterity :)

@@ -1193,7 +1193,11 @@ internal static bool PyString_Check(IntPtr ob)

internal static IntPtr PyString_FromString(string value)
{
#if PYTHON3
return PyUnicode_FromKindAndData(_UCS, value, value.Length);
#elif PYTHON2
return PyString_FromStringAndSize(value, value.Length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe check here whether the string is ASCII? I'm not sure where this one is used, but we should probably distinguish the case where we actually want Unicode (and should also return that on Python 2) from the cases that should be ASCII-only on Python 2 (like identifiers).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filmor can you file a separate issue for this with a suggested fix? this PR is now ready to merge!

@filmor filmor added this to the 2.4.0 milestone Jul 23, 2018
@filmor filmor merged commit b6417ca into pythonnet:master Aug 24, 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

Successfully merging this pull request may close these issues.

3 participants