Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 829143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:50:09+00:00 2026-05-15T03:50:09+00:00

this may not be an earth-shattering deficiency of python, but i still wonder about

  • 0

this may not be an earth-shattering deficiency of python, but i still
wonder about the rationale behind the following behavior: when i
run

source = """
print( 'helo' )
if __name__ == '__main__':
  print( 'yeah!' )

#"""

print( compile( source, '<whatever>', 'exec' ) )

i get ::

  File "<whatever>", line 6
    #
    ^
SyntaxError: invalid syntax

i can avoid this exception by (1) deleting the trailing #; (2)
deleting or outcommenting the if __name__ == '__main__':\n
print( 'yeah!' )
lines; (3) add a newline to very end of the
source.

moreover, if i have the source end without a trailing newline right
behind the print( 'yeah!' ), the source will also compile without
error.

i could also reproduce this behavior with python 2.6, so it’s not new
to the 3k series.

i find this error to be highly irritating, all the more since when i
put above source inside a file and execute it directly or have it
imported, no error will occur—which is the expected behavior.

a # (hash) outside a string literal should always represent the
start of a (possibly empty) comment in a python source; moreover, the
presence or absence of a if __name__ == '__main__' clause should
not change the interpretation of a soure on a syntactical level.

can anyone reproduce the above problem, and/or comment on the
phenomenon?

cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:50:10+00:00Added an answer on May 15, 2026 at 3:50 am

    update

    turns out this is indeed a bug as pointed out by http://groups.google.com/group/comp.lang.python/msg/b4842cc7abd75fe9; the bug report is at http://bugs.python.org/issue1184112; it appears to be fixed in 2.7 and 3.2.

    solution

    once recognized, this bug is extremely simple to fix: since a valid python source should stay both syntactically valid and semantically unchanged when a newline is added to the source text, just mechanically do just that to any source text. this reminds me of the ; semicolon you mechanically put in between source texts when assembling a multi-file javascript source for efficient gzipped delivery to the remote client.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this may not be strictly about programming, but if I find no ready-made solution
This may not really be a Python related question, but pertains to language encoding
This may not be a programming question, but I don't know where to ask
This may not be the kind of question one should ask on StackOverflow, but
This may not be possible for the carousel but is there a way to
Sorry, this may or may not be a programming question directly, but I am
I fear this just may not be possible, but I'm trying to create a
This may seem a basic question but my is not currently working. I am
This may not be considered to be directly programming related but I am at
This may not be possible, but I'd like to ask anyway and see if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.