Skip to content

Conversation

@amake
Copy link

@amake amake commented Apr 22, 2015

When loading httplib2 in Jython 2.7 you get an error:

>>> import httplib2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "testenv/lib/python2.7/site-packages/httplib2/__init__.py", line 59, in <module>
    from httplib2 import socks
  File "testenv/lib/python2.7/site-packages/httplib2/__init__.py", line 59, in <module>
    from httplib2 import socks
  File "testenv/lib/python2.7/site-packages/httplib2/socks.py", line 116, in <module>
    class socksocket(socket.socket):
TypeError: Error when calling the metaclass bases
    function() argument 1 must be code, not str

In Jython 2.7b3 and later, socket.socket is defined as a function like this:

def socket(family=None, type=None, proto=None):
    return _socketobject(family, type, proto)

...

class _socketobject(object):
...

Thus it seems the only way to extend this class without knowing about Jython library internals is to instantiate and get the class at runtime.

I don't know if this is the best solution, but this patch works for me with Jython 2.7rc2 and Python 2.7.9, tested with the Google Drive API.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is a good idea. How about to have the following code just after the getattr check?

if hasattr(socket.socket.__class__, 'func_name'):
    # Jython's socket.socket is a function which wraps a class constructor.
    socket.socket = socket.socket().__class__

@tmatsuo
Copy link

tmatsuo commented Sep 22, 2015

I looked at Jython's _socket.py, then I started to doubt that it (the proxy code) works with this approach. However, now we can not actually import this module, so I would just put the following code at the top for now

if hasattr(socket.socket.__class__, 'func_name'):
    # Jython's socket.socket is not compatible with cython's, so we can not use it. 
    raise ImportError('incompatible socket.socket detected, proxy support unusable')

At least, you can import httplib2 with this change, although proxy support is not there.

What do you think?

@amake
Copy link
Author

amake commented Sep 24, 2015

Thanks for taking a look.

I personally don't need proxy support, so I'm happy with any solution that lets me safely import httplib2 and use whatever functionality is compatible.

@mbettan
Copy link

mbettan commented Oct 4, 2015

@tmatsuo @amake - It seems that from socket import socket is still mandatory. Can we add it to the release?

mbettan pushed a commit to mbettan/httplib2 that referenced this pull request Oct 4, 2015
Relating to issue jcgregorio#300
0.9.2 release didn't fix Jython compatibility issue

Proposing fix : ```from socket import socket``` instead of import socket
@mbettan
Copy link

mbettan commented Dec 16, 2015

Any updates @tmatsuo @amake ?

@amake amake closed this Jul 19, 2018
@amake amake deleted the jython27 branch July 19, 2018 07:55
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.

3 participants