[Python-Dev] SyntaxError for illegal literals

Ka-Ping Yee ping@lfw.org
Wed, 14 Feb 2001 03:21:51 -0800 (PST)


On Wed, 14 Feb 2001, Thomas Wouters wrote:
> >     3.  A ValueError means "i got a value that wasn't allowed or
> >         expected here".  That is not at all what is happening.
> >         There is *no* defined value at all.  It's not that there
> >         was a value and it was wrong -- the value was never even
> >         brought into existence.
> 
> Not quite true. It wasn't *compiled*, but it's a literal, so it does exist.
> The problem is not the value of a compiled \x escape, but the value after
> the \x. 

No, it doesn't exist -- not in the Python world, anyway.  There is no
Python object corresponding to the literal.  That's what i meant by
not existing.  I think this is an okay choice of meaning for "exist",
since, after all, the point of the language is to abstract away lower
levels so programmers can think in that higher-level "Python world".

> > I hope you will agree with me that solving only #4 by changing
> > ValueErrors so they behave a little more like SyntaxErrors in
> > certain particular situations isn't the best solution.
> 
> I don't, really. The name 'ValueError' is exactly right: what is wrong (in
> the \x escape example) is the *value* of something (of the \x escape in
> question.)

The previous paragraph pretty much answers this, but i'll clarify.
My understanding of ValueError, as it holds in all other situations
but this one, is that a Python value of the right type was supplied
but it was otherwise wrong -- illegal, or unexpected, or something
of that sort.

The documentation on the exceptions module says:

    ValueError
          Raised when a built-in operation or function receives an
          argument that has the right type but an inappropriate value,
          and the situation is not described by a more precise exception
          such as IndexError. 

That doesn't apply to "\xgh" or 1982391879487124.

> If a syntax error was raised, I would think something was wrong
> with the syntax.

But there is.  "\x45" is syntax for the letter E.  It generates the
semantics "the character object with ordinal 69 (corresponding to
the uppercase letter E in ASCII)".  "\xgh" doesn't generate any
semantics -- we stop before we get there, because the syntax is wrong.


-- ?!ng