Repository: kjd/idna

Issue:
IndexError with new trailing hyphen check
#64 introduced the following uncaught exception. Is this intended?

```python
>>> idna.ulabel('xn--')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/John/.pyenv/versions/3.6.5/Python.framework/Versions/3.6/lib/python3.6/site-packages/idna/core.py", line 303, in ulabel
    if label.decode('ascii')[-1] == '-':
IndexError: string index out of range
```

I would've expected `UnicodeError` or `IDNAError`, which are more descriptive.

Discussion:

Patch:
diff --git a/idna/core.py b/idna/core.py
index b861d14..2c193d6 100644
--- a/idna/core.py
+++ b/idna/core.py
@@ -296,6 +296,8 @@ def ulabel(label):
     label = label.lower()
     if label.startswith(_alabel_prefix):
         label = label[len(_alabel_prefix):]
+        if not label:
+            raise IDNAError('Malformed A-label, no Punycode eligible content found')
         if label.decode('ascii')[-1] == '-':
             raise IDNAError('A-label must not end with a hyphen')
     else:


Test patch:
diff --git a/tests/test_idna.py b/tests/test_idna.py
index b62b9c6..9e1d2d9 100755
--- a/tests/test_idna.py
+++ b/tests/test_idna.py
@@ -258,6 +258,7 @@ class IDNATests(unittest.TestCase):
                          '\u0521\u0525\u0523-\u0523\u0523-----\u0521\u0523\u0523\u0523.aa')
         self.assertRaises(idna.IDNAError, idna.decode, 'XN---------90GGLBAGAAC.AA')
         self.assertRaises(idna.IDNAError, idna.decode, 'xn---------90gglbagaac.aa')
+        self.assertRaises(idna.IDNAError, idna.decode, 'xn--')
 
 if __name__ == '__main__':
     unittest.main()