Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit03a401e

Browse files
committed
Remove unnecessaryCheckExceptionOccurred calls
1 parent7a9dcfa commit03a401e

File tree

11 files changed

+63
-72
lines changed

11 files changed

+63
-72
lines changed

‎src/runtime/NewReference.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ public PyObject MoveToPyObject()
2727
this.pointer=IntPtr.Zero;
2828
returnresult;
2929
}
30+
3031
/// <summary>
3132
/// Removes this reference to a Python object, and sets it to <c>null</c>.
3233
/// </summary>
3334
publicvoidDispose()
3435
{
35-
if(!this.IsNull())
36-
Runtime.XDecref(this.pointer);
37-
this.pointer=IntPtr.Zero;
36+
if(this.IsNull())
37+
{
38+
return;
39+
}
40+
Runtime.XDecref(this.pointer);
3841
}
3942

4043
/// <summary>

‎src/runtime/converter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,10 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
758758
gototype_error;
759759
}
760760
doubledd=Runtime.PyFloat_AsDouble(op);
761-
Runtime.CheckExceptionOccurred();
761+
if(dd==-1.0)
762+
{
763+
Runtime.CheckExceptionOccurred();
764+
}
762765
Runtime.XDecref(op);
763766
if(dd>Single.MaxValue||dd<Single.MinValue)
764767
{
@@ -777,7 +780,10 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
777780
gototype_error;
778781
}
779782
doubled=Runtime.PyFloat_AsDouble(op);
780-
Runtime.CheckExceptionOccurred();
783+
if(d==-1.0)
784+
{
785+
Runtime.CheckExceptionOccurred();
786+
}
781787
Runtime.XDecref(op);
782788
result=d;
783789
returntrue;

‎src/runtime/pyansistring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public PyAnsiString(PyObject o)
4545
publicPyAnsiString(strings)
4646
{
4747
obj=Runtime.PyString_FromString(s);
48-
Runtime.CheckExceptionOccurred();
48+
PythonException.ThrowIfIsNull(obj);
4949
}
5050

5151

‎src/runtime/pyfloat.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public PyFloat(PyObject o)
5151
publicPyFloat(doublevalue)
5252
{
5353
obj=Runtime.PyFloat_FromDouble(value);
54-
Runtime.CheckExceptionOccurred();
54+
PythonException.ThrowIfIsNull(obj);
5555
}
5656

5757

@@ -66,7 +66,7 @@ public PyFloat(string value)
6666
using(vars=newPyString(value))
6767
{
6868
obj=Runtime.PyFloat_FromString(s.obj,IntPtr.Zero);
69-
Runtime.CheckExceptionOccurred();
69+
PythonException.ThrowIfIsNull(obj);
7070
}
7171
}
7272

@@ -94,7 +94,7 @@ public static bool IsFloatType(PyObject value)
9494
publicstaticPyFloatAsFloat(PyObjectvalue)
9595
{
9696
IntPtrop=Runtime.PyNumber_Float(value.obj);
97-
Runtime.CheckExceptionOccurred();
97+
PythonException.ThrowIfIsNull(op);
9898
returnnewPyFloat(op);
9999
}
100100
}

‎src/runtime/pyint.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public PyInt(PyObject o)
5151
publicPyInt(intvalue)
5252
{
5353
obj=Runtime.PyInt_FromInt32(value);
54-
Runtime.CheckExceptionOccurred();
54+
PythonException.ThrowIfIsNull(obj);
5555
}
5656

5757

@@ -65,7 +65,7 @@ public PyInt(int value)
6565
publicPyInt(uintvalue)
6666
{
6767
obj=Runtime.PyInt_FromInt64(value);
68-
Runtime.CheckExceptionOccurred();
68+
PythonException.ThrowIfIsNull(obj);
6969
}
7070

7171

@@ -78,7 +78,7 @@ public PyInt(uint value)
7878
publicPyInt(longvalue)
7979
{
8080
obj=Runtime.PyInt_FromInt64(value);
81-
Runtime.CheckExceptionOccurred();
81+
PythonException.ThrowIfIsNull(obj);
8282
}
8383

8484

@@ -92,7 +92,7 @@ public PyInt(long value)
9292
publicPyInt(ulongvalue)
9393
{
9494
obj=Runtime.PyInt_FromInt64((long)value);
95-
Runtime.CheckExceptionOccurred();
95+
PythonException.ThrowIfIsNull(obj);
9696
}
9797

9898

@@ -151,7 +151,7 @@ public PyInt(sbyte value) : this((int)value)
151151
publicPyInt(stringvalue)
152152
{
153153
obj=Runtime.PyInt_FromString(value,IntPtr.Zero,0);
154-
Runtime.CheckExceptionOccurred();
154+
PythonException.ThrowIfIsNull(obj);
155155
}
156156

157157

@@ -178,7 +178,7 @@ public static bool IsIntType(PyObject value)
178178
publicstaticPyIntAsInt(PyObjectvalue)
179179
{
180180
IntPtrop=Runtime.PyNumber_Int(value.obj);
181-
Runtime.CheckExceptionOccurred();
181+
PythonException.ThrowIfIsNull(op);
182182
returnnewPyInt(op);
183183
}
184184

‎src/runtime/pylong.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public PyLong(PyObject o)
5151
publicPyLong(intvalue)
5252
{
5353
obj=Runtime.PyLong_FromLong(value);
54-
Runtime.CheckExceptionOccurred();
54+
PythonException.ThrowIfIsNull(obj);
5555
}
5656

5757

@@ -65,7 +65,7 @@ public PyLong(int value)
6565
publicPyLong(uintvalue)
6666
{
6767
obj=Runtime.PyLong_FromLong(value);
68-
Runtime.CheckExceptionOccurred();
68+
PythonException.ThrowIfIsNull(obj);
6969
}
7070

7171

@@ -78,7 +78,7 @@ public PyLong(uint value)
7878
publicPyLong(longvalue)
7979
{
8080
obj=Runtime.PyLong_FromLongLong(value);
81-
Runtime.CheckExceptionOccurred();
81+
PythonException.ThrowIfIsNull(obj);
8282
}
8383

8484

@@ -92,7 +92,7 @@ public PyLong(long value)
9292
publicPyLong(ulongvalue)
9393
{
9494
obj=Runtime.PyLong_FromUnsignedLongLong(value);
95-
Runtime.CheckExceptionOccurred();
95+
PythonException.ThrowIfIsNull(obj);
9696
}
9797

9898

@@ -105,7 +105,7 @@ public PyLong(ulong value)
105105
publicPyLong(shortvalue)
106106
{
107107
obj=Runtime.PyLong_FromLong(value);
108-
Runtime.CheckExceptionOccurred();
108+
PythonException.ThrowIfIsNull(obj);
109109
}
110110

111111

@@ -119,7 +119,7 @@ public PyLong(short value)
119119
publicPyLong(ushortvalue)
120120
{
121121
obj=Runtime.PyLong_FromLong(value);
122-
Runtime.CheckExceptionOccurred();
122+
PythonException.ThrowIfIsNull(obj);
123123
}
124124

125125

@@ -132,7 +132,7 @@ public PyLong(ushort value)
132132
publicPyLong(bytevalue)
133133
{
134134
obj=Runtime.PyLong_FromLong(value);
135-
Runtime.CheckExceptionOccurred();
135+
PythonException.ThrowIfIsNull(obj);
136136
}
137137

138138

@@ -146,7 +146,7 @@ public PyLong(byte value)
146146
publicPyLong(sbytevalue)
147147
{
148148
obj=Runtime.PyLong_FromLong(value);
149-
Runtime.CheckExceptionOccurred();
149+
PythonException.ThrowIfIsNull(obj);
150150
}
151151

152152

@@ -159,7 +159,7 @@ public PyLong(sbyte value)
159159
publicPyLong(doublevalue)
160160
{
161161
obj=Runtime.PyLong_FromDouble(value);
162-
Runtime.CheckExceptionOccurred();
162+
PythonException.ThrowIfIsNull(obj);
163163
}
164164

165165

@@ -172,7 +172,7 @@ public PyLong(double value)
172172
publicPyLong(stringvalue)
173173
{
174174
obj=Runtime.PyLong_FromString(value,IntPtr.Zero,0);
175-
Runtime.CheckExceptionOccurred();
175+
PythonException.ThrowIfIsNull(obj);
176176
}
177177

178178

@@ -199,7 +199,7 @@ public static bool IsLongType(PyObject value)
199199
publicstaticPyLongAsLong(PyObjectvalue)
200200
{
201201
IntPtrop=Runtime.PyNumber_Long(value.obj);
202-
Runtime.CheckExceptionOccurred();
202+
PythonException.ThrowIfIsNull(op);
203203
returnnewPyLong(op);
204204
}
205205

‎src/runtime/pyscope.cs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ internal PyScope(IntPtr ptr, PyScopeManager manager)
6666
obj=ptr;
6767
//Refcount of the variables not increase
6868
variables=Runtime.PyModule_GetDict(obj);
69-
Runtime.CheckExceptionOccurred();
69+
PythonException.ThrowIfIsNull(variables);
7070

71-
Runtime.PyDict_SetItemString(
71+
intres=Runtime.PyDict_SetItemString(
7272
variables,"__builtins__",
7373
Runtime.PyEval_GetBuiltins()
7474
);
75+
PythonException.ThrowIfIsNotZero(res);
7576
this.Name=this.Get<string>("__name__");
7677
}
7778

@@ -237,7 +238,7 @@ public PyObject Execute(PyObject script, PyDict locals = null)
237238
Check();
238239
IntPtr_locals=locals==null?variables:locals.obj;
239240
IntPtrptr=Runtime.PyEval_EvalCode(script.Handle,variables,_locals);
240-
Runtime.CheckExceptionOccurred();
241+
PythonException.ThrowIfIsNull(ptr);
241242
if(ptr==Runtime.PyNone)
242243
{
243244
Runtime.XDecref(ptr);
@@ -282,15 +283,8 @@ public PyObject Eval(string code, PyDict locals = null)
282283
NewReferencereference=Runtime.PyRun_String(
283284
code,flag,variables,_locals
284285
);
285-
try
286-
{
287-
Runtime.CheckExceptionOccurred();
288-
returnreference.MoveToPyObject();
289-
}
290-
finally
291-
{
292-
reference.Dispose();
293-
}
286+
PythonException.ThrowIfIsNull(reference);
287+
returnreference.MoveToPyObject();
294288
}
295289

296290
/// <summary>
@@ -327,19 +321,8 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
327321
NewReferencereference=Runtime.PyRun_String(
328322
code,flag,_globals,_locals
329323
);
330-
331-
try
332-
{
333-
Runtime.CheckExceptionOccurred();
334-
if(!reference.IsNone())
335-
{
336-
thrownewPythonException();
337-
}
338-
}
339-
finally
340-
{
341-
reference.Dispose();
342-
}
324+
PythonException.ThrowIfIsNull(reference);
325+
reference.Dispose();
343326
}
344327

345328
/// <summary>

‎src/runtime/pystring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public PyString(PyObject o)
5454
publicPyString(strings)
5555
{
5656
obj=Runtime.PyUnicode_FromUnicode(s,s.Length);
57-
Runtime.CheckExceptionOccurred();
57+
PythonException.ThrowIfIsNull(obj);
5858
}
5959

6060

‎src/runtime/pythonengine.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public static void EndAllowThreads(IntPtr ts)
465465
publicstaticPyObjectImportModule(stringname)
466466
{
467467
IntPtrop=Runtime.PyImport_ImportModule(name);
468-
Runtime.CheckExceptionOccurred();
468+
PythonException.ThrowIfIsNull(op);
469469
returnnewPyObject(op);
470470
}
471471

@@ -480,7 +480,7 @@ public static PyObject ImportModule(string name)
480480
publicstaticPyObjectReloadModule(PyObjectmodule)
481481
{
482482
IntPtrop=Runtime.PyImport_ReloadModule(module.Handle);
483-
Runtime.CheckExceptionOccurred();
483+
PythonException.ThrowIfIsNull(op);
484484
returnnewPyObject(op);
485485
}
486486

@@ -495,17 +495,17 @@ public static PyObject ReloadModule(PyObject module)
495495
publicstaticPyObjectModuleFromString(stringname,stringcode)
496496
{
497497
IntPtrc=Runtime.Py_CompileString(code,"none",(int)RunFlagType.File);
498-
Runtime.CheckExceptionOccurred();
498+
PythonException.ThrowIfIsNull(c);
499499
IntPtrm=Runtime.PyImport_ExecCodeModule(name,c);
500-
Runtime.CheckExceptionOccurred();
500+
PythonException.ThrowIfIsNull(m);
501501
returnnewPyObject(m);
502502
}
503503

504504
publicstaticPyObjectCompile(stringcode,stringfilename="",RunFlagTypemode=RunFlagType.File)
505505
{
506506
varflag=(int)mode;
507507
IntPtrptr=Runtime.Py_CompileString(code,filename,flag);
508-
Runtime.CheckExceptionOccurred();
508+
PythonException.ThrowIfIsNull(ptr);
509509
returnnewPyObject(ptr);
510510
}
511511

@@ -587,17 +587,8 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
587587
NewReferenceresult=Runtime.PyRun_String(
588588
code,(IntPtr)flag,globals.Value,locals.Value
589589
);
590-
591-
try
592-
{
593-
Runtime.CheckExceptionOccurred();
594-
595-
returnresult.MoveToPyObject();
596-
}
597-
finally
598-
{
599-
result.Dispose();
600-
}
590+
PythonException.ThrowIfIsNull(result);
591+
returnresult.MoveToPyObject();
601592
}
602593
finally
603594
{

‎src/runtime/pythonexception.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ public static void ThrowIfIsNull(IntPtr ob)
253253
}
254254
}
255255

256+
internalstaticvoidThrowIfIsNull(inNewReferencereference)
257+
{
258+
if(reference.IsNull())
259+
{
260+
thrownewPythonException();
261+
}
262+
}
263+
256264
publicstaticvoidThrowIfIsNotZero(intvalue)
257265
{
258266
if(value!=0)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp