Skip to content

Commit f5245dc

Browse files
[3.12] pythongh-128146: Exclude os/log.h import on older macOS versions. (pythonGH-128165) (python#128575)
pythongh-128146: Exclude os/log.h import on older macOS versions. (pythonGH-128165) Reworks the handling of Apple system log handling to account for older macOS versions that don't provide os-log. (cherry picked from commit e837a1f) Co-authored-by: Russell Keith-Magee <russell@keith-magee.com>
1 parent 661160c commit f5245dc

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Usage of the unified Apple System Log APIs was disabled when the minimum
2+
macOS version is earlier than 10.12.

‎Python/pylifecycle.c‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,25 @@
3535

3636
#if defined(__APPLE__)
3737
# include <AvailabilityMacros.h>
38+
# include <TargetConditionals.h>
3839
# include <mach-o/loader.h>
39-
# include <os/log.h>
40+
// The os_log unified logging APIs were introduced in macOS 10.12, iOS 10.0,
41+
// tvOS 10.0, and watchOS 3.0;
42+
# if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
43+
# define HAS_APPLE_SYSTEM_LOG 1
44+
# elif defined(TARGET_OS_OSX) && TARGET_OS_OSX
45+
# if defined(MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
46+
# define HAS_APPLE_SYSTEM_LOG 1
47+
# else
48+
# define HAS_APPLE_SYSTEM_LOG 0
49+
# endif
50+
# else
51+
# define HAS_APPLE_SYSTEM_LOG 0
52+
# endif
53+
54+
# if HAS_APPLE_SYSTEM_LOG
55+
# include <os/log.h>
56+
# endif
4057
#endif
4158

4259
#ifdef HAVE_SIGNAL_H
@@ -68,7 +85,7 @@ static PyStatus add_main_module(PyInterpreterState *interp);
6885
static PyStatus init_import_site(void);
6986
static PyStatus init_set_builtins_open(void);
7087
static PyStatus init_sys_streams(PyThreadState *tstate);
71-
#if defined(__APPLE__)
88+
#if defined(__APPLE__) && HAS_APPLE_SYSTEM_LOG
7289
static PyStatus init_apple_streams(PyThreadState *tstate);
7390
#endif
7491
static void wait_for_thread_shutdown(PyThreadState *tstate);
@@ -1187,7 +1204,7 @@ init_interp_main(PyThreadState *tstate)
11871204
return status;
11881205
}
11891206

1190-
#if defined(__APPLE__)
1207+
#if defined(__APPLE__) && HAS_APPLE_SYSTEM_LOG
11911208
if (config->use_system_logger) {
11921209
status = init_apple_streams(tstate);
11931210
if (_PyStatus_EXCEPTION(status)) {
@@ -2659,7 +2676,7 @@ init_sys_streams(PyThreadState *tstate)
26592676
return res;
26602677
}
26612678

2662-
#if defined(__APPLE__)
2679+
#if defined(__APPLE__) && HAS_APPLE_SYSTEM_LOG
26632680

26642681
static PyObject *
26652682
apple_log_write_impl(PyObject *self, PyObject *args)
@@ -2670,14 +2687,9 @@ apple_log_write_impl(PyObject *self, PyObject *args)
26702687
return NULL;
26712688
}
26722689

2673-
// Call the underlying Apple logging API. The os_log unified logging APIs
2674-
// were introduced in macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0;
2675-
// this call is a no-op on older versions.
2676-
#if TARGET_OS_IPHONE || (TARGET_OS_OSX && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12)
26772690
// Pass the user-provided text through explicit %s formatting
26782691
// to avoid % literals being interpreted as a formatting directive.
26792692
os_log_with_type(OS_LOG_DEFAULT, logtype, "%s", text);
2680-
#endif
26812693
Py_RETURN_NONE;
26822694
}
26832695

@@ -2712,7 +2724,6 @@ init_apple_streams(PyThreadState *tstate)
27122724
if (result == NULL) {
27132725
goto error;
27142726
}
2715-
27162727
goto done;
27172728

27182729
error:
@@ -2726,7 +2737,7 @@ init_apple_streams(PyThreadState *tstate)
27262737
return status;
27272738
}
27282739

2729-
#endif // __APPLE__
2740+
#endif // __APPLE__ && HAS_APPLE_SYSTEM_LOG
27302741

27312742

27322743
static void

0 commit comments

Comments
 (0)