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

Commit451fae6

Browse files
koubaalostmsu
andauthored
Remove parameterlessPyObject constructor (#1226)
Co-authored-by: Victor <lost@losttech.software>
1 parentef2e6b4 commit451fae6

File tree

13 files changed

+216
-159
lines changed

13 files changed

+216
-159
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1414
-`clr.AddReference` may now throw errors besides`FileNotFoundException`, that provide more
1515
details about the cause of the failure
1616
-`clr.AddReference` no longer adds ".dll" implicitly
17+
-`PyIter(PyObject)` constructor replaced with static`PyIter.GetIter(PyObject)` method
1718

1819
###Fixed
1920

‎src/runtime/pyansistring.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public PyAnsiString(IntPtr ptr) : base(ptr)
1717
}
1818

1919

20+
privatestaticIntPtrFromObject(PyObjecto)
21+
{
22+
if(o==null||!IsStringType(o))
23+
{
24+
thrownewArgumentException("object is not a string");
25+
}
26+
Runtime.XIncref(o.obj);
27+
returno.obj;
28+
}
29+
2030
/// <summary>
2131
/// PyString Constructor
2232
/// </summary>
@@ -25,14 +35,14 @@ public PyAnsiString(IntPtr ptr) : base(ptr)
2535
/// An ArgumentException will be thrown if the given object is not
2636
/// a Python string object.
2737
/// </remarks>
28-
publicPyAnsiString(PyObjecto)
38+
publicPyAnsiString(PyObjecto):base(FromObject(o))
2939
{
30-
if(!IsStringType(o))
31-
{
32-
thrownewArgumentException("object is not a string");
33-
}
34-
Runtime.XIncref(o.obj);
35-
obj=o.obj;
40+
}
41+
privatestaticIntPtrFromString(strings)
42+
{
43+
IntPtrval=Runtime.PyString_FromString(s);
44+
PythonException.ThrowIfIsNull(val);
45+
returnval;
3646
}
3747

3848

@@ -42,10 +52,8 @@ public PyAnsiString(PyObject o)
4252
/// <remarks>
4353
/// Creates a Python string from a managed string.
4454
/// </remarks>
45-
publicPyAnsiString(strings)
55+
publicPyAnsiString(strings):base(FromString(s))
4656
{
47-
obj=Runtime.PyString_FromString(s);
48-
PythonException.ThrowIfIsNull(obj);
4957
}
5058

5159

‎src/runtime/pydict.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ public PyDict(IntPtr ptr) : base(ptr)
2929
/// <remarks>
3030
/// Creates a new Python dictionary object.
3131
/// </remarks>
32-
publicPyDict()
32+
publicPyDict():base(Runtime.PyDict_New())
3333
{
34-
obj=Runtime.PyDict_New();
3534
if(obj==IntPtr.Zero)
3635
{
3736
thrownewPythonException();
@@ -47,14 +46,13 @@ public PyDict()
4746
/// ArgumentException will be thrown if the given object is not a
4847
/// Python dictionary object.
4948
/// </remarks>
50-
publicPyDict(PyObjecto)
49+
publicPyDict(PyObjecto):base(o.obj)
5150
{
51+
Runtime.XIncref(o.obj);
5252
if(!IsDictType(o))
5353
{
5454
thrownewArgumentException("object is not a dict");
5555
}
56-
Runtime.XIncref(o.obj);
57-
obj=o.obj;
5856
}
5957

6058

‎src/runtime/pyfloat.cs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace Python.Runtime
44
{
55
/// <summary>
66
/// Represents a Python float object. See the documentation at
7-
/// PY2: https://docs.python.org/2/c-api/float.html
87
/// PY3: https://docs.python.org/3/c-api/float.html
98
/// for details.
109
/// </summary>
@@ -31,14 +30,8 @@ public PyFloat(IntPtr ptr) : base(ptr)
3130
/// ArgumentException will be thrown if the given object is not a
3231
/// Python float object.
3332
/// </remarks>
34-
publicPyFloat(PyObjecto)
33+
publicPyFloat(PyObjecto):base(FromObject(o))
3534
{
36-
if(!IsFloatType(o))
37-
{
38-
thrownewArgumentException("object is not a float");
39-
}
40-
Runtime.XIncref(o.obj);
41-
obj=o.obj;
4235
}
4336

4437

@@ -48,26 +41,45 @@ public PyFloat(PyObject o)
4841
/// <remarks>
4942
/// Creates a new Python float from a double value.
5043
/// </remarks>
51-
publicPyFloat(doublevalue)
44+
publicPyFloat(doublevalue):base(FromDouble(value))
45+
{
46+
}
47+
48+
privatestaticIntPtrFromObject(PyObjecto)
49+
{
50+
if(o==null||!IsFloatType(o))
51+
{
52+
thrownewArgumentException("object is not a float");
53+
}
54+
Runtime.XIncref(o.obj);
55+
returno.obj;
56+
}
57+
58+
privatestaticIntPtrFromDouble(doublevalue)
5259
{
53-
obj=Runtime.PyFloat_FromDouble(value);
54-
PythonException.ThrowIfIsNull(obj);
60+
IntPtrval=Runtime.PyFloat_FromDouble(value);
61+
PythonException.ThrowIfIsNull(val);
62+
returnval;
5563
}
5664

65+
privatestaticIntPtrFromString(stringvalue)
66+
{
67+
using(vars=newPyString(value))
68+
{
69+
IntPtrval=Runtime.PyFloat_FromString(s.obj,IntPtr.Zero);
70+
PythonException.ThrowIfIsNull(val);
71+
returnval;
72+
}
73+
}
5774

5875
/// <summary>
5976
/// PyFloat Constructor
6077
/// </summary>
6178
/// <remarks>
6279
/// Creates a new Python float from a string value.
6380
/// </remarks>
64-
publicPyFloat(stringvalue)
81+
publicPyFloat(stringvalue):base(FromString(value))
6582
{
66-
using(vars=newPyString(value))
67-
{
68-
obj=Runtime.PyFloat_FromString(s.obj,IntPtr.Zero);
69-
PythonException.ThrowIfIsNull(obj);
70-
}
7183
}
7284

7385

‎src/runtime/pyint.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,35 @@ public PyInt(IntPtr ptr) : base(ptr)
3131
/// ArgumentException will be thrown if the given object is not a
3232
/// Python int object.
3333
/// </remarks>
34-
publicPyInt(PyObjecto)
34+
publicPyInt(PyObjecto):base(FromObject(o))
3535
{
36-
if(!IsIntType(o))
36+
}
37+
38+
privatestaticIntPtrFromObject(PyObjecto)
39+
{
40+
if(o==null||!IsIntType(o))
3741
{
3842
thrownewArgumentException("object is not an int");
3943
}
4044
Runtime.XIncref(o.obj);
41-
obj=o.obj;
45+
returno.obj;
4246
}
4347

48+
privatestaticIntPtrFromInt(intvalue)
49+
{
50+
IntPtrval=Runtime.PyInt_FromInt32(value);
51+
PythonException.ThrowIfIsNull(val);
52+
returnval;
53+
}
4454

4555
/// <summary>
4656
/// PyInt Constructor
4757
/// </summary>
4858
/// <remarks>
4959
/// Creates a new Python int from an int32 value.
5060
/// </remarks>
51-
publicPyInt(intvalue)
61+
publicPyInt(intvalue):base(FromInt(value))
5262
{
53-
obj=Runtime.PyInt_FromInt32(value);
54-
PythonException.ThrowIfIsNull(obj);
5563
}
5664

5765

@@ -62,10 +70,8 @@ public PyInt(int value)
6270
/// Creates a new Python int from a uint32 value.
6371
/// </remarks>
6472
[CLSCompliant(false)]
65-
publicPyInt(uintvalue)
73+
publicPyInt(uintvalue):base(FromLong(value))
6674
{
67-
obj=Runtime.PyInt_FromInt64(value);
68-
PythonException.ThrowIfIsNull(obj);
6975
}
7076

7177

@@ -75,10 +81,15 @@ public PyInt(uint value)
7581
/// <remarks>
7682
/// Creates a new Python int from an int64 value.
7783
/// </remarks>
78-
publicPyInt(longvalue)
84+
publicPyInt(longvalue):base(FromLong(value))
7985
{
80-
obj=Runtime.PyInt_FromInt64(value);
81-
PythonException.ThrowIfIsNull(obj);
86+
}
87+
88+
privatestaticIntPtrFromLong(longvalue)
89+
{
90+
IntPtrval=Runtime.PyInt_FromInt64(value);
91+
PythonException.ThrowIfIsNull(val);
92+
returnval;
8293
}
8394

8495

@@ -89,10 +100,8 @@ public PyInt(long value)
89100
/// Creates a new Python int from a uint64 value.
90101
/// </remarks>
91102
[CLSCompliant(false)]
92-
publicPyInt(ulongvalue)
103+
publicPyInt(ulongvalue):base(FromLong((long)value))
93104
{
94-
obj=Runtime.PyInt_FromInt64((long)value);
95-
PythonException.ThrowIfIsNull(obj);
96105
}
97106

98107

@@ -142,16 +151,21 @@ public PyInt(sbyte value) : this((int)value)
142151
}
143152

144153

154+
privatestaticIntPtrFromString(stringvalue)
155+
{
156+
IntPtrval=Runtime.PyInt_FromString(value,IntPtr.Zero,0);
157+
PythonException.ThrowIfIsNull(val);
158+
returnval;
159+
}
160+
145161
/// <summary>
146162
/// PyInt Constructor
147163
/// </summary>
148164
/// <remarks>
149165
/// Creates a new Python int from a string value.
150166
/// </remarks>
151-
publicPyInt(stringvalue)
167+
publicPyInt(stringvalue):base(FromString(value))
152168
{
153-
obj=Runtime.PyInt_FromString(value,IntPtr.Zero,0);
154-
PythonException.ThrowIfIsNull(obj);
155169
}
156170

157171

‎src/runtime/pyiter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,22 @@ public PyIter(IntPtr ptr) : base(ptr)
2626
}
2727

2828
/// <summary>
29-
/// PyIterConstructor
29+
/// PyIterfactory function.
3030
/// </summary>
3131
/// <remarks>
32-
///Creates aPython iterator fromaniterable. Like doing "iter(iterable)" in python.
32+
///Create anew PyIter froma giveniterable. Like doing "iter(iterable)" in python.
3333
/// </remarks>
34-
publicPyIter(PyObjectiterable)
34+
/// <param name="iterable"></param>
35+
/// <returns></returns>
36+
publicstaticPyIterGetIter(PyObjectiterable)
3537
{
36-
obj=Runtime.PyObject_GetIter(iterable.obj);
37-
if(obj==IntPtr.Zero)
38+
if(iterable==null)
3839
{
39-
thrownewPythonException();
40+
thrownewArgumentNullException();
4041
}
42+
IntPtrval=Runtime.PyObject_GetIter(iterable.obj);
43+
PythonException.ThrowIfIsNull(val);
44+
returnnewPyIter(val);
4145
}
4246

4347
protectedoverridevoidDispose(booldisposing)

‎src/runtime/pylist.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public PyList(IntPtr ptr) : base(ptr)
2828
internalPyList(BorrowedReferencereference):base(reference){}
2929

3030

31+
privatestaticIntPtrFromObject(PyObjecto)
32+
{
33+
if(o==null||!IsListType(o))
34+
{
35+
thrownewArgumentException("object is not a list");
36+
}
37+
Runtime.XIncref(o.obj);
38+
returno.obj;
39+
}
40+
3141
/// <summary>
3242
/// PyList Constructor
3343
/// </summary>
@@ -36,14 +46,8 @@ internal PyList(BorrowedReference reference) : base(reference) { }
3646
/// ArgumentException will be thrown if the given object is not a
3747
/// Python list object.
3848
/// </remarks>
39-
publicPyList(PyObjecto)
49+
publicPyList(PyObjecto):base(FromObject(o))
4050
{
41-
if(!IsListType(o))
42-
{
43-
thrownewArgumentException("object is not a list");
44-
}
45-
Runtime.XIncref(o.obj);
46-
obj=o.obj;
4751
}
4852

4953

@@ -53,36 +57,40 @@ public PyList(PyObject o)
5357
/// <remarks>
5458
/// Creates a new empty Python list object.
5559
/// </remarks>
56-
publicPyList()
60+
publicPyList():base(Runtime.PyList_New(0))
5761
{
58-
obj=Runtime.PyList_New(0);
5962
if(obj==IntPtr.Zero)
6063
{
6164
thrownewPythonException();
6265
}
6366
}
6467

65-
66-
/// <summary>
67-
/// PyList Constructor
68-
/// </summary>
69-
/// <remarks>
70-
/// Creates a new Python list object from an array of PyObjects.
71-
/// </remarks>
72-
publicPyList(PyObject[]items)
68+
privatestaticIntPtrFromArray(PyObject[]items)
7369
{
7470
intcount=items.Length;
75-
obj=Runtime.PyList_New(count);
71+
IntPtrval=Runtime.PyList_New(count);
7672
for(vari=0;i<count;i++)
7773
{
7874
IntPtrptr=items[i].obj;
7975
Runtime.XIncref(ptr);
80-
intr=Runtime.PyList_SetItem(obj,i,ptr);
76+
intr=Runtime.PyList_SetItem(val,i,ptr);
8177
if(r<0)
8278
{
79+
Runtime.Py_DecRef(val);
8380
thrownewPythonException();
8481
}
8582
}
83+
returnval;
84+
}
85+
86+
/// <summary>
87+
/// PyList Constructor
88+
/// </summary>
89+
/// <remarks>
90+
/// Creates a new Python list object from an array of PyObjects.
91+
/// </remarks>
92+
publicPyList(PyObject[]items):base(FromArray(items))
93+
{
8694
}
8795

8896

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp