Opened 8 months ago

Closed 7 months ago

#29351 closed defect (not a bug)

BandwidthFile TypeError when initializing or accessing attributes

Reported by: juga Owned by: atagar
Priority: Medium Milestone:
Component: Core Tor/Stem Version:
Severity: Normal Keywords: tor-bwauth
Cc: Actual Points:
Parent ID: Points:
Reviewer: Sponsor:

Description

I obtain the same error both in Python 2.7 and 3.5.
Maybe i'm doing something wrong

>>> from stem.descriptor import bandwidth_file
>>> content = """1523911758
... node_id=$68A483E05A2ABDCA6DA5A3EF8DB5177638A27F80 bw=760 nick=Test"""
>>> bwfile = bandwidth_file.BandwidthFile(content, validate=True)
Traceback (most recent call last):
  File "<ipython-input-3-54b8af3f518d>", line 1, in <module>
    bwfile = bandwidth_file.BandwidthFile(content, validate=True)
  File "/home/dev/stem/stem/descriptor/bandwidth_file.py", line 262, in __init__
    _parse_timestamp(self, None)
  File "/home/dev/stem/stem/descriptor/bandwidth_file.py", line 132, in _parse_timestamp
    first_line = io.BytesIO(descriptor.get_bytes()).readline().strip()
TypeError: a bytes-like object is required, not 'str'
>>> bwfile = bandwidth_file.BandwidthFile(content)
>>> bwfile.header
Traceback (most recent call last):
  File "<ipython-input-5-0fc01c24bd9c>", line 1, in <module>
    bwfile.header
  File "/home/user/_my/code/bwauth-related/stem/stem/descriptor/__init__.py", line 1041, in __getattr__
    parsing_function(self, self._entries)
  File "/home/dev/stem/stem/descriptor/bandwidth_file.py", line 105, in _parse_header
    content = io.BytesIO(descriptor.get_bytes())
TypeError: a bytes-like object is required, not 'str'

Child Tickets

Change History (3)

comment:1 Changed 8 months ago by atagar

Hi juga, there's several ways of creating a descriptor depending on what you are making it from...

String Descriptor Content

>>> from stem.descriptor.bandwidth_file import BandwidthFile
>>> content = """1523911758
... node_id=$68A483E05A2ABDCA6DA5A3EF8DB5177638A27F80 bw=760 nick=Test"""
>>> bwfile = BandwidthFile.from_str(content)
>>> bwfile.measurements
{u'68A483E05A2ABDCA6DA5A3EF8DB5177638A27F80': {u'nick': u'Test', u'node_id': u'$68A483E05A2ABDCA6DA5A3EF8DB5177638A27F80', u'bw': u'760'}}

File Descriptor Content

>>> import stem.descriptor
>>> bwfile = next(stem.descriptor.parse_file('bandwidth_file_v1.2', 'bandwidth-file 1.2'))
>>> print("This file has data for %i relays" % len(bwfile.measurements))
This file has data for 81 relays

Individual Attributes

>>> from stem.descriptor.bandwidth_file import BandwidthFile
>>> bwfile = BandwidthFile.create({
...   'version': '5.4.3',
...   'software': 'my_application',
...   'content': [
...     'bw=1 bw_mean=191643 bw_median=218251 node_id=$92808CA58D8F32CA34A34C547610869BF4E2A6EC',
...     'bw=1 bw_mean=93766 bw_median=93606 node_id=$A6443E49306288C7DAE9B8466568F08DA5BD58D4',
...   ],
... })
>>> print(bwfile)
1549479544
version=5.4.3
software=my_application
=====
bw=1 bw_mean=191643 bw_median=218251 node_id=$92808CA58D8F32CA34A34C547610869BF4E2A6EC
bw=1 bw_mean=93766 bw_median=93606 node_id=$A6443E49306288C7DAE9B8466568F08DA5BD58D4

Did you have any other questions?

comment:2 in reply to:  1 ; Changed 8 months ago by juga

Replying to atagar:

Hi juga, there's several ways of creating a descriptor depending on what you are making it from...

String Descriptor Content

>>> from stem.descriptor.bandwidth_file import BandwidthFile
>>> content = """1523911758
... node_id=$68A483E05A2ABDCA6DA5A3EF8DB5177638A27F80 bw=760 nick=Test"""
>>> bwfile = BandwidthFile.from_str(content)
>>> bwfile.measurements
{u'68A483E05A2ABDCA6DA5A3EF8DB5177638A27F80': {u'nick': u'Test', u'node_id': u'$68A483E05A2ABDCA6DA5A3EF8DB5177638A27F80', u'bw': u'760'}}

Hmm, weird, doing the same, i get from bwfile.measurements an empty dictionary (all the attributes are empty or None except _raw_contents).
What i'm doing wrong this time?

The other examples work. Sorry i didn't read the docs.

comment:3 in reply to:  2 Changed 7 months ago by juga

Resolution: not a bug
Status: newclosed

Replying to juga:

Hmm, weird, doing the same, i get from bwfile.measurements an empty dictionary (all the attributes are empty or None except _raw_contents).
What i'm doing wrong this time?

i don't know what i did, but checking second time works.
So this can be closed, thanks for the explanations.

Note: See TracTickets for help on using tickets.