6

The Problem

This code

#!/usr/bin/env python
import pynotify
import time
import datetime

c='5/1/12 1:15 PM'
print c
dt = time.strptime(c, "%d/%m/%y %H:%M %p")

produces

5/1/12 1:15 PM
Traceback (most recent call last):
  File "tmp.py", line 9, in <module>
    dt = time.strptime(c, "%d/%m/%y %H:%M %p")
  File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: PM

Removing the import pynotify,

#!/usr/bin/env python
import time
import datetime

c='5/1/12 1:15 PM'
print c
dt = time.strptime(c, "%d/%m/%y %H:%M %p")

Removes the error.

5/1/12 1:15 PM

WHY?!!?!

Python Version

Python 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1] on linux2

pynotify.file

I added print calls for pynotify.__file__ and datetime.__file__

/usr/lib/python2.7/lib-dynload/datetime.so
/usr/lib/python2.7/dist-packages/gtk-2.0/pynotify/__init__.pyc
5/1/12 1:15 PM
Traceback (most recent call last):
  File "a.py", line 11, in <module>
    dt = time.strptime(c, "%d/%m/%y %H:%M %p")
  File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: PM

PDB

5/1/12 1:15 PM
> /usr/lib/python2.7/_strptime.py(324)_strptime()
-> found = format_regex.match(data_string)
(Pdb) format
'%d/%m/%y %H:%M %p'
(Pdb) continue
> /usr/lib/python2.7/_strptime.py(329)_strptime()
-> if len(data_string) != found.end():
(Pdb) continue
> /usr/lib/python2.7/_strptime.py(331)_strptime()
-> raise ValueError("unconverted data remains: %s" %
(Pdb) len(data_string)
14
(Pdb) found.end()
12
(Pdb) found.group(0)
'5/1/12 1:15 '

It would appear that '%d/%m/%y %H:%M %p' isn't capturing ALL of '5/1/12 1:15 PM'

dreftymac
  • 31,404
  • 26
  • 119
  • 182
EricR
  • 1,487
  • 2
  • 21
  • 42
  • Is that all the code? Running 2.7.3 here and have no problems with running your first version with pynotify. – Andrew Barrett May 01 '12 at 18:36
  • Yes. I'm afraid that's all the code. I copied/pasted my samples from my question, re-ran them, and have the same problem. – EricR May 01 '12 at 19:39
  • Are you sure you don't have a `pynotify.pyc` or `datetime.pyc` hiding somewhere? The stack trace seems to suggest otherwise... – Wayne Werner May 01 '12 at 19:44
  • Worth sanity checking with print pynotify.__file__, otherwise debugger I guess? – Andrew Barrett May 01 '12 at 19:47
  • @AndrewBarrett,@WayneWerner printed .__file__ information, as seen in my edits. – EricR May 01 '12 at 19:51
  • Looks like time to start digging in `_strptime.py`. I'd copy it to _strptime.py.bak then add `import pdb` in the file and toss `pdb.set_trace()` at line 328, and possibly 454. Also might be worth posting the md5 of of the _strptime file. – Wayne Werner May 01 '12 at 20:26
  • `md5sum _strptime.py -> 306a8ed9beec04a870f0c881e69981d5 _strptime.py` – EricR May 01 '12 at 20:31
  • @WayneWerner, pdb information added as per request (thanks for posting directions, I've never had to use pdb before) – EricR May 01 '12 at 20:37
  • Seems like something might be pooched in your `_strptime.py` - my md5 is `61fc98ce5e5c720e60cc3fa5acdd1a72`. Or was that after your `pdb` edits? ;) – Wayne Werner May 01 '12 at 20:48
  • Annnnd you're right Wayne, that was before edits. `esr@emi:/usr/lib/python2.7$ md5sum _strptime.py* 0214e707f3013d4d2a54491ab5517f7b _strptime.py 61fc98ce5e5c720e60cc3fa5acdd1a72 _strptime.py.bak` – EricR May 01 '12 at 20:50

1 Answers1

3

That's a fun problem. I'd wager what's happening is that pynotify is changing your locale settings, which is breaking strptime's interpretation of your timestamp string.

Here's your code with a couple of debugging print statements that will illustrate the theory:

#!/usr/bin/env python

import time
import datetime
import locale

print locale.getlocale()
import pynotify
print locale.getlocale()
c='5/1/12 1:15 PM'
print c
dt = time.strptime(c, "%d/%m/%y %H:%M %p")

On my system, I get the following:

(None, None)
('en_US', 'UTF8')
5/1/12 1:15 PM

I am not getting your error, but it's possible pynotify is setting your locale to something completely silly which is confusing strptime.

Maybe have a look at that and fiddle a bit with your locale settings, either unset it before calling strptime (and set it back after, no knowing what kind of assumptions pynotify makes) or set it to something sane if you discover it's set to something silly.

JosefAssad
  • 4,018
  • 28
  • 37
  • Well I'll be. That's it. Never in a million years would I have thought of that. Thank you so much. – EricR May 02 '12 at 07:51