[BitTorrent] bencode float support

ed_ooyer agril at cryptomail.org
Sat Jul 31 06:51:54 EDT 2004


The following patch by Connelly Barnes adds float bencode and bdecode
support to bencode.py
While bittorrent itself doesn't need float bencoding, its CVS is the
primary location of the useful bencode module, so hopefully float
support can become part of the bencode 'standard'.

---cut---

--- bencode.bak	Sat Jul 31 00:29:30 2004
+++ bencode.py	Sat Jul 31 01:53:08 2004
@@ -1,5 +1,14 @@
+
+# --------------------------------------------------
+# bencode.py: Safe object pickling/unpickling.
+# --------------------------------------------------
+
 # Written by Petru Paler
-# see LICENSE.txt for license information
+# Added support for floats, Connelly Barnes 2004 June 14.
+# This file is licensed by Bram Cohen under the MIT license.
+# see LICENSE.txt for license information.
+
+import string
 
 def decode_int(x, f):
     f += 1
@@ -15,6 +24,23 @@
         raise ValueError
     return (n, newf+1)
 
+def assert_finite(n):
+  """Raises ValueError if n is NaN or infinite."""
+
+  valid_chars = '0123456789.-+eE'
+  if repr(n).translate(string.maketrans('',''), valid_chars) != '':
+    raise ValueError('encountered NaN or infinite')
+
+def decode_float(x, f):
+    f += 1
+    newf = x.index('e', f)
+    try:
+        n = float(x[f:newf].replace('E', 'e'))
+        assert_finite(n)
+    except (OverflowError, ValueError):
+        raise ValueError('encountered NaN or infinite')
+    return (n, newf+1)
+
 def decode_string(x, f):
     colon = x.index(':', f)
     try:
@@ -48,6 +74,7 @@
 decode_func['l'] = decode_list
 decode_func['d'] = decode_dict
 decode_func['i'] = decode_int
+decode_func['f'] = decode_float
 decode_func['0'] = decode_string
 decode_func['1'] = decode_string
 decode_func['2'] = decode_string
@@ -226,7 +253,7 @@
         pass
     bdecode('d0:i3ee')
 
-from types import StringType, IntType, LongType, DictType, ListType,
TupleType
+from types import StringType, IntType, LongType, DictType, ListType,
TupleType, FloatType
 
 class Bencached(object):
     __slots__ = ['bencoded']
@@ -240,6 +267,10 @@
 def encode_int(x, r):
     r.extend(('i', str(x), 'e'))
 
+def encode_float(x, r):
+    assert_finite(x)
+    r.extend(('f', repr(x).replace('e', 'E'), 'e'))
+
 def encode_string(x, r):
     r.extend((str(len(x)), ':', x))
 
@@ -262,6 +293,7 @@
 encode_func[type(Bencached(0))] = encode_bencached
 encode_func[IntType] = encode_int
 encode_func[LongType] = encode_int
+encode_func[FloatType] = encode_float
 encode_func[StringType] = encode_string
 encode_func[ListType] = encode_list
 encode_func[TupleType] = encode_list




------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/dkFolB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/BitTorrent/

<*> To unsubscribe from this group, send an email to:
    BitTorrent-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



More information about the BitTorrent mailing list