Skip to content

Commit dbfc37a

Browse files
[3.13] gh-123797: Check for runtime availability of ptsname_r on macos (GH-123806) (#124270)
gh-123797: Check for runtime availability of `ptsname_r` on macos (GH-123806) (cherry picked from commit 3e36e5a) Co-authored-by: sobolevn <[email protected]>
1 parent dddae66 commit dbfc37a

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

Lib/test/test_posix.py

+7
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,13 @@ def test_stat(self):
21402140
with self.assertRaisesRegex(NotImplementedError, "dir_fd unavailable"):
21412141
os.stat("file", dir_fd=0)
21422142

2143+
def test_ptsname_r(self):
2144+
self._verify_available("HAVE_PTSNAME_R")
2145+
if self.mac_ver >= (10, 13, 4):
2146+
self.assertIn("HAVE_PTSNAME_R", posix._have_functions)
2147+
else:
2148+
self.assertNotIn("HAVE_PTSNAME_R", posix._have_functions)
2149+
21432150
def test_access(self):
21442151
self._verify_available("HAVE_FACCESSAT")
21452152
if self.mac_ver >= (10, 10):
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

+36-10
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

@@ -17739,6 +17758,9 @@ PROBE(probe_futimens, HAVE_FUTIMENS_RUNTIME)
1773917758
PROBE(probe_utimensat, HAVE_UTIMENSAT_RUNTIME)
1774017759
#endif
1774117760

17761+
#ifdef HAVE_PTSNAME_R
17762+
PROBE(probe_ptsname_r, HAVE_PTSNAME_R_RUNTIME)
17763+
#endif
1774217764

1774317765

1774417766

@@ -17879,6 +17901,10 @@ static const struct have_function {
1787917901
{ "HAVE_UTIMENSAT", probe_utimensat },
1788017902
#endif
1788117903

17904+
#ifdef HAVE_PTSNAME_R
17905+
{ "HAVE_PTSNAME_R", probe_ptsname_r },
17906+
#endif
17907+
1788217908
#ifdef MS_WINDOWS
1788317909
{ "MS_WINDOWS", NULL },
1788417910
#endif

0 commit comments

Comments
 (0)