Skip to content

Fix regexp error message in dictionaries#14557

Merged
seanbudd merged 10 commits into
nvaccess:masterfrom
CyrilleB79:regexpError
Feb 14, 2023
Merged

Fix regexp error message in dictionaries#14557
seanbudd merged 10 commits into
nvaccess:masterfrom
CyrilleB79:regexpError

Conversation

@CyrilleB79

@CyrilleB79 CyrilleB79 commented Jan 17, 2023

Copy link
Copy Markdown
Contributor

Link to issue number:

Fixes #14556

Summary of the issue:

Dictionaries do not allow to use backslash character normally in the replacement field when regexp is not selected as entry type. When such a character is used, a message box complaining of a regexp error appears.

Description of user facing changes

  • The user can use backslash in replacement field normally when entry type is not regexp
  • Error messages are more precise and more specific when entering erroneous values in the fields for a regexp entry

Description of development approach

  • When the entry type is not regexp, escape the replacement string
  • When entries are created:
    • filter exception on re.error instead of the too global Exception to catch only exceptions related to regexps
    • separate the try/except statement in two in order to indicate in which field the regexp error lies
    • In case of a regexp error, after validating "OK" in the message box, the field where the error lies is re-focused
    • Reports the regexp error only in case of entry type = regexp; in the other cases,re-raise the exception, in order not to catch unrelated exceptions, e.g. code bug.

Testing strategy:

Manual tests:

  • Add entry with backslash in replacement field and entry type = everywhere
  • Add entry with error either in pattern or in replacement field with entry type = regexp.

Known issues with pull request:

None

Change log entries:

Bug fixes
It is now possible to use the backslash character in the replacement field of a dictionaries entry, when the type is not set to regular expression. (#14556)

Code Review Checklist:

[x] Pull Request description:

  • description is up to date
  • change log entries
  • Testing:
    • Unit tests
    • System (end to end) tests
    • Manual testing
  • API is compatible with existing add-ons.
  • Documentation:
    • User Documentation
    • Developer / Technical Documentation
    • Context sensitive help for GUI changes
  • UX of all users considered:
    • Speech
    • Braille
    • Low Vision
    • Different web browsers
    • Localization in other languages / culture than English
  • Security precautions taken.

@XLTechie

Copy link
Copy Markdown
Collaborator

For changelog:

It is now again possible to use backslash character in the replacement field of dictionaries. (#14556)

Suggestion:

It is now possible to use the backslash character in the replacement field of dictionaries, when the pattern is not set to regular expression. (#14556)

Comment thread source/gui/speechDict.py Outdated
@CyrilleB79

Copy link
Copy Markdown
Contributor Author

@XLTechie thanks for the review. I have addressed the raised points in the code and in the initial description.

@CyrilleB79 CyrilleB79 marked this pull request as ready for review January 17, 2023 16:36
@CyrilleB79 CyrilleB79 requested a review from a team as a code owner January 17, 2023 16:36
@CyrilleB79 CyrilleB79 requested a review from seanbudd January 17, 2023 16:36

@seanbudd seanbudd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @CyrilleB79

Comment thread source/speechDictHandler/__init__.py Outdated
Comment thread source/gui/speechDict.py Outdated
@CyrilleB79 CyrilleB79 marked this pull request as draft January 17, 2023 23:36
@CyrilleB79

Copy link
Copy Markdown
Contributor Author

@seanbudd, I have addressed review comments. However, it is not possible to use f-strings with gettext so I have turned back to % interpolation as it was before.

I have also added two more lines to focus the field causing the regexp error as it was already done in case of empty pattern field error.

@CyrilleB79 CyrilleB79 marked this pull request as ready for review January 18, 2023 07:47
@CyrilleB79 CyrilleB79 requested a review from seanbudd January 18, 2023 07:47
@XLTechie

XLTechie commented Jan 18, 2023 via email

Copy link
Copy Markdown
Collaborator

@XLTechie

XLTechie commented Jan 18, 2023 via email

Copy link
Copy Markdown
Collaborator

@CyrilleB79

Copy link
Copy Markdown
Contributor Author

However, it is not possible to use f-strings with gettext so I have turned back to % interpolation as it was before.
I meant only to suggest that for the log messages, not the user messages. Apologies if I commented on the wrong line.

No worry, you commented on the correct line. But I have then added another extra f-string in an inappropriate place, i.e. where gettext is used.

Regarding % versus .format() syntax, is there a recommendation or a preference to use the latter? (NVDA/NVAccess recommendation and/or Python/PEP recommendation)
I'll let @seanbudd decide if a change should be done.

@josephsl

josephsl commented Jan 18, 2023 via email

Copy link
Copy Markdown
Contributor

@seanbudd

Copy link
Copy Markdown
Member

Agreed with @josephsl, the label in the format string e.g. {settingsName} is more explicit and helpful than %s

@CyrilleB79

Copy link
Copy Markdown
Contributor Author

Agreed with @josephsl, the label in the format string e.g. {settingsName} is more explicit and helpful than %s

OK @seanbudd, I have fixed it.

@seanbudd seanbudd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me, other than a minor syntax issue

Comment thread source/gui/speechDict.py Outdated
Comment thread source/gui/speechDict.py Outdated
@AppVeyorBot

Copy link
Copy Markdown

See test results for failed build of commit 5ed1c6b852

Comment thread source/speechDictHandler/__init__.py Outdated
@seanbudd seanbudd merged commit 8b1b943 into nvaccess:master Feb 14, 2023
@nvaccessAuto nvaccessAuto added this to the 2023.2 milestone Feb 14, 2023
CyrilleB79 added a commit to CyrilleB79/nvda that referenced this pull request Feb 22, 2023
seanbudd pushed a commit that referenced this pull request Feb 22, 2023
…t expression of dictionary (#14664)

Fixes #14663
Fix-up of #14557

Summary of the issue:
When reading text containing a non-regexp dictionary entry, if the replacement field contains some special characters (such as space, +, etc.), these characters are escaped with a backslash. Thus this backslash is heard in the text spoken by NVDA.

Description of user facing changes
No unappropriated backslash will be spoken by NVDA when the replacement field of a dictionary entry contains space or other special character.

Description of development approach
Use theString.replace('\\', '\\\\') instead of re.escape(theString).

Indeed, it was well documented in the doc of re.escape:

This function must not be used for the replacement string in sub() and subn(), only backslashes should be escaped.
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.

Unable to use backslash in the replacement value of dictionaries

6 participants