Permalink
Browse files

Python 3 imports: try importing Python 3 version before resorting to …

…using Python 2 names (PR #8606)

Attempt to import python 2 module name first, and then import Python 3 version for performance considerations. This can be changed for the first Python 3 release.

* Python 3: import _winreg > winreg. Re #8527.

Python 3 introduces winreg - just _winreg without the leading underscore. Thus use the new import form:
>>> try:
>>>  import winreg
>>> except ImportError:
>>>  import _winreg as winreg
Note: spaces were used for this fragment.
This change allows NVDA to work with Python 2 or 3 version of winreg module. Also, code that uses (_)winreg module has been modified to call winreg.something.

* Queue handler: import Queue -> queue. Re #8527.

Python 3 ships with queue (lowercase queue). This module is used in three places: queue handler, JaB handler, and Espeak module. For this commit, queue handler has been modified to import Python 3's queue first before resorting to loading Queue.queue (Python 2). JAB and Espeak will be done on a separate commit due to extensive edits required (these use Python 2 module directly).

* Espeak internal module: import Queue > queue. Re #8527.

Python 3 renames Queue to queue, and internal Espeak module imports this directly. Thus try loading Python 3 version first before resorting to useing Python 2 name.

* JAB handler: import Queue > queue. re #8527.

* App modules/aliases: use relative imports of the form 'from .appmod import *'. Re #8649.

* Python 3/imports: use relative import of the form 'from .mod import something'. Re #8590.
  • Loading branch information...
josephsl authored and feerrenrut committed Aug 22, 2018
1 parent ec3eeee commit 8e2df9fdec6a5a3d9f47ee9290d9584dacf7bcc9
Showing with 191 additions and 145 deletions.
  1. +6 −3 site_scons/site_tools/doxygen.py
  2. +5 −2 source/JABHandler.py
  3. +5 −2 source/NVDAHelper.py
  4. +1 −1 source/NVDAObjects/inputComposition.py
  5. +1 −1 source/NVDAObjects/window/akelEdit.py
  6. +1 −1 source/NVDAObjects/window/excel.py
  7. +1 −1 source/appModules/digitaleditionspreview.py
  8. +1 −1 source/appModules/esybraille.py
  9. +1 −1 source/appModules/hxoutlook.py
  10. +1 −1 source/appModules/miranda64.py
  11. +1 −1 source/appModules/mpc-hc.py
  12. +1 −2 source/appModules/mpc-hc64.py
  13. +1 −1 source/appModules/sts.py
  14. +1 −1 source/appModules/teamtalk3.py
  15. +1 −1 source/appModules/totalcmd64.py
  16. +1 −1 source/appModules/win32calc.py
  17. +1 −1 source/appModules/winmail.py
  18. +1 −1 source/appModules/wlmail.py
  19. +1 −1 source/appModules/zend-eclipse-php.py
  20. +1 −1 source/appModules/zendstudio.py
  21. +7 −4 source/brailleDisplayDrivers/papenmeier.py
  22. +23 −20 source/config/__init__.py
  23. +15 −12 source/easeOfAccess.py
  24. +1 −1 source/extensionPoints/__init__.py
  25. +4 −4 source/gui/__init__.py
  26. +1 −1 source/gui/configProfiles.py
  27. +2 −2 source/gui/settingsDialogs.py
  28. +4 −1 source/hwPortUtils.py
  29. +53 −51 source/installer.py
  30. +4 −1 source/mathType.py
  31. +6 −3 source/nvda.pyw
  32. +5 −2 source/queueHandler.py
  33. +6 −3 source/synthDrivers/_espeak.py
  34. +6 −3 source/synthDrivers/audiologic.py
  35. +1 −1 source/synthDrivers/espeak.py
  36. +1 −1 source/synthDrivers/mssp.py
  37. +8 −5 source/synthDrivers/oneCore.py
  38. +6 −3 source/synthDrivers/sapi4.py
  39. +5 −2 source/synthDrivers/sapi5.py
@@ -23,12 +23,15 @@
import os.path
import glob
from fnmatch import fnmatch
import _winreg
try:
import _winreg as winreg # Python 2.7 import
except:
import winreg # python 3 import
def fetchDoxygenPath():
try:
with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\doxygen_is1", 0, _winreg.KEY_READ | _winreg.KEY_WOW64_64KEY) as doxygenKey:
doxygenPath= '"%s"'%os.path.join(_winreg.QueryValueEx(doxygenKey, "InstallLocation")[0], "Bin", "doxygen.exe")
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\doxygen_is1", 0, winreg.KEY_READ | winreg.KEY_WOW64_64KEY) as doxygenKey:
doxygenPath= '"%s"'%os.path.join(winreg.QueryValueEx(doxygenKey, "InstallLocation")[0], "Bin", "doxygen.exe")
except WindowsError:
return 'doxygen'
return doxygenPath
@@ -5,7 +5,10 @@
#This file is covered by the GNU General Public License.
#See the file COPYING for more details.
import Queue
try:
import Queue as queue # Python 2.7 import
except ImportError:
import queue # Python 3 import
from ctypes import *
from ctypes.wintypes import *
import time
@@ -292,7 +295,7 @@ class AccessibleKeyBindings(Structure):
# the issue, we use this cache as a fallback when either getTopLevelObject or
# getHWNDFromAccessibleContext fails.
vmIDsToWindowHandles={}
internalFunctionQueue=Queue.Queue(1000)
internalFunctionQueue=queue.Queue(1000)
internalFunctionQueue.__name__="JABHandler.internalFunctionQueue"
def internalQueueFunction(func,*args,**kwargs):
@@ -6,7 +6,10 @@
import os
import sys
import _winreg
try:
import _winreg as winreg # Python 2.7 import
except ImportError:
import winreg # Python 3 import
import msvcrt
import versionInfo
import winKernel
@@ -78,7 +81,7 @@ def _lookupKeyboardLayoutNameWithHexString(layoutString):
buf=create_unicode_buffer(1024)
bufSize=c_int(2048)
key=HKEY()
if windll.advapi32.RegOpenKeyExW(_winreg.HKEY_LOCAL_MACHINE,u"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\"+ layoutString,0,_winreg.KEY_QUERY_VALUE,byref(key))==0:
if windll.advapi32.RegOpenKeyExW(winreg.HKEY_LOCAL_MACHINE,u"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\"+ layoutString,0,winreg.KEY_QUERY_VALUE,byref(key))==0:
try:
if windll.advapi32.RegQueryValueExW(key,u"Layout Display Name",0,None,buf,byref(bufSize))==0:
windll.shlwapi.SHLoadIndirectString(buf.value,buf,1023,None)
@@ -5,7 +5,7 @@
import speech
import config
from NVDAObjects.window import Window
from behaviors import EditableTextWithAutoSelectDetection, CandidateItem as CandidateItemBehavior
from .behaviors import EditableTextWithAutoSelectDetection, CandidateItem as CandidateItemBehavior
from textInfos.offsets import OffsetsTextInfo
def calculateInsertedChars(oldComp,newComp):
@@ -3,7 +3,7 @@
#This file is covered by the GNU General Public License.
#See the file COPYING for more details.
import edit
from . import edit
import winUser
import winKernel
import ctypes
@@ -199,7 +199,7 @@
xlPatternRectangularGradient:_("rectangular gradient"),
}
from excelCellBorder import getCellBorderStyleDescription
from .excelCellBorder import getCellBorderStyleDescription
re_RC=re.compile(r'R(?:\[(\d+)\])?C(?:\[(\d+)\])?')
re_absRC=re.compile(r'^R(\d+)C(\d+)(?::R(\d+)C(\d+))?$')
@@ -4,4 +4,4 @@
#See the file COPYING for more details.
#Copyright (C) 2012 NV Access Limited
from digitaleditions import *
from .digitaleditions import *
@@ -8,4 +8,4 @@
This imports the esysuite appModule,
"""
from esysuite import *
from .esysuite import *
@@ -4,4 +4,4 @@
#See the file COPYING for more details.
# An alias for hxmail appModule
from hxmail import *
from .hxmail import *
@@ -2,4 +2,4 @@
This simply uses the miranda32 app module.
"""
from miranda32 import *
from .miranda32 import *
@@ -1 +1 @@
from mplayerc import AppModule
from .mplayerc import AppModule
@@ -4,5 +4,4 @@
# This file may be used under the terms of the GNU General Public License, version 2 or later.
# For more details see: https://www.gnu.org/licenses/gpl-2.0.html
from mplayerc import *
from .mplayerc import *
@@ -2,4 +2,4 @@
This simply uses the app module for Eclipse.
"""
from eclipse import *
from .eclipse import *
@@ -1 +1 @@
from teamtalk4classic import AppModule
from .teamtalk4classic import AppModule
@@ -1,2 +1,2 @@
#Use the app module for Total Commander 32 bit version.
from totalcmd import *
from .totalcmd import *
@@ -7,4 +7,4 @@
"""App module for Windows Calculator (desktop version) for Windows 10 LTSB (Long-Term Servicing Branch) and Server, only difference being executable name.
"""
from calc import *
from .calc import *
@@ -1,2 +1,2 @@
#Uses the Outlook Express app module
from msimn import *
from .msimn import *
@@ -10,7 +10,7 @@
import winUser
from keyboardHandler import KeyboardInputGesture
from NVDAObjects.IAccessible.MSHTML import MSHTML
import msimn
from . import msimn
class AboutBlankDocument(MSHTML):
"""A document called about:blank which hosts the HTML message composer document using viewlink.
@@ -2,4 +2,4 @@
This simply uses the app module for Eclipse.
"""
from eclipse import *
from .eclipse import *
@@ -2,4 +2,4 @@
This simply uses the app module for Eclipse.
"""
from eclipse import *
from .eclipse import *
@@ -27,7 +27,10 @@
#for brxcom
import ctypes as c
import _winreg
try:
import _winreg as winreg # Python 2.7 import
except ImportError:
import winreg # Python 3 import
import winUser
#for scripting
@@ -206,9 +209,9 @@ def check(cls):
def connectBrxCom(self):#connect to brxcom server (provided by papenmeier)
try:
brxcomkey=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\\FHP\\BrxCom")
value, vtype = _winreg.QueryValueEx(brxcomkey, "InstallPath")
_winreg.CloseKey(brxcomkey)
brxcomkey=winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\\FHP\\BrxCom")
value, vtype = winreg.QueryValueEx(brxcomkey, "InstallPath")
winreg.CloseKey(brxcomkey)
self._brxnvda = c.cdll.LoadLibrary(str(value+"\\brxnvda.dll"))
if(self._brxnvda.brxnvda_init(str(value+"\\BrxCom.dll").decode("mbcs"))==0):
self._baud=1 #prevent bluetooth from connecting
@@ -12,7 +12,10 @@
"""
import globalVars
import _winreg
try:
import _winreg as winreg # Python 2.7 import
except ImportError:
import winreg # Python 3 import
import ctypes
import ctypes.wintypes
import os
@@ -32,7 +35,7 @@
from fileUtils import FaultTolerantFile
import winKernel
import extensionPoints
import profileUpgrader
from . import profileUpgrader
from .configSpec import confspec
#: True if NVDA is running as a Windows Store Desktop Bridge application
@@ -76,11 +79,11 @@ def saveOnExit():
def isInstalledCopy():
"""Checks to see if this running copy of NVDA is installed on the system"""
try:
k=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NVDA")
instDir=_winreg.QueryValueEx(k,"UninstallDirectory")[0]
k=winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NVDA")
instDir=winreg.QueryValueEx(k,"UninstallDirectory")[0]
except WindowsError:
return False
_winreg.CloseKey(k)
winreg.CloseKey(k)
try:
return os.stat(instDir)==os.stat(os.getcwdu())
except WindowsError:
@@ -98,8 +101,8 @@ def isInstalledCopy():
def getInstalledUserConfigPath():
try:
k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, NVDA_REGKEY)
configInLocalAppData = bool(_winreg.QueryValueEx(k, CONFIG_IN_LOCAL_APPDATA_SUBKEY)[0])
k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, NVDA_REGKEY)
configInLocalAppData = bool(winreg.QueryValueEx(k, CONFIG_IN_LOCAL_APPDATA_SUBKEY)[0])
except WindowsError:
configInLocalAppData=False
configParent=shlobj.SHGetFolderPath(0, shlobj.CSIDL_LOCAL_APPDATA if configInLocalAppData else shlobj.CSIDL_APPDATA)
@@ -156,11 +159,11 @@ def initConfigPath(configPath=None):
def getStartAfterLogon():
if (easeOfAccess.isSupported and easeOfAccess.canConfigTerminateOnDesktopSwitch
and easeOfAccess.willAutoStart(_winreg.HKEY_CURRENT_USER)):
and easeOfAccess.willAutoStart(winreg.HKEY_CURRENT_USER)):
return True
try:
k = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, RUN_REGKEY)
val = _winreg.QueryValueEx(k, u"nvda")[0]
k = winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_REGKEY)
val = winreg.QueryValueEx(k, u"nvda")[0]
return os.stat(val) == os.stat(sys.argv[0])
except (WindowsError, OSError):
return False
@@ -169,20 +172,20 @@ def setStartAfterLogon(enable):
if getStartAfterLogon() == enable:
return
if easeOfAccess.isSupported and easeOfAccess.canConfigTerminateOnDesktopSwitch:
easeOfAccess.setAutoStart(_winreg.HKEY_CURRENT_USER, enable)
easeOfAccess.setAutoStart(winreg.HKEY_CURRENT_USER, enable)
if enable:
return
# We're disabling, so ensure the run key is cleared,
# as it might have been set by an old version.
run = False
else:
run = enable
k = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, RUN_REGKEY, 0, _winreg.KEY_WRITE)
k = winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_REGKEY, 0, winreg.KEY_WRITE)
if run:
_winreg.SetValueEx(k, u"nvda", None, _winreg.REG_SZ, sys.argv[0])
winreg.SetValueEx(k, u"nvda", None, winreg.REG_SZ, sys.argv[0])
else:
try:
_winreg.DeleteValue(k, u"nvda")
winreg.DeleteValue(k, u"nvda")
except WindowsError:
pass
@@ -223,22 +226,22 @@ def execElevated(path, params=None, wait=False,handleAlreadyElevated=False):
NVDA_REGKEY = ur"SOFTWARE\NVDA"
def getStartOnLogonScreen():
if easeOfAccess.isSupported and easeOfAccess.willAutoStart(_winreg.HKEY_LOCAL_MACHINE):
if easeOfAccess.isSupported and easeOfAccess.willAutoStart(winreg.HKEY_LOCAL_MACHINE):
return True
try:
k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, NVDA_REGKEY)
return bool(_winreg.QueryValueEx(k, u"startOnLogonScreen")[0])
k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, NVDA_REGKEY)
return bool(winreg.QueryValueEx(k, u"startOnLogonScreen")[0])
except WindowsError:
return False
def _setStartOnLogonScreen(enable):
if easeOfAccess.isSupported:
# The installer will have migrated service config to EoA if appropriate,
# so we only need to deal with EoA here.
easeOfAccess.setAutoStart(_winreg.HKEY_LOCAL_MACHINE, enable)
easeOfAccess.setAutoStart(winreg.HKEY_LOCAL_MACHINE, enable)
else:
k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, NVDA_REGKEY, 0, _winreg.KEY_WRITE)
_winreg.SetValueEx(k, u"startOnLogonScreen", None, _winreg.REG_DWORD, int(enable))
k = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, NVDA_REGKEY, 0, winreg.KEY_WRITE)
winreg.SetValueEx(k, u"startOnLogonScreen", None, winreg.REG_DWORD, int(enable))
def setSystemConfigToCurrentConfig():
fromPath=os.path.abspath(globalVars.appArgs.configPath)
@@ -7,7 +7,10 @@
"""Utilities for working with the Windows Ease of Access Center.
"""
import _winreg
try:
import _winreg as winreg # Python 2.7 import
except ImportError:
import winreg # Python 3 import
import ctypes
import winUser
from winVersion import winVersion
@@ -23,17 +26,17 @@
def isRegistered():
try:
_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, APP_KEY_PATH, 0,
_winreg.KEY_READ | _winreg.KEY_WOW64_64KEY)
winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, APP_KEY_PATH, 0,
winreg.KEY_READ | winreg.KEY_WOW64_64KEY)
return True
except WindowsError:
return False
def notify(signal):
if not isRegistered():
return
with _winreg.CreateKey(_winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows NT\CurrentVersion\AccessibilityTemp") as rkey:
_winreg.SetValueEx(rkey, APP_KEY_NAME, None, _winreg.REG_DWORD, signal)
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows NT\CurrentVersion\AccessibilityTemp") as rkey:
winreg.SetValueEx(rkey, APP_KEY_NAME, None, winreg.REG_DWORD, signal)
keys = []
# The user might be holding unwanted modifiers.
for vk in winUser.VK_SHIFT, winUser.VK_CONTROL, winUser.VK_MENU:
@@ -60,18 +63,18 @@ def notify(signal):
def willAutoStart(hkey):
try:
k = _winreg.OpenKey(hkey, ROOT_KEY, 0,
_winreg.KEY_READ | _winreg.KEY_WOW64_64KEY)
k = winreg.OpenKey(hkey, ROOT_KEY, 0,
winreg.KEY_READ | winreg.KEY_WOW64_64KEY)
return (APP_KEY_NAME in
_winreg.QueryValueEx(k, "Configuration")[0].split(","))
winreg.QueryValueEx(k, "Configuration")[0].split(","))
except WindowsError:
return False
def setAutoStart(hkey, enable):
k = _winreg.OpenKey(hkey, ROOT_KEY, 0,
_winreg.KEY_READ | _winreg.KEY_WRITE | _winreg.KEY_WOW64_64KEY)
k = winreg.OpenKey(hkey, ROOT_KEY, 0,
winreg.KEY_READ | winreg.KEY_WRITE | winreg.KEY_WOW64_64KEY)
try:
conf = _winreg.QueryValueEx(k, "Configuration")[0].split(",")
conf = winreg.QueryValueEx(k, "Configuration")[0].split(",")
except WindowsError:
conf = []
else:
@@ -89,5 +92,5 @@ def setAutoStart(hkey, enable):
except ValueError:
pass
if changed:
_winreg.SetValueEx(k, "Configuration", None, _winreg.REG_SZ,
winreg.SetValueEx(k, "Configuration", None, winreg.REG_SZ,
",".join(conf))
@@ -12,7 +12,7 @@
See the L{Action}, L{Filter}, L{Decider} classes.
"""
from logHandler import log
from util import HandlerRegistrar, callWithSupportedKwargs, BoundMethodWeakref
from .util import HandlerRegistrar, callWithSupportedKwargs, BoundMethodWeakref
class Action(HandlerRegistrar):
Oops, something went wrong.

0 comments on commit 8e2df9f

Please sign in to comment.