From 943aec432fe02ced3295ba9ef4e5251ab0e7ebb2 Mon Sep 17 00:00:00 2001 From: gryf Date: Sat, 20 Jul 2019 11:56:54 +0200 Subject: [PATCH] Change a way for detecting python3 vs python2. --- replacer.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/replacer.py b/replacer.py index fbfc7cb..4a76749 100644 --- a/replacer.py +++ b/replacer.py @@ -21,7 +21,6 @@ Examples: import os import json -import sys import weechat @@ -40,15 +39,20 @@ COLOR_RESET = weechat.color('reset') def _decode(string): - if sys.version_info.major == 2: + try: return string.decode('utf-8') - return string + except AttributeError: + return string def _encode(string): - if sys.version_info.major == 2: + try: + # Encode only, if we have decode attribute, which is available for + # string only on python2 + string.decode return string.encode('utf-8') - return string + except AttributeError: + return string class Replacer(object):