Skip to content
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

[Out] parameters no longer added to return tuple #1308

Merged
merged 1 commit into from
Dec 8, 2020

Conversation

lostmsu
Copy link
Member

@lostmsu lostmsu commented Dec 8, 2020

Parameters marked with ParameterAttributes.Out (aka [Out]) are no longer returned in addition to the regular method return value (unless they are passed with ref or out keyword).

What does this implement/fix? Explain your changes.

As it is now possible to pass raw .NET objects to .NET methods, there's no need to marshal [Out] parameters back to Python manually. Python users can simply access modified object's data directly.

Any other comments?

It was confusing to see read, _ = stream.Read(buff, 0, buff.Length) in the test code, as Read method does not really have any out or ref parameters. It also failed on some .NET implementations, as the corresponding parameter does not always have an [Out] attribute.

Related issues

This should unblock #1307 (tests there are failing because of this difference).

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

…ed in addition to the regular method return value (unless they are passed with `ref` or `out` keyword).
@filmor
Copy link
Member

filmor commented Dec 8, 2020

I think this was included for compatibility with IronPython. How would you handle out parameters that still have automatic conversions like int and str?

@codecov-io
Copy link

codecov-io commented Dec 8, 2020

Codecov Report

Merging #1308 (ff89811) into master (57f4a80) will decrease coverage by 13.84%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff             @@
##           master    #1308       +/-   ##
===========================================
- Coverage   87.88%   74.04%   -13.85%     
===========================================
  Files           1        1               
  Lines         289      289               
===========================================
- Hits          254      214       -40     
- Misses         35       75       +40     
Flag Coverage Δ
setup_linux ?
setup_windows 74.04% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
setup.py 74.04% <0.00%> (-13.85%) ⬇️

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 57f4a80...ff89811. Read the comment docs.

@lostmsu
Copy link
Member Author

lostmsu commented Dec 8, 2020

@filmor this is the confusing part. This change does not affect GetInt(out int val) functions. It affects GetInt([Out] int val) functions, where [Out] parameter does nothing. The only real use case I see is that in Read([Out] byte[] buf) [Out] indicates, that the data will be put into the buf. The data can not be put into string or int without ref int/out int, and these are handled separately.

This annotation is normally meant for P/Invoke marshaling scenarios, where you have to add [Out] to byte[] arr into PInvoke signature for data to be properly marshaled back into arr upon exiting C function. I am not even sure why MemoryStream.Read has this annotation except for informational purposes. I believe passing Python lists in place of buf is why it was added, but considering lack of [Out] annotations in most places, I don't think it is a good thing to rely on.

@lostmsu
Copy link
Member Author

lostmsu commented Dec 8, 2020

For an instance of API difference between implementations in regards to [Out] attribute:

In .NET Core MemoryStream.Read has none: https://github.com/dotnet/runtime/blob/5432a66018c2499b88a54276f468ea903ed2b8de/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs#L333

In Mono it is present for COM-compatibility: https://github.com/mono/mono/blob/c5b88ec4f323f2bdb7c7d0a595ece28dae66579c/mcs/class/referencesource/mscorlib/system/io/memorystream.cs#L339

Without this change the same Python code would not work with both Mono and .NET Core.

@filmor filmor merged commit 5b09ba6 into pythonnet:master Dec 8, 2020
@filmor
Copy link
Member

filmor commented Dec 8, 2020

Thanks for the explanation. I ran into this problem while testing .NET Core and couldn't really make sense of it, wasn't aware of [Out].

@lostmsu lostmsu deleted the PR/OutMarshaling branch December 8, 2020 23:11
@lostmsu lostmsu mentioned this pull request Dec 18, 2020
4 tasks
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