1 /* Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 */
2 /* For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt */
3 
4 #include "util.h"
5 #include "tracer.h"
6 #include "filedisp.h"
7 
8 /* Module definition */
9 
10 #define MODULE_DOC PyDoc_STR("Fast coverage tracer.")
11 
12 #if PY_MAJOR_VERSION >= 3
13 
14 static PyModuleDef
15 moduledef = {
16     PyModuleDef_HEAD_INIT,
17     "coverage.tracer",
18     MODULE_DOC,
19     -1,
20     NULL,       /* methods */
21     NULL,
22     NULL,       /* traverse */
23     NULL,       /* clear */
24     NULL
25 };
26 
27 
28 PyObject *
PyInit_tracer(void)29 PyInit_tracer(void)
30 {
31     PyObject * mod = PyModule_Create(&moduledef);
32     if (mod == NULL) {
33         return NULL;
34     }
35 
36     if (CTracer_intern_strings() < 0) {
37         return NULL;
38     }
39 
40     /* Initialize CTracer */
41     CTracerType.tp_new = PyType_GenericNew;
42     if (PyType_Ready(&CTracerType) < 0) {
43         Py_DECREF(mod);
44         return NULL;
45     }
46 
47     Py_INCREF(&CTracerType);
48     if (PyModule_AddObject(mod, "CTracer", (PyObject *)&CTracerType) < 0) {
49         Py_DECREF(mod);
50         Py_DECREF(&CTracerType);
51         return NULL;
52     }
53 
54     /* Initialize CFileDisposition */
55     CFileDispositionType.tp_new = PyType_GenericNew;
56     if (PyType_Ready(&CFileDispositionType) < 0) {
57         Py_DECREF(mod);
58         Py_DECREF(&CTracerType);
59         return NULL;
60     }
61 
62     Py_INCREF(&CFileDispositionType);
63     if (PyModule_AddObject(mod, "CFileDisposition", (PyObject *)&CFileDispositionType) < 0) {
64         Py_DECREF(mod);
65         Py_DECREF(&CTracerType);
66         Py_DECREF(&CFileDispositionType);
67         return NULL;
68     }
69 
70     return mod;
71 }
72 
73 #else
74 
75 void
inittracer(void)76 inittracer(void)
77 {
78     PyObject * mod;
79 
80     mod = Py_InitModule3("coverage.tracer", NULL, MODULE_DOC);
81     if (mod == NULL) {
82         return;
83     }
84 
85     if (CTracer_intern_strings() < 0) {
86         return;
87     }
88 
89     /* Initialize CTracer */
90     CTracerType.tp_new = PyType_GenericNew;
91     if (PyType_Ready(&CTracerType) < 0) {
92         return;
93     }
94 
95     Py_INCREF(&CTracerType);
96     PyModule_AddObject(mod, "CTracer", (PyObject *)&CTracerType);
97 
98     /* Initialize CFileDisposition */
99     CFileDispositionType.tp_new = PyType_GenericNew;
100     if (PyType_Ready(&CFileDispositionType) < 0) {
101         return;
102     }
103 
104     Py_INCREF(&CFileDispositionType);
105     PyModule_AddObject(mod, "CFileDisposition", (PyObject *)&CFileDispositionType);
106 }
107 
108 #endif /* Py3k */
109