Skip to content

Commit 5ae66af

Browse files
committed
Make uuid.pyx compilable into a standalone module
1 parent a417814 commit 5ae66af

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
*~
66
.DS_Store
77
__pycache__/
8-
/pgproto.c
8+
/*.c
99
*.html

uuid.pyx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import functools
22
import uuid
33

4+
cimport cython
5+
cimport cpython
6+
47
from libc.stdint cimport uint8_t, int8_t
58
from libc.string cimport memcpy, memcmp
69

710

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+
824
# A more efficient UUID type implementation
925
# (6-7x faster than the starndard uuid.UUID):
1026
#
@@ -61,13 +77,13 @@ cdef std_UUID = uuid.UUID
6177

6278
cdef pg_uuid_bytes_from_str(str u, char *out):
6379
cdef:
64-
char *orig_buf
80+
const char *orig_buf
6581
Py_ssize_t size
6682
unsigned char ch
6783
uint8_t acc, part, acc_set
6884
int i, j
6985

70-
orig_buf = <char*>cpythonx.PyUnicode_AsUTF8AndSize(u, &size)
86+
orig_buf = PyUnicode_AsUTF8AndSize(u, &size)
7187
if size > 36 or size < 32:
7288
raise ValueError(
7389
f'invalid UUID {u!r}: '
@@ -102,8 +118,8 @@ cdef pg_uuid_bytes_from_str(str u, char *out):
102118
f'invalid UUID {u!r}: decodes to more than 16 bytes')
103119

104120
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')
107123

108124

109125
cdef class __UUIDReplaceMe:
@@ -165,16 +181,14 @@ cdef class UUID(__UUIDReplaceMe):
165181

166182
def __str__(self):
167183
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)
171186

172187
@property
173188
def hex(self):
174189
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)
178192

179193
def __repr__(self):
180194
return f"UUID('{self}')"

0 commit comments

Comments
 (0)