11# coding: utf-8
22
33from cpython cimport *
4-
54cdef extern from " Python.h" :
65 ctypedef struct PyObject
76 cdef int PyObject_AsReadBuffer(object o, const void ** buff, Py_ssize_t* buf_len) except - 1
@@ -21,17 +20,22 @@ from .exceptions import (
2120)
2221from .ext import ExtType, Timestamp
2322
23+ cdef object giga = 1 _000_000_000
24+
2425
2526cdef extern from " unpack.h" :
2627 ctypedef struct msgpack_user:
2728 bint use_list
2829 bint raw
2930 bint has_pairs_hook # call object_hook with k-v pairs
3031 bint strict_map_key
32+ int timestamp
3133 PyObject* object_hook
3234 PyObject* list_hook
3335 PyObject* ext_hook
3436 PyObject* timestamp_t
37+ PyObject * giga;
38+ PyObject * utc;
3539 char * unicode_errors
3640 Py_ssize_t max_str_len
3741 Py_ssize_t max_bin_len
@@ -57,7 +61,8 @@ cdef extern from "unpack.h":
5761cdef inline init_ctx(unpack_context * ctx,
5862 object object_hook, object object_pairs_hook,
5963 object list_hook, object ext_hook,
60- bint use_list, bint raw, bint strict_map_key,
64+ bint use_list, bint raw, int timestamp,
65+ bint strict_map_key,
6166 const char * unicode_errors,
6267 Py_ssize_t max_str_len, Py_ssize_t max_bin_len,
6368 Py_ssize_t max_array_len, Py_ssize_t max_map_len,
@@ -99,8 +104,14 @@ cdef inline init_ctx(unpack_context *ctx,
99104 raise TypeError (" ext_hook must be a callable." )
100105 ctx.user.ext_hook = < PyObject* > ext_hook
101106
107+ if timestamp < 0 or 3 < timestamp:
108+ raise ValueError (" timestamp must be 0..3" )
109+
102110 # Add Timestamp type to the user object so it may be used in unpack.h
111+ ctx.user.timestamp = timestamp
103112 ctx.user.timestamp_t = < PyObject* > Timestamp
113+ ctx.user.giga = < PyObject* > giga
114+ ctx.user.utc = < PyObject* > utc
104115 ctx.user.unicode_errors = unicode_errors
105116
106117def default_read_extended_type (typecode , data ):
@@ -131,7 +142,7 @@ cdef inline int get_data_from_buffer(object obj,
131142
132143
133144def unpackb (object packed , *, object object_hook = None , object list_hook = None ,
134- bint use_list = True , bint raw = False , bint strict_map_key = True ,
145+ bint use_list = True , bint raw = False , int timestamp = 0 , bint strict_map_key = True ,
135146 unicode_errors = None ,
136147 object_pairs_hook = None , ext_hook = ExtType,
137148 Py_ssize_t max_str_len = - 1 ,
@@ -179,7 +190,7 @@ def unpackb(object packed, *, object object_hook=None, object list_hook=None,
179190
180191 try :
181192 init_ctx(& ctx, object_hook, object_pairs_hook, list_hook, ext_hook,
182- use_list, raw, strict_map_key, cerr,
193+ use_list, raw, timestamp, strict_map_key, cerr,
183194 max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len)
184195 ret = unpack_construct(& ctx, buf, buf_len, & off)
185196 finally :
@@ -304,7 +315,7 @@ cdef class Unpacker(object):
304315 self .buf = NULL
305316
306317 def __init__ (self , file_like = None , *, Py_ssize_t read_size = 0 ,
307- bint use_list = True , bint raw = False , bint strict_map_key = True ,
318+ bint use_list = True , bint raw = False , int timestamp = 0 , bint strict_map_key = True ,
308319 object object_hook = None , object object_pairs_hook = None , object list_hook = None ,
309320 unicode_errors = None , Py_ssize_t max_buffer_size = 100 * 1024 * 1024 ,
310321 object ext_hook = ExtType,
@@ -359,7 +370,7 @@ cdef class Unpacker(object):
359370 cerr = unicode_errors
360371
361372 init_ctx(& self .ctx, object_hook, object_pairs_hook, list_hook,
362- ext_hook, use_list, raw, strict_map_key, cerr,
373+ ext_hook, use_list, raw, timestamp, strict_map_key, cerr,
363374 max_str_len, max_bin_len, max_array_len,
364375 max_map_len, max_ext_len)
365376
0 commit comments