ironpython3 icon indicating copy to clipboard operation
ironpython3 copied to clipboard

Global __class__ statement in class declaration leads to InvalidCastException

Open xiaxinmeng opened this issue 4 years ago • 1 comments
trafficstars

See the following code. __class__ is in class declaration which lead to an Unhandled Exception- InvalidCastException.

class A:
    global  __class__
    def a(self):
        super()
A()

Environmental:

IronPython 3.4.0a1 (3.4.0.0001) [.NETFramework,Version=v4.6 on Mono 6.12.0.122 (tarball Mon Feb 22 17:28:32 UTC 2021) (64-bit)] on linux

(base) xxm@xxm-System-Product-Name:~$ ipy test.py

Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
at IronPython.Compiler.Ast.ClassDefinition.MakeClassBody () [0x00082] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at IronPython.Compiler.Ast.ClassDefinition.GetLambda () [0x00008] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at IronPython.Runtime.FunctionCode.get_Code () [0x00000] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at IronPython.Runtime.FunctionCode.GetGeneratorOrNormalLambda () [0x0000b] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at IronPython.Runtime.FunctionCode.UpdateDelegate (IronPython.Runtime.PythonContext context, System.Boolean forceCreation) [0x000d8] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at IronPython.Compiler.Ast.ClassDefinition+<>c__DisplayClass64_0.<Reduce>b__0 (System.Object x) [0x00011] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context (System.Object state) [0x00007] in <533173d24dae460899d2b10975534bb0>:0 
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <533173d24dae460899d2b10975534bb0>:0 
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <533173d24dae460899d2b10975534bb0>:0 
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00021] in <533173d24dae460899d2b10975534bb0>:0 
at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in <533173d24dae460899d2b10975534bb0>:0 
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in <533173d24dae460899d2b10975534bb0>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Specified cast is not valid.
at IronPython.Compiler.Ast.ClassDefinition.MakeClassBody () [0x00082] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at IronPython.Compiler.Ast.ClassDefinition.GetLambda () [0x00008] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at IronPython.Runtime.FunctionCode.get_Code () [0x00000] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at IronPython.Runtime.FunctionCode.GetGeneratorOrNormalLambda () [0x0000b] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at IronPython.Runtime.FunctionCode.UpdateDelegate (IronPython.Runtime.PythonContext context, System.Boolean forceCreation) [0x000d8] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at IronPython.Compiler.Ast.ClassDefinition+<>c__DisplayClass64_0.<Reduce>b__0 (System.Object x) [0x00011] in <5cd2739cd27844619789a1aa1ffdb0b0>:0 
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context (System.Object state) [0x00007] in <533173d24dae460899d2b10975534bb0>:0 
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <533173d24dae460899d2b10975534bb0>:0 
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <533173d24dae460899d2b10975534bb0>:0 
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00021] in <533173d24dae460899d2b10975534bb0>:0 
at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in <533173d24dae460899d2b10975534bb0>:0 
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in <533173d24dae460899d2b10975534bb0>:0 

xiaxinmeng avatar Jul 26 '21 07:07 xiaxinmeng

Also fails an assertion check in ScopeStatement.AddFreeVariable.

slozier avatar Jul 27 '21 02:07 slozier

@BCSharp This seems to have been resolved by your recent PRs (https://github.com/IronLanguages/ironpython3/pull/1531). Do you remember if there's a test covering something like this?

slozier avatar Aug 19 '22 00:08 slozier

I am aware it has been resolved — sort of… There is no crash during compilation anymore, but the semantics/behaviour of this construct is different than CPython's. This is covered by test_various__class__pathologies in test_super.py from StdLib, currently disabled though. I will address this issue separately, after #1535, since it is a more complicated story.

BCSharp avatar Aug 19 '22 18:08 BCSharp