Skip to content

Commit

Permalink
fix(py): str(obj) falls back to use repr
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed May 9, 2024
1 parent 8d963ec commit 62873d5
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/pylib/pystring/strimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ type

type StringLike* = string | char | PyStr

func str*(`object` = ""): PyStr =
PyStr(`object`)

func str*(self: PyStr): PyStr = self ## copy
func str*(`object` = ""): PyStr = PyStr(`object`)
func str*(a: Rune): PyStr = str $a
func str*(c: char): PyStr = str $c

template str*[O: object](o: O): PyStr =
'<' & $O & " object at " & $o.addr & '>'
template str*[T](a: T): PyStr =
## convert any object based on `repr`
##
## This is alreadly a fallback,
## used for types without `str` defined.
##
## In Python, if a object's type has no `__str__`,
## then it falls to use `__repr__`
mixin repr
str repr a

Expand Down

0 comments on commit 62873d5

Please sign in to comment.