Skip to content

Commit 469f300

Browse files
NO-SNOW: Refactor _detect_libc to use platform.libc_ver (#2780)
1 parent 7c4408f commit 469f300

2 files changed

Lines changed: 12 additions & 30 deletions

File tree

src/snowflake/connector/_utils.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import time
1111
from enum import Enum
1212
from inspect import stack
13-
from pathlib import Path
1413
from random import choice
1514
from threading import Timer
1615
from uuid import UUID
@@ -188,26 +187,10 @@ def _detect_arch() -> str:
188187
@staticmethod
189188
def _detect_libc() -> str:
190189
"""Detect libc type on Linux (glibc vs musl)."""
191-
# Check if we're on Alpine/musl
192-
if Path("/etc/alpine-release").exists():
193-
return "musl"
194-
195-
# Check for musl by looking at the libc library
196-
try:
197-
import subprocess
198-
199-
result = subprocess.run(
200-
["ldd", "--version"],
201-
capture_output=True,
202-
text=True,
203-
)
204-
if "musl" in result.stdout.lower() or "musl" in result.stderr.lower():
205-
return "musl"
206-
except Exception:
207-
pass
208-
209-
# Default to glibc
210-
return "glibc"
190+
lib, _ = platform.libc_ver()
191+
if lib == "glibc":
192+
return "glibc"
193+
return "musl"
211194

212195
@staticmethod
213196
def _get_platform_subdir() -> str:

test/unit/test_util.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,16 @@ def test_detect_arch(self, machine, expected):
8585
with mock.patch("platform.machine", return_value=machine):
8686
assert _CoreLoader._detect_arch() == expected
8787

88-
def test_detect_libc_alpine(self, tmp_path):
89-
"""Test _detect_libc returns musl on Alpine Linux."""
90-
with mock.patch("pathlib.Path.exists", return_value=True):
88+
def test_detect_libc_glibc(self):
89+
"""Test _detect_libc returns glibc when platform reports glibc."""
90+
with mock.patch("platform.libc_ver", return_value=("glibc", "2.31")):
91+
assert _CoreLoader._detect_libc() == "glibc"
92+
93+
def test_detect_libc_musl(self):
94+
"""Test _detect_libc returns musl when platform does not report glibc."""
95+
with mock.patch("platform.libc_ver", return_value=("", "")):
9196
assert _CoreLoader._detect_libc() == "musl"
9297

93-
def test_detect_libc_glibc_default(self):
94-
"""Test _detect_libc returns glibc by default."""
95-
with mock.patch("pathlib.Path.exists", return_value=False):
96-
with mock.patch("subprocess.run", side_effect=Exception("not found")):
97-
assert _CoreLoader._detect_libc() == "glibc"
98-
9998
@pytest.mark.parametrize(
10099
"os_name,arch,libc,expected_subdir",
101100
[

0 commit comments

Comments
 (0)