Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/IronPython.StdLib
10 changes: 10 additions & 0 deletions src/core/IronPython/Runtime/Binding/PythonGetMemberBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public override T BindDelegate<T>(CallSite<T> site, object[] args) {
break;
case "__file__":
return (T)(object)new Func<CallSite, object, CodeContext, object>(new NamespaceTrackerDelegate(_name).GetFile);
case "__path__":
return (T)(object)new Func<CallSite, object, CodeContext, object>(new NamespaceTrackerDelegate(_name).GetPath);
case "__dict__":
return (T)(object)new Func<CallSite, object, CodeContext, object>(new NamespaceTrackerDelegate(_name).GetDict);
case "__name__":
Expand Down Expand Up @@ -622,6 +624,14 @@ public object GetFile(CallSite site, object self, CodeContext context) {
return Update(site, self, context);
}

public object GetPath(CallSite site, object self, CodeContext context) {
if (self != null && self.GetType() == typeof(NamespaceTracker)) {
return NamespaceTrackerOps.Get__path__((NamespaceTracker)self);
}

return Update(site, self, context);
}

public object GetDict(CallSite site, object self, CodeContext context) {
if (self != null && self.GetType() == typeof(NamespaceTracker)) {
return NamespaceTrackerOps.Get__dict__(context, (NamespaceTracker)self);
Expand Down
23 changes: 13 additions & 10 deletions src/core/IronPython/Runtime/Operations/NamespaceTrackerOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,18 @@ public static string __repr__(NamespaceTracker self) {

public static string __str__(NamespaceTracker self) {
if (self.PackageAssemblies.Count != 1) {
return string.Format("<module '{0}' (CLS module, {1} assemblies loaded)>", Get__name__(self.Name), self.PackageAssemblies.Count);
return string.Format("<module '{0}' (CLS module, {1} assemblies loaded)>", self.Name, self.PackageAssemblies.Count);
}
return string.Format("<module '{0}' (CLS module from {1})>", Get__name__(self.Name), self.PackageAssemblies[0].FullName);
return string.Format("<module '{0}' (CLS module from {1})>", self.Name, self.PackageAssemblies[0].FullName);
}

/// <summary>
/// Namespaces are packages for importlib, but their children are not found on the file system,
/// so there are no locations to search.
/// </summary>
[SpecialName, PropertyMethod]
public static PythonList Get__path__(NamespaceTracker self) => new PythonList();

[SpecialName, PropertyMethod]
public static PythonDictionary Get__dict__(CodeContext context, NamespaceTracker self) {
PythonDictionary res = new PythonDictionary();
Expand All @@ -57,16 +64,12 @@ public static PythonDictionary Get__dict__(CodeContext context, NamespaceTracker
return res;
}

/// <summary>
/// The full name of the namespace.
/// </summary>
[SpecialName, PropertyMethod]
public static string Get__name__(CodeContext context, NamespaceTracker self) {
return Get__name__(self.Name);
}

private static string Get__name__(string name) {
int lastDot = name.LastIndexOf('.');
if (lastDot == -1) return name;

return name.Substring(lastDot + 1);
return self.Name;
}

[SpecialName]
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/test_clrload.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,12 @@ def test_local_dll(self):
self.assertEqual(repr(x), "<Assembly c3, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null>")
self.assertEqual(repr(x.Foo), "<class 'Foo'>")
self.assertEqual(repr(x.BarNamespace), "<module 'BarNamespace' (CLS module from c3, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)>")
self.assertEqual(repr(x.BarNamespace.NestedNamespace), "<module 'NestedNamespace' (CLS module from c3, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)>")
self.assertEqual(repr(x.BarNamespace.NestedNamespace), "<module 'BarNamespace.NestedNamespace' (CLS module from c3, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)>")
self.assertEqual(repr(x.BarNamespace.Bar.NestedBar), "<class 'NestedBar'>")
self.assertEqual(x.__dict__["BarNamespace"], x.BarNamespace)
self.assertEqual(x.BarNamespace.__dict__["Bar"], x.BarNamespace.Bar)
self.assertEqual(x.BarNamespace.__dict__["NestedNamespace"], x.BarNamespace.NestedNamespace)
self.assertEqual(x.BarNamespace.NestedNamespace.__name__, "NestedNamespace")
self.assertEqual(x.BarNamespace.NestedNamespace.__name__, "BarNamespace.NestedNamespace")
self.assertEqual(x.BarNamespace.NestedNamespace.__file__, "c3, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")
self.assertRaises(AttributeError, lambda: x.BarNamespace.NestedNamespace.not_exist)
self.assertRaises(AttributeError, lambda: x.Foo2) # assembly c3 has no type Foo2
Expand Down
Loading