Skip to content

Commit ba8ad88

Browse files
Add more tests. Refactor solution
1 parent 47300d1 commit ba8ad88

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

src/embed_tests/QCTest.cs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class QCTests
1616
private static dynamic withArgs_PythonSuperInitDefault;
1717
private static dynamic withArgs_PythonSuperInitInt;
1818

19+
private static dynamic pureCSharpConstruction;
20+
1921
private static dynamic containsTest;
2022
private static dynamic module;
2123
private static string testModule = @"
@@ -63,6 +65,9 @@ def __init__(self):
6365
class PythonSuperInitNone(SuperInit):
6466
def jose(self):
6567
return 1
68+
69+
def PureCSharpConstruction():
70+
return SuperInit(1)
6671
";
6772

6873
[OneTimeSetUp]
@@ -81,6 +86,8 @@ public void Setup()
8186
withArgs_PythonSuperInitNotCallingBase = pyModule.GetAttr("WithArgs_PythonSuperInitNotCallingBase");
8287
withArgs_PythonSuperInitDefault = pyModule.GetAttr("WithArgs_PythonSuperInitDefault");
8388
withArgs_PythonSuperInitInt = pyModule.GetAttr("WithArgs_PythonSuperInitInt");
89+
90+
pureCSharpConstruction = pyModule.GetAttr("PureCSharpConstruction");
8491
}
8592

8693
[OneTimeTearDown]
@@ -107,15 +114,26 @@ public void ContainsTest(string key, bool expected)
107114
Assert.AreEqual(expected, (bool)containsTest(key, dic));
108115
}
109116

117+
[Test]
118+
public void PureCSharpConstruction()
119+
{
120+
using (Py.GIL())
121+
{
122+
var instance = pureCSharpConstruction();
123+
Assert.AreEqual(1, (int)instance.CalledInt);
124+
Assert.AreEqual(1, (int)instance.CalledDefault);
125+
}
126+
}
127+
110128
[Test]
111129
public void WithArgs_NoBaseConstructorCall()
112130
{
113131
using (Py.GIL())
114132
{
115133
var instance = withArgs_PythonSuperInitNotCallingBase(1);
116-
// this is true because we call the constructor always
117-
Assert.IsTrue((bool)instance.CalledInt);
118-
Assert.IsFalse((bool)instance.CalledDefault);
134+
Assert.AreEqual(0, (int)instance.CalledInt);
135+
// we call the constructor always
136+
Assert.AreEqual(1, (int)instance.CalledDefault);
119137
}
120138
}
121139

@@ -125,8 +143,8 @@ public void WithArgs_IntConstructor()
125143
using (Py.GIL())
126144
{
127145
var instance = withArgs_PythonSuperInitInt(1);
128-
Assert.IsTrue((bool)instance.CalledInt);
129-
Assert.IsFalse((bool)instance.CalledDefault);
146+
Assert.AreEqual(1, (int)instance.CalledInt);
147+
Assert.AreEqual(1, (int)instance.CalledDefault);
130148
}
131149
}
132150

@@ -136,8 +154,8 @@ public void WithArgs_DefaultConstructor()
136154
using (Py.GIL())
137155
{
138156
var instance = withArgs_PythonSuperInitDefault(1);
139-
Assert.IsTrue((bool)instance.CalledInt);
140-
Assert.IsTrue((bool)instance.CalledDefault);
157+
Assert.AreEqual(0, (int)instance.CalledInt);
158+
Assert.AreEqual(2, (int)instance.CalledDefault);
141159
}
142160
}
143161

@@ -147,9 +165,9 @@ public void NoArgs_NoBaseConstructorCall()
147165
using (Py.GIL())
148166
{
149167
var instance = pythonSuperInitNotCallingBase();
150-
Assert.IsFalse((bool)instance.CalledInt);
168+
Assert.AreEqual(0, (int)instance.CalledInt);
151169
// this is true because we call the default constructor always
152-
Assert.IsTrue((bool)instance.CalledDefault);
170+
Assert.AreEqual(1, (int)instance.CalledDefault);
153171
}
154172
}
155173

@@ -159,9 +177,9 @@ public void NoArgs_IntConstructor()
159177
using (Py.GIL())
160178
{
161179
var instance = pythonSuperInitInt();
162-
Assert.IsTrue((bool)instance.CalledInt);
180+
Assert.AreEqual(1, (int)instance.CalledInt);
163181
// this is true because we call the default constructor always
164-
Assert.IsTrue((bool)instance.CalledDefault);
182+
Assert.AreEqual(1, (int)instance.CalledDefault);
165183
}
166184
}
167185

@@ -171,8 +189,8 @@ public void NoArgs_DefaultConstructor()
171189
using (Py.GIL())
172190
{
173191
var instance = pythonSuperInitNone();
174-
Assert.IsFalse((bool)instance.CalledInt);
175-
Assert.IsTrue((bool)instance.CalledDefault);
192+
Assert.AreEqual(0, (int)instance.CalledInt);
193+
Assert.AreEqual(2, (int)instance.CalledDefault);
176194
}
177195
}
178196

@@ -183,8 +201,8 @@ public void NoArgs_NoConstructor()
183201
{
184202
var instance = pythonSuperInitDefault.Invoke();
185203

186-
Assert.IsFalse((bool)instance.CalledInt);
187-
Assert.IsTrue((bool)instance.CalledDefault);
204+
Assert.AreEqual(0, (int)instance.CalledInt);
205+
Assert.AreEqual(2, (int)instance.CalledDefault);
188206
}
189207
}
190208
}
@@ -210,15 +228,15 @@ public void EmitInsights(params Insight[] insights)
210228

211229
public class SuperInit
212230
{
213-
public bool CalledInt { get; private set; }
214-
public bool CalledDefault { get; private set; }
231+
public int CalledInt { get; private set; }
232+
public int CalledDefault { get; private set; }
215233
public SuperInit(int a)
216234
{
217-
CalledInt = true;
235+
CalledInt++;
218236
}
219237
public SuperInit()
220238
{
221-
CalledDefault = true;
239+
CalledDefault++;
222240
}
223241
}
224242

src/runtime/Types/ClassObject.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,12 @@ static NewReference tp_new_impl(BorrowedReference tp, BorrowedReference args, Bo
121121
binder.AddMethod(self.constructors[i]);
122122
}
123123

124-
// let's try to generate a binding using the args/kw we have
125-
var binding = binder.Bind(pythonObj.Borrow(), args, kw);
124+
using var tuple = Runtime.PyTuple_New(0);
125+
var binding = binder.Bind(pythonObj.Borrow(), tuple.Borrow(), null);
126126
if (binding != null)
127127
{
128128
binding.info.Invoke(obj, BindingFlags.Default, null, binding.args, null);
129129
}
130-
else
131-
{
132-
// if we didn't match any constructor let's fall back into the default constructor, no args
133-
using var tuple = Runtime.PyTuple_New(0);
134-
binding = binder.Bind(pythonObj.Borrow(), tuple.Borrow(), null);
135-
if(binding != null)
136-
{
137-
binding.info.Invoke(obj, BindingFlags.Default, null, binding.args, null);
138-
}
139-
}
140130
}
141131
catch (Exception)
142132
{

0 commit comments

Comments
 (0)