Repository: nathandines/SPF2IP

Issue:
Broken on 3.6
The current code always returns an empty answer on Python 3.6. This fixes it for me:

```
diff --git a/SPF2IP.py b/SPF2IP.py
index e6210f3..84683ff 100644
--- a/SPF2IP.py
+++ b/SPF2IP.py
@@ -34,7 +34,7 @@ def dns_request_unicode(hostname,record_type,*args,**kwargs):
         value = value.__str__().encode('utf-8').decode('utf-8')
       output.append(value)
     elif record_type == "TXT":
-      value = ''.join([str(ent) for ent in entry.strings])
+      value = ''.join([str(ent, encoding='ascii') for ent in entry.strings])
       if type(value) is not unicode:
         value = value.decode('utf-8')
       output.append(value)
```
I only know python3 so I can't give good advice on making code that works on both 2 and 3. But a friend of mine's package has this function that you might find useful:

```
def to_native_str(value, encoding='utf-8'):
    if isinstance(value, str):
        return value

    if six.PY3 and isinstance(value, six.binary_type):  #pragma: no cover                                                                                      
        return value.decode(encoding)
    elif six.PY2 and isinstance(value, six.text_type):  #pragma: no cover                                                                                      
        return value.encode(encoding)
```



Discussion:

Patch:
diff --git a/SPF2IP.py b/SPF2IP.py
index e6210f3..b95903e 100644
--- a/SPF2IP.py
+++ b/SPF2IP.py
@@ -29,14 +29,22 @@ def dns_request_unicode(hostname,record_type,*args,**kwargs):
         value = value.decode('utf-8')
       output.append(value)
     elif record_type == "MX":
-      value = entry.exchange
-      if type(value) is not unicode:
-        value = value.__str__().encode('utf-8').decode('utf-8')
+      try:
+        value = entry.exchange.decode('utf-8')
+      except AttributeError as err:
+        if err.args[0] == "'Name' object has no attribute 'decode'":
+          value = unicode(entry.exchange)
+        else:
+          raise
       output.append(value)
     elif record_type == "TXT":
-      value = ''.join([str(ent) for ent in entry.strings])
-      if type(value) is not unicode:
-        value = value.decode('utf-8')
+      value_array = []
+      for ent in entry.strings:
+        if type(ent) is not unicode:
+          value_array.append(ent.decode('utf-8'))
+        else:
+          value_array.append(ent)
+      value = ''.join(value_array)
       output.append(value)
   return output
 
diff --git a/setup.py b/setup.py
index 3b958d6..3561be0 100755
--- a/setup.py
+++ b/setup.py
@@ -11,7 +11,7 @@ with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
 
 setup(
     name='SPF2IP',
-    version='1.0.4',
+    version='1.0.5',
 
     description='Python module to get IP addresses from an SPF record',
     long_description=long_description,


Test patch:
diff --git a/test_SPF2IP.py b/test_SPF2IP.py
index 54caff5..6e88918 100644
--- a/test_SPF2IP.py
+++ b/test_SPF2IP.py
@@ -125,11 +125,13 @@ dns_records = {
 class fakedns:
   def __init__(self,value,record_type):
     if record_type == 'TXT':
-      self.strings = value
+      self.strings = []
+      for entry in value:
+        self.strings.append(entry.encode('utf-8'))
     elif record_type == 'A' or record_type == 'AAAA':
-      self.address = value
+      self.address = value.encode('utf-8')
     elif record_type == 'MX':
-      self.exchange = value
+      self.exchange = value.encode('utf-8')
 def fake_dns_resolver(hostname,record_type):
   try:
     dns_records[hostname]