IronLanguages / main

Work for this repo has moved to https://github.com/IronLanguages/ironpython2
1.16k stars 350 forks source link

Module loses some methods and propreties when imported into another python script #1540

Closed gtalarico closed 7 years ago

gtalarico commented 7 years ago

I have been struggling with this for while, and haven't been able to find any information on it. I understand this might not be enough information for you to be able to help, so please feel free to close it.

I have no idea, if this is a IronPython bug, or something about imports in Python I am not aware of. I also can't provide a good repro case, since this assembly will not load outside the program environment (Autodesk Revit), so will provide a generic equivalent case below.

Any information/ideas on what could be causing this would be greatly appreciated.

File1.py

import clr
clr.AddReference('RevitAPI')          # Contains Autodesk.Revit.DB namespace
from Autodesk.Revit import DB
DB.Element.Name                         # OK.
DB.Element.Name.GetValue()        # OK: Method exists. As Expected

File2.py

from File1 import DB
DB.Element.Name                         # OK
DB.Element.Name.GetValue()        # **Method DOES NOT Exist**, get an Attribute Error is raised.
from Autodesk.Revit import DB    # Re-import the same namespace again
DB.Element.Name.GetValue()        # OK:  Method Exists 
slide commented 7 years ago

Should File2.py have "from File1 import DB"?

On Sun, Nov 13, 2016 at 5:30 PM Gui Talarico notifications@github.com wrote:

I have been struggling with this for while, and haven't been able to find any information on it. I understand this might not be enough information for you to be able to help, so please feel free to close it.

I have no idea, if this is a IronPython bug, or something about imports in Python I am not aware of. I also can't provide a good repro case, since this assembly will not load outside the program environment (Autodesk Revit), so will provide a generic equivalent case below.

Any information/ideas on what could be causing this would be greatly appreciated. File1.py

import clr clr.AddReference('RevitAPI') # Contains Autodesk.Revit.DB namespace from Autodesk.Revit import DBDB.Element.Name # OK. DB.Element.Name.GetValue() # OK: Method exists. As Expected

File2.py

from File2 import DBDB.Element.Name # OK DB.Element.Name.GetValue() # Method DOES NOT Exist, get an Attribute Error is raised. from Autodesk.Revit import DB # Re-import the same namespace again DB.Element.Name.GetValue() # OK: Method Exists

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/IronLanguages/main/issues/1540, or mute the thread https://github.com/notifications/unsubscribe-auth/AADaP4Pp1GAbgPF8TWqrlfFRyf-jbzksks5q96u3gaJpZM4Kw26Q .

gtalarico commented 7 years ago

Thanks slide, sorry, typo in example. I have revised the code sample.

PS: cross posted this on Stack Overflow, I will post back and close If I get a response. http://stackoverflow.com/questions/40580471/net-assembly-loses-some-methods-when-imported-from-one-python-file-into-another

andygraham commented 7 years ago

I don't know if this is related to the above or is a different feature concerning the mapping of .NET Object class to the Python object class.

I tried the same but using Form from System.Windows.Form and found four methods missing. Interestingly they seem to be four base methods of the .NET Object class. They are visible in the Form class in file1 but missing in the imported Form class in file2.

Equals GetHashCode GetType ReferenceEquals

Doing it with Int16 includes Equals and GetHashCode but loses

GetType MemberwiseClone ReferenceEquals

different but again base methods from Object.

Trying it with various .NET classes I get various numbers of different methods missing. There seems no consistency except they always seem to be the Object base methods.

gtalarico commented 7 years ago

@slide can you reproduce this as well?

slide commented 7 years ago

I can look into this next week.

andygraham commented 7 years ago

Here is the code I used to test it - might save you a few minutes!

Test.zip

produce it. Run file2.py

PMoureu commented 7 years ago

This may be linked to clr, and how it handles the references to the DLL used in each cases.

In each 2nd file, all references come back just by adding : import clr

Same results if you import another namespace from the assembly like from Autodesk.Revit.UI import TaskDialog or from System.Windows.Forms import Label

"Such context change is per-python-module" https://blogs.msdn.microsoft.com/haibo_luo/2007/09/

andygraham commented 7 years ago

That's interesting! I didn't think of trying that - though I probably should have :(. I would have expected all the .NET methods or none to be available without clr imported. I wonder why only a random few go missing!

slide commented 7 years ago

This issue was moved to IronLanguages/ironpython2#79