|
1 | 1 | import functools
|
2 | 2 | import uuid
|
3 | 3 |
|
| 4 | +cimport cython |
| 5 | +cimport cpython |
| 6 | + |
4 | 7 | from libc.stdint cimport uint8_t, int8_t
|
5 | 8 | from libc.string cimport memcpy, memcmp
|
6 | 9 |
|
7 | 10 |
|
| 11 | +cdef extern from "Python.h": |
| 12 | + int PyUnicode_1BYTE_KIND |
| 13 | + const char* PyUnicode_AsUTF8AndSize( |
| 14 | + object unicode, Py_ssize_t *size) except NULL |
| 15 | + object PyUnicode_FromKindAndData( |
| 16 | + int kind, const void *buffer, Py_ssize_t size) |
| 17 | + |
| 18 | + |
| 19 | +cdef extern from "./tohex.h": |
| 20 | + cdef void uuid_to_str(const char *source, char *dest) |
| 21 | + cdef void uuid_to_hex(const char *source, char *dest) |
| 22 | + |
| 23 | + |
8 | 24 | # A more efficient UUID type implementation
|
9 | 25 | # (6-7x faster than the starndard uuid.UUID):
|
10 | 26 | #
|
@@ -61,13 +77,13 @@ cdef std_UUID = uuid.UUID
|
61 | 77 |
|
62 | 78 | cdef pg_uuid_bytes_from_str(str u, char *out):
|
63 | 79 | cdef:
|
64 |
| - char *orig_buf |
| 80 | + const char *orig_buf |
65 | 81 | Py_ssize_t size
|
66 | 82 | unsigned char ch
|
67 | 83 | uint8_t acc, part, acc_set
|
68 | 84 | int i, j
|
69 | 85 |
|
70 |
| - orig_buf = <char*>cpythonx.PyUnicode_AsUTF8AndSize(u, &size) |
| 86 | + orig_buf = PyUnicode_AsUTF8AndSize(u, &size) |
71 | 87 | if size > 36 or size < 32:
|
72 | 88 | raise ValueError(
|
73 | 89 | f'invalid UUID {u!r}: '
|
@@ -102,8 +118,8 @@ cdef pg_uuid_bytes_from_str(str u, char *out):
|
102 | 118 | f'invalid UUID {u!r}: decodes to more than 16 bytes')
|
103 | 119 |
|
104 | 120 | if j != 16:
|
105 |
| - raise ValueError( |
106 |
| - f'invalid UUID {u!r}: decodes to less than 16 bytes') |
| 121 | + raise ValueError( |
| 122 | + f'invalid UUID {u!r}: decodes to less than 16 bytes') |
107 | 123 |
|
108 | 124 |
|
109 | 125 | cdef class __UUIDReplaceMe:
|
@@ -165,16 +181,14 @@ cdef class UUID(__UUIDReplaceMe):
|
165 | 181 |
|
166 | 182 | def __str__(self):
|
167 | 183 | cdef char out[36]
|
168 |
| - tohex.uuid_to_str(self._data, out) |
169 |
| - return cpythonx.PyUnicode_FromKindAndData( |
170 |
| - cpythonx.PyUnicode_1BYTE_KIND, <void*>out, 36) |
| 184 | + uuid_to_str(self._data, out) |
| 185 | + return PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, <void*>out, 36) |
171 | 186 |
|
172 | 187 | @property
|
173 | 188 | def hex(self):
|
174 | 189 | cdef char out[32]
|
175 |
| - tohex.uuid_to_hex(self._data, out) |
176 |
| - return cpythonx.PyUnicode_FromKindAndData( |
177 |
| - cpythonx.PyUnicode_1BYTE_KIND, <void*>out, 32) |
| 190 | + uuid_to_hex(self._data, out) |
| 191 | + return PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, <void*>out, 32) |
178 | 192 |
|
179 | 193 | def __repr__(self):
|
180 | 194 | return f"UUID('{self}')"
|
|
0 commit comments