8000 gh-123797: Check for runtime availability of `ptsname_r` on macos (#1… · python/cpython@3e36e5a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e36e5a

Browse files
authored
gh-123797: Check for runtime availability of ptsname_r on macos (#123806)
1 parent aae1267 commi
10000
t 3e36e5a

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

Lib/test/test_posix.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,13 @@ def test_stat(self):
21422142
with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
21432143
os.stat("file", dir_fd=0)
21442144

2145+
def test_ptsname_r(self):
2146+
self._verify_available("HAVE_PTSNAME_R")
2147+
if self.mac_ver >= (10, 13, 4):
2148+
self.assertIn("HAVE_PTSNAME_R", posix._have_functions)
2149+
else:
2150+
self.assertNotIn("HAVE_PTSNAME_R", posix._have_functions)
2151+
21452152
def test_access(self):
21462153
self._verify_available("HAVE_FACCESSAT")
21472154
if self.mac_ver >= (10, 10):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Check for runtime availability of ``ptsname_r`` function on macos.

Modules/posixmodule.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
# define HAVE_PWRITEV_RUNTIME __builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
126126
# define HAVE_MKFIFOAT_RUNTIME __builtin_available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
127127
# define HAVE_MKNODAT_RUNTIME __builtin_available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
128+
# define HAVE_PTSNAME_R_RUNTIME __builtin_available(macOS 10.13.4, iOS 11.3, tvOS 11.3, watchOS 4.3, *)
128129

129130
# define HAVE_POSIX_SPAWN_SETSID_RUNTIME __builtin_available(macOS 10.15, *)
130131

@@ -206,6 +207,10 @@
206207
# define HAVE_MKNODAT_RUNTIME (mknodat != NULL)
207208
# endif
208209

210+
# ifdef HAVE_PTSNAME_R
211+
# define HAVE_PTSNAME_R_RUNTIME (ptsname_r != NULL)
212+
# endif
213+
209214
#endif
210215

211216
#ifdef HAVE_FUTIMESAT
@@ -231,6 +236,7 @@
231236
# define HAVE_PWRITEV_RUNTIME 1
232237
# define HAVE_MKFIFOAT_RUNTIME 1
233238
# define HAVE_MKNODAT_RUNTIME 1
239+
# define HAVE_PTSNAME_R_RUNTIME 1
234240
#endif
235241

236242

@@ -8635,6 +8641,19 @@ os_unlockpt_impl(PyObject *module, int fd)
86358641
#endif /* HAVE_UNLOCKPT */
86368642

86378643
#if defined(HAVE_PTSNAME) || defined(HAVE_PTSNAME_R)
8644+
static PyObject *
8645+
py_ptsname(int fd)
8646+
{
8647+
// POSIX manpage: Upon failure, ptsname() shall return a null pointer
8648+
// and may set errno. Always initialize errno to avoid undefined behavior.
8649+
errno = 0;
8650+
char *name = ptsname(fd);
8651+
if (name == NULL) {
8652+
return posix_error();
8653+
}
8654+
return PyUnicode_DecodeFSDefault(name);
8655+
}
8656+
86388657
/*[clinic input]
86398658
os.ptsname
86408659
@@ -8656,22 +8675,22 @@ os_ptsname_impl(PyObject *module, int fd)
86568675
int ret;
86578676
char name[MAXPATHLEN+1];
86588677

8659-
ret = ptsname_r(fd, name, sizeof(name));
8678+
if (HAVE_PTSNAME_R_RUNTIME) {
8679+
ret = ptsname_r(fd, name, sizeof(name));
8680+
}
8681+
else {
8682+
// fallback to ptsname() if ptsname_r() is not available in runtime.
8683+
return py_ptsname(fd);
8684+
}
86608685
if (ret != 0) {
86618686
errno = ret;
86628687
return posix_error();
86638688
}
8664-
#else
8665-
char *name;
8666-
8667-
name = ptsname(fd);
8668-
/* POSIX manpage: Upon failure, ptsname() shall return a null pointer and may set errno.
8669-
*MAY* set errno? Hmm... */
8670-
if (name == NULL)
8671-
return posix_error();
8672-
#endif /* HAVE_PTSNAME_R */
86738689

86748690
return PyUnicode_DecodeFSDefault(name);
8691+
#else
8692+
return py_ptsname(fd);
8693+
#endif /* HAVE_PTSNAME_R */
86758694
}
86768695
#endif /* defined(HAVE_PTSNAME) || defined(HAVE_PTSNAME_R) */
86778696

@@ -17751,6 +17770,9 @@ PROBE(probe_futimens, HAVE_FUTIMENS_RUNTIME)
1775117770
PROBE(probe_utimensat, HAVE_UTIMENSAT_RUNTIME)
1775217771
#endif
1775317772

17773+
#ifdef HAVE_PTSNAME_R
17774+
PROBE(probe_ptsname_r, HAVE_PTSNAME_R_RUNTIME)
17775+
#endif
1775417776

1775517777

1775617778

@@ -17891,6 +17913,10 @@ static const struct have_function {
1789117913
{ "HAVE_UTIMENSAT", probe_utimensat },
1789217914
#endif
1789317915

17916+
#ifdef HAVE_PTSNAME_R
17917+
{ "HAVE_PTSNAME_R", probe_ptsname_r },
17918+
#endif
17919+
1789417920
#ifdef MS_WINDOWS
1789517921
{ "MS_WINDOWS", NULL },
1789617922
#endif

0 commit comments

Comments
 (0)
0