Repository: nvaccess/nvda

Issue:
Remove Python 2 imports for modules renamed in python 3
Certain modules have been renamed between python 2.7 and 3.7.

In https://github.com/nvaccess/nvda/pull/8606 a fall back to the python 3 name for these modules was introduced. Since our releases are using Python 2.7, we first attempt to import the Python 2.7 module.

When we reach the point of including Python 3 support in a release of NVDA we should remove the python 2 imports.

See also: #8727 (Python 3 imports: additional work on builtins, relative imports, importlib)



Discussion:
Does this mean that both python runtimes are going to be used in the 
releases. Sorry if this is a stupid question!

Brian 


At some point, there may be different branches of NVDA using different Python versions, e.g. a stable release using Python 2.7 and an alpha or beta build using Python 3.x.  But NVDA will only run with one Python version at a time.
Hi,

Most are taken care of now. Closing.

Thanks.
> Most are taken care of now

Most? Have you checked? If there are others not taken care of, then this should be re-opened.
Hi, I bet there are instances of importing winreg, CStringIO and others that we aren’t aware of at the moment. Thanks.

I have searched the `source/` directory for all the [changed names referenced in the python 3 porting guide.](http://python3porting.com/stdlib.html) There

The only one I have found is: `im_self` which should become `__self__`. This is used in `scriptHandler.py`, the line is as follows:
```
		givenFunc=getattr(scriptFunc.im_self.__class__, scriptFunc.__name__)
```

This should be tested to confirm that it fails. If so, it should be changed.
@josephsl: Are you willing to do this?
As we compile with py2exe, we can be assured that all our imports are correct, as otherwise py2exe would fail. Thus to close this issue, we only need to verify changes to special attribute names (such as im_self etc). which I am doing now. 
I have checked all builtin and attribute renames, and have confirmed @feerrenrut 's  findings that the only one not handled is im_self in scritHandler.
I have also ran py2exe and read through all missing modules. They are all optional modues imported by modules in the standard library or other dependencies, or comtypes auto-generated files. There are no missing modules directly imported by NVDA's codebase.
scriptHandler.isCurrentScript tries to access im_self which will always fail. However, we never call isCurrentScript anywhere in the NVDA codebase. Do people have evidence that it is ever used in add-ons? If not we should just remove the function all together.
Hi, ran grep on my own repo of add-ons, along with addons installed on a portable alpha version (the latter has 67 add-ons, all of them from community add-ons website, installed for the express purpose of testing compatibility with NVDA 2019.3). Nowhere in add-ons code is isCurrentScript mentioned. Thanks.

 

From: Michael Curran <notifications@github.com> 
Sent: Thursday, October 31, 2019 7:41 PM
To: nvaccess/nvda <nvda@noreply.github.com>
Cc: Joseph Lee <joseph.lee22590@gmail.com>; Mention <mention@noreply.github.com>
Subject: Re: [nvaccess/nvda] Remove Python 2 imports for modules renamed in python 3 (#8677)

 

scriptHandler.isCurrentScript tries to access im_self which will always fail. However, we never call isCurrentScript anywhere in the NVDA codebase. Do people have evidence that it is ever used in add-ons? If not we should just remove the function all together.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <https://github.com/nvaccess/nvda/issues/8677?email_source=notifications&email_token=AB4AXEFF3RTQSLMGNGUI6U3QROJKRA5CNFSM4FRUCBL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECZ2KII#issuecomment-548644129> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/AB4AXEBXBGF5XX5B2FWI6SLQROJKRANCNFSM4FRUCBLQ> .



Resolving patch:
diff --git a/source/scriptHandler.py b/source/scriptHandler.py
--- a/source/scriptHandler.py
+++ b/source/scriptHandler.py
@@ -224,31 +224,6 @@ def getLastScriptRepeatCount():
 def isScriptWaiting():
 	return bool(_numScriptsQueued)
 
-def isCurrentScript(scriptFunc):
-	"""Finds out if the given script is equal to the script that L{isCurrentScript} is being called from.
-	@param scriptFunc: the script retreaved from ScriptableObject.getScript(gesture)
-	@type scriptFunc: Instance method
-	@returns: True if they are equal, False otherwise
-	@rtype: boolean
-	"""
-	try:
-	 	givenFunc=getattr(scriptFunc.im_self.__class__,scriptFunc.__name__)
-	except AttributeError:
-		log.debugWarning("Could not get unbound method from given script",exc_info=True) 
-		return False
-	parentFrame=inspect.currentframe().f_back
-	try:
-		realObj=parentFrame.f_locals['self']
-	except KeyError:
-		log.debugWarning("Could not get self instance from parent frame instance method",exc_info=True)
-		return False
-	try:
-		realFunc=getattr(realObj.__class__,parentFrame.f_code.co_name)
-	except AttributeError:
-		log.debugWarning("Could not get unbound method from parent frame instance",exc_info=True)
-		return False
-	return givenFunc==realFunc
-
 def script(
 	description="",
 	category=None,


Test patch: