[tor-bugs] #17051 [Stem]: The binary reading on Windows platforms don't resolve the "0D0A"/"0A" problem.

Tor Bug Tracker & Wiki blackhole at torproject.org
Sun Sep 13 20:22:32 UTC 2015


#17051: The binary reading on Windows platforms don't resolve the "0D0A"/"0A"
problem.
------------------------+------------------------
 Reporter:  TORques     |          Owner:  atagar
     Type:  defect      |         Status:  new
 Priority:  normal      |      Milestone:
Component:  Stem        |        Version:
 Keywords:  descriptor  |  Actual Points:
Parent ID:              |         Points:
------------------------+------------------------
 When I run the first example from:
 https://stem.torproject.org/api/descriptor/microdescriptor.html
 {{{
 import os

 from stem.control import Controller
 from stem.descriptor import parse_file

 with Controller.from_port(port = 9051) as controller:
   controller.authenticate()

   exit_digests = set()
   data_dir = controller.get_conf('DataDirectory')

   for desc in controller.get_microdescriptors():
     if desc.exit_policy.is_exiting_allowed():
       exit_digests.add(desc.digest)

   print 'Exit Relays:'

   for desc in parse_file(os.path.join(data_dir, 'cached-microdesc-
 consensus')):
     if desc.digest in exit_digests:
       print '  %s (%s)' % (desc.nickname, desc.fingerprint)
 }}}
 on Windows XP or Windows 10 the result was:
 {{{
 Microsoft Windows XP [Version 5.1.2600]
 (C) Copyright 1985-2001 Microsoft Corp.

 C:\>example1.py
 Exit Relays:

 C:\>
 }}}
 After I replaced in "C:\Python27\Lib\site-
 packages\stem\descriptor\__init__.py":
 {{{
 def _parse_file_for_path(descriptor_file, *args, **kwargs):
                 with open(descriptor_file, 'rb') as desc_file:
                         for desc in parse_file(desc_file, *args,
 **kwargs):
                                 yield desc
 }}}
 with
 {{{
 def _parse_file_for_path(descriptor_file, *args, **kwargs):
         if os.environ.get('OS','') != 'Windows_NT':
                 with open(descriptor_file, 'rb') as desc_file:
                         for desc in parse_file(desc_file, *args,
 **kwargs):
                                 yield desc
         if os.environ.get('OS','') == 'Windows_NT':
                 with open(descriptor_file, 'r') as desc_file:
                         for desc in parse_file(desc_file, *args,
 **kwargs):
                                 yield desc
 }}}
 the result is:
 {{{
 Microsoft Windows XP [Version 5.1.2600]
 (C) Copyright 1985-2001 Microsoft Corp.

 C:\>example1.py
 Exit Relays:
   CalyxInstitute14 (0011BD2485AD45D984EC4159C88FC066E5E3300E)
   ieditedtheconfig (0098C475875ABC4AA864738B1D1079F711C38287)
   default (00AE2BBFB5C0BBF25853B49E04CC76895044A795)
   ...
 }}}

 This bug was reported on #tor IRC channel by maiena and I fixed it with
 this patch.

 The file "cached-microdesc-consensus" created by tor on Windows platforms
 end any line with CRLF (0D0A). As Python stated about "open(name[, mode[,
 buffering]])", "The most commonly-used values of mode are 'r' for reading,
 'w' for writing (truncating the file if it already exists). If mode is
 omitted, it defaults to 'r'. The default is to use text mode, which may
 convert '\n' characters to a platform-specific representation on writing
 and back on reading."

 That means that the binary reading on Windows platforms don't resolve the
 "0D0A"/"0A" problem.

--
Ticket URL: <https://trac.torproject.org/projects/tor/ticket/17051>
Tor Bug Tracker & Wiki <https://trac.torproject.org/>
The Tor Project: anonymity online


More information about the tor-bugs mailing list