Repository navigation
Closed
Labels
bugSomething isn't working
Description
repro: https://github.com/rebcabin/lpython/tree/brian-lasr/lasr/LP-pycharm/Issue1981
This is related to Issue #1978; i'm digging in to find out what's wrong. I reduced the test case to the bare minimum that illustrates the problem.
The problem is that when I initialize a StringIO object from inside a function, an attribute value gets bogusly changed to a random number.
Consider the following code in Issue1981/lasr_lexer.py
from lpython import (dataclass, InOut, i32,)
from lasr_stringio import (StringIO, __post_init__,)
NEWLINE : str = "\n"
WHITESPACE : list[str] = [" ", ",", NEWLINE, "\r", "\t"]
EOF_SIGNAL : str = ''
@dataclass
class LasrLexer:
fd : StringIO = StringIO('', 0, 0) # prefer Optional[StringIO]
line : i32 = 1 # prefer Line
col : i32 = 1 # prefer Col
type : str = '' # prefer LTType
val : str = '' # prefer LTVal
def lexer_test():
print('LEXER TEST')
lexer : LasrLexer = LasrLexer(StringIO(' foo bar '))
__post_init__(lexer.fd)
if __name__ == '__main__':
lexer_test()When I run it with LPython, I get
/Users/brian/CLionProjects/lpython/src/bin/python /Users/brian/CLionProjects/lpython/lasr/LP-pycharm/Issue1981/lasr_lexer.py
LEXER TEST
StringIO __post_init__ preconditions:
_buf = foo bar .
_len = 1.
_0cursor = 1830662080. ################ ATTENTION #############
StringIO __post_init__ postconditions:
_buf = foo bar .
_len = 12.
_0cursor = 1830662080.When I change the code to the following, just moving the initialization inside main and out of the function lexer_test, I get the correct value for _0cursor:
from lpython import (dataclass, InOut, i32,)
from lasr_stringio import (StringIO, __post_init__,)
NEWLINE : str = "\n"
WHITESPACE : list[str] = [" ", ",", NEWLINE, "\r", "\t"]
EOF_SIGNAL : str = ''
@dataclass
class LasrLexer:
fd : StringIO = StringIO('', 0, 0) # prefer Optional[StringIO]
line : i32 = 1 # prefer Line
col : i32 = 1 # prefer Col
type : str = '' # prefer LTType
val : str = '' # prefer LTVal
if __name__ == '__main__':
print('LEXER TEST')
lexer : LasrLexer = LasrLexer(StringIO(' foo bar '))
__post_init__(lexer.fd)produces:
/Users/brian/CLionProjects/lpython/src/bin/python /Users/brian/CLionProjects/lpython/lasr/LP-pycharm/Issue1981/lasr_lexer.py
LEXER TEST
StringIO __post_init__ preconditions:
_buf = foo bar .
_len = 0.
_0cursor = 0. ################ ATTENTION ###############
StringIO __post_init__ postconditions:
_buf = foo bar .
_len = 12.
_0cursor = 0.Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Activity
rebcabin commentedon Jun 21, 2023
This could be related to a recent fix that allowed constructors to be called with partial argument lists. Investigating ...
Smit-create commentedon Jun 21, 2023
Using this will fix.
rebcabin commentedon Jun 21, 2023
ok, that's fair, but shouldn't
i32(0)be allowed as an initial value?rebcabin commentedon Jun 21, 2023
Also, this fixes it too:
instead of
probably for the same reason as your fix works.
rebcabin commentedon Jun 21, 2023
This also fixes it:
So I think this bug still stands. The following ought to work:
rebcabin commentedon Jun 21, 2023
and the following ought to work, too:
rebcabin commentedon Jun 21, 2023
The fact that the original code fails with a large random number that looks like a 64-bit address is evidence of a pointer calculation going wrong in the C++ code :)
rebcabin commentedon Jun 21, 2023
I'm leaving the repro case untouched for investigation purposes.
Thirumalai-Shaktivel commentedon Jun 21, 2023
Yup, it can also mean that the value 0 in
_0cursor : i32 = i32(0)is not getting initialised!rebcabin commentedon Jun 21, 2023
you guys are great! this is fun!
Thirumalai-Shaktivel commentedon Jun 21, 2023
I see, anything apart from constants is skipped in the StructType definition:
So the struct member seems to be uninitialized here.
Thirumalai-Shaktivel commentedon Jun 21, 2023
lpython/src/lpython/semantics/python_ast_to_asr.cpp
Line 1332 in bcd882a
This becomes nullptr for i32(0)
Smit-create commentedon Jun 21, 2023
Struct members for initialization at present need compile-time constant values. And so that's the reason for failure.
Smit-create commentedon Jun 21, 2023
It should, I'll look into that for sure.
Smit-create commentedon Jun 21, 2023
Okay, the bug here is that
i32(0)is a Call AST node that is skipped while visiting the Symbol table, and so, the value for it is kept asnullptr.Smit-create commentedon Jun 21, 2023
One fix is to make the Classdef node revisit again in body-visitor to capture this.
certik commentedon Jun 21, 2023
I think that expressions have to be visited in the symbol table visitor, just like we do in LFortran. Because as part of types as well as initializations you need to visit them.
Smit-create commentedon Jun 30, 2023
Fixed in #2056