Debian Codegraph @

src/base.pyx

raw
1 #-------------------------------------------------------------------------
2 # Copyright (c) Microsoft Corporation. All rights reserved.
3 # Licensed under the MIT License. See License.txt in the project root for
4 # license information.
5 #--------------------------------------------------------------------------
6
7 # Python imports
8 import logging
9
10 # C imports
11 cimport c_tlsio
12 cimport c_utils
13
14
15 _logger = logging.getLogger(__name__)
16
17
18 cdef class StructBase(object):
19 """Base class for wrapped C structs."""
20
21 def _memory_error(self):
22 message = "Failed to allocate memory to create {}"
23 raise MemoryError(message.format(self.__class__.__name__))
24
25 def _value_error(self, error_message=None, error_code=None):
26 message = "Operation failed."
27 if error_message:
28 message += "\nError: {}".format(error_message)
29 if error_code:
30 message += "\nErrorCode: {}".format(error_code)
31 raise ValueError(message)
32
33 def _null_error(self, error_message=None):
34 message = "NULL error occurred in {}.".format(self.__class__.__name__)
35 if error_message:
36 message += "\nError: {}".format(error_message)
37 raise ValueError(message)
38
39
40 cdef class TickCounter(object):
41
42 cdef c_utils.TICK_COUNTER_HANDLE _c_value
43
44 def __cinit__(self):
45 self._c_value = c_utils.tickcounter_create()
46 if <void*>self._c_value == NULL:
47 raise MemoryError("Failed to create tick counter.")
48
49 def __dealloc__(self):
50 self.destroy()
51
52 cpdef destroy(self):
53 if <void*>self._c_value != NULL:
54 c_utils.tickcounter_destroy(self._c_value)
55 self._c_value = <c_utils.TICK_COUNTER_HANDLE>NULL
56
57 cpdef get_current_ms(self):
58 cdef c_utils.tickcounter_ms_t current_ms
59 if c_utils.tickcounter_get_current_ms(self._c_value, &current_ms) != 0:
60 raise ValueError("Failed to get current ms time.")
61 return current_ms