2

I'm trying to emulate Python's repr exactly in Java; this includes the use of single quotes where possible. What method does Python use to determine what sort of quotes it should emit?

Edit: I'm looking for some actual code, somewhere in the web of Python. I've already looked at Objects/unicodeobject.c and some of Objects/strlib/, but I couldn't find anything besides escape sequences for Unicode.

6
  • Why don't you read the python source? Commented Dec 13, 2014 at 23:22
  • On my terminal, the output by repr does not seem to change with respect to the type of quotes used to create the string. >>> repr("Hello") "'Hello'" >>> repr('Hello') "'Hello'" Commented Dec 13, 2014 at 23:25
  • @simonzack I have. I already extracted what I could from Objects/unicodeobject.c. Commented Dec 13, 2014 at 23:25
  • 1
    @Smac89 It doesn't store quote information, I got that much from the interpreter. Commented Dec 13, 2014 at 23:26
  • 1
    @Smac89 I'm looking to replicate the behavior of repr exactly in Java. This includes quotes. Commented Dec 13, 2014 at 23:27

3 Answers 3

1

I guess it will use single quotes unless it need to use it inside the string.

As show in:

print repr("Hello")
print repr('Hello')
print repr("Hell'o")
print repr('Hell"o')
print repr("""Hell'o Worl"o""")

Output:

'Hello'
'Hello'
"Hell'o" # only one using double quotes
'Hell"o'
'Hell\'o Worl"o' # handles the single quote with a \'
Sign up to request clarification or add additional context in comments.

2 Comments

I'm actually looking for an algorithm or similar from the source. I'll update the question.
@ıɯɐƃoʇǝızuǝʞ IMO your question is clear enough, but some stackoverflow users do not read the question at all.
1

From what I could extract out of Objects/byteobject.c (here), this is the section that does it:

quote = '\'';
if (smartquotes && squotes && !dquotes)
    quote = '"';
if (squotes && quote == '\'') {
    if (newsize > PY_SSIZE_T_MAX - squotes)
        goto overflow;
    newsize += squotes;
}

So, if there is no double quotes and there is single quotes it uses double quotes, otherwise single quotes.

1 Comment

Yeah.. I think it depends on whether you want the ascii strings or the unicode strings. There's a similar block of code in unicode_repr. And since strings are unicode by default in python3 it's more likely the unicode version is the one you want.
1

https://github.com/python/cpython/tree/master/Objects/unicodeobject.c

static PyObject *
unicode_repr(PyObject *unicode)
{ ...

unicode_repr in here github.com/python/cpython/blob/master/Objects/unicodeobject.c by the looks of it.

NOTE: I've updated this answer to remove out-of-date info and point to the current repo

7 Comments

Additionally, latest 3.4 source, I should have said. Oh well, this extremely outdated git mirror will work too. I might be able to track down where stringobject.c disappeared to.
Get the source distro from python.org.
(for future reference the official mirror is here)
Hmm .. not immediately obvious.. PyObject_Repr is calling res = (*v->ob_type->tp_repr)(v); I can't see something that's obviously the unicode (or string) object type.
Also, I'm not a C person so this is half guess work on my part.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.