python-undef 0.1.1__tar.gz → 0.1.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python_undef
3
- Version: 0.1.1
3
+ Version: 0.1.5
4
4
  Summary: Python undefined header in pyconfig.h
5
5
  Project-URL: homepage, https://github.com/Locked-chess-official/python_undef
6
6
  Author-email: Locked-chess-official <13140752715@163.com>
@@ -10,6 +10,12 @@ Description-Content-Type: text/markdown
10
10
 
11
11
  # Python_undef
12
12
 
13
+ This is a Python script that generates a header file "Python_undef.h" which undefine many macros in "pyconfig.h" that doesn't match the rule that "should start with PY_".
14
+
15
+ ## Why
16
+
17
+ The "pyconfig.h" continue many macros that doesn't math the rule that "should start with PY_" which may cause the comflict with the other projects. This project undefines them.
18
+
13
19
  ## Download
14
20
 
15
21
  ```bash
@@ -18,13 +24,23 @@ pip install python_undef
18
24
 
19
25
  ## Usage
20
26
 
21
- This will create the file "Python_undef.h"
27
+ This command under will create the file "Python_undef.h"
22
28
 
23
29
  ```bash
24
30
  python -m python_undef --generate
25
31
  ```
26
32
 
27
- This will output the include path of "Python_undef.h"
33
+ It defaults output the file "Python_undef.h" in the package directory. You can use the `--output` option to specify the output file:
34
+
35
+ ```bash
36
+ python -m python_undef --generate --output <path>
37
+ ```
38
+
39
+ The command under will output the include path of "Python_undef.h".
40
+
41
+ You can use this command in your C/C++ project such as "cmake", "gyp" and so on.
42
+
43
+ If hadn't run `python -m python_undef --generate` it will exit with code 1.
28
44
 
29
45
  ```bash
30
46
  python -m python_undef --include
@@ -0,0 +1,53 @@
1
+ # Python_undef
2
+
3
+ This is a Python script that generates a header file "Python_undef.h" which undefine many macros in "pyconfig.h" that doesn't match the rule that "should start with PY_".
4
+
5
+ ## Why
6
+
7
+ The "pyconfig.h" continue many macros that doesn't math the rule that "should start with PY_" which may cause the comflict with the other projects. This project undefines them.
8
+
9
+ ## Download
10
+
11
+ ```bash
12
+ pip install python_undef
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ This command under will create the file "Python_undef.h"
18
+
19
+ ```bash
20
+ python -m python_undef --generate
21
+ ```
22
+
23
+ It defaults output the file "Python_undef.h" in the package directory. You can use the `--output` option to specify the output file:
24
+
25
+ ```bash
26
+ python -m python_undef --generate --output <path>
27
+ ```
28
+
29
+ The command under will output the include path of "Python_undef.h".
30
+
31
+ You can use this command in your C/C++ project such as "cmake", "gyp" and so on.
32
+
33
+ If hadn't run `python -m python_undef --generate` it will exit with code 1.
34
+
35
+ ```bash
36
+ python -m python_undef --include
37
+ ```
38
+
39
+ You can include the "Python_undef.h" file in your project:
40
+
41
+ ```c
42
+ #include <Python.h>
43
+ #include <Python_undef.h>
44
+ #include <other_header.h>
45
+ ```
46
+
47
+ The "pyconfig.h" continue many macros that doesn't math the rule that "should start with PY_". This file undefine them.
48
+
49
+ If you want to save the macro, use `#define DONOTUNDEF_macro_name` before include "Python_undef.h" to keep it.
50
+
51
+ ## License
52
+
53
+ MIT License
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "python_undef"
7
- version = "0.1.1"
7
+ version = "0.1.5"
8
8
  description = "Python undefined header in pyconfig.h"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
@@ -1,9 +1,24 @@
1
+ """
2
+ python_undef - A utility to generate a header file that undefines non-standard Python macros in pyconfig.h.
3
+
4
+ Usage:
5
+ python -m python_undef --generate
6
+ This is the main command to generate Python_undef.h based on the system's pyconfig.h to the include directly under the package directly.
7
+ python -m python_undef --generate --output <path>
8
+ This command generates Python_undef.h and saves it to the specified output path.
9
+ python -m python_undef --include
10
+ This command prints the include path where Python_undef.h is located.
11
+ python -m python_undef --help
12
+ Display this help message.
13
+ """
14
+
1
15
  import os
2
16
  import re
3
17
  import datetime
4
18
  import sys
19
+ from pathlib import Path
5
20
 
6
- def is_valid_macro_name(macro_name):
21
+ def is_valid_macro_name(macro_name: str):
7
22
  """
8
23
  Determine whether a macro name is valid using Python's standard library methods.
9
24
 
@@ -20,7 +35,7 @@ def is_valid_macro_name(macro_name):
20
35
  # Use str.isidentifier() to check for valid identifier syntax
21
36
  return macro_name.isidentifier()
22
37
 
23
- def extract_macro_name(line):
38
+ def extract_macro_name(line: str):
24
39
  """Extract the macro name from a #define line (handles spaces between # and define)."""
25
40
  line = line.strip()
26
41
 
@@ -36,7 +51,7 @@ def extract_macro_name(line):
36
51
  return candidate
37
52
  return None
38
53
 
39
- def is_standard_python_macro(macro_name):
54
+ def is_standard_python_macro(macro_name: str):
40
55
  """
41
56
  Check whether a macro follows Python's standard naming conventions.
42
57
  Rules: Starts with Py, PY, _Py, _PY, or ends with _H.
@@ -44,7 +59,7 @@ def is_standard_python_macro(macro_name):
44
59
  standard_prefixes = ('Py', 'PY', '_Py', '_PY')
45
60
  return macro_name.startswith(standard_prefixes) or macro_name.endswith('_H')
46
61
 
47
- def generate_undef_code(macro_name):
62
+ def generate_undef_code(macro_name: str):
48
63
  """Generate the code to undefine a macro."""
49
64
  return f"""#ifndef DONOTUNDEF_{macro_name}
50
65
  #ifdef {macro_name}
@@ -54,7 +69,7 @@ def generate_undef_code(macro_name):
54
69
 
55
70
  """
56
71
 
57
- def generate_python_undef_header(pyconfig_path, output_path=None):
72
+ def generate_python_undef_header(pyconfig_path: str, output_path: str|None=None):
58
73
  """
59
74
  Generate the Python_undef.h header file.
60
75
 
@@ -64,19 +79,24 @@ def generate_python_undef_header(pyconfig_path, output_path=None):
64
79
  """
65
80
  if output_path is None:
66
81
  file_dir = os.path.dirname(os.path.abspath(__file__))
67
- output_path = f'{file_dir}/include/Python_undef.h'
68
- if not os.path.exists(f'{file_dir}/include'):
69
- os.makedirs(f'{file_dir}/include')
82
+ include_dir = Path(file_dir) / 'include'
83
+ output_path = str(include_dir / 'Python_undef.h')
84
+ if not include_dir.exists():
85
+ try:
86
+ os.makedirs(f'{file_dir}/include')
87
+ except Exception as e:
88
+ print(f"Error creating include directory: {e}", file=sys.stderr)
89
+ return False
70
90
 
71
91
  # Read pyconfig.h
72
92
  try:
73
93
  with open(pyconfig_path, 'r', encoding='utf-8') as f:
74
94
  lines = f.readlines()
75
95
  except FileNotFoundError:
76
- print(f"Error: File not found {pyconfig_path}")
96
+ print(f"Error: File not found {pyconfig_path}", file=sys.stderr)
77
97
  return False
78
98
  except Exception as e:
79
- print(f"Error reading file: {e}")
99
+ print(f"Error reading file: {e}", file=sys.stderr)
80
100
  return False
81
101
 
82
102
  # Collect macros
@@ -126,7 +146,7 @@ def generate_python_undef_header(pyconfig_path, output_path=None):
126
146
  * #define DONOTUNDEF_MACRO_NAME
127
147
  *
128
148
  * Generation rules:
129
- * - Macros starting with Py_, PY_, _Py, _PY are preserved (Python standard)
149
+ * - Macros starting with Py, PY, _Py, _PY are preserved (Python standard)
130
150
  * - Macros ending with _H are preserved (header guards)
131
151
  * - All other macros are undefined
132
152
  * - Macro name validation uses Python's standard identifier checking
@@ -194,26 +214,28 @@ def generate_python_undef_header(pyconfig_path, output_path=None):
194
214
  print(f" ... and {len(macros_to_undef) - 50} more")
195
215
 
196
216
  print(f"\nUsage Notes:")
197
- print(f" 1. Include this file before including other library headers.")
217
+ print(f" 1. Include this file before including other library headers but must be after '<Python.h>'.")
198
218
  print(f" 2. Use DONOTUNDEF_XXX to protect macros that must be kept.")
199
219
  print(f" 3. Regenerate this file whenever rebuilding Python.")
200
220
 
201
221
  return True
202
222
 
203
223
  except Exception as e:
204
- print(f"Error writing file: {e}")
224
+ print(f"Error writing file: {e}", file=sys.stderr)
205
225
  return False
206
226
 
207
227
  def main():
208
228
  import sysconfig
209
- from pathlib import Path
210
229
  if sys.argv[1:]:
211
230
  if sys.argv[1] in ('-h', '--help'):
212
231
  print("""Usage:
213
232
  python -m python_undef --generate
214
- Generate Python_undef.h based on the system's pyconfig.h.
233
+ Generate Python_undef.h based on the system's pyconfig.h to the include directly under the package directly.
234
+ python -m python_undef --generate --output <path>
235
+ Generate Python_undef.h and specify output path.
215
236
  python -m python_undef --include
216
237
  Print the include path where Python_undef.h is located.""")
238
+ sys.exit(0)
217
239
  elif sys.argv[1] == '--generate':
218
240
  include_dir = Path(sysconfig.get_path('include'))
219
241
  print(f"\n{'='*60}")
@@ -223,30 +245,44 @@ python -m python_undef --include
223
245
  pyconfig_path = include_dir / "pyconfig.h"
224
246
 
225
247
  if os.path.exists(pyconfig_path):
226
- success = generate_python_undef_header(pyconfig_path)
227
-
248
+ if sys.argv[2:]:
249
+ if sys.argv[2] == "--output" and len(sys.argv) == 4:
250
+ if not os.path.isdir(sys.argv[3]):
251
+ print("Error: Specified output path does not exist.", file=sys.stderr)
252
+ sys.exit(1)
253
+ output_path = str(Path(sys.argv[3]) / "Python_undef.h")
254
+ print(f"Output path specified: {output_path}")
255
+ else:
256
+ print("Invalid output argument. Use --output <path> to specify output file.", file=sys.stderr)
257
+ sys.exit(1)
258
+ else:
259
+ output_path = None
260
+ success = generate_python_undef_header(pyconfig_path, output_path)
261
+
228
262
  if success:
229
263
  print(f"\n✅ Generation complete!")
230
- print(f"💡 Tip: Place Python_undef.h inside Python include search path.")
264
+ if not output_path:
265
+ print(f"💡 Tip: Use '{sys.executable} -m python_undef --include' to add this header file path to search path.")
266
+ sys.exit(0)
231
267
  else:
232
- print(f"\n❌ Generation failed!")
233
-
268
+ print(f"\n❌ Generation failed!", file=sys.stderr)
269
+ sys.exit(1)
270
+
234
271
  else:
235
- print(f"File {pyconfig_path} not found.")
236
- print("Please update the pyconfig_path variable to the actual pyconfig.h path.")
237
- print("\nTypical paths on Windows:")
238
- print(" C:\\\\Python3x\\\\include\\\\pyconfig.h")
239
- print("\nTypical paths on Unix/Linux:")
240
- print(" /usr/include/python3.x/pyconfig.h")
241
- print(" /usr/local/include/python3.x/pyconfig.h")
272
+ print(f"File {pyconfig_path} not found.", file=sys.stderr)
273
+ print("Please ensure the python is standard installation with headers.", file=sys.stderr)
274
+ sys.exit(1)
242
275
  elif sys.argv[1] == "--include":
243
276
  file_dir = os.path.dirname(os.path.abspath(__file__))
244
- if not Path(file_dir).exists():
245
- print("File not found. Use 'python -m python_undef --generate' to generate the header first.")
246
- return
277
+ if not (Path(file_dir) / "include" / "Python_undef.h").exists():
278
+ print(f"File not found. Use '{sys.executable} -m python_undef --generate' to generate the header first.", file=sys.stderr)
279
+ sys.exit(1)
247
280
  include_path = os.path.abspath(os.path.join(file_dir, 'include'))
248
281
  print(include_path)
282
+ sys.exit(0)
249
283
  else:
250
- print("Unknown argument. Use --help for usage information.")
284
+ print("Unknown argument. Use --help for usage information.", file=sys.stderr)
285
+ sys.exit(1)
251
286
  else:
252
- print("No arguments provided. Use --help for usage information.")
287
+ print("No arguments provided. Use --help for usage information.", file=sys.stderr)
288
+ sys.exit(1)
@@ -1,37 +0,0 @@
1
- # Python_undef
2
-
3
- ## Download
4
-
5
- ```bash
6
- pip install python_undef
7
- ```
8
-
9
- ## Usage
10
-
11
- This will create the file "Python_undef.h"
12
-
13
- ```bash
14
- python -m python_undef --generate
15
- ```
16
-
17
- This will output the include path of "Python_undef.h"
18
-
19
- ```bash
20
- python -m python_undef --include
21
- ```
22
-
23
- You can include the "Python_undef.h" file in your project:
24
-
25
- ```c
26
- #include <Python.h>
27
- #include <Python_undef.h>
28
- #include <other_header.h>
29
- ```
30
-
31
- The "pyconfig.h" continue many macros that doesn't math the rule that "should start with PY_". This file undefine them.
32
-
33
- If you want to save the macro, use `#define DONOTUNDEF_macro_name` before include "Python_undef.h" to keep it.
34
-
35
- ## License
36
-
37
- MIT License
File without changes
File without changes