Skip to content

Struct default constructor #65

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 2 commits into from
Feb 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/runtime/classobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class ClassObject : ClassBase {

internal ClassObject(Type tp) : base(tp) {
ctors = type.GetConstructors();
binder = new ConstructorBinder();
binder = new ConstructorBinder(type);

for (int i = 0; i < ctors.Length; i++) {
binder.AddMethod(ctors[i]);
Expand Down
29 changes: 27 additions & 2 deletions src/runtime/constructorbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ namespace Python.Runtime {
//========================================================================

internal class ConstructorBinder : MethodBinder {
private Type _containingType = null;

internal ConstructorBinder () : base() {}
internal ConstructorBinder(Type containingType) : base() {
_containingType = containingType;
}

//====================================================================
// Constructors get invoked when an instance of a wrapped managed
Expand Down Expand Up @@ -53,9 +56,31 @@ internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw) {
/// </remarks>
internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw,
MethodBase info) {
Binding binding = this.Bind(inst, args, kw, info);
Object result;

if (_containingType.IsValueType && !_containingType.IsPrimitive &&
!_containingType.IsEnum && _containingType != typeof (decimal) &&
Runtime.PyTuple_Size(args) == 0) {
// If you are trying to construct an instance of a struct by
// calling its default constructor, that ConstructorInfo
// instance will not appear in reflection and the object must
// instead be constructed via a call to
// Activator.CreateInstance().
try {
result = Activator.CreateInstance(_containingType);
}
catch (Exception e) {
if (e.InnerException != null) {
e = e.InnerException;
}
Exceptions.SetError(e);
return null;
}
return result;
}

Binding binding = this.Bind(inst, args, kw, info);

if (binding == null) {
// It is possible for __new__ to be invoked on construction
// of a Python subclass of a managed class, so args may
Expand Down
7 changes: 3 additions & 4 deletions src/tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,9 @@ def testStructConstruction(self):
"""Test construction of structs."""
from System.Drawing import Point

def test():
p = Point()

self.assertRaises(TypeError, test)
p = Point()
self.assertTrue(p.X == 0)
self.assertTrue(p.Y == 0)

p = Point(0, 0)
self.assertTrue(p.X == 0)
Expand Down