Title: plistlib handling of real datatype
Type: Stage: resolved
Components: Versions: Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 2.7, Python 2.6
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: ned.deily, pvg
Priority: normal Keywords:

Created on 2012-05-24 01:16 by pvg, last changed 2012-05-24 03:17 by ned.deily. This issue is now closed.

Messages (2)
msg161471 - (view) Author: Peter VG (pvg) Date: 2012-05-24 01:16
Since strings cannot reliably be converted to floats and back, plistlib should provide an option to treat the real datatype as strings/data or to use the Decimal library class. Currently, reading and then writing a real value can change its representation in the plist even if it has not been modified. For instance:



msg161477 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2012-05-24 03:17
This should no longer be an issue on most platforms as of Python 2.7 and Python 3.1.  Both added a new algorithm such that "the repr() of a floating-point number x now returns a result based on the shortest decimal string that’s guaranteed to round back to x under correct rounding".  The new float repr applies to plistlib as well.  Here's an example:

$ cat
from plistlib import readPlistFromString, writePlistToString
d1 = dict(a=0.15)
s1 = writePlistToString(d1)
d2 = readPlistFromString(s1)
assert d1['a'] == d2['a']

$ python2.6
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "">
<plist version="1.0">

{'a': 0.14999999999999999}
$ python2.7
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

{'a': 0.15}

Note that the binary representation of the float is the same on both 2.6 and 2.7 in this case anyway.

For more info, see:
Date User Action Args
2012-05-24 03:17:00ned.deilysetstatus: open -> closed

nosy: + ned.deily
messages: + msg161477

resolution: out of date
stage: resolved
2012-05-24 01:16:54pvgcreate