Thanks to visit codestin.com
Credit goes to github.com

Skip to content

LPython bogusly changes attribute value #1981

Closed
@rebcabin

Description

@rebcabin

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

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions