From 117f80e2ccf87b241ca94101a3d67948e24d4588 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 2 Dec 2024 14:47:21 +0100 Subject: [PATCH 1/3] feat: provide escaped version of `PRODUCT_DIR_ABS` Since Node.js uses this in a C string, we should provide a version that escapes it as a C string. --- pylib/gyp/__init__.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/pylib/gyp/__init__.py b/pylib/gyp/__init__.py index befa76a..84bc141 100755 --- a/pylib/gyp/__init__.py +++ b/pylib/gyp/__init__.py @@ -24,6 +24,17 @@ DEBUG_VARIABLES = "variables" DEBUG_INCLUDES = "includes" +def EscapeForCString(string): + if isinstance(string, str): + string = string.encode(encoding='utf8') + + result = '' + for char in string: + if not (32 <= char < 127) or char in (ord('\\'), ord('"')): + result += '\\%03o' % char + else: + result += chr(char) + return result def DebugOutput(mode, message, *args): if "all" in gyp.debug or mode in gyp.debug: @@ -106,18 +117,19 @@ def Load( output_dir = params["options"].generator_output or params["options"].toplevel_dir if default_variables["GENERATOR"] == "ninja": - default_variables.setdefault( - "PRODUCT_DIR_ABS", - os.path.join( - output_dir, "out", default_variables.get("build_type", "default") - ), + product_dir_abs = os.path.join( + output_dir, "out", default_variables.get("build_type", "default") ) else: - default_variables.setdefault( - "PRODUCT_DIR_ABS", - os.path.join(output_dir, default_variables["CONFIGURATION_NAME"]), + product_dir_abs = os.path.join( + output_dir, default_variables["CONFIGURATION_NAME"] ) + default_variables.setdefault("PRODUCT_DIR_ABS", product_dir_abs) + default_variables.setdefault( + "PRODUCT_DIR_ABS_CSTR", EscapeForCString(product_dir_abs) + ) + # Give the generator the opportunity to set additional variables based on # the params it will receive in the output phase. if getattr(generator, "CalculateVariables", None): From 0bb4d6862a7f176679b22939e1682de177e682a0 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 2 Dec 2024 16:47:31 +0100 Subject: [PATCH 2/3] fixup! feat: provide escaped version of `PRODUCT_DIR_ABS` Co-authored-by: Christian Clauss --- pylib/gyp/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pylib/gyp/__init__.py b/pylib/gyp/__init__.py index 84bc141..1cf3d68 100755 --- a/pylib/gyp/__init__.py +++ b/pylib/gyp/__init__.py @@ -24,13 +24,14 @@ DEBUG_VARIABLES = "variables" DEBUG_INCLUDES = "includes" -def EscapeForCString(string): +def EscapeForCString(string: bytes | str) -> str: if isinstance(string, str): string = string.encode(encoding='utf8') - result = '' + backslash_or_double_quote = {ord('\\'), ord('"')} + result = [] for char in string: - if not (32 <= char < 127) or char in (ord('\\'), ord('"')): + if char in backslash_or_double_quote or not 32 <= char < 127: result += '\\%03o' % char else: result += chr(char) From 976253da758d542171c3438c954d3687e8d097ba Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 2 Dec 2024 16:47:31 +0100 Subject: [PATCH 3/3] fixup! feat: provide escaped version of `PRODUCT_DIR_ABS` Co-authored-by: Christian Clauss --- pylib/gyp/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylib/gyp/__init__.py b/pylib/gyp/__init__.py index 1cf3d68..5b16e0b 100755 --- a/pylib/gyp/__init__.py +++ b/pylib/gyp/__init__.py @@ -4,7 +4,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. - +from __future__ import annotations import copy import gyp.input import argparse