The following bug has been logged on the website:
Bug reference: 13960
Logged by: Jim Nasby
Email address: Jim.Nasby@BlueTreble.com
PostgreSQL version: 9.5.0
Operating system: OS X
Description:
If a Postgres function contains characters that are illegal for python
identifiers, compilation fails. Error message is not very helpful either:
~@decina.local/21474# CREATE FUNCTION "test[]"() RETURNS text LANGUAGE
plpythonu AS $$return 'test'$$;
ERROR: could not compile PL/Python function "test[]"
DETAIL: SyntaxError: invalid syntax (<string>, line 1)
~@decina.local/21474# CREATE FUNCTION "test"() RETURNS text LANGUAGE
plpythonu AS $$return 'test'$$;
CREATE FUNCTION
~@decina.local/21474#
One possibility is to simply strip out invalid characters[1]. What's valid
expands in 3.5, but I don't think that really matters.
Another possibility would be to catch the python exception, but that's
probably more trouble than it's worth.
[1]
https://docs.python.org/2/reference/lexical_analysis.html#grammar-token-identifier
identifier ::= (letter|"_") (letter | digit | "_")*
letter ::= lowercase | uppercase
lowercase ::= "a"..."z"
uppercase ::= "A"..."Z"
digit ::= "0"..."9"
3.5:
https://docs.python.org/3.5/reference/lexical_analysis.html#grammar-token-identifier