Skip to content

msvs.py: Use floor division when escaping command-line arguments#338

Open
cclauss wants to merge 1 commit intomainfrom
msvs.py_use_floor_division
Open

msvs.py: Use floor division when escaping command-line arguments#338
cclauss wants to merge 1 commit intomainfrom
msvs.py_use_floor_division

Conversation

@cclauss
Copy link
Copy Markdown
Contributor

@cclauss cclauss commented Apr 10, 2026

Fixes nodejs/node-gyp#3296

% python3.14

>>> import re
>>> s = "TEST_STRING=\\\"TEST\\\""
>>> quote_replacer_regex2 = re.compile(r'(\\+)"')
...
...
... def _EscapeCommandLineArgumentForMSBuild(s):
...     """Escapes a Windows command-line argument for use by MSBuild."""
...
...     def _Replace(match):
...         return (len(match.group(1)) / 2 * 4) * "\\" + '\\"'
...
...     # Escape all quotes so that they are interpreted literally.
...     s = quote_replacer_regex2.sub(_Replace, s)
...     return s
...
>>> _EscapeCommandLineArgumentForMSBuild(s)
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    _EscapeCommandLineArgumentForMSBuild(s)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
  File "<python-input-1>", line 11, in _EscapeCommandLineArgumentForMSBuild
    s = quote_replacer_regex2.sub(_Replace, s)
  File "<python-input-1>", line 8, in _Replace
    return (len(match.group(1)) / 2 * 4) * "\\" + '\\"'
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
TypeError: can't multiply sequence by non-int of type 'float'

>>> Modify _Replace() to use floor division...
>>> _EscapeCommandLineArgumentForMSBuild(s)
'TEST_STRING=\\"TEST\\"'

@kuenzign

Fixes nodejs/node-gyp#3296
* nodejs/node-gyp#3296

% `python3.14`
```
>>> import re
>>> s = "TEST_STRING=\\\"TEST\\\""
>>> quote_replacer_regex2 = re.compile(r'(\\+)"')
...
...
... def _EscapeCommandLineArgumentForMSBuild(s):
...     """Escapes a Windows command-line argument for use by MSBuild."""
...
...     def _Replace(match):
...         return (len(match.group(1)) / 2 * 4) * "\\" + '\\"'
...
...     # Escape all quotes so that they are interpreted literally.
...     s = quote_replacer_regex2.sub(_Replace, s)
...     return s
...
>>> _EscapeCommandLineArgumentForMSBuild(s)
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    _EscapeCommandLineArgumentForMSBuild(s)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
  File "<python-input-1>", line 11, in _EscapeCommandLineArgumentForMSBuild
    s = quote_replacer_regex2.sub(_Replace, s)
  File "<python-input-1>", line 8, in _Replace
    return (len(match.group(1)) / 2 * 4) * "\\" + '\\"'
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
TypeError: can't multiply sequence by non-int of type 'float'

>>> Modify _Replace() to use floor division...
>>> _EscapeCommandLineArgumentForMSBuild(s)
'TEST_STRING=\\"TEST\\"'
@cclauss cclauss added bug Something isn't working python Pull requests that update Python code labels Apr 10, 2026
@cclauss cclauss requested a review from Copilot April 10, 2026 07:45
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a Python 3 runtime error in the MSBuild command-line argument escaping logic used by the MSVS generator, restoring the intended integer-division behavior from Python 2.

Changes:

  • Replace / with // in _EscapeCommandLineArgumentForMSBuild to avoid producing a float and triggering a TypeError when multiplying strings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

msvs.py _EscapeCommandLineArgumentForMSBuild() TypeError: can't multiply sequence by non-int of type 'float'

2 participants