Skip to content

int(...) string parsing fails for short prefixed strings and underscore inputs #785

@BI71317

Description

@BI71317

Summary

This issue is about runtime 'string-to-int' path in builtin.codon, int._from_str.

Not the earlier parser-level integer literal issue from #783 .

Two problems were found in stdlib/internal/builtin.codon:

  • short prefixed (especially, with length 3) strings such as 0x0, 0b1, or 0o6 fail
  • numeric strings containing underscores such as 1_101 or 0b1_101 fail

Problem 1: short prefixed strings

Inputs like below should work:

int("0b0", 0)
int("0x1", 0)
int("0o6", 0)

But Instead, they shows below error.

ValueError: invalid literal for int() with base 0: '0b0'

Raised from: int._from_str:0.parse_error.0:0
/home/swchoi/src/codon/install/lib/codon/stdlib/internal/builtin.codon:380:13
Aborted (core dumped)

Problem 2: underscores in numeric strings

Inputs like below should work:

print("w08 python gap string underscores binary")
print(int("1_101", 2))
print(int("0b1_101", 0))

And they shows

w08 python gap string underscores binary
ValueError: invalid literal for int() with base 2: '1_101'

Raised from: int._from_str:0.parse_error.0:0
/home/swchoi/src/codon/install/lib/codon/stdlib/internal/builtin.codon:380:13

Backtrace:
  [0x75cd18ad1738] int._from_str:0.parse_error.0:0[str,int].1342 at /home/swchoi/src/codon/install/lib/codon/stdlib/internal/builtin.codon:380:13
  [0x75cd18ad24d3] int._from_str:0[str,int].1390 at /home/swchoi/src/codon/install/lib/codon/stdlib/internal/builtin.codon:457:29
  [0x75cd18ad250f] int.__new__:1[str,int].1393 at /home/swchoi/src/codon/install/lib/codon/stdlib/internal/types/int.codon:14:33
  [0x75cd18ad5090] main.0 at /home/swchoi/src/test_code/codon_counter_exp/w08_python_gap_string_underscores_binary.codon:2:20
Aborted (core dumped)

This issue is seperate from #783 I think.

Since this issue is not octal-only, and affects every prefixed numeric strings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions