From 3c0088f037583c92cdc3939e5ead7e9b41adeb45 Mon Sep 17 00:00:00 2001 From: ObserverOfTime Date: Thu, 22 May 2025 08:44:03 +0300 Subject: [PATCH] fix(bindings): improve python platform detection (cherry picked from commit 99988b7081dc78c417864340851e6fd3380b68a2) --- cli/src/init.rs | 7 ++++++- cli/src/templates/setup.py | 5 ++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cli/src/init.rs b/cli/src/init.rs index 64d1c107..dffc0294 100644 --- a/cli/src/init.rs +++ b/cli/src/init.rs @@ -624,10 +624,15 @@ pub fn generate_grammar_files( allow_update, |path| generate_file(path, SETUP_PY_TEMPLATE, language_name, &generate_opts), |path| { - let contents = fs::read_to_string(path)?; + let mut contents = fs::read_to_string(path)?; if !contents.contains("egg_info") || !contents.contains("Py_GIL_DISABLED") { eprintln!("Replacing setup.py"); generate_file(path, SETUP_PY_TEMPLATE, language_name, &generate_opts)?; + } else { + contents = contents + .replace("path\nfrom platform import system", "name as os_name, path") + .replace("system() != \"Windows\"", "os_name != \"nt\""); + write_file(path, contents)?; } Ok(()) }, diff --git a/cli/src/templates/setup.py b/cli/src/templates/setup.py index 534bd9f2..9fdc3098 100644 --- a/cli/src/templates/setup.py +++ b/cli/src/templates/setup.py @@ -1,5 +1,4 @@ -from os import path -from platform import system +from os import name as os_name, path from sysconfig import get_config_var from setuptools import Extension, find_packages, setup @@ -21,7 +20,7 @@ macros: list[tuple[str, str | None]] = [ if limited_api := not get_config_var("Py_GIL_DISABLED"): macros.append(("Py_LIMITED_API", "0x030A0000")) -if system() != "Windows": +if os_name != "nt": cflags = ["-std=c11", "-fvisibility=hidden"] else: cflags = ["/std:c11", "/utf-8"]