Skip to content

Another shot at #495 #503

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 10 commits into from
Jun 26, 2017
Merged

Another shot at #495 #503

merged 10 commits into from
Jun 26, 2017

Conversation

rickardraysearch
Copy link
Contributor

@rickardraysearch rickardraysearch commented Jun 21, 2017

What does this implement/fix? Explain your changes.

Remove the extraneous call to __init__ from ClassDerived.InvokeCtor.

Does this close any currently open issues?

#495

Any other comments?

This is a different shot at fixing #495 . @filmor has one at #496 but it fails a couple of tests. This one might have some bugs wrt XDecref in MetaType.tp_call still.

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

@mention-bot
Copy link

@rickardraysearch, thanks! @vmuriart, @tonyroberts, @tiran, @ArvidJB and @brianlloyd, please review this.

@codecov
Copy link

codecov bot commented Jun 21, 2017

Codecov Report

Merging #503 into master will increase coverage by 0.79%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #503      +/-   ##
==========================================
+ Coverage   76.41%   77.21%   +0.79%     
==========================================
  Files          64       64              
  Lines        5572     5534      -38     
  Branches      895      887       -8     
==========================================
+ Hits         4258     4273      +15     
+ Misses       1020      975      -45     
+ Partials      294      286       -8
Flag Coverage Δ
#setup_linux 75.71% <ø> (ø) ⬆️
#setup_windows 76.56% <100%> (+0.79%) ⬆️
Impacted Files Coverage Δ
src/runtime/runtime.cs 91.6% <ø> (+0.76%) ⬆️
src/runtime/classderived.cs 88.34% <ø> (+7.91%) ⬆️
src/runtime/metatype.cs 71.55% <100%> (+0.82%) ⬆️
src/runtime/classbase.cs 80.95% <0%> (+2.85%) ⬆️
src/runtime/delegateobject.cs 65.9% <0%> (+11.36%) ⬆️

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 c8ae358...db5f43e. Read the comment docs.

assert calls[0][0] == (1,2,3)
assert calls[0][1] == {"foo":"bar"}

def test_clr_subclass_without_init_args():
Copy link
Contributor

Choose a reason for hiding this comment

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

please rename test_clr_subclass_init_without_args

IntPtr type = Runtime.PyObject_TYPE(obj);
IntPtr init = Runtime._PyType_Lookup(type, py__init__);
Runtime.XDecref(py__init__);
var init = Runtime.PyObject_GetAttrString(obj, "__init__");
Copy link
Member

Choose a reason for hiding this comment

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

You are leaking this reference.

Copy link
Contributor Author

@rickardraysearch rickardraysearch Jun 21, 2017

Choose a reason for hiding this comment

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

Oh. Should be fixed now.

@filmor
Copy link
Member

filmor commented Jun 21, 2017

Could you explain how and why this works?

/edit: Never mind, got it :)
@tonyroberts Do you know why the explicit (additional) call to __init__ was put into InvokeCtor in the first place?

@pythonnet pythonnet deleted a comment from den-run-ai Jun 21, 2017
@rickardraysearch
Copy link
Contributor Author

I believe that what's happening is that MetaType.tp_call is calling __init__ on the class, just as PyType_Type does in type_call. It has access to all of the passed arguments, including those not available in the CLR constructor.

Since the class' __init__ is called from there, there is no need to call it from ClassDerived.InvokeCtor.

When constructing python objects from e.g. C#, the __init__ method would need to be called separately, since the CLR constructor only creates the object. If that should change, I believe we should have a look at actually calling __new__ for python types with __namespace__ and make those arguments match whatever is generated for the CLR constructors.

@rickardraysearch
Copy link
Contributor Author

rickardraysearch commented Jun 22, 2017

From the commit history, it looks as if the call was added to invoke __init__ when instantiating python classes from managed code. The tests that @tonyroberts added then still work though, so hopefully this PR isn't breaking any existing major use cases by removing the call to __init__ when constructing python objects from managed code.

By the way, there are two tests in test_subclass.py that are currently skipped with the comment FIXME: test randomly pass/fails. I have run them a couple of hundred times, both on macos and windows, with python 2.7 and 3.6, and not seen a single fail. Can I try reenabling them? They are both relevant to this PR.

/edit: I reenabled them, and they worked on both Travis and Appveyor, at least this time.

@filmor filmor merged commit a5f5dc2 into pythonnet:master Jun 26, 2017
@filmor
Copy link
Member

filmor commented Jun 26, 2017

Thank you very much for your contribution. We'll have to see, whether this breaks compatibility for anyone, we probably should put a note into the changelog, documenting this change of behaviour.

@rickardraysearch
Copy link
Contributor Author

rickardraysearch commented Jun 26, 2017

Yes, it definitely makes sense to put a good explanation in the changelog. Also, could we have a discussion on how to bring back that functionality and exposing additional arguments to the managed constructors? Maybe a new issue is the best place for that.

Many thanks for merging this, by the way! It really makes a big difference for my chances of getting us to use pythonnet here.

@rickardraysearch rickardraysearch deleted the fix-init branch June 26, 2017 10:34
@filmor
Copy link
Member

filmor commented Jun 26, 2017

Feel free to open one :)

testrunner123 pushed a commit to testrunner123/pythonnet that referenced this pull request Sep 22, 2017
* Add tests of subclassing with __namespace__

* Remove __init__ call from ClassDerived.InvokeCtor

* Trying out __init__

* Cleanup

* Add tests constructing python type from CLR and calling __init__

* Revert borked changelog update

* Don't leak init reference

* Rename tests

* Remove unused internal Runtime.GetBoundArgTuple

* Reenable skipped tests in test_subclass.py
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.

4 participants