Commit 188ad694 authored by Elizaveta Shashkova's avatar Elizaveta Shashkova Committed by Andrey Vlasovskikh
Browse files

Updates from PyDev: Fix for case when python-future is installed and provides...

Updates from PyDev: Fix for case when python-future is installed and provides a dummy 'builtins' module even on Python 2. #PyDev-845

(cherry picked from commit 11482b4b)
parent fb02927c
Showing with 84 additions and 95 deletions
+84 -95
import pydevconsole
import sys
try:
if sys.version_info[0] >= 3:
import builtins as __builtin__ # Py3
else:
import __builtin__
except ImportError:
import builtins as __builtin__
try:
import java.lang #@UnusedImport
......@@ -22,23 +23,23 @@ dir2 = _pydev_imports_tipper.generate_imports_tip_for_module
#=======================================================================================================================
class _StartsWithFilter:
'''
Used because we can't create a lambda that'll use an outer scope in jython 2.1
Used because we can't create a lambda that'll use an outer scope in jython 2.1
'''
def __init__(self, start_with):
self.start_with = start_with.lower()
def __call__(self, name):
return name.lower().startswith(self.start_with)
#=======================================================================================================================
# Completer
#
# This class was gotten from IPython.completer (dir2 was replaced with the completer already in pydev)
# This class was gotten from IPython.completer (dir2 was replaced with the completer already in pydev)
#=======================================================================================================================
class Completer:
def __init__(self, namespace=None, global_namespace=None):
"""Create a new completer for the command line.
......@@ -84,7 +85,7 @@ class Completer:
#In pydev this option should never be used
raise RuntimeError('Namespace must be provided!')
self.namespace = __main__.__dict__ #@UndefinedVariable
if "." in text:
return self.attr_matches(text)
else:
......@@ -97,18 +98,18 @@ class Completer:
defined in self.namespace or self.global_namespace that match.
"""
def get_item(obj, attr):
return obj[attr]
a = {}
for dict_with_comps in [__builtin__.__dict__, self.namespace, self.global_namespace]: #@UndefinedVariable
a.update(dict_with_comps)
filter = _StartsWithFilter(text)
return dir2(a, a.keys(), get_item, filter)
def attr_matches(self, text):
......@@ -131,7 +132,7 @@ class Completer:
if not m:
return []
expr, attr = m.group(1, 3)
try:
obj = eval(expr, self.namespace)
......@@ -146,8 +147,8 @@ class Completer:
words = dir2(obj, filter=filter)
return words
#=======================================================================================================================
# generate_completions_as_xml
#=======================================================================================================================
......
......@@ -25,10 +25,10 @@ class ImportHookManager(ModuleType):
sys.stderr.write("Matplotlib support failed\n")
return module
try:
if sys.version_info[0] >= 3:
import builtins # py3
else:
import __builtin__ as builtins
except ImportError:
import builtins
import_hook_manager = ImportHookManager(__name__ + '.import_hook', builtins.__import__)
builtins.__import__ = import_hook_manager.do_import
......
......@@ -101,10 +101,12 @@ def _patch_import_to_patch_pyqt_on_import(patch_qt_on_import, get_qt_core_module
_internal_patch_qt(get_qt_core_module()) # Patch it only when the user would import the qt module
return original_import(name, *args, **kwargs)
try:
import builtins
except ImportError:
import sys
if sys.version_info[0] >= 3:
import builtins # Py3
else:
import __builtin__ as builtins
builtins.__import__ = patched_import
......
......@@ -29,10 +29,10 @@ def patched_reload(orig_reload):
def patch_reload():
try:
if sys.version_info[0] >= 3:
import builtins # Py3
else:
import __builtin__ as builtins
except ImportError:
import builtins
if hasattr(builtins, "reload"):
sys.builtin_orig_reload = builtins.reload
......@@ -56,10 +56,10 @@ def patch_reload():
def cancel_patches_in_sys_module():
sys.exc_info = sys.system_exc_info # @UndefinedVariable
try:
if sys.version_info[0] >= 3:
import builtins # Py3
else:
import __builtin__ as builtins
except ImportError:
import builtins
if hasattr(sys, "builtin_orig_reload"):
builtins.reload = sys.builtin_orig_reload
......
'''For debug purpose we are replacing actual builtin property by the debug property
'''
from _pydevd_bundle.pydevd_comm import get_global_debugger
from _pydevd_bundle.pydevd_constants import DebugInfoHolder, IS_PY3K
from _pydevd_bundle.pydevd_constants import DebugInfoHolder, IS_PY2
from _pydevd_bundle import pydevd_tracing
#=======================================================================================================================
......@@ -11,7 +11,7 @@ def replace_builtin_property(new_property=None):
if new_property is None:
new_property = DebugProperty
original = property
if not IS_PY3K:
if IS_PY2:
try:
import __builtin__
__builtin__.__dict__['property'] = new_property
......
......@@ -33,11 +33,10 @@ except: # ImportError or AttributeError.
return a + '/' + b
IS_PYTHON_3K = 0
IS_PYTHON_3_ONWARDS = 0
try:
if sys.version_info[0] == 3:
IS_PYTHON_3K = 1
IS_PYTHON_3_ONWARDS = sys.version_info[0] >= 3
except:
# That's OK, not all versions of python have sys.version_info
pass
......@@ -116,7 +115,7 @@ def getfilesystemencoding():
file_system_encoding = getfilesystemencoding()
if IS_PYTHON_3K:
if IS_PYTHON_3_ONWARDS:
unicode_type = str
bytes_type = bytes
......@@ -246,7 +245,7 @@ if __name__ == '__main__':
contents.append(tounicode('</xml>'))
unic = tounicode('\n').join(contents)
inasciixml = toasciimxl(unic)
if IS_PYTHON_3K:
if IS_PYTHON_3_ONWARDS:
# This is the 'official' way of writing binary output in Py3K (see: http://bugs.python.org/issue4571)
sys.stdout.buffer.write(inasciixml)
else:
......
......@@ -3,12 +3,13 @@ Entry-point module to start the code-completion server for PyDev.
@author Fabio Zadrozny
'''
IS_PYTHON3K = 0
try:
import sys
IS_PYTHON_3_ONWARDS = sys.version_info[0] >= 3
if not IS_PYTHON_3_ONWARDS:
import __builtin__
except ImportError:
else:
import builtins as __builtin__ # Python 3.0
IS_PYTHON3K = 1
from _pydevd_bundle.pydevd_constants import IS_JYTHON
......@@ -230,7 +231,7 @@ class CompletionServer:
#Older versions (jython 2.1)
self.emulated_sendall(msg)
else:
if IS_PYTHON3K:
if IS_PYTHON_3_ONWARDS:
self.socket.sendall(bytearray(msg, 'utf-8'))
else:
self.socket.sendall(msg)
......@@ -256,7 +257,7 @@ class CompletionServer:
received = self.socket.recv(BUFFER_SIZE)
if len(received) == 0:
raise Exit() # ok, connection ended
if IS_PYTHON3K:
if IS_PYTHON_3_ONWARDS:
data = data + received.decode('utf-8')
else:
data = data + received
......@@ -354,8 +355,8 @@ class CompletionServer:
try:
self.send(msg)
except socket.error:
pass # Ok, may be closed already
pass # Ok, may be closed already
raise e # raise original error.
except:
......@@ -369,7 +370,7 @@ class CompletionServer:
try:
self.send(msg)
except socket.error:
pass # Ok, may be closed already
pass # Ok, may be closed already
finally:
......
......@@ -3,9 +3,9 @@
- change the input() and raw_input() commands to change \r\n or \r into \n
- execute the user site customize -- if available
- change raw_input() and input() to also remove any trailing \r
Up to PyDev 3.4 it also was setting the default encoding, but it was removed because of differences when
running from a shell (i.e.: now we just set the PYTHONIOENCODING related to that -- which is properly
running from a shell (i.e.: now we just set the PYTHONIOENCODING related to that -- which is properly
treated on Py 2.7 onwards).
'''
DEBUG = 0 #0 or 1 because of jython
......@@ -13,20 +13,18 @@ DEBUG = 0 #0 or 1 because of jython
import sys
encoding = None
IS_PYTHON_3K = 0
IS_PYTHON_3_ONWARDS = 0
try:
if sys.version_info[0] == 3:
IS_PYTHON_3K = 1
IS_PYTHON_3_ONWARDS = sys.version_info[0] >= 3
except:
#That's OK, not all versions of python have sys.version_info
if DEBUG:
import traceback;traceback.print_exc() #@Reimport
#-----------------------------------------------------------------------------------------------------------------------
#Line buffering
if IS_PYTHON_3K:
if IS_PYTHON_3_ONWARDS:
#Python 3 has a bug (http://bugs.python.org/issue4705) in which -u doesn't properly make output/input unbuffered
#so, we need to enable that ourselves here.
try:
......@@ -41,8 +39,8 @@ if IS_PYTHON_3K:
sys.stdin._line_buffering = True
except:
pass
try:
import org.python.core.PyDictionary #@UnresolvedImport @UnusedImport -- just to check if it could be valid
def dict_contains(d, key):
......@@ -59,7 +57,7 @@ except:
return d.has_key(key)
#-----------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------
#now that we've finished the needed pydev sitecustomize, let's run the default one (if available)
#Ok, some weirdness going on in Python 3k: when removing this module from the sys.module to import the 'real'
......@@ -82,7 +80,7 @@ try:
if c.find('pydev_sitecustomize') == -1:
#We'll re-add any paths removed but the pydev_sitecustomize we added from pydev.
paths_removed.append(c)
if dict_contains(sys.modules, 'sitecustomize'):
del sys.modules['sitecustomize'] #this module
except:
......@@ -95,11 +93,11 @@ else:
sitecustomize.__pydev_sitecustomize_module__ = __pydev_sitecustomize_module__
except:
pass
if not dict_contains(sys.modules, 'sitecustomize'):
#If there was no sitecustomize, re-add the pydev sitecustomize (pypy gives a KeyError if it's not there)
sys.modules['sitecustomize'] = __pydev_sitecustomize_module__
try:
if paths_removed:
if sys is None:
......@@ -115,41 +113,41 @@ else:
if not IS_PYTHON_3K:
if sys.version_info[0] < 3:
try:
#Redefine input and raw_input only after the original sitecustomize was executed
#(because otherwise, the original raw_input and input would still not be defined)
import __builtin__
original_raw_input = __builtin__.raw_input
original_input = __builtin__.input
def raw_input(prompt=''):
#the original raw_input would only remove a trailing \n, so, at
#this point if we had a \r\n the \r would remain (which is valid for eclipse)
#so, let's remove the remaining \r which python didn't expect.
ret = original_raw_input(prompt)
if ret.endswith('\r'):
return ret[:-1]
return ret
raw_input.__doc__ = original_raw_input.__doc__
def input(prompt=''):
#input must also be rebinded for using the new raw_input defined
return eval(raw_input(prompt))
input.__doc__ = original_input.__doc__
__builtin__.raw_input = raw_input
__builtin__.input = input
except:
#Don't report errors at this stage
if DEBUG:
import traceback;traceback.print_exc() #@Reimport
else:
try:
import builtins #Python 3.0 does not have the __builtin__ module @UnresolvedImport
......@@ -159,10 +157,10 @@ else:
#this point if we had a \r\n the \r would remain (which is valid for eclipse)
#so, let's remove the remaining \r which python didn't expect.
ret = original_input(prompt)
if ret.endswith('\r'):
return ret[:-1]
return ret
input.__doc__ = original_input.__doc__
builtins.input = input
......@@ -170,7 +168,7 @@ else:
#Don't report errors at this stage
if DEBUG:
import traceback;traceback.print_exc() #@Reimport
try:
......@@ -192,7 +190,7 @@ try:
if hasattr(getpass, 'GetPassWarning'):
warnings.simplefilter("ignore", category=getpass.GetPassWarning)
fix_get_pass()
except:
#Don't report errors at this stage
if DEBUG:
......
......@@ -34,18 +34,8 @@ except:
from _pydev_bundle.pydev_console_utils import BaseInterpreterInterface, BaseStdIn
from _pydev_bundle.pydev_console_utils import CodeFragment
IS_PYTHON_3K = False
IS_PY24 = False
try:
if sys.version_info[0] == 3:
IS_PYTHON_3K = True
elif sys.version_info[0] == 2 and sys.version_info[1] == 4:
IS_PY24 = True
except:
#That's OK, not all versions of python have sys.version_info
pass
IS_PYTHON_3_ONWARDS = sys.version_info[0] >= 3
IS_PY24 = sys.version_info[0] == 2 and sys.version_info[1] == 4
class Command:
def __init__(self, interpreter, code_fragment):
......@@ -84,14 +74,13 @@ except:
# Pull in runfile, the interface to UMD that wraps execfile
from _pydev_bundle.pydev_umd import runfile, _set_globals_function
try:
if sys.version_info[0] >= 3:
import builtins # @UnresolvedImport
builtins.runfile = runfile
except:
else:
import __builtin__
__builtin__.runfile = runfile
#=======================================================================================================================
# InterpreterInterface
#=======================================================================================================================
......
......@@ -8,7 +8,7 @@ from _pydev_imps._pydev_saved_modules import thread
start_new_thread = thread.start_new_thread
IS_PYTHON_3K = 0
IS_PYTHON_3_ONWARDS = sys.version_info[0] >= 3
IS_JYTHON = sys.platform.find('java') != -1
try:
......@@ -21,12 +21,11 @@ except ImportError:
if not IS_JYTHON:
import pycompletionserver
import socket
try:
if not IS_PYTHON_3_ONWARDS:
from urllib import quote_plus, unquote_plus
def send(s, msg):
s.send(msg)
except ImportError:
IS_PYTHON_3K = 1
else:
from urllib.parse import quote_plus, unquote_plus #Python 3.0
def send(s, msg):
s.send(bytearray(msg, 'utf-8'))
......@@ -76,7 +75,7 @@ class TestCPython(unittest.TestCase):
msg = ''
while finish == False:
m = self.socket.recv(1024 * 4)
if IS_PYTHON_3K:
if IS_PYTHON_3_ONWARDS:
m = m.decode('utf-8')
if m.startswith('@@PROCESSING'):
sys.stdout.write('Status msg: %s\n' % (msg,))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment