Skip to content

Conversation

@jcfr
Copy link
Contributor

@jcfr jcfr commented Sep 4, 2025

This pull request addresses compilation issues on older macOS toolchains caused by ctype macros defined by <Python.h>. The changes include:

  1. Undefining problematic ctype macros (isalnum, isalpha, islower, isspace, isupper, tolower, toupper) after including Python headers, to avoid clashes with C++ standard library headers like <locale>.
  2. Restricting the undefinition of these macros to macOS C++ builds by checking for _PY_PORT_CTYPE_UTF8_ISSUE and __cplusplus macros.

Note

For reference, those patches were developed in the context of the commontk/PythonQt fork.

Cherry picked from commits commontk/PythonQt@4ce028d

pieper and others added 2 commits September 4, 2025 14:10
…to avoid libstdc++ <locale> clashes

On older Apple toolchains (e.g., macOS 10.7.4/Xcode 4.3.3 with libstdc++ 4.2.1),
including <Python.h> pulls in <ctype.h>, which defines macros like isspace/isalnum.
When C++ headers such as <locale> (transitively included by moc-generated code)
declare the templated overloads (e.g., `bool std::isspace(_CharT, const std::locale&)`),
those C macros expand and break compilation with errors.

In `PythonQtPythonInclude.h`, explicitly `#undef` the problematic <ctype.h> macros
(isalnum, isalpha, islower, isspace, isupper, tolower, toupper) after including Python
headers. This keeps macro pollution out of downstream C++ includes and lets the std:: functions/
overloads compile cleanly.

It fixes error like the following:

```
  /usr/include/c++/4.2.1/bits/localefwd.h:57:21: error:
  too many arguments provided to function-like macro invocation
      isspace(_CharT, const locale&);
```

```
moc_PythonQtStdDecorators.cxx:152:25:
error: expected unqualified-id
        case 4: _t->emit((*reinterpret_cast<
QObject*(*)>(_a[1])),(*reinterpret_cast< const
`
```

(cherry picked from commit commontk/PythonQt@4ce028d)
… macOS C++ builds

CPython’s `pyport.h` defines `_PY_PORT_CTYPE_UTF8_ISSUE` on macOS to work
around BSD libc ctype behavior in UTF-8 locales. Those ctype names can
surface as macros and collide with libstdc++’s `<locale>` templated
overloads (e.g., `std::isspace`), leading to compilation errors.

Previously we unconditionally `#undef`’d several ctype macros. Restrict
this to the affected environment by guarding with
`_PY_PORT_CTYPE_UTF8_ISSUE` and `__cplusplus`. This keeps macOS C++ builds
healthy while avoiding unnecessary macro tampering elsewhere.

Adapted from Kitware/VTK@a4fa448099 ("Remove the toupper macro defined by
Python.h.", 2015-11-12).
// Avoid clashes with libstdc++ <locale> by undefining ctype macros
// that CPython may introduce on macOS when the UTF-8 ctype quirk is enabled.
// (_PY_PORT_CTYPE_UTF8_ISSUE is defined by CPython’s pyport.h; we apply these
// undefs only in C++ builds.)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Searching for this reveals a number of similar issues in CPython itself, which they seem to have resolved by using the macros from pyctype.h like Py_ISALPHA etc.

@mrbean-bremen mrbean-bremen merged commit f1b466b into MeVisLab:master Sep 4, 2025
19 checks passed
@jcfr jcfr deleted the backport-commontk/fix-macos-ctype-clashes branch September 26, 2025 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants