@@ -28,58 +28,112 @@ extern "C"
2828 Py_RETURN_NONE;
2929 }
3030
31+ static PyObject* try_region_begin (PyObject* self, PyObject* args)
32+ {
33+ PyObject* identifier = nullptr ;
34+ if (!PyArg_ParseTuple (args, " O" , &identifier))
35+ {
36+ return NULL ;
37+ }
38+
39+ bool success = scorepy::try_region_begin (reinterpret_cast <PyCodeObject*>(identifier));
40+ if (success)
41+ {
42+ Py_RETURN_TRUE;
43+ }
44+ else
45+ {
46+ Py_RETURN_FALSE;
47+ }
48+ }
49+
3150 /* * This code is not thread save. However, this does not matter as the python GIL is not
3251 * released.
3352 */
3453 static PyObject* region_begin (PyObject* self, PyObject* args)
3554 {
36- const char * module ;
37- const char * function_name;
38- const char * file_name;
55+ const char * module_cstr;
56+ const char * function_name_cstr;
57+ const char * file_name_cstr;
58+ Py_ssize_t module_len;
59+ Py_ssize_t function_name_len;
60+ Py_ssize_t file_name_len;
61+
3962 PyObject* identifier = nullptr ;
4063 std::uint64_t line_number = 0 ;
4164
42- if (!PyArg_ParseTuple (args, " sssKO" , &module , &function_name, &file_name, &line_number,
65+ if (!PyArg_ParseTuple (args, " s#s#s#KO" , &module_cstr, &module_len, &function_name_cstr,
66+ &function_name_len, &file_name_cstr, &file_name_len, &line_number,
4367 &identifier))
4468 {
4569 return NULL ;
4670 }
4771
72+ std::string_view module (module_cstr, module_len);
73+ std::string_view function_name (function_name_cstr, function_name_len);
74+ std::string_view file_name (file_name_cstr, file_name_len);
75+
76+ std::string file_name_abs = scorepy::abspath (file_name);
77+
4878 if (identifier == nullptr or identifier == Py_None)
4979 {
50- scorepy::region_begin (function_name, module , file_name , line_number);
80+ scorepy::region_begin (function_name, module , file_name_abs , line_number);
5181 }
5282 else
5383 {
54- scorepy::region_begin (function_name, module , file_name , line_number,
55- reinterpret_cast <std:: uintptr_t >(identifier));
84+ scorepy::region_begin (function_name, module , file_name_abs , line_number,
85+ reinterpret_cast <PyCodeObject* >(identifier));
5686 }
5787
5888 Py_RETURN_NONE;
5989 }
6090
91+ static PyObject* try_region_end (PyObject* self, PyObject* args)
92+ {
93+ PyObject* identifier = nullptr ;
94+ if (!PyArg_ParseTuple (args, " O" , &identifier))
95+ {
96+ return NULL ;
97+ }
98+
99+ bool success = scorepy::try_region_end (reinterpret_cast <PyCodeObject*>(identifier));
100+ if (success)
101+ {
102+ Py_RETURN_TRUE;
103+ }
104+ else
105+ {
106+ Py_RETURN_FALSE;
107+ }
108+ }
109+
61110 /* * This code is not thread save. However, this does not matter as the python GIL is not
62111 * released.
63112 */
64113 static PyObject* region_end (PyObject* self, PyObject* args)
65114 {
66- const char * module ;
67- const char * function_name;
115+ const char * module_cstr;
116+ const char * function_name_cstr;
117+ Py_ssize_t module_len;
118+ Py_ssize_t function_name_len;
68119 PyObject* identifier = nullptr ;
69120
70- if (!PyArg_ParseTuple (args, " ssO" , &module , &function_name, &identifier))
121+ if (!PyArg_ParseTuple (args, " s#s#O" , &module_cstr, &module_len, &function_name_cstr,
122+ &function_name_len, &identifier))
71123 {
72124 return NULL ;
73125 }
74126
127+ std::string_view module (module_cstr, module_len);
128+ std::string_view function_name (function_name_cstr, function_name_len);
129+
75130 if (identifier == nullptr or identifier == Py_None)
76131 {
77132 scorepy::region_end (function_name, module );
78133 }
79134 else
80135 {
81- scorepy::region_end (function_name, module ,
82- reinterpret_cast <std::uintptr_t >(identifier));
136+ scorepy::region_end (function_name, module , reinterpret_cast <PyCodeObject*>(identifier));
83137 }
84138
85139 Py_RETURN_NONE;
@@ -182,7 +236,11 @@ extern "C"
182236
183237 static PyMethodDef ScorePMethods[] = {
184238 { " region_begin" , region_begin, METH_VARARGS, " enter a region." },
239+ { " try_region_begin" , try_region_begin, METH_VARARGS,
240+ " Tries to begin a region, returns True on Sucess." },
185241 { " region_end" , region_end, METH_VARARGS, " exit a region." },
242+ { " try_region_end" , try_region_end, METH_VARARGS,
243+ " Tries to end a region, returns True on Sucess." },
186244 { " rewind_begin" , rewind_begin, METH_VARARGS, " rewind begin." },
187245 { " rewind_end" , rewind_end, METH_VARARGS, " rewind end." },
188246 { " enable_recording" , enable_recording, METH_VARARGS, " disable scorep recording." },
0 commit comments