beeware / voc

A transpiler that converts Python code into Java bytecode
http://beeware.org/voc
BSD 3-Clause "New" or "Revised" License
869 stars 518 forks source link

str.repr should use double quotes when value needs escaping #568

Closed eliasdorneles closed 6 years ago

eliasdorneles commented 7 years ago

This is what CPython does:

>>> s = "hello"
>>> repr(s)
"'hello'"
>>> s = "hello'"
>>> repr(s)
'"hello\'"'

Code compiled with VOC is currently working like this:

>>> s = "hello"
>>> repr(s)  # this works as expected ...
'hello'
>>> s = "hello'"
>>> repr(s)  # ... but this results in broken representation
'hello''

It would be nice to have Str.__repr__ handling these cases, this will make testing easier.

cflee commented 7 years ago

Actually, should this issue be generalised to include escaping within the quotes, or should it be a separate issue? There is a partial fix that handles edge cases for representing/escaping various non-printable characters (but not the outer quotes) in newly-closed #465, and I'm wondering where to track that.

CPython ref: unicodeobject.c

seocam commented 7 years ago

Well the first issue (created by @eliasdorneles) would be fixed by #578 but still there things to be done to get this closer to the CPython ref.

starlord1311 commented 6 years ago

@eliasdorneles can i take up this issue?

jonkiparsky commented 6 years ago

This seems to be up for grabs, so I'm going to see what I can do with it. If I don't make traction, I will come back and make a note to that effect. @eliasdorneles tagging you in case you want to remove the "Up for grabs" flag. (not sure what your preferred procedure is)

eliasdorneles commented 6 years ago

@jonkiparsky feel free to have a look around! However, I was just checking and the specific case I reported seems that it was already implemented... Did you find anything?

jonkiparsky commented 6 years ago

@eliasdorneles

Yep, looks good from here.

(env) [jpk@Jons-MacBook-Pro-2/~/code/voc-dev/jpk_test: Sun May 20 15:45]:547 $ java -classpath ../voc/dist/python-java-support.jar:. python.quote_test
'hello'
hello'
'hello
"'hello"'
(env) [jpk@Jons-MacBook-Pro-2/~/code/voc-dev/jpk_test: Sun May 20 15:45]:548 $ python3 quote_test.py 
'hello'
hello'
'hello
"'hello"'
(env) [jpk@Jons-MacBook-Pro-2/~/code/voc-dev/jpk_test: Sun May 20 15:45]:549 $ cat quote_test.py 
class quote_test(object):
    s = "'hello'"
    t = "hello'"
    u = "'hello"
    v = '"\'hello"\''
    print(s)
    print(t)
    print(u)
    print(v)
(env) [jpk@Jons-MacBook-Pro-2/~/code/voc-dev/jpk_test: Sun May 20 15:46]:550 $ 

I suppose this issue can be closed?

eliasdorneles commented 6 years ago

Yup, seems like it! Closing it now, thanks for digging! :+1: