0

I made a Java program that uses unicode escaped characters to break a multiline comment and hide some functionality. The program below prints "Hello Cruel World". I'm wondering if this is possible to do in Python (any version). If it is not possible, how is this prevented in the language?

public static void main(String[] args) {
    print("Hello");

    /*
     * \u002A\u002F\u0070\u0072\u0069\u006E\u0074\u0028\u0022\u0043\u0072\u0075\u0065\u006C\u0022\u0029\u003B\u002F\u002A
     */
    print("World");
}

private static void print(String s){
    System.out.print(s + " ");
}

Note that the unicode is the escaped string */print("Cruel");/*

My fruitless attempt so far...

test = False

#\u0023test = True

'''
\u0027\u0027\u0027
test = True 
\u0027\u0027\u0027
'''

print(test)
6
  • 2
    dont ever do this ... what a horrible thing Mar 6, 2015 at 22:38
  • 1
    I agree. I'm looking for edge cases in program analysis tools... Mar 6, 2015 at 22:40
  • 2
    I dont think you can do that in python ... thankfully Mar 6, 2015 at 22:40
  • If you're looking for corner cases in a program analysis in Python, I imagine your worst nightmare would be the "exec" functionnality. Following your idea, it is possible to get some obfuscated code executed. i.e. exec("\u0074\u0065\u0073\u0074\u0020\u003d\u0020\u0054\u0072\u0075\u0065")
    – Cilyan
    Mar 6, 2015 at 23:00
  • @Cilyan yeah but you still could not put that in the comments (or inside triple quoted string) Mar 6, 2015 at 23:04

1 Answer 1

5

Python lexer only processes Unicode escapes within Unicode string literals (u'' in Python 2, '' in Python 3), thus such an approach is not possible.

If you try just the simple space, \u0020, python spits out:

SyntaxError: unexpected character after line continuation character

That is because outside string literals, the \ character is chiefly used in for breaking a long line into several shorter:

spam = foo + bar + baz + ham + \
       eggs + ham + spam + spam + \
       spam + sausages + spam 

Outside strings, the only allowed character after \ is a newline.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.