8000 remove internal uses of PythonException parameterless constructor · losttech/pythonnet@35ac4ec · GitHub
[go: up one dir, main page]

Skip to content

Commit 35ac4ec

Browse files
committed
remove internal uses of PythonException parameterless constructor
pythonnet#893
1 parent e797f7f commit 35ac4ec

File tree

10 files changed

+51
-96
lines changed

10 files changed

+51
-96
lines changed

src/embed_tests/TestPythonException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void TestMessage()
3737
[Test]
3838
public void TestNoError()
3939
{
40-
var e = new PythonException(); // There is no PyErr to fetch
40+
var e = PythonException.FromPyErr(); // There is no PyErr to fetch
4141
Assert.AreEqual("", e.Message);
4242
}
4343

src/runtime/delegatemanager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public object TrueDispatch(ArrayList args)
248248

249249
if (op == IntPtr.Zero)
250250
{
251-
var e = new PythonException();
251+
var e = PythonException.FromPyErr();
252252
throw e;
253253
}
254254

src/runtime/exceptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ internal static void ErrorCheck(IntPtr pointer)
172172
{
173173
if (pointer == IntPtr.Zero)
174174
{
175-
throw new PythonException();
175+
throw PythonException.FromPyErr();
176176
}
177177
}
178178

@@ -183,7 +183,7 @@ internal static void ErrorOccurredCheck(IntPtr pointer)
183183
{
184184
if (pointer == IntPtr.Zero || ErrorOccurred())
185185
{
186-
throw new PythonException();
186+
throw PythonException.FromPyErr();
187187
}
188188
}
189189

src/runtime/pydict.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public PyDict()
3434
obj = Runtime.PyDict_New();
3535
if (obj == IntPtr.Zero)
3636
{
37-
throw new PythonException();
37+
throw PythonException.FromPyErr();
3838
}
3939
}
4040

@@ -106,10 +106,7 @@ public bool HasKey(string key)
106106
public PyObject Keys()
107107
{
108108
IntPtr items = Runtime.PyDict_Keys(obj);
109-
if (items == IntPtr.Zero)
110-
{
111-
throw new PythonException();
112-
}
109+
Exceptions.ErrorCheck(items);
113110
return new PyObject(items);
114111
}
115112

@@ -123,10 +120,7 @@ public PyObject Keys()
123120
public PyObject Values()
124121
{
125122
IntPtr items = Runtime.PyDict_Values(obj);
126-
if (items == IntPtr.Zero)
127-
{
128-
throw new PythonException();
129-
}
123+
Exceptions.ErrorCheck(items);
130124
return new PyObject(items);
131125
}
132126

@@ -140,10 +134,7 @@ public PyObject Values()
140134
public PyObject Items()
141135
{
142136
IntPtr items = Runtime.PyDict_Items(obj);
143-
if (items == IntPtr.Zero)
144-
{
145-
throw new PythonException();
146-
}
137+
Exceptions.ErrorCheck(items);
147138
return new PyObject(items);
148139
}
149140

@@ -157,10 +148,7 @@ public PyObject Items()
157148
public PyDict Copy()
158149
{
159150
IntPtr op = Runtime.PyDict_Copy(obj);
160-
if (op == IntPtr.Zero)
161-
{
162-
throw new PythonException();
163-
}
151+
Exceptions.ErrorCheck(op);
164152
return new PyDict(op);
165153
}
166154

@@ -176,7 +164,7 @@ public void Update(PyObject other)
176164
int result = Runtime.PyDict_Update(obj, other.obj);
177165
if (result < 0)
178166
{
179-
throw new PythonException();
167+
throw PythonException.FromPyErr();
180168
}
181169
}
182170

src/runtime/pyiter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public PyIter(IntPtr ptr) : base(ptr)
3434
public PyIter(PyObject iterable)
3535
{
3636
obj = Runtime.PyObject_GetIter(iterable.obj);
37-
if (obj == IntPtr.Zero)
38-
{
39-
throw new PythonException();
40-
}
37+
Exceptions.ErrorCheck(obj);
4138
}
4239

4340
protected override void Dispose(bool disposing)

src/runtime/pylist.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ public PyList(PyObject o)
5151
public PyList()
5252
{
5353
obj = Runtime.PyList_New(0);
54-
if (obj == IntPtr.Zero)
55-
{
56-
throw new PythonException();
57-
}
54+
Exceptions.ErrorCheck(obj);
5855
}
5956

6057

@@ -75,7 +72,7 @@ public PyList(PyObject[] items)
7572
int r = Runtime.PyList_SetItem(obj, i, ptr);
7673
if (r < 0)
7774
{
78-
throw new PythonException();
75+
throw PythonException.FromPyErr();
7976
}
8077
}
8178
}
@@ -104,10 +101,7 @@ public static bool IsListType(PyObject value)
104101
public static PyList AsList(PyObject value)
105102
{
106103
IntPtr op = Runtime.PySequence_List(value.obj);
107-
if (op == IntPtr.Zero)
108-
{
109-
throw new PythonException();
110-
}
104+
Exceptions.ErrorCheck(op);
111105
return new PyList(op);
112106
}
113107

@@ -123,7 +117,7 @@ public void Append(PyObject item)
123117
int r = Runtime.PyList_Append(obj, item.obj);
124118
if (r < 0)
125119
{
126-
throw new PythonException();
120+
throw PythonException.FromPyErr();
127121
}
128122
}
129123

@@ -138,7 +132,7 @@ public void Insert(int index, PyObject item)
138132
int r = Runtime.PyList_Insert(obj, index, item.obj);
139133
if (r < 0)
140134
{
141-
throw new PythonException();
135+
throw PythonException.FromPyErr();
142136
}
143137
}
144138

@@ -154,7 +148,7 @@ public void Reverse()
154148
int r = Runtime.PyList_Reverse(obj);
155149
if (r < 0)
156150
{
157-
throw new PythonException();
151+
throw PythonException.FromPyErr();
158152
}
159153
}
160154

@@ -170,7 +164,7 @@ public void Sort()
170164
int r = Runtime.PyList_Sort(obj);
171165
if (r < 0)
172166
{
173-
throw new PythonException();
167+
throw PythonException.FromPyErr();
174168
}
175169
}
176170
}

src/runtime/pyobject.cs

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,7 @@ public bool HasAttr(PyObject name)
220220
public PyObject GetAttr(string name)
221221
{
222222
IntPtr op = Runtime.PyObject_GetAttrString(obj, name);
223-
if (op == IntPtr.Zero)
224-
{
225-
throw new PythonException();
226-
}
223+
Exceptions.ErrorCheck(op);
227224
return new PyObject(op);
228225
}
229226

@@ -258,10 +255,7 @@ public PyObject GetAttr(string name, PyObject _default)
258255
public PyObject GetAttr(PyObject name)
259256
{
260257
IntPtr op = Runtime.PyObject_GetAttr(obj, name.obj);
261-
if (op == IntPtr.Zero)
262-
{
263-
throw new PythonException();
264-
}
258+
Exceptions.ErrorCheck(op);
265259
return new PyObject(op);
266260
}
267261

@@ -298,7 +292,7 @@ public void SetAttr(string name, PyObject value)
298292
int r = Runtime.PyObject_SetAttrString(obj, name, value.obj);
299293
if (r < 0)
300294
{
301-
throw new PythonException();
295+
throw PythonException.FromPyErr();
302296
}
303297
}
304298

@@ -316,7 +310,7 @@ public void SetAttr(PyObject name, PyObject value)
316310
int r = Runtime.PyObject_SetAttr(obj, name.obj, value.obj);
317311
if (r < 0)
318312
{
319-
throw new PythonException();
313+
throw PythonException.FromPyErr();
320314
}
321315
}
322316

@@ -333,7 +327,7 @@ public void DelAttr(string name)
333327
int r = Runtime.PyObject_SetAttrString(obj, name, IntPtr.Zero);
334328
if (r < 0)
335329
{
336-
throw new PythonException();
330+
throw PythonException.FromPyErr();
337331
}
338332
}
339333

@@ -351,7 +345,7 @@ public void DelAttr(PyObject name)
351345
int r = Runtime.PyObject_SetAttr(obj, name.obj, IntPtr.Zero);
352346
if (r < 0)
353347
{
354-
throw new PythonException();
348+
throw PythonException.FromPyErr();
355349
}
356350
}
357351

@@ -369,7 +363,7 @@ public virtual PyObject GetItem(PyObject key)
369363
IntPtr op = Runtime.PyObject_GetItem(obj, key.obj);
370364
if (op == IntPtr.Zero)
371365
{
372-
throw new PythonException();
366+
throw PythonException.FromPyErr();
373367
}
374368
return new PyObject(op);
375369
}
@@ -422,7 +416,7 @@ public virtual void SetItem(PyObject key, PyObject value)
422416
int r = Runtime.PyObject_SetItem(obj, key.obj, value.obj);
423417
if (r < 0)
424418
{
425-
throw new PythonException();
419+
throw PythonException.FromPyErr();
426420
}
427421
}
428422

@@ -474,7 +468,7 @@ public virtual void DelItem(PyObject key)
474468
int r = Runtime.PyObject_DelItem(obj, key.obj);
475469
if (r < 0)
476470
{
477-
throw new PythonException();
471+
throw PythonException.FromPyErr();
478472
}
479473
}
480474

@@ -585,10 +579,7 @@ public virtual PyObject this[int index]
585579
public PyObject GetIterator()
586580
{
587581
IntPtr r = Runtime.PyObject_GetIter(obj);
588-
if (r == IntPtr.Zero)
589-
{
590-
throw new PythonException();
591-
}
582+
Exceptions.ErrorCheck(r);
592583
return new PyObject(r);
593584
}
594585

@@ -618,10 +609,7 @@ public PyObject Invoke(params PyObject[] args)
618609
var t = new PyTuple(args);
619610
IntPtr r = Runtime.PyObject_Call(obj, t.obj, IntPtr.Zero);
620611
t.Dispose();
621-
if (r == IntPtr.Zero)
622-
{
623-
throw new PythonException();
624-
}
612+
Exceptions.ErrorCheck(r);
625613
return new PyObject(r);
626614
}
627615

@@ -636,10 +624,7 @@ public PyObject Invoke(params PyObject[] args)
636624
public PyObject Invoke(PyTuple args)
637625
{
638626
IntPtr r = Runtime.PyObject_Call(obj, args.obj, IntPtr.Zero);
639-
if (r == IntPtr.Zero)
640-
{
641-
throw new PythonException();
642-
}
627+
Exceptions.ErrorCheck(r);
643628
return new PyObject(r);
644629
}
645630

@@ -656,10 +641,7 @@ public PyObject Invoke(PyObject[] args, PyDict kw)
656641
var t = new PyTuple(args);
657642
IntPtr r = Runtime.PyObject_Call(obj, t.obj, kw != null ? kw.obj : IntPtr.Zero);
658643
t.Dispose();
659-
if (r == IntPtr.Zero)
660-
{
661-
throw new PythonException();
662-
}
644+
Exceptions.ErrorCheck(r);
663645
return new PyObject(r);
664646
}
665647

@@ -674,10 +656,7 @@ public PyObject Invoke(PyObject[] args, PyDict kw)
674656
public PyObject Invoke(PyTuple args, PyDict kw)
675657
{
676658
IntPtr r = Runtime.PyObject_Call(obj, args.obj, kw != null ? kw.obj : IntPtr.Zero);
677-
if (r == IntPtr.Zero)
678-
{
679-
throw new PythonException();
680-
}
659+
Exceptions.ErrorCheck(r);
681660
return new PyObject(r);
682661
}
683662

@@ -835,10 +814,7 @@ public bool IsTrue()
835814
public PyList Dir()
836815
{
837816
IntPtr r = Runtime.PyObject_Dir(obj);
838-
if (r == IntPtr.Zero)
839-
{
840-
throw new PythonException();
841-
}
817+
Exceptions.ErrorCheck(r);
842818
return new PyList(r);
843819
}
844820

@@ -895,7 +871,7 @@ public override bool Equals(object o)
895871
int r = Runtime.PyObject_Compare(obj, ((PyObject)o).obj);
896872
if (Exceptions.ErrorOccurred())
897873
{
898-
throw new PythonException();
874+
throw PythonException.FromPyErr();
899875
}
900876
return r == 0;
901877
}
@@ -936,7 +912,7 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
936912
int r = Runtime.PyObject_SetAttrString(obj, binder.Name, ptr);
937913
if (r < 0)
938914
{
939-
throw new PythonException();
915+
throw PythonException.FromPyErr();
940916
}
941917
Runtime.XDecref(ptr);
942918
return true;
@@ -1008,7 +984,7 @@ private static void AddArgument(IntPtr argtuple, int i, object target)
1008984

1009985
if (Runtime.PyTuple_SetItem(argtuple, i, ptr) < 0)
1010986
{
1011-
throw new PythonException();
987+
throw PythonException.FromPyErr();
1012988
}
1013989
}
1014990

0 commit comments

Comments
 (0)
0