Library of Assembled Shared Sources
python_common.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2025 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_PYTHON_PYTHON_COMMON_H
44#define LASS_GUARDIAN_OF_INCLUSION_PYTHON_PYTHON_COMMON_H
45
46#include "../lass_common.h"
47
48/** @namespace lass::python
49 * @brief Comprehensive C++ to Python binding library
50 *
51 * The lass::python namespace provides a comprehensive Python binding system that allows
52 * easy integration between C++ code and Python scripts. Lass Python bindings support
53 * both native Python-aware classes and shadow classes for existing C++ types.
54 *
55 * **Key Components:**
56 *
57 * - @ref ModuleDefinition "ModuleDefinition": Macros to create and configure Python modules
58 * - @ref ClassDefinition "ClassDefinition": Macros to export C++ classes as Python types
59 * - PyObjectPlus: Base class for Python-aware C++ objects
60 * - Shadow Classes: Wrapper system for existing non-Python-aware C++ classes
61 * - @ref PyExportTraits "PyExportTraits": Type conversion system between C++ and Python objects
62 * - Overload Resolution: Automatic dispatcher generation for overloaded functions/methods
63 *
64 * **Class Export Approaches:**
65 *
66 * 1. **Native Python Classes**: C++ classes that inherit from PyObjectPlus
67 * and are designed to be Python-compatible from the start
68 *
69 * 2. **Shadow Classes**: Wrapper classes for existing C++ types using
70 * PY_SHADOW_CLASS macros - the shadow inherits from PyObjectPlus and
71 * wraps the original type
72 *
73 * **Binding Features:**
74 *
75 * - Multiple constructor overloads with automatic dispatching
76 * - Method overloading mixing member functions and free functions
77 * - Property binding with getter/setter pairs or direct member access
78 * - Python special methods (`__add__`, `__str__`, `__eq__`, etc.)
79 * - Static methods and class constants
80 * - Nested classes and enumerations
81 * - Automatic type conversion and argument validation
82 * - Exception mapping between C++ and Python
83 */
84namespace lass::python
85{
86
87/** @defgroup Python Python bindings
88 * @copydoc lass::python
89 */
90
91}
92
93#ifdef LASS_PYTHON_DLL
94# undef LASS_PYTHON_DLL
95#endif
96#if LASS_SHARED_LIBRARY // yes, it's the same flag.
97# if defined(LASS_PYTHON_EXPORTS) || defined (lass_python_EXPORTS)
98# define LASS_PYTHON_DLL LASS_DLL_EXPORT
99# else
100# define LASS_PYTHON_DLL LASS_DLL_IMPORT
101# endif
102#else
103# define LASS_PYTHON_DLL LASS_DLL_EXPORT
104#endif
105
106// Python.h is a bit blunt in (re)defining _POSIX_C_SOURCE causing a nice warning.
107// Undefing it before including Python.h will suppress that warning.
108// Remove this once Python plays nice again.
109// [Bramz]
110#if defined(LASS_HAVE_PYTHON_POSIX_C_SOURCE) && defined(_POSIX_C_SOURCE)
111# undef _POSIX_C_SOURCE
112#endif
113#if defined(LASS_HAVE_PYTHON_FILE_OFFSET_BITS) && defined(_FILE_OFFSET_BITS)
114# undef _FILE_OFFSET_BITS
115#endif
116#if defined(LASS_HAVE_PYTHON_XOPEN_SOURCE) && defined(_XOPEN_SOURCE)
117# undef _XOPEN_SOURCE
118#endif
119
120// Python 3.12 yields following warning in unicodeobject.h:
121// warning C4100: '_unused_op': unreferenced formal parameter
122#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
123# pragma warning(push)
124# pragma warning(disable: 4100) // unreferenced formal parameter
125#endif
126
127// Python >= 3.2 defines `PyType_Spec` in `object.h` which has a member called
128// `slots`. When combining this with Qt, `slots` may already be defined as a
129// macro. It is sufficient to temporarily undefine `slots` since this member
130// is not being used on the client side.
131// `push_macro` is supported by MSVC, GCC and clang.
132#pragma push_macro("slots")
133#undef slots
134
135#if defined(_DEBUG) && LASS_PYTHON_HAS_DEBUG_BUILD == 0
136# undef _DEBUG
137# include "Python.h"
138# define _DEBUG
139#else
140# include "Python.h"
141#endif
142
143#pragma pop_macro("slots")
144
145#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
146# pragma warning(pop)
147#endif
148
149// oh r'ly? Because of some issue lost to ancient history, pyport.h will
150// redefine following functions on FreeBSD > 500039 (and Apple?),
151// causing really havoc with the C++ code.
152// Undefine! Hopefully it's not to late yet.
153#ifdef _PY_PORT_CTYPE_UTF8_ISSUE
154# undef isalnum
155# undef isalpha
156# undef islower
157# undef isspace
158# undef isupper
159# undef tolower
160# undef toupper
161#endif
162
163#endif
Comprehensive C++ to Python binding library.