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

Commit42697ba

Browse files
authored
Merge branch 'master' into list-codec
2 parentscde2eca +50d947f commit42697ba

33 files changed

+460
-188
lines changed

‎AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- Christoph Gohlke ([@cgohlke](https://github.com/cgohlke))
2929
- Christopher Bremner ([@chrisjbremner](https://github.com/chrisjbremner))
3030
- Christopher Pow ([@christopherpow](https://github.com/christopherpow))
31+
- Daniel Abrahamsson ([@danabr](https://github.com/danabr))
3132
- Daniel Fernandez ([@fdanny](https://github.com/fdanny))
3233
- Daniel Santana ([@dgsantana](https://github.com/dgsantana))
3334
- Dave Hirschfeld ([@dhirschfeld](https://github.com/dhirschfeld))

‎CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ 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
18+
- Return values from .NET methods that return an interface are now automatically
19+
wrapped in that interface. This is a breaking change for users that rely on being
20+
able to access members that are part of the implementation class, but not the
21+
interface. Use the new__implementation__ or__raw_implementation__ properties to
22+
if you need to "downcast" to the implementation class.
1723

1824
###Fixed
1925

2026
- Fix incorrect dereference of wrapper object in`tp_repr`, which may result in a program crash
2127
- Fix incorrect dereference in params array handling
2228
- Fix`object[]` parameters taking precedence when should not in overload resolution
29+
- Fixed a bug where all .NET class instances were considered Iterable
30+
- Fix incorrect choice of method to invoke when using keyword arguments.
2331

2432
##[2.5.0][] - 2020-06-14
2533

‎src/embed_tests/TestFinalizer.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ public void CollectBasicObject()
8787
Assert.GreaterOrEqual(objectCount,1);
8888
}
8989

90+
[Test]
91+
publicvoidCollectOnShutdown()
92+
{
93+
MakeAGarbage(outvarshortWeak,outvarlongWeak);
94+
FullGCCollect();
95+
vargarbage=Finalizer.Instance.GetCollectedObjects();
96+
Assert.IsNotEmpty(garbage);
97+
PythonEngine.Shutdown();
98+
garbage=Finalizer.Instance.GetCollectedObjects();
99+
Assert.IsEmpty(garbage);
100+
}
101+
90102
privatestaticvoidMakeAGarbage(outWeakReferenceshortWeak,outWeakReferencelongWeak)
91103
{
92104
PyLongobj=newPyLong(1024);

‎src/perf_tests/Python.PerformanceTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</ItemGroup>
2929

3030
<TargetName="GetRuntimeLibBuildOutput"BeforeTargets="Build">
31-
<MSBuildProjects="..\runtime\Python.Runtime.15.csproj"Properties="PYTHONNET_PY3_VERSION=PYTHON35;Configuration=$(Configuration);TargetFramework=net40;Python3Version=PYTHON35;OutputPath=bin\for_perf\">
31+
<MSBuildProjects="..\runtime\Python.Runtime.15.csproj"Properties="PYTHONNET_PY3_VERSION=PYTHON38;Configuration=$(Configuration);TargetFramework=net40;Python3Version=PYTHON38;OutputPath=bin\for_perf\">
3232
<OutputTaskParameter="TargetOutputs"ItemName="NewPythonRuntime" />
3333
</MSBuild>
3434
</Target>

‎src/runtime/arrayobject.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
4343
publicstaticIntPtrmp_subscript(IntPtrob,IntPtridx)
4444
{
4545
varobj=(CLRObject)GetManagedObject(ob);
46+
vararrObj=(ArrayObject)GetManagedObjectType(ob);
4647
varitems=obj.instasArray;
47-
TypeitemType=obj.inst.GetType().GetElementType();
48+
TypeitemType=arrObj.type.GetElementType();
4849
intrank=items.Rank;
4950
intindex;
5051
objectvalue;

‎src/runtime/constructorbinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info)
8989
// any extra args are intended for the subclass' __init__.
9090

9191
IntPtreargs=Runtime.PyTuple_New(0);
92-
binding=Bind(inst,eargs,kw);
92+
binding=Bind(inst,eargs,IntPtr.Zero);
9393
Runtime.XDecref(eargs);
9494

9595
if(binding==null)

‎src/runtime/converter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,21 @@ internal static IntPtr ToPython(object value, Type type)
173173
}
174174
}
175175

176+
if(type.IsInterface)
177+
{
178+
varifaceObj=(InterfaceObject)ClassManager.GetClass(type);
179+
returnifaceObj.WrapObject(value);
180+
}
181+
182+
// We need to special case interface array handling to ensure we
183+
// produce the correct type. Value may be an array of some concrete
184+
// type (FooImpl[]), but we want access to go via the interface type
185+
// (IFoo[]).
186+
if(type.IsArray&&type.GetElementType().IsInterface)
187+
{
188+
returnCLRObject.GetInstHandle(value,type);
189+
}
190+
176191
// it the type is a python subclass of a managed type then return the
177192
// underlying python object rather than construct a new wrapper object.
178193
varpyderived=valueasIPythonDerivedType;

‎src/runtime/interfaceobject.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,43 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
7171
returnIntPtr.Zero;
7272
}
7373

74-
returnCLRObject.GetInstHandle(obj,self.pyHandle);
74+
returnself.WrapObject(obj);
75+
}
76+
77+
/// <summary>
78+
/// Wrap the given object in an interface object, so that only methods
79+
/// of the interface are available.
80+
/// </summary>
81+
publicIntPtrWrapObject(objectimpl)
82+
{
83+
varobjPtr=CLRObject.GetInstHandle(impl,pyHandle);
84+
returnobjPtr;
85+
}
86+
87+
/// <summary>
88+
/// Expose the wrapped implementation through attributes in both
89+
/// converted/encoded (__implementation__) and raw (__raw_implementation__) form.
90+
/// </summary>
91+
publicstaticIntPtrtp_getattro(IntPtrob,IntPtrkey)
92+
{
93+
varclrObj=(CLRObject)GetManagedObject(ob);
94+
95+
if(!Runtime.PyString_Check(key))
96+
{
97+
returnExceptions.RaiseTypeError("string expected");
98+
}
99+
100+
stringname=Runtime.GetManagedString(key);
101+
if(name=="__implementation__")
102+
{
103+
returnConverter.ToPython(clrObj.inst);
104+
}
105+
elseif(name=="__raw_implementation__")
106+
{
107+
returnCLRObject.GetInstHandle(clrObj.inst);
108+
}
109+
110+
returnRuntime.PyObject_GenericGetAttr(ob,key);
75111
}
76112
}
77113
}

‎src/runtime/managedtype.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ internal static ManagedType GetManagedObject(IntPtr ob)
4545
returnnull;
4646
}
4747

48+
/// <summary>
49+
/// Given a Python object, return the associated managed object type or null.
50+
/// </summary>
51+
internalstaticManagedTypeGetManagedObjectType(IntPtrob)
52+
{
53+
if(ob!=IntPtr.Zero)
54+
{
55+
IntPtrtp=Runtime.PyObject_TYPE(ob);
56+
varflags=Util.ReadCLong(tp,TypeOffset.tp_flags);
57+
if((flags&TypeFlags.Managed)!=0)
58+
{
59+
tp=Marshal.ReadIntPtr(tp,TypeOffset.magic());
60+
vargc=(GCHandle)tp;
61+
return(ManagedType)gc.Target;
62+
}
63+
}
64+
returnnull;
65+
}
66+
4867

4968
internalstaticManagedTypeGetManagedObjectErr(IntPtrob)
5069
{

‎src/runtime/methodbinder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa
580580
varmatch=false;
581581
paramsArray=parameters.Length>0?Attribute.IsDefined(parameters[parameters.Length-1],typeof(ParamArrayAttribute)):false;
582582

583-
if(positionalArgumentCount==parameters.Length)
583+
if(positionalArgumentCount==parameters.Length&&kwargDict.Count==0)
584584
{
585585
match=true;
586586
}
@@ -744,7 +744,7 @@ internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase i
744744
Typept=pi[i].ParameterType;
745745
if(pi[i].IsOut||pt.IsByRef)
746746
{
747-
v=Converter.ToPython(binding.args[i],pt);
747+
v=Converter.ToPython(binding.args[i],pt.GetElementType());
748748
Runtime.PyTuple_SetItem(t,n,v);
749749
n++;
750750
}

‎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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp