Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Correctly dispose the result of PyRun_String #1071

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 1 commit into from
Mar 2, 2020

Conversation

lostmsu
Copy link
Member

@lostmsu lostmsu commented Feb 28, 2020

this also changes a few members of NewReference type to make them work in PyRun_String scenario

What does this implement/fix? Explain your changes.

This PR is motivated by an observation of a crash after PyRunString.TestExec2. It calls PythonEngine.Exec, which in case of exception fails to decref PyRun_String result.

I replace PyRun_String return type with NewReference and handle it accordingly.

NewReference instance members replaced with extension methods

In the process of making this change it was discovered, that members of NewReference should be moved to an extension class, because in regular instance members this is passed by mutable reference (e.g. analogous to ref NewReference in C# or NewReference& in C++),
which requires any method calling this member to have a mutable reference to NewReference:

bool TakeReference(NewReference reference)
  // can use `IsNull` here, because `reference` is a mutable parameter-variable
  // but this causes a problem below
  => reference.IsNull; 
NewReference reference = ...;
TakeReference(reference); // <= ERROR: `reference` has to be copied to be passed by value
                          // but we can't simply copy instances of `NewReference`
bool TakeReference(in NewReference reference)
  => reference.IsNull; // <= ERROR: instance member IsNull requires mutable `reference`
                       // but `in` makes it read-only. We could fix it by using `ref` instead,
                       // but that would force us to use `ref` in the caller, its caller, etc

Instead we do this:

bool TakeReference(in NewReference reference) => reference.IsNull();
static bool IsNull(this in NewReference reference)
    // NOTE: `this in` above impossible in regular instance members
  => reference.pointer == IntPtr.Zero;

This would be obsolete one day once dotnet/csharplang#1710 is released.

Does this close any currently open issues?

This might (unlikely) fix this test crash: https://ci.appveyor.com/project/pythonnet/pythonnet/builds/31115525/job/kkciw8xk5jaux5t7

Checklist

  • Make sure to include one or more tests for your change

@lostmsu lostmsu force-pushed the References/PyRun_String branch from dd72efc to e2d6741 Compare February 28, 2020 19:32
this also changes a few members of NewReference type to make them work in PyRun_String scenario
@lostmsu lostmsu force-pushed the References/PyRun_String branch from e2d6741 to 6c23644 Compare February 28, 2020 19:37
@codecov-io
Copy link

codecov-io commented Feb 28, 2020

Codecov Report

Merging #1071 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1071   +/-   ##
=======================================
  Coverage   86.75%   86.75%           
=======================================
  Files           1        1           
  Lines         302      302           
=======================================
  Hits          262      262           
  Misses         40       40
Flag Coverage Δ
#setup_linux 65.56% <ø> (ø) ⬆️
#setup_windows 71.52% <ø> (ø) ⬆️

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 4d1442d...6c23644. Read the comment docs.

@lostmsu lostmsu requested a review from filmor February 29, 2020 09:03
@filmor filmor merged commit d932176 into pythonnet:master Mar 2, 2020
@lostmsu lostmsu deleted the References/PyRun_String branch March 9, 2020 19:29
AlexCatarino pushed a commit to QuantConnect/pythonnet that referenced this pull request Jun 29, 2020
this also changes a few members of NewReference type to make them work in PyRun_String scenario
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