Strange problem when running python code

Discussion in 'Python' started by ishtar2020, Apr 4, 2006.

  1. ishtar2020

    ishtar2020 Guest

    Hi everybody

    I've been writing my very first application in Python and everything is
    running smoothly, except for a strange problem that pops up every once
    in a while. I'm sure is the kind
    of newbie thing every seasoned programmer knows.

    Sometimes a receive strange Syntax Errors from parts of code that
    worked perfectly minutes ago. What's even more puzzling is that those
    errors are pointed to another part of the module when I do some random,
    innofensive changes in the code (like inserting a line or deleting some
    comments). Sometimes those changes are enough to make the error
    dissapear.

    Could it be that python has found a real error but is failing to tell
    me where it is?
    ishtar2020, Apr 4, 2006
    #1
    1. Advertising

  2. ishtar2020

    Brian Beck Guest

    For certain errors like Syntax Errors, you'll get a much more helpful
    response if you post some actual code. Strip it down if you have to,
    but make sure we can reproduce the errors.

    --
    Brian Beck
    Adventurer of the First Order
    Brian Beck, Apr 4, 2006
    #2
    1. Advertising

  3. ishtar2020

    John Salerno Guest

    ishtar2020 wrote:
    > Hi everybody
    >
    > I've been writing my very first application in Python and everything is
    > running smoothly, except for a strange problem that pops up every once
    > in a while. I'm sure is the kind
    > of newbie thing every seasoned programmer knows.
    >
    > Sometimes a receive strange Syntax Errors from parts of code that
    > worked perfectly minutes ago. What's even more puzzling is that those
    > errors are pointed to another part of the module when I do some random,
    > innofensive changes in the code (like inserting a line or deleting some
    > comments). Sometimes those changes are enough to make the error
    > dissapear.
    >
    > Could it be that python has found a real error but is failing to tell
    > me where it is?
    >


    I recently had quite a hassle with an indentation problem, so you might
    want to check on your whitespace.
    John Salerno, Apr 4, 2006
    #3
  4. "ishtar2020" wrote:

    > Sometimes a receive strange Syntax Errors from parts of code that
    > worked perfectly minutes ago. What's even more puzzling is that those
    > errors are pointed to another part of the module when I do some random,
    > innofensive changes in the code (like inserting a line or deleting some
    > comments). Sometimes those changes are enough to make the error
    > dissapear.


    Python 2.4.1 has a bug where this can happen if you're 1) using coding
    directives, and 2) your module is relatively large.

    I suggest upgrading to 2.4.3.

    </F>
    Fredrik Lundh, Apr 4, 2006
    #4
  5. ishtar2020

    Roy Smith Guest

    ishtar2020 <> wrote:
    >I've been writing my very first application in Python and everything is
    >running smoothly, except for a strange problem that pops up every once
    >in a while. I'm sure is the kind
    >of newbie thing every seasoned programmer knows.


    Nobody here has a crystal ball. Please post your code, tell us what
    changes you made, and cut-and-paste the entire error message and the
    associated stack trace.
    Roy Smith, Apr 4, 2006
    #5
  6. ishtar2020

    ishtar2020 Guest

    I must add, when the python interpreter displays the traceback, with
    the line that is producing the error, it doesn't look like the one I
    got in the code.

    This is the line where the interpreter finds the error

    if text.list.toString() in limits:list)): <- Here is where
    the error is found, but this line is not on my file

    SyntaxError: invalid syntax

    And this is the line I got on the file, looks like the interpreter is
    mixing the second sentence with the end of the first one:

    for i in range(self.initialPositionl+1,len(text.list)):
    if text.list.toString() in limits:
    self.finalPosition=i
    break

    It's quite puzzling. And if I change some lousy thing, like inserting a
    newline between the sentences, the interpreter will find another error
    somewhere else, even when that
    part of the code was working flawlessly in previous runs
    ishtar2020, Apr 4, 2006
    #6
  7. Roy Smith wrote:

    > ishtar2020 <> wrote:
    >>I've been writing my very first application in Python and everything is
    >>running smoothly, except for a strange problem that pops up every once
    >>in a while. I'm sure is the kind
    >>of newbie thing every seasoned programmer knows.

    >
    > Nobody here has a crystal ball. Please post your code, tell us what
    > changes you made, and cut-and-paste the entire error message and the
    > associated stack trace.


    Speak for yourself, Mr. I-Have-No-Crystal-Ball. Besides, any lummox knows
    that crystal balls are for seeing the future, not for mind-reading. No, to
    do proper mind reading you need youself a pointy hat and (preferably) a
    deck of cards.

    Sorry, weird day at work. Had to vent.
    --
    Steve Juranich
    Tucson, AZ
    USA
    Steve Juranich, Apr 4, 2006
    #7
  8. ishtar2020

    James Stroud Guest

    ishtar2020 wrote:
    > Hi everybody
    >
    > I've been writing my very first application in Python and everything is
    > running smoothly, except for a strange problem that pops up every once
    > in a while. I'm sure is the kind
    > of newbie thing every seasoned programmer knows.
    >
    > Sometimes a receive strange Syntax Errors from parts of code that
    > worked perfectly minutes ago. What's even more puzzling is that those
    > errors are pointed to another part of the module when I do some random,
    > innofensive changes in the code (like inserting a line or deleting some
    > comments). Sometimes those changes are enough to make the error
    > dissapear.
    >
    > Could it be that python has found a real error but is failing to tell
    > me where it is?
    >


    I don't mean to accuse you or to start a tab v. non-tab war (is tab v.
    non-tab a holy war?, it should be because tabs are so damn useful for
    formatting and getting everything just-right), but don't use tabs if you
    are using them. Here are the reasons: 1. they personally annoy me. 2.
    you can't see the damn things (hence the basis for why they are so
    annoying). "Tab" comes from the root for the Greek for "table" (or is it
    the other way around?, or is it Latin? or Arabic? or Hebrew?). Thus,
    tabs should be reserved for thier god-given intended purpose, which is
    tab-delimited tables, where a parser can easily separate by them.
    Internal, and hence annoying, tabs should not be used elsewhere because
    it screws everything up. For example, here is a tab-delimited parser
    written in python


    table = []
    for line in tab_delimited_table:
    table.append(line.split('\t'))


    Man that was easy! Now, when people put annoying tabs everywhere, to get
    their indent just-so, then it totally screws everything up, including
    this parser, which was designed for tables delimited by tabs--because
    that is the tab's special purpose.

    There is one exception however. Its called the tab-space-tab approach.
    If you are using tabs to indent your code, then always put at least one
    space between tabs. Never put two tabs together. This helps the editor
    to display your code correctly.

    James

    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095

    http://www.jamesstroud.com/
    James Stroud, Apr 4, 2006
    #8
  9. On 04/04/2006-12:01PM, ishtar2020 wrote:
    > This is the line where the interpreter finds the error
    >
    > if text.list.toString() in limits:list)): <- Here is where


    That line has two extra close parens before the :

    Can you show the traceback?
    Christopher Weimann, Apr 4, 2006
    #9
  10. ishtar2020

    Guest

    Are U Using any IDE for Python?
    If yes then check out the setting and make sure that u are running same
    code.
    , Apr 5, 2006
    #10
  11. On 4 Apr 2006 12:01:12 -0700, "ishtar2020" <>
    declaimed the following in comp.lang.python:

    > I must add, when the python interpreter displays the traceback, with
    > the line that is producing the error, it doesn't look like the one I
    > got in the code.
    >
    > This is the line where the interpreter finds the error
    >
    > if text.list.toString() in limits:list)): <- Here is where
    > the error is found, but this line is not on my file


    You must have something unwanted in the file...

    > And this is the line I got on the file, looks like the interpreter is
    > mixing the second sentence with the end of the first one:
    >
    > for i in range(self.initialPositionl+1,len(text.list)):
    > if text.list.toString() in limits:


    ...as it is carrying data from the upper line into the lower line!

    That is, the list)): appears to be part of the line
    above.


    I'd almost suspect your editor somehow has stored cursor movement
    characters such that the file looks correct in the editor, but is full
    of garbage in a parser...
    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Apr 5, 2006
    #11
  12. On Tue, 04 Apr 2006 14:03:40 -0700, Steve Juranich <>
    declaimed the following in comp.lang.python:


    > Speak for yourself, Mr. I-Have-No-Crystal-Ball. Besides, any lummox knows
    > that crystal balls are for seeing the future, not for mind-reading. No, to
    > do proper mind reading you need youself a pointy hat and (preferably) a
    > deck of cards.
    >

    As an occassional dabbler in the Tarot, I can assure you that cards
    are NOT used for "mind-reading"; they merely offer up a possible future
    which must be interpreted in light of the querant's situation... (or,
    since I typically read for myself -- the allow my subconscious to reveal
    my inclinations based upon the cards' meanings)
    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Apr 5, 2006
    #12
  13. "ishtar2020" wrote:

    > It's quite puzzling. And if I change some lousy thing, like inserting a
    > newline between the sentences, the interpreter will find another error
    > somewhere else, even when that part of the code was working flaw-
    > lessly in previous runs


    what Python version are you using ?

    (I've already provided an explanation in another post, but it seems that
    neither you nor the "it's your fault" crowd read that...).

    </F>
    Fredrik Lundh, Apr 5, 2006
    #13
  14. On Tue, 04 Apr 2006 12:01:12 -0700, ishtar2020 wrote:

    > I must add, when the python interpreter displays the traceback, with
    > the line that is producing the error, it doesn't look like the one I
    > got in the code.


    I sometimes get that problem when I'm running code, I make a change in the
    source file, use reload() to update my module, but forget to reinitialise
    my objects still in memory.

    So I end up with (buggy) instances in memory, but when I hit a traceback,
    the source code displayed has nothing to do with the actual error because
    the source file has been edited.

    If you are getting these weird errors when you do nothing but change
    comments, there is a good chance that's what is happening.

    Here is an example of that behaviour. Is this the sort of thing which is
    happening to you?


    # === File tester.py ===

    class Parrot(object):

    def speak(self):
    print "I'm pining for the fjords."

    def species(self, colour=None):
    return "Norwegian " + colour


    Now import the file:

    >>> import tester
    >>> p = tester.Parrot()
    >>> p.speak()

    I'm pining for the fjords.
    >>> p.species()

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "tester.py", line 9, in species
    return "Norwegian " + colour
    TypeError: cannot concatenate 'str' and 'NoneType' objects


    Now I edit the file, just adding comments and nothing else:


    # === File tester.py ===

    class Parrot(object):

    # Add some comments here.
    # Line two.

    def speak(self):
    print "I'm pining for the fjords."

    def species(self, colour=None):
    return "Norwegian " + colour


    But if I execute the old existing object, I get a nonsensical error:

    >>> p.species()

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "tester.py", line 9, in species
    print "I'm pining for the fjords."
    TypeError: cannot concatenate 'str' and 'NoneType' objects

    Even doing a reload() doesn't help. I have to delete the old instance, and
    create a new one.



    --
    Steven.
    Steven D'Aprano, Apr 5, 2006
    #14
  15. Dennis Lee Bieber wrote:

    > As an occassional dabbler in the Tarot, I can assure you that cards
    > are NOT used for "mind-reading"; they merely offer up a possible future
    > which must be interpreted in light of the querant's situation... (or,
    > since I typically read for myself -- the allow my subconscious to reveal
    > my inclinations based upon the cards' meanings)


    Who said anything about tarot? I'm talking about the old "Pick a card, any
    card..." bit.

    Cheers.
    --
    Steve Juranich
    Tucson, AZ
    USA
    Steve Juranich, Apr 5, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Cheshire [MSFT]

    RE: Strange problem running app remotely

    Jim Cheshire [MSFT], Jul 21, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    452
    Greg Burns
    Jul 22, 2004
  2. Harvey Twyman
    Replies:
    8
    Views:
    551
    August Derleth
    Oct 25, 2003
  3. Benjamin Rutt
    Replies:
    2
    Views:
    919
    Peter Hansen
    Dec 20, 2005
  4. kunal
    Replies:
    7
    Views:
    471
    White Wolf
    Dec 19, 2006
  5. Replies:
    1
    Views:
    266
    Terry Reedy
    Sep 4, 2012
Loading...

Share This Page