Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeError at import time #18

Closed
wants to merge 2 commits into from
Closed

Conversation

rsommer
Copy link

@rsommer rsommer commented Jul 3, 2015

Commit python/cpython@562385d introduced _METHODS_EXPECTING_BODY at module level in httplib.py. The code in gruvi.http failes at import time with TypeError: unhashable type: 'set' on python 2.7.10 which includes this commit. Ignoring _METHODS_EXPECTING_BODY in the loop fixes the error, but I'm not sure if it has any side effects. The proposed change is more an example than a real solution i think.

Python 2.7.10 (default, May 28 2015, 16:39:33) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gruvi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "gruvi/__init__.py", line 37, in <module>
    from .http import *
  File "gruvi/http.py", line 78, in <module>
    if name.isupper() and value in http_client.responses:
TypeError: unhashable type: 'set'

Commit python/cpython@562385d introduced _METHODS_EXPECTING_BODY at module level in httplib.py. The code in `gruvi.http` failes at import time with `TypeError: unhashable type: 'set'` on python 2.7.10 which includes this commit. Ignoring _METHODS_EXPECTING_BODY in the loop fixes the error, but I'm not sure if it has any side effects. The proposed change is more an example than a real solution i think.

```
Python 2.7.10 (default, May 28 2015, 16:39:33) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gruvi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "gruvi/__init__.py", line 37, in <module>
    from .http import *
  File "gruvi/http.py", line 78, in <module>
    if name.isupper() and value in http_client.responses:
TypeError: unhashable type: 'set'
```
@geertj
Copy link
Owner

geertj commented Jul 3, 2015

Thnaks @rsommer! I would prefer the following patch that doesn't hardcode the attribute name:

diff --git a/gruvi/http.py b/gruvi/http.py
index b4bbc45..87bf36d 100644
--- a/gruvi/http.py
+++ b/gruvi/http.py
@@ -75,7 +75,9 @@ __all__ = ['HttpError', 'HttpRequest', 'HttpResponse', 'HttpProtocol',
 # Export some definitions from  http.client.
 for name in dir(http_client):
     value = getattr(http_client, name)
-    if name.isupper() and value in http_client.responses:
+    if not name.isupper() or not isinstance(value, int):
+        return
+    if value in http_client.responses:
         globals()[name] = value

 HTTP_PORT = http_client.HTTP_PORT

I don't have the Python version you mention. Can you check if this works?

@rsommer
Copy link
Author

rsommer commented Jul 3, 2015

I think you meant to use continue, not return? Seems to works with continue ...

diff --git a/gruvi/http.py b/gruvi/http.py
index b4bbc45..4ce1246 100644
--- a/gruvi/http.py
+++ b/gruvi/http.py
@@ -75,7 +75,10 @@ __all__ = ['HttpError', 'HttpRequest', 'HttpResponse', 'HttpProtocol',
 # Export some definitions from  http.client.
 for name in dir(http_client):
     value = getattr(http_client, name)
-    if name.isupper() and value in http_client.responses:
+    if not name.isupper() or not isinstance(value, int):
+        continue
+
+    if value in http_client.responses:
         globals()[name] = value

 HTTP_PORT = http_client.HTTP_PORT

As discussed, incorporated the proposed fix from geertj.
@geertj
Copy link
Owner

geertj commented Jul 3, 2015

arg.. Yes sorry, that needs to be continue :)

@geertj
Copy link
Owner

geertj commented Jul 3, 2015

Fixed by 5c3a741.

@geertj geertj closed this Jul 3, 2015
@geertj
Copy link
Owner

geertj commented Jul 3, 2015

@rsommer thanks!

@rsommer rsommer deleted the patch-1 branch July 3, 2015 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants