strange test for None

  • Thread starter karoly.kiripolszky
  • Start date
K

karoly.kiripolszky

in my server i use the following piece of code:

ims = self.headers["if-modified-since"]
if ims != None:
t = int(ims)

and i'm always getting the following error:

t = int(ims)
ValueError: invalid literal for int(): None

i wanna know what the hell is going on... first i tried to test using
is not None, but it makes no difference.

sorry i forgot, it's interpreter 2.4.4.
 
S

skip

"karoly" == karoly kiripolszky <karoly.kiripolszky> writes:

karoly> in my server i use the following piece of code:
karoly> ims = self.headers["if-modified-since"]
karoly> if ims != None:
karoly> t = int(ims)

karoly> and i'm always getting the following error:

karoly> t = int(ims)
karoly> ValueError: invalid literal for int(): None

Try printing repr(ims), type(ims) and id(ims) then compare that with
repr(None), type(None) and id(None). My guess is you have a string whose
value is "None":

% python2.4
Python 2.4.1 (#3, Jul 28 2005, 22:08:40)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1671)] on darwin
Type "help", "copyright", "credits" or "license" for more information. Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): None

The output in 2.5 makes it more clear what you've got:

% python2.5
Python 2.5c1 (release25-maint:51339, Aug 17 2006, 22:15:14)
[GCC 4.0.0 (Apple Computer, Inc. build 5026)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
in>>> int("None")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'None'

Skip
 
P

Peter Otten

karoly.kiripolszky said:
in my server i use the following piece of code:

ims = self.headers["if-modified-since"]
if ims != None:
t = int(ims)

and i'm always getting the following error:

t = int(ims)
ValueError: invalid literal for int(): None

i wanna know what the hell is going on... first i tried to test using
is not None, but it makes no difference.

sorry i forgot, it's interpreter 2.4.4.

Instead of the None singleton...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: int() argument must be a string or a number

....you seem to have the /string/ "None" in your dictionary
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): None

While the immediate fix would be

if ims != "None":
t = int(ims)

there is probably an erroneous

self.header["if-modified-since"] = str(value)

elsewhere in your code that you should replace with

if value is not None:
self.header["if-modified-since"] = str(value)

The quoted portion would then become

if "if-modified-since" in self.header:
t = int(self.headers["if-modified-since"])

Peter
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top