0

I ran a .py script and got the following:

Traceback (most recent call last):
  File "decrypt_bitcoinj_seed.pyw", line 313, in <module>
    wallet = load_wallet(wallet_file, get_password)
  File "decrypt_bitcoinj_seed.pyw", line 126, in load_wallet
    password = get_password_fn(takes_long)
  File "decrypt_bitcoinj_seed.pyw", line 301, in get_password
    return password.decode('ASCII') if isinstance(password, str) else password
AttributeError: 'str' object has no attribute 'decode' 

What I don't get is why doesn't 'decode' interpret 'str' as an instance for a string of characters? Aren't decode and str compatible in trying to complete this block of code?

Is this a candidate for using except AttributeError?

Octavia Togami
  • 4,186
  • 4
  • 31
  • 49
ubunt guy
  • 1
  • 1
  • [The string that you are trying to decode is already decoded](https://stackoverflow.com/questions/28583565/str-object-has-no-attribute-decode-python-3-error) – TSpark Jul 24 '20 at 15:56
  • So, I would drop the .decode and str parts and it becomes: return password('ASCII') if isinstance(password) else password ? this doesn't look right. – ubunt guy Jul 24 '20 at 16:30
  • no, I mean if the string is already decoded, this line throw an error, and it is also useless so you may as well just remove it (replace by `return password`. – TSpark Jul 24 '20 at 16:33
  • The correct way would be to surround the decode with a try/catch – TSpark Jul 24 '20 at 16:33
  • I don't see how it's useless... what if the file is in ASCII format which then would need to be decoded? It looks like this line is wanting to return either the password OR the str if the string is not a complete password but a partial password string, right? – ubunt guy Jul 24 '20 at 17:11

1 Answers1

0

We would need additional information on your program, but the line that is throwing an error could be modified this way.

from:

return password.decode('ASCII') if isinstance(password, str) else password

to:

try:
    return password.decode('ASCII')
except AttributeError:
    return password

It looks like your code is checking if the variable password is a string, and if it is, it decodes it. However, if password is not encoded, it throws AttributeError. The same error is thrown if password is not a string, so you do not need to check isinstance(password, str), it will be caught by the try statement, and will simply return as-is.

TSpark
  • 189
  • 1
  • 14
  • # These functions differ between command-line and GUI runs `def get_password(takes_long): # must return Unicode password = simpledialog.askstring('Password', 'This wallet file is encrypted, please enter its password:', show='*') if takes_long: init_window() # display the progress bar if this could take a while try: return password.decode('ASCII') except AttributeError: return password def get_pin(takes_long): # must return Unicode` – ubunt guy Jul 24 '20 at 19:43
  • `pin = simpledialog.askstring('Password', 'This wallet is seed is encrypted with a PIN or password, please enter it:', show='*') if takes_long: init_window() # display the progress bar if this could take a while try: return pin.decode('ASCII') except AttributeError: return pin def display_error(msg): return messagebox.showerror('Error', msg) File "decrypt_bitcoinj_seed.pyw", line 302 return password.decode('ASCII')` ^ **SyntaxError: 'return' outside function**` – ubunt guy Jul 24 '20 at 19:44
  • The carrot is supposed to be on the r for return.... obviously I haven't figured out this stack overflow posting yet and they seem to think 5 minutes is enough time to edit stuff. – ubunt guy Jul 24 '20 at 19:48
  • After inserting your try: except code, the error is now **SyntaxError: 'return' outside function** – ubunt guy Jul 25 '20 at 02:14