Skip to content

remove use of unichr, basestring and unicode objects.#9724

Merged
michaelDCurran merged 5 commits into
threshold_py3_stagingfrom
py3_unichr
Jun 15, 2019
Merged

remove use of unichr, basestring and unicode objects.#9724
michaelDCurran merged 5 commits into
threshold_py3_stagingfrom
py3_unichr

Conversation

@michaelDCurran

Copy link
Copy Markdown
Member

Link to issue number:

None

Summary of the issue:

In Python3, strings are unicode by default. str is unicode, and unicode and basestring no longer exist.

Description of how this pull request fixes the issue:

  • Changed all unichr calls to chr calls. As chr was never used in the code base, logically it is safe enough to simply replace all of these.
  • Replaced occurances of basestring with str. If we were allowing both unicode and ascii, then now it should just be unicode.
  • All usage of unicode() has been removed where we can assume a string should already be unicode, or changed to str() where we are converting from something that isn't a string (E.g. an int).
  • nvwave now initializes its buffers with bytes objects.
  • The eSpeak synthDriver now correctly converts voice/variant IDs/names to utf8 from unicode and back again.

This pr was produced by grepping for the various symbols, and handling each case specifically by looking at the code.

Testing performed:

With one or two other runtime changes to do with hashing etc (addressed in a separate pr), NVDA can now run, and all synthDrivers, including eSpeak function correctly.

Known issues with pull request:

No brailleDisplay drivers have been touched yet, nor has hwIo in any significant way. These will have to be handled carefully in regards to what should be bytes and what should be strings.

Change log entry:

None.

@LeonarddeR

Copy link
Copy Markdown
Collaborator
* Changed all unichr calls to chr calls. As chr was never used in the code base, logically it is safe enough to simply replace all of these.

I find at least an occurrence of chr in handyTech.py. Having said that, you just said that you haven't touched braille display drivers in this pr, so I assume you mean that chr isn't used outside braille display drivers.

* Replaced occurances of basestring with str. If we were allowing both unicode and ascii, then now it should just be unicode.

How about the cases where str is already used? I'm pretty sure we need a way to distinguish between cases where str has now been introduced instead of basestring, and cases where str was already efined.

@LeonarddeR LeonarddeR left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Apart from my question about whether this is the right moment to convert basestring to str without covering all the str cases first, here are some comments, particularly about very long lines that could be fixed while at it. I don't insist on doing this as part of this pr, though.

Comment thread source/NVDAObjects/__init__.py Outdated
This string should be included as returned. There is no need to call repr.
@param string: The string to format.
@type string: nbasestring
@type string: nstr

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Typo, nstr > str

text=ctypes.cast(buf,ctypes.c_wchar_p).value
else:
text=unicode(ctypes.cast(buf,ctypes.c_char_p).value, errors="replace", encoding=locale.getlocale()[1])
encoding=locale.getlocale()[1]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I wonder whether we should change this into locale.getpreferredencoding() while at it.

@@ -128,7 +128,7 @@
}

def bytesToInt(bytes):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This function can be removed anyway, but that will be dealt with later.

Comment thread source/browseMode.py Outdated
# Return True if the URL indicates that this is probably a web browser document.
# We do this check because we don't want to remember caret positions for email messages, etc.
return isinstance(docConstId, basestring) and docConstId.split("://", 1)[0] in ("http", "https", "ftp", "ftps", "file")
return isinstance(docConstId, str) and docConstId.split("://", 1)[0] in ("http", "https", "ftp", "ftps", "file")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This line is really huge. Could you split it while at it?

Comment thread source/globalCommands.py
if scriptHandler.getLastScriptRepeatCount()>=1:
if curObject.TextInfo!=NVDAObjectTextInfo:
textList=[]
if curObject.name and isinstance(curObject.name, basestring) and not curObject.name.isspace():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you also split this one while at it?

Comment thread source/louisHelper.py Outdated
@@ -55,7 +55,7 @@ def translate(tableList, inbuf, typeform=None, cursorPos=None, mode=0):
* returns a list of integers instead of an string with cells, and

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ugh, this should be a string instead of an string.

Comment thread source/speechXml.py Outdated

def _escapeXml(text):
text = unicode(text).translate(XML_ESCAPES)
text = str(text).translate(XML_ESCAPES)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we ever expect text to be something else than str? if not, we can safely omit wrapping this into str

Suggested change
text = str(text).translate(XML_ESCAPES)
text = text.translate(XML_ESCAPES)

Comment thread source/synthDrivers/_espeak.py Outdated
for fileName in os.listdir(dir):
if os.path.isfile("%s\\%s"%(dir,fileName)):
file=codecs.open("%s\\%s"%(dir,fileName))
file=open("%s\\%s"%(dir,fileName))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there something that makes this change necessary?

Comment thread source/ui.py Outdated
dialogArguements = automation.VARIANT( dialogString )
gui.mainFrame.prePopup()
windll.mshtml.ShowHTMLDialogEx( gui.mainFrame.Handle , moniker , HTMLDLG_MODELESS , addressof( dialogArguements ) , unicode(DIALOG_OPTIONS ), None)
windll.mshtml.ShowHTMLDialogEx( gui.mainFrame.Handle , moniker , HTMLDLG_MODELESS , addressof( dialogArguements ) , DIALOG_OPTIONS, None)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wow, this is long as well!

Comment thread source/virtualBuffers/__init__.py Outdated
if log.isEnabledFor(log.DEBUG):
startTime = time.time()
self.VBufHandle=NVDAHelper.localLib.VBuf_createBuffer(self.rootNVDAObject.appModule.helperLocalBindingHandle,self.rootDocHandle,self.rootID,unicode(self.backendName))
self.VBufHandle=NVDAHelper.localLib.VBuf_createBuffer(self.rootNVDAObject.appModule.helperLocalBindingHandle,self.rootDocHandle,self.rootID,self.backendName)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Another pretty long line

@michaelDCurran

michaelDCurran commented Jun 12, 2019 via email

Copy link
Copy Markdown
Member Author

@LeonarddeR

LeonarddeR commented Jun 12, 2019 via email

Copy link
Copy Markdown
Collaborator

@michaelDCurran

michaelDCurran commented Jun 12, 2019 via email

Copy link
Copy Markdown
Member Author

@feerrenrut

Copy link
Copy Markdown
Contributor

This modifies a few braille drivers, and other braille related files. I expect it will conflict with the work I am doing. Please see #9736.

Can you revert the changes to the overlapping files:

 source/bdDetect.py
 source/braille.py
 source/brailleDisplayDrivers/eurobraille.py
 source/brailleInput.py
 source/brailleTables.py
source/hwIo.py

@michaelDCurran

Copy link
Copy Markdown
Member Author

@LeonarddeR and @feerrenrut this is ready for another review again.

Comment thread source/speechXml.py
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.

4 participants