Skip to content

Commit 6079afd

Browse files
committed
Address @picnixz's review
1 parent ba9217e commit 6079afd

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

Modules/_uuidmodule.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
// UUID accelerator base type.
1+
/*
2+
* Python UUID module:
3+
* - wraps libuuid or Windows rpcrt4.dll.
4+
* - implements fast version of the uuid.py:UUID class.
5+
* - re-implements uuid4() and uuid7() functions with improved performance
6+
* by virtue of them being implemented in C and better entropy fetching
7+
* strategy.
8+
*
9+
* DCE compatible Universally Unique Identifier library.
10+
*/
211

312
#ifndef Py_BUILD_CORE_BUILTIN
413
# define Py_BUILD_CORE_MODULE 1
@@ -137,6 +146,8 @@ typedef struct uuidobject {
137146
// Bytes 8-9: clock_seq_and_variant (16 bits)
138147
// Bytes 10-15: node (48 bits)
139148
//
149+
// Note that the time attributes are only relevant to versions 1, 6 and 7.
150+
//
140151
// Version field is located in byte 6; most significant 4 bits:
141152
//
142153
// Variant field is located in byte 8; most significant variable bits:
@@ -486,8 +497,7 @@ _uuid_UUID___init___impl(uuidobject *self, PyObject *hex, Py_buffer *bytes,
486497
if (validated == NULL) {
487498
return -1;
488499
}
489-
Py_CLEAR(self->is_safe);
490-
self->is_safe = validated; // reuse reference
500+
Py_XSETREF(self->is_safe, validated);
491501
}
492502

493503
return 0;
@@ -734,12 +744,12 @@ extract_field(
734744
int overflow;
735745
uint64_t value = PyLong_AsLongLongAndOverflow(field, &overflow);
736746
if (overflow || (value == (uint64_t)-1 && PyErr_Occurred())) {
737-
PyErr_Format(PyExc_ValueError, "%s", error_msg);
747+
PyErr_SetString(PyExc_ValueError, error_msg);
738748
goto fail;
739749
}
740750

741751
if (value > max_value) {
742-
PyErr_Format(PyExc_ValueError, "%s", error_msg);
752+
PyErr_SetString(PyExc_ValueError, error_msg);
743753
goto fail;
744754
}
745755

@@ -767,14 +777,16 @@ from_fields(uuidobject *self, PyObject *fields)
767777
return -1;
768778
}
769779

770-
#define EXTRACT_FIELD(field_num, max_value, error_msg, type, name) \
780+
# define EXTRACT_FIELD(field_num, max_value, error_msg, type, name) \
771781
type name; \
772-
uint64_t name##_extracted; \
773-
if (extract_field(fields, field_num, max_value, error_msg, \
774-
&(name##_extracted)) < 0) { \
775-
return -1; \
776-
} \
777-
name = (type)name##_extracted;
782+
do { \
783+
uint64_t name##_extracted; \
784+
if (extract_field(fields, field_num, max_value, error_msg, \
785+
&(name##_extracted)) < 0) { \
786+
return -1; \
787+
} \
788+
name = (type)name##_extracted; \
789+
} while(0)
778790

779791
EXTRACT_FIELD(
780792
0, (1ULL << 32) - 1, "field 1 out of range (need a 32-bit value)",

0 commit comments

Comments
 (0)