- Notifications
You must be signed in to change notification settings - Fork750
Mixins for standard collections that implementcollections.abc
interfaces#1543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
5cb300a
added a few mixins to reflected .NET collection types, that implement…
lostmsucd044c8
fixed MappingMixin implementation (untested)
lostmsu85897e3
fixed implementation of mixins for Mapping and MutableMapping (still …
lostmsu38ac73b
fixed mixin calls to TryGetValue
lostmsub77b7ce
added a few tests for collection mixins
lostmsua38849e
mentioned collection mixins in changelog
lostmsu6b20409
refactored LoadExtraModules for Mixins into LoadSubmodule + LoadMixins
lostmsu197689e
added a workaround for tp_clear implementations, that do not check, t…
lostmsu6ae373c
merge latest master
lostmsuFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletionsCHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletionssrc/runtime/InteropConfiguration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletionssrc/runtime/Mixins/CollectionMixinsProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
namespace Python.Runtime.Mixins | ||
{ | ||
class CollectionMixinsProvider : IPythonBaseTypeProvider | ||
{ | ||
readonly Lazy<PyObject> mixinsModule; | ||
public CollectionMixinsProvider(Lazy<PyObject> mixinsModule) | ||
{ | ||
this.mixinsModule = mixinsModule ?? throw new ArgumentNullException(nameof(mixinsModule)); | ||
} | ||
public PyObject Mixins => this.mixinsModule.Value; | ||
public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases) | ||
{ | ||
if (type is null) | ||
throw new ArgumentNullException(nameof(type)); | ||
if (existingBases is null) | ||
throw new ArgumentNullException(nameof(existingBases)); | ||
var interfaces = NewInterfaces(type).Select(GetDefinition).ToArray(); | ||
var newBases = new List<PyType>(); | ||
newBases.AddRange(existingBases); | ||
// dictionaries | ||
if (interfaces.Contains(typeof(IDictionary<,>))) | ||
{ | ||
newBases.Add(new PyType(this.Mixins.GetAttr("MutableMappingMixin"))); | ||
} | ||
else if (interfaces.Contains(typeof(IReadOnlyDictionary<,>))) | ||
{ | ||
newBases.Add(new PyType(this.Mixins.GetAttr("MappingMixin"))); | ||
} | ||
// item collections | ||
if (interfaces.Contains(typeof(IList<>)) | ||
|| interfaces.Contains(typeof(System.Collections.IList))) | ||
{ | ||
newBases.Add(new PyType(this.Mixins.GetAttr("MutableSequenceMixin"))); | ||
} | ||
else if (interfaces.Contains(typeof(IReadOnlyList<>))) | ||
{ | ||
newBases.Add(new PyType(this.Mixins.GetAttr("SequenceMixin"))); | ||
} | ||
else if (interfaces.Contains(typeof(ICollection<>)) | ||
|| interfaces.Contains(typeof(System.Collections.ICollection))) | ||
{ | ||
newBases.Add(new PyType(this.Mixins.GetAttr("CollectionMixin"))); | ||
} | ||
else if (interfaces.Contains(typeof(System.Collections.IEnumerable))) | ||
{ | ||
newBases.Add(new PyType(this.Mixins.GetAttr("IterableMixin"))); | ||
} | ||
// enumerators | ||
if (interfaces.Contains(typeof(System.Collections.IEnumerator))) | ||
{ | ||
newBases.Add(new PyType(this.Mixins.GetAttr("IteratorMixin"))); | ||
} | ||
if (newBases.Count == existingBases.Count) | ||
{ | ||
return existingBases; | ||
} | ||
if (type.IsInterface && type.BaseType is null) | ||
{ | ||
newBases.RemoveAll(@base => @base.Handle == Runtime.PyBaseObjectType); | ||
} | ||
return newBases; | ||
} | ||
static Type[] NewInterfaces(Type type) | ||
{ | ||
var result = type.GetInterfaces(); | ||
return type.BaseType != null | ||
? result.Except(type.BaseType.GetInterfaces()).ToArray() | ||
: result; | ||
} | ||
static Type GetDefinition(Type type) | ||
=> type.IsGenericType ? type.GetGenericTypeDefinition() : type; | ||
} | ||
} |
82 changes: 82 additions & 0 deletionssrc/runtime/Mixins/collections.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
""" | ||
Implements collections.abc for common .NET types | ||
https://docs.python.org/3.6/library/collections.abc.html | ||
""" | ||
import collections.abc as col | ||
class IteratorMixin(col.Iterator): | ||
def close(self): | ||
self.Dispose() | ||
class IterableMixin(col.Iterable): | ||
pass | ||
class SizedMixin(col.Sized): | ||
def __len__(self): return self.Count | ||
class ContainerMixin(col.Container): | ||
def __contains__(self, item): return self.Contains(item) | ||
try: | ||
abc_Collection = col.Collection | ||
except AttributeError: | ||
# Python 3.5- does not have collections.abc.Collection | ||
abc_Collection = col.Container | ||
class CollectionMixin(SizedMixin, IterableMixin, ContainerMixin, abc_Collection): | ||
pass | ||
class SequenceMixin(CollectionMixin, col.Sequence): | ||
pass | ||
class MutableSequenceMixin(SequenceMixin, col.MutableSequence): | ||
pass | ||
class MappingMixin(CollectionMixin, col.Mapping): | ||
def __contains__(self, item): return self.ContainsKey(item) | ||
def keys(self): return self.Keys | ||
def items(self): return [(k,self.get(k)) for k in self.Keys] | ||
def values(self): return self.Values | ||
def __iter__(self): return self.Keys.__iter__() | ||
def get(self, key, default=None): | ||
existed, item = self.TryGetValue(key, None) | ||
return item if existed else default | ||
class MutableMappingMixin(MappingMixin, col.MutableMapping): | ||
_UNSET_ = object() | ||
def __delitem__(self, key): | ||
self.Remove(key) | ||
def clear(self): | ||
self.Clear() | ||
def pop(self, key, default=_UNSET_): | ||
existed, item = self.TryGetValue(key, None) | ||
if existed: | ||
self.Remove(key) | ||
return item | ||
elif default == self._UNSET_: | ||
raise KeyError(key) | ||
else: | ||
return default | ||
def setdefault(self, key, value=None): | ||
existed, item = self.TryGetValue(key, None) | ||
if existed: | ||
return item | ||
else: | ||
self[key] = value | ||
return value | ||
def update(self, items, **kwargs): | ||
if isinstance(items, col.Mapping): | ||
for key, value in items.items(): | ||
self[key] = value | ||
else: | ||
for key, value in items: | ||
self[key] = value | ||
for key, value in kwargs.items(): | ||
self[key] = value |
2 changes: 1 addition & 1 deletionsrc/runtime/Python.Runtime.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletionssrc/runtime/Util.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
28 changes: 25 additions & 3 deletionssrc/runtime/classbase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionsrc/runtime/clrobject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletionsrc/runtime/managedtype.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionsrc/runtime/pyscope.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
18 changes: 14 additions & 4 deletionssrc/runtime/pythonengine.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletionssrc/runtime/pytype.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletionsrc/runtime/runtime.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletionstests/test_collection_mixins.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import System.Collections.Generic as C | ||
def test_contains(): | ||
l = C.List[int]() | ||
l.Add(42) | ||
assert 42 in l | ||
assert 43 not in l | ||
def test_dict_items(): | ||
d = C.Dictionary[int, str]() | ||
d[42] = "a" | ||
items = d.items() | ||
assert len(items) == 1 | ||
k,v = items[0] | ||
assert k == 42 | ||
assert v == "a" |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.