@@ -2834,6 +2834,18 @@ do { \
2834
2834
} \
2835
2835
} while (0)
2836
2836
2837
+ #define ADD_ULONG_CONSTANT (module , name , value ) \
2838
+ do { \
2839
+ PyObject *o = PyLong_FromUnsignedLong(value); \
2840
+ if (!o) \
2841
+ goto error; \
2842
+ int res = PyModule_AddObjectRef(module, name, o); \
2843
+ Py_DECREF(o); \
2844
+ if (res <) { \
2845
+ goto error; \
2846
+ } \
2847
+ } while (0)
2848
+
2837
2849
static int
2838
2850
sre_exec (PyObject * m )
2839
2851
{
@@ -2847,37 +2859,21 @@ sre_exec(PyObject *m)
2847
2859
CREATE_TYPE (m , state -> Match_Type , & match_spec );
2848
2860
CREATE_TYPE (m , state -> Scanner_Type , & scanner_spec );
2849
2861
2850
- d = PyModule_GetDict (m );
2851
-
2852
- x = PyLong_FromLong (SRE_MAGIC );
2853
- if (x ) {
2854
- PyDict_SetItemString (d , "MAGIC" , x );
2855
- Py_DECREF (x );
2862
+ if (PyModule_AddIntConstant (m , "MAGIC" , SRE_MAGIC ) < 0 ) {
2863
+ goto error ;
2856
2864
}
2857
2865
2858
- x = PyLong_FromLong (sizeof (SRE_CODE ));
2859
- if (x ) {
2860
- PyDict_SetItemString (d , "CODESIZE" , x );
2861
- Py_DECREF (x );
2866
+ if (PyModule_AddIntConstant (m , "CODESIZE" , sizeof (SRE_CODE )) < 0 ) {
2867
+ goto error ;
2862
2868
}
2863
2869
2864
- x = PyLong_FromUnsignedLong (SRE_MAXREPEAT );
2865
- if (x ) {
2866
- PyDict_SetItemString (d , "MAXREPEAT" , x );
2867
- Py_DECREF (x );
2868
- }
2870
+ ADD_ULONG_CONSTANT (m , "MAXREPEAT" , SRE_MAXREPEAT );
2871
+ ADD_ULONG_CONSTANT (m , "MAXGROUPS" , SRE_MAXGROUPS );
2869
2872
2870
- x = PyLong_FromUnsignedLong (SRE_MAXGROUPS );
2871
- if (x ) {
2872
- PyDict_SetItemString (d , "MAXGROUPS" , x );
2873
- Py_DECREF (x );
2873
+ if (PyModule_AddStringConstant (m , "copyright" , copyright ) < 0 ) {
2874
+ goto error ;
2874
2875
}
2875
2876
2876
- x = PyUnicode_FromString (copyright );
2877
- if (x ) {
2878
- PyDict_SetItemString (d , "copyright" , x );
2879
- Py_DECREF (x );
2880
- }
2881
2877
return 0 ;
2882
2878
2883
2879
error :
0 commit comments