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

socket module setattr #826

Closed
bstaint opened this issue Jan 6, 2017 · 6 comments
Closed

socket module setattr #826

bstaint opened this issue Jan 6, 2017 · 6 comments

Comments

@bstaint
Copy link
Contributor

bstaint commented Jan 6, 2017

in dev branch

import jedi                                                                                                                                                                                             
for c in jedi.Script("import socket;s = socket.socket(); s.c").completions():                        
    print c 

can't completion such as connect, i try traced in socket.py.

_socketmethods = (
    'bind', 'connect', 'connect_ex', 'fileno', 'listen',
    'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
    'sendall', 'setblocking',
    'settimeout', 'gettimeout', 'shutdown')

//...

for _m in _socketmethods:
    p = partial(meth,_m)
    p.__name__ = _m
    p.__doc__ = getattr(_realsocket,_m).__doc__
    m = MethodType(p,None,_socketobject)
    setattr(_socketobject,_m,m)

set auto_import_modules

auto_import_modules = [
    'hashlib',  # setattr
    'socket'
]

but param is empty.

import jedi

print jedi.Script("import socket;s = socket.socket(); s.connect()", 1, 45).call_signatures()

I try patched it

diff -urN a/jedi-dev/jedi/evaluate/instance.py b/jedi-dev/jedi/evaluate/instance.py
--- a/jedi-dev/jedi/evaluate/instance.py	2017-01-05 15:08:00.000000000 +0800
+++ b/jedi-dev/jedi/evaluate/instance.py	2017-01-06 19:26:55.123356400 +0800
@@ -271,7 +271,8 @@
             func.evaluator, func.obj, func.parent_context, func.tree_node)
 
     def get_param_names(self):
-        return list(super(CompiledBoundMethod, self).get_param_names())[1:]
+        params = list(super(CompiledBoundMethod, self).get_param_names())
+        return params[1:] if len(params) > 1 else params
 
 
 class InstanceNameDefinition(filters.TreeNameDefinition):
diff -urN a/jedi-dev/jedi/settings.py b/jedi-dev/jedi/settings.py
--- a/jedi-dev/jedi/settings.py	2017-01-05 15:08:00.000000000 +0800
+++ b/jedi-dev/jedi/settings.py	2017-01-06 19:21:11.773717900 +0800
@@ -170,6 +170,7 @@
 
 auto_import_modules = [
     'hashlib',  # setattr
+    'socket',
 ]
 """
 Modules that are not analyzed but imported, although they contain Python code.

have problem?

@bstaint
Copy link
Contributor Author

bstaint commented Jan 8, 2017

fixed empty param function.

print jedi.Script("import socket;s = socket.socket(); s.accept()", 1, 44).call_signatures()

patch:

diff -urN a/bundle/jedi-vim/jedi/evaluate/compiled/__init__.py b/bundle/jedi-vim/jedi/evaluate/compiled/__init__.py
--- a/bundle/jedi-vim/jedi/evaluate/compiled/__init__.py	2017-01-07 18:57:34.000000000 +0800
+++ b/bundle/jedi-vim/jedi/evaluate/compiled/__init__.py	2017-01-08 21:34:18.499759500 +0800
@@ -122,7 +122,9 @@
             parts = p.strip().split('=')
             if len(parts) > 1:
                 parts.insert(1, Operator('=', (0, 0)))
-            yield UnresolvableParamName(self, parts[0])
+
+            if parts[0]:
+                yield UnresolvableParamName(self, parts[0])
 
     def __repr__(self):
         return '<%s: %s>' % (self.__class__.__name__, repr(self.obj))

@davidhalter
Copy link
Owner

What Python version are you on? I'm getting decent completions on Python 3, but I'm not sure about Python 2.

@bstaint
Copy link
Contributor Author

bstaint commented Jan 9, 2017

python 2.7

in jedi/evaluate/compiled/__init__.py line 119
python3: <method 'connect' of '_socket.socket' objects>
python2: <unbound method _socketobject.connect>

@davidhalter
Copy link
Owner

I think it will be too complicated to get it working for 2.7. I'm closing for now. Use the new Python, it's awesome :)

@micbou
Copy link
Contributor

micbou commented Apr 6, 2017

@davidhalter Would you accept a PR that adds the socket module to the auto_import_modules option (like the hashlib module) but only on Python 2? Several methods from the socket module are defined with setattr on Python 2 so according to the docs, it would make sense to add this module.

@davidhalter
Copy link
Owner

No. Dump your legacy Python and enjoy the brave new world ;-)

However, the actual reason is that I don't want to maintain that. #385 is pretty much the next thing I'm going to do and it would be one more thing that I'd have to migrate. So at least wait until that is done.

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

No branches or pull requests

3 participants