|
29 | 29 | get_path, get_path_names, _INSTALL_SCHEMES, |
30 | 30 | get_default_scheme, get_scheme_names, get_config_var, |
31 | 31 | _expand_vars, _get_preferred_schemes, |
32 | | - is_python_build, _PROJECT_BASE) |
| 32 | + is_python_build, _PROJECT_BASE, parse_config_h) |
33 | 33 | from sysconfig.__main__ import _main, _parse_makefile, _get_pybuilddir, _get_json_data_name |
34 | 34 | import _imp |
35 | 35 | import _osx_support |
@@ -767,6 +767,72 @@ def test_sysconfig_config_vars_no_prefix_cache(self): |
767 | 767 | self.assertEqual(config_vars['exec_prefix'], sys.exec_prefix) |
768 | 768 | self.assertEqual(config_vars['platbase'], sys.exec_prefix) |
769 | 769 |
|
| 770 | + def test_parse_config_h(self): |
| 771 | + config = textwrap.dedent(''' |
| 772 | + #ifndef Py_PYCONFIG_H |
| 773 | + #define Py_PYCONFIG_H |
| 774 | +
|
| 775 | + /* C comment */ |
| 776 | +
|
| 777 | + #define ALIGNOF_LONG 8 |
| 778 | + #define HAVE_ACCEPT 1 |
| 779 | + #define _Py_HAVE_COSPI 1 |
| 780 | + #define INVALID_NUMBER abc |
| 781 | + #define ALT_SOABI "cpython-316t-x86_64-linux-gnu" |
| 782 | +
|
| 783 | + // Undef macros must be written as "/* #undef NAME */": |
| 784 | + // name must be valid and there is not value. |
| 785 | + /* #undef ANDROID_API_LEVEL */ |
| 786 | + #undef IGNORE_UNDEF |
| 787 | + /* #undef IGNORE_VALUE 1 */ |
| 788 | +
|
| 789 | + # _ALWAYS_STR: don't convert values to an integer, |
| 790 | + # but quotes are removed |
| 791 | + #define IPHONEOS_DEPLOYMENT_TARGET "13.0" |
| 792 | + #define MACOSX_DEPLOYMENT_TARGET 10 |
| 793 | +
|
| 794 | + // Spaces are tolerated after the name, not before |
| 795 | + #define SPACES_AFTER 1 |
| 796 | + #define IGNORED_SPACES_BEFORE 1 |
| 797 | +
|
| 798 | + // Ignore macro without value |
| 799 | + #define IGNORE_NO_VALUE |
| 800 | +
|
| 801 | + // Ignore macros with an invalid name |
| 802 | + #define _PRIVATE_IGNORED 1 |
| 803 | + #define aLOWER_IGNORED 1 |
| 804 | + #define 123IGNORED 1 |
| 805 | + #define INVALID-NAME 1 |
| 806 | + #define INVALID#NAME 1 |
| 807 | + #define NONASCII_NAME_é 1 |
| 808 | +
|
| 809 | + // Ignore single letter names |
| 810 | + #define A 1 |
| 811 | + /* #undef A */ |
| 812 | +
|
| 813 | + #endif /*Py_PYCONFIG_H*/ |
| 814 | + ''') |
| 815 | + |
| 816 | + filename = TESTFN |
| 817 | + self.addCleanup(unlink, filename) |
| 818 | + with open(filename, "w", encoding="utf-8") as fp: |
| 819 | + fp.write(config) |
| 820 | + vars = {} |
| 821 | + with open(filename, encoding="utf-8") as fp: |
| 822 | + parse_config_h(fp, vars) |
| 823 | + expected = { |
| 824 | + 'ALIGNOF_LONG': 8, |
| 825 | + 'HAVE_ACCEPT': 1, |
| 826 | + '_Py_HAVE_COSPI': 1, |
| 827 | + 'INVALID_NUMBER': 'abc', |
| 828 | + 'ALT_SOABI': 'cpython-316t-x86_64-linux-gnu', |
| 829 | + 'ANDROID_API_LEVEL': 0, |
| 830 | + 'IPHONEOS_DEPLOYMENT_TARGET': '13.0', |
| 831 | + 'MACOSX_DEPLOYMENT_TARGET': '10', # str, not int |
| 832 | + 'SPACES_AFTER': 1, |
| 833 | + } |
| 834 | + self.assertEqual(vars, expected) |
| 835 | + |
770 | 836 |
|
771 | 837 | class MakefileTests(unittest.TestCase): |
772 | 838 |
|
|
0 commit comments