bpo-32030: Add _PyMainInterpreterConfig.warnoptions (#4855) · python/cpython@374c6e1 (original) (raw)
`@@ -752,29 +752,67 @@ pymain_parse_cmdline_impl(_PyMain *pymain)
`
752
752
``
753
753
``
754
754
`static int
`
755
``
`-
pymain_add_xoptions(_PyMain *pymain)
`
``
755
`+
pymain_add_xoption(PyObject *opts, const wchar_t *s)
`
``
756
`+
{
`
``
757
`+
PyObject *name, *value;
`
``
758
+
``
759
`+
const wchar_t *name_end = wcschr(s, L'=');
`
``
760
`+
if (!name_end) {
`
``
761
`+
name = PyUnicode_FromWideChar(s, -1);
`
``
762
`+
value = Py_True;
`
``
763
`+
Py_INCREF(value);
`
``
764
`+
}
`
``
765
`+
else {
`
``
766
`+
name = PyUnicode_FromWideChar(s, name_end - s);
`
``
767
`+
value = PyUnicode_FromWideChar(name_end + 1, -1);
`
``
768
`+
}
`
``
769
`+
if (name == NULL || value == NULL) {
`
``
770
`+
goto error;
`
``
771
`+
}
`
``
772
`+
if (PyDict_SetItem(opts, name, value) < 0) {
`
``
773
`+
goto error;
`
``
774
`+
}
`
``
775
`+
Py_DECREF(name);
`
``
776
`+
Py_DECREF(value);
`
``
777
`+
return 0;
`
``
778
+
``
779
`+
error:
`
``
780
`+
Py_XDECREF(name);
`
``
781
`+
Py_XDECREF(value);
`
``
782
`+
return -1;
`
``
783
`+
}
`
``
784
+
``
785
`+
static int
`
``
786
`+
pymain_init_xoptions_dict(_PyMain *pymain)
`
756
787
`{
`
757
788
`_Py_OptList *options = &pymain->cmdline.xoptions;
`
``
789
`+
PyObject *dict = PyDict_New();
`
``
790
`+
if (dict == NULL) {
`
``
791
`+
return -1;
`
``
792
`+
}
`
``
793
+
758
794
`for (size_t i=0; i < options->len; i++) {
`
759
795
`wchar_t *option = options->options[i];
`
760
``
`-
if (_PySys_AddXOptionWithError(option) < 0) {
`
761
``
`-
pymain->err = _Py_INIT_NO_MEMORY();
`
``
796
`+
if (pymain_add_xoption(dict, option) < 0) {
`
``
797
`+
Py_DECREF(dict);
`
762
798
`return -1;
`
763
799
` }
`
764
800
` }
`
``
801
+
``
802
`+
pymain->config.xoptions = dict;
`
765
803
`return 0;
`
766
804
`}
`
767
805
``
768
806
``
769
807
`static int
`
770
``
`-
pymain_add_warnings_optlist(_Py_OptList *warnings)
`
``
808
`+
pymain_add_warnings_optlist(PyObject *warnoptions, _Py_OptList *warnings)
`
771
809
`{
`
772
810
`for (size_t i = 0; i < warnings->len; i++) {
`
773
811
`PyObject *option = PyUnicode_FromWideChar(warnings->options[i], -1);
`
774
812
`if (option == NULL) {
`
775
813
`return -1;
`
776
814
` }
`
777
``
`-
if (_PySys_AddWarnOptionWithError(option)) {
`
``
815
`+
if (PyList_Append(warnoptions, option)) {
`
778
816
`Py_DECREF(option);
`
779
817
`return -1;
`
780
818
` }
`
`@@ -785,14 +823,14 @@ pymain_add_warnings_optlist(_Py_OptList *warnings)
`
785
823
``
786
824
``
787
825
`static int
`
788
``
`-
pymain_add_warning_dev_mode(_PyCoreConfig *core_config)
`
``
826
`+
pymain_add_warning_dev_mode(PyObject *warnoptions, _PyCoreConfig *core_config)
`
789
827
`{
`
790
828
`if (core_config->dev_mode) {
`
791
829
`PyObject *option = PyUnicode_FromString("default");
`
792
830
`if (option == NULL) {
`
793
831
`return -1;
`
794
832
` }
`
795
``
`-
if (_PySys_AddWarnOptionWithError(option)) {
`
``
833
`+
if (PyList_Append(warnoptions, option)) {
`
796
834
`Py_DECREF(option);
`
797
835
`return -1;
`
798
836
` }
`
`@@ -803,33 +841,38 @@ pymain_add_warning_dev_mode(_PyCoreConfig *core_config)
`
803
841
``
804
842
``
805
843
`static int
`
806
``
`-
pymain_add_warning_bytes_flag(int bytes_warning_flag)
`
``
844
`+
pymain_add_warning_bytes_flag(PyObject *warnoptions, int bytes_warning_flag)
`
807
845
`{
`
808
846
`/* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
`
809
847
` * don't even try to emit a warning, so we skip setting the filter in that
`
810
848
` * case.
`
811
849
` */
`
812
``
`-
if (bytes_warning_flag) {
`
813
``
`-
const char *filter = (bytes_warning_flag > 1) ? "error::BytesWarning":
`
814
``
`-
"default::BytesWarning";
`
815
``
`-
PyObject *option = PyUnicode_FromString(filter);
`
816
``
`-
if (option == NULL) {
`
817
``
`-
return -1;
`
818
``
`-
}
`
819
``
`-
if (_PySys_AddWarnOptionWithError(option)) {
`
820
``
`-
Py_DECREF(option);
`
821
``
`-
return -1;
`
822
``
`-
}
`
``
850
`+
if (!bytes_warning_flag) {
`
``
851
`+
return 0;
`
``
852
`+
}
`
``
853
+
``
854
`+
const char *filter = (bytes_warning_flag > 1) ? "error::BytesWarning":
`
``
855
`+
"default::BytesWarning";
`
``
856
`+
PyObject *option = PyUnicode_FromString(filter);
`
``
857
`+
if (option == NULL) {
`
``
858
`+
return -1;
`
``
859
`+
}
`
``
860
`+
if (PyList_Append(warnoptions, option)) {
`
823
861
`Py_DECREF(option);
`
``
862
`+
return -1;
`
824
863
` }
`
``
864
`+
Py_DECREF(option);
`
825
865
`return 0;
`
826
866
`}
`
827
867
``
828
868
``
829
869
`static int
`
830
``
`-
pymain_add_warnings_options(_PyMain *pymain)
`
``
870
`+
pymain_init_warnoptions(_PyMain *pymain)
`
831
871
`{
`
832
``
`-
PySys_ResetWarnOptions();
`
``
872
`+
PyObject *warnoptions = PyList_New(0);
`
``
873
`+
if (warnoptions == NULL) {
`
``
874
`+
return -1;
`
``
875
`+
}
`
833
876
``
834
877
`/* The priority order for warnings configuration is (highest precedence
`
835
878
` * first):
`
`@@ -846,23 +889,25 @@ pymain_add_warnings_options(_PyMain *pymain)
`
846
889
` * the lowest precedence entries first so that later entries override them.
`
847
890
` */
`
848
891
``
849
``
`-
if (pymain_add_warning_dev_mode(&pymain->core_config) < 0) {
`
850
``
`-
pymain->err = _Py_INIT_NO_MEMORY();
`
851
``
`-
return -1;
`
``
892
`+
if (pymain_add_warning_dev_mode(warnoptions, &pymain->core_config) < 0) {
`
``
893
`+
goto error;
`
852
894
` }
`
853
``
`-
if (pymain_add_warnings_optlist(&pymain->env_warning_options) < 0) {
`
854
``
`-
pymain->err = _Py_INIT_NO_MEMORY();
`
855
``
`-
return -1;
`
``
895
`+
if (pymain_add_warnings_optlist(warnoptions, &pymain->env_warning_options) < 0) {
`
``
896
`+
goto error;
`
856
897
` }
`
857
``
`-
if (pymain_add_warnings_optlist(&pymain->cmdline.warning_options) < 0) {
`
858
``
`-
pymain->err = _Py_INIT_NO_MEMORY();
`
859
``
`-
return -1;
`
``
898
`+
if (pymain_add_warnings_optlist(warnoptions, &pymain->cmdline.warning_options) < 0) {
`
``
899
`+
goto error;
`
860
900
` }
`
861
``
`-
if (pymain_add_warning_bytes_flag(pymain->cmdline.bytes_warning) < 0) {
`
862
``
`-
pymain->err = _Py_INIT_NO_MEMORY();
`
863
``
`-
return -1;
`
``
901
`+
if (pymain_add_warning_bytes_flag(warnoptions, pymain->cmdline.bytes_warning) < 0) {
`
``
902
`+
goto error;
`
864
903
` }
`
``
904
+
``
905
`+
pymain->config.warnoptions = warnoptions;
`
865
906
`return 0;
`
``
907
+
``
908
`+
error:
`
``
909
`+
Py_DECREF(warnoptions);
`
``
910
`+
return -1;
`
866
911
`}
`
867
912
``
868
913
``
`@@ -1092,7 +1137,7 @@ pymain_header(_PyMain *pymain)
`
1092
1137
``
1093
1138
``
1094
1139
`static int
`
1095
``
`-
pymain_create_argv_list(_PyMain *pymain)
`
``
1140
`+
pymain_init_argv(_PyMain *pymain)
`
1096
1141
`{
`
1097
1142
`int argc = pymain->sys_argc;
`
1098
1143
`wchar_t** argv = pymain->sys_argv;
`
`@@ -1918,7 +1963,6 @@ _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *config, _PyCoreConfig *c
`
1918
1963
`return err;
`
1919
1964
` }
`
1920
1965
` }
`
1921
``
-
1922
1966
`return _Py_INIT_OK();
`
1923
1967
`}
`
1924
1968
``
`@@ -1941,15 +1985,15 @@ pymain_init_python_core(_PyMain *pymain)
`
1941
1985
`static int
`
1942
1986
`pymain_init_python_main(_PyMain *pymain)
`
1943
1987
`{
`
1944
``
`-
if (pymain_add_xoptions(pymain)) {
`
``
1988
`+
if (pymain_init_xoptions_dict(pymain)) {
`
``
1989
`+
pymain->err = _Py_INIT_NO_MEMORY();
`
1945
1990
`return -1;
`
1946
1991
` }
`
1947
``
`-
if (pymain_add_warnings_options(pymain)) {
`
``
1992
`+
if (pymain_init_warnoptions(pymain)) {
`
``
1993
`+
pymain->err = _Py_INIT_NO_MEMORY();
`
1948
1994
`return -1;
`
1949
1995
` }
`
1950
``
-
1951
``
`-
/* Create sys.argv list */
`
1952
``
`-
if (pymain_create_argv_list(pymain) < 0) {
`
``
1996
`+
if (pymain_init_argv(pymain) < 0) {
`
1953
1997
`pymain->err = _Py_INIT_ERR("failed to create sys.argv");
`
1954
1998
`return -1;
`
1955
1999
` }
`