@@ -271,6 +271,56 @@ static PyObject *sonyflake_next(struct sonyflake_state *self) {
271271 return PyLong_FromUnsignedLongLong (sonyflake_id );
272272}
273273
274+ static PyObject * sonyflake_repr (struct sonyflake_state * self ) {
275+ PyObject * s , * args_list = PyList_New (self -> machine_ids_len + 1 );
276+
277+ if (!args_list ) {
278+ return NULL ;
279+ }
280+
281+ s = PyUnicode_FromFormat ("start_time=%ld" , (long ) self -> start_time .tv_sec );
282+
283+ if (!s ) {
284+ Py_DECREF (args_list );
285+ return NULL ;
286+ }
287+
288+ PyList_SetItem (args_list , self -> machine_ids_len , s );
289+
290+ for (Py_ssize_t i = 0 ; i < self -> machine_ids_len ; i ++ ) {
291+ s = PyUnicode_FromFormat ("%u" , (unsigned ) self -> machine_ids [i ]);
292+
293+ if (!s ) {
294+ Py_DECREF (args_list );
295+ return NULL ;
296+ }
297+
298+ PyList_SetItem (args_list , i , s );
299+ }
300+
301+ s = PyUnicode_FromString (", " );
302+
303+ if (!s ) {
304+ Py_DECREF (args_list );
305+ return NULL ;
306+ }
307+
308+ PyObject * args_str = PyUnicode_Join (s , args_list );
309+
310+ Py_DECREF (args_list );
311+ Py_DECREF (s );
312+
313+ if (!args_str ) {
314+ return NULL ;
315+ }
316+
317+ s = PyUnicode_FromFormat ("SonyFlake(%U)" , args_str );
318+
319+ Py_DECREF (args_str );
320+
321+ return s ;
322+ }
323+
274324PyDoc_STRVAR (sonyflake_doc ,
275325"SonyFlake(*machine_id, start_time=None)\n--\n\n"
276326"SonyFlake ID generator implementation that combines multiple ID generators into one to improve throughput.\n"
@@ -286,6 +336,7 @@ static PyType_Slot sonyflake_type_slots[] = {
286336 {Py_tp_new , sonyflake_new },
287337 {Py_tp_init , sonyflake_init },
288338 {Py_tp_doc , sonyflake_doc },
339+ {Py_tp_repr , sonyflake_repr },
289340 {0 , 0 },
290341};
291342
@@ -357,6 +408,13 @@ static PyObject *machine_id_lcg_next(struct machine_id_lcg_state *self) {
357408 return PyLong_FromLong (machine_id_lcg_atomic (& self -> machine_id ));
358409}
359410
411+ static PyObject * machine_id_lcg_repr (struct machine_id_lcg_state * self ) {
412+ return PyUnicode_FromFormat (
413+ "MachineIDLCG(%u)" ,
414+ atomic_load_explicit (& self -> machine_id , memory_order_relaxed )
415+ );
416+ }
417+
360418static PyObject * machine_id_lcg_call (struct machine_id_lcg_state * self , PyObject * args , PyObject * kwargs ) {
361419 return machine_id_lcg_next (self );
362420}
@@ -376,6 +434,7 @@ static PyType_Slot machine_id_lcg_slots[] = {
376434 {Py_tp_new , machine_id_lcg_new },
377435 {Py_tp_call , machine_id_lcg_call },
378436 {Py_tp_doc , machine_id_lcg_doc },
437+ {Py_tp_repr , machine_id_lcg_repr },
379438 {0 , 0 },
380439};
381440
0 commit comments