@@ -668,8 +668,8 @@ set_date_fields(PyDateTime_Date *self, int y, int m, int d)
668
668
* String parsing utilities and helper functions
669
669
*/
670
670
671
- static const char *
672
- parse_digits (const char * ptr , int * var , size_t num_digits )
671
+ static const char *
672
+ parse_digits (const char * ptr , int * var , size_t num_digits )
673
673
{
674
674
for (size_t i = 0 ; i < num_digits ; ++ i ) {
675
675
unsigned int tmp = (unsigned int )(* (ptr ++ ) - '0' );
@@ -683,15 +683,16 @@ parse_digits(const char* ptr, int* var, size_t num_digits)
683
683
return ptr ;
684
684
}
685
685
686
- static int parse_isoformat_date (const char * dtstr ,
687
- int * year , int * month , int * day ) {
686
+ static int
687
+ parse_isoformat_date (const char * dtstr , int * year , int * month , int * day )
688
+ {
688
689
/* Parse the date components of the result of date.isoformat()
689
- *
690
- * Return codes:
691
- * 0: Success
692
- * -1: Failed to parse date component
693
- * -2: Failed to parse dateseparator
694
- */
690
+ *
691
+ * Return codes:
692
+ * 0: Success
693
+ * -1: Failed to parse date component
694
+ * -2: Failed to parse dateseparator
695
+ */
695
696
const char * p = dtstr ;
696
697
p = parse_digits (p , year , 4 );
697
698
if (NULL == p ) {
@@ -720,8 +721,9 @@ static int parse_isoformat_date(const char *dtstr,
720
721
}
721
722
722
723
static int
723
- parse_hh_mm_ss_ff (const char * tstr , const char * tstr_end ,
724
- int * hour , int * minute , int * second , int * microsecond ) {
724
+ parse_hh_mm_ss_ff (const char * tstr , const char * tstr_end , int * hour ,
725
+ int * minute , int * second , int * microsecond )
726
+ {
725
727
const char * p = tstr ;
726
728
const char * p_end = tstr_end ;
727
729
int * vals [3 ] = {hour , minute , second };
@@ -736,12 +738,15 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end,
736
738
char c = * (p ++ );
737
739
if (p >= p_end ) {
738
740
return c != '\0' ;
739
- } else if (c == ':' ) {
741
+ }
742
+ else if (c == ':' ) {
740
743
continue ;
741
- } else if (c == '.' ) {
744
+ }
745
+ else if (c == '.' ) {
742
746
break ;
743
- } else {
744
- return -4 ; // Malformed time separator
747
+ }
748
+ else {
749
+ return -4 ; // Malformed time separator
745
750
}
746
751
}
747
752
@@ -765,9 +770,10 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end,
765
770
}
766
771
767
772
static int
768
- parse_isoformat_time (const char * dtstr , size_t dtlen ,
769
- int * hour , int * minute , int * second , int * microsecond ,
770
- int * tzoffset , int * tzmicrosecond ) {
773
+ parse_isoformat_time (const char * dtstr , size_t dtlen , int * hour , int * minute ,
774
+ int * second , int * microsecond , int * tzoffset ,
775
+ int * tzmicrosecond )
776
+ {
771
777
// Parse the time portion of a datetime.isoformat() string
772
778
//
773
779
// Return codes:
@@ -785,19 +791,21 @@ parse_isoformat_time(const char *dtstr, size_t dtlen,
785
791
if (* tzinfo_pos == '+' || * tzinfo_pos == '-' ) {
786
792
break ;
787
793
}
788
- } while (++ tzinfo_pos < p_end );
794
+ } while (++ tzinfo_pos < p_end );
789
795
790
- int rv = parse_hh_mm_ss_ff (dtstr , tzinfo_pos ,
791
- hour , minute , second , microsecond );
796
+ int rv = parse_hh_mm_ss_ff (dtstr , tzinfo_pos , hour , minute , second ,
797
+ microsecond );
792
798
793
799
if (rv < 0 ) {
794
800
return rv ;
795
- } else if (tzinfo_pos == p_end ) {
801
+ }
802
+ else if (tzinfo_pos == p_end ) {
796
803
// We know that there's no time zone, so if there's stuff at the
797
804
// end of the string it's an error.
798
805
if (rv == 1 ) {
799
806
return -5 ;
800
- } else {
807
+ }
808
+ else {
801
809
return 0 ;
802
810
}
803
811
}
@@ -812,19 +820,18 @@ parse_isoformat_time(const char *dtstr, size_t dtlen,
812
820
return -5 ;
813
821
}
814
822
815
- int tzsign = (* tzinfo_pos == '-' )? -1 : 1 ;
823
+ int tzsign = (* tzinfo_pos == '-' ) ? -1 : 1 ;
816
824
tzinfo_pos ++ ;
817
825
int tzhour = 0 , tzminute = 0 , tzsecond = 0 ;
818
- rv = parse_hh_mm_ss_ff (tzinfo_pos , p_end ,
819
- & tzhour , & tzminute , & tzsecond , tzmicrosecond );
826
+ rv = parse_hh_mm_ss_ff (tzinfo_pos , p_end , & tzhour , & tzminute , & tzsecond ,
827
+ tzmicrosecond );
820
828
821
829
* tzoffset = tzsign * ((tzhour * 3600 ) + (tzminute * 60 ) + tzsecond );
822
830
* tzmicrosecond *= tzsign ;
823
831
824
- return rv ? -5 : 1 ;
832
+ return rv ? -5 : 1 ;
825
833
}
826
834
827
-
828
835
/* ---------------------------------------------------------------------------
829
836
* Create various objects, mostly without range checking.
830
837
*/
@@ -839,30 +846,33 @@ new_date_ex(int year, int month, int day, PyTypeObject *type)
839
846
return NULL ;
840
847
}
841
848
842
- self = (PyDateTime_Date * ) (type -> tp_alloc (type , 0 ));
849
+ self = (PyDateTime_Date * )(type -> tp_alloc (type , 0 ));
843
850
if (self != NULL )
844
851
set_date_fields (self , year , month , day );
845
- return (PyObject * ) self ;
852
+ return (PyObject * )self ;
846
853
}
847
854
848
855
#define new_date (year , month , day ) \
849
856
new_date_ex(year, month, day, &PyDateTime_DateType)
850
857
851
858
// Forward declaration
852
- static PyObject * new_datetime_ex ( int , int , int , int , int , int , int ,
853
- PyObject * , PyTypeObject * );
859
+ static PyObject *
860
+ new_datetime_ex ( int , int , int , int , int , int , int , PyObject * , PyTypeObject * );
854
861
855
862
/* Create date instance with no range checking, or call subclass constructor */
856
863
static PyObject *
857
- new_date_subclass_ex (int year , int month , int day , PyObject * cls ) {
864
+ new_date_subclass_ex (int year , int month , int day , PyObject * cls )
865
+ {
858
866
PyObject * result ;
859
867
// We have "fast path" constructors for two subclasses: date and datetime
860
868
if ((PyTypeObject * )cls == & PyDateTime_DateType ) {
861
869
result = new_date_ex (year , month , day , (PyTypeObject * )cls );
862
- } else if ((PyTypeObject * )cls == & PyDateTime_DateTimeType ) {
870
+ }
871
+ else if ((PyTypeObject * )cls == & PyDateTime_DateTimeType ) {
863
872
result = new_datetime_ex (year , month , day , 0 , 0 , 0 , 0 , Py_None ,
864
873
(PyTypeObject * )cls );
865
- } else {
874
+ }
875
+ else {
866
876
result = PyObject_CallFunction (cls , "iii" , year , month , day );
867
877
}
868
878
@@ -1281,7 +1291,8 @@ append_keyword_fold(PyObject *repr, int fold)
1281
1291
}
1282
1292
1283
1293
static inline PyObject *
1284
- tzinfo_from_isoformat_results (int rv , int tzoffset , int tz_useconds ) {
1294
+ tzinfo_from_isoformat_results (int rv , int tzoffset , int tz_useconds )
1295
+ {
1285
1296
PyObject * tzinfo ;
1286
1297
if (rv == 1 ) {
1287
1298
// Create a timezone from offset in seconds (0 returns UTC)
@@ -1296,7 +1307,8 @@ tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds) {
1296
1307
}
1297
1308
tzinfo = new_timezone (delta , NULL );
1298
1309
Py_DECREF (delta );
1299
- } else {
1310
+ }
1311
+ else {
1300
1312
tzinfo = Py_None ;
1301
1313
Py_INCREF (Py_None );
1302
1314
}
@@ -2886,17 +2898,19 @@ date_fromordinal(PyObject *cls, PyObject *args)
2886
2898
2887
2899
/* Return the new date from a string as generated by date.isoformat() */
2888
2900
static PyObject *
2889
- date_fromisoformat (PyObject * cls , PyObject * dtstr ) {
2901
+ date_fromisoformat (PyObject * cls , PyObject * dtstr )
2902
+ {
2890
2903
assert (dtstr != NULL );
2891
2904
2892
2905
if (!PyUnicode_Check (dtstr )) {
2893
- PyErr_SetString (PyExc_TypeError , "fromisoformat: argument must be str" );
2906
+ PyErr_SetString (PyExc_TypeError ,
2907
+ "fromisoformat: argument must be str" );
2894
2908
return NULL ;
2895
2909
}
2896
2910
2897
2911
Py_ssize_t len ;
2898
2912
2899
- const char * dt_ptr = PyUnicode_AsUTF8AndSize (dtstr , & len );
2913
+ const char * dt_ptr = PyUnicode_AsUTF8AndSize (dtstr , & len );
2900
2914
if (dt_ptr == NULL ) {
2901
2915
goto invalid_string_error ;
2902
2916
}
@@ -2906,7 +2920,8 @@ date_fromisoformat(PyObject *cls, PyObject *dtstr) {
2906
2920
int rv ;
2907
2921
if (len == 10 ) {
2908
2922
rv = parse_isoformat_date (dt_ptr , & year , & month , & day );
2909
- } else {
2923
+ }
2924
+ else {
2910
2925
rv = -1 ;
2911
2926
}
2912
2927
@@ -2917,12 +2932,10 @@ date_fromisoformat(PyObject *cls, PyObject *dtstr) {
2917
2932
return new_date_subclass_ex (year , month , day , cls );
2918
2933
2919
2934
invalid_string_error :
2920
- PyErr_Format (PyExc_ValueError , "Invalid isoformat string: %R" ,
2921
- dtstr );
2935
+ PyErr_Format (PyExc_ValueError , "Invalid isoformat string: %R" , dtstr );
2922
2936
return NULL ;
2923
2937
}
2924
2938
2925
-
2926
2939
/*
2927
2940
* Date arithmetic.
2928
2941
*/
@@ -4863,7 +4876,8 @@ datetime_combine(PyObject *cls, PyObject *args, PyObject *kw)
4863
4876
}
4864
4877
4865
4878
static PyObject *
4866
- _sanitize_isoformat_str (PyObject * dtstr , int * needs_decref ) {
4879
+ _sanitize_isoformat_str (PyObject * dtstr , int * needs_decref )
4880
+ {
4867
4881
// `fromisoformat` allows surrogate characters in exactly one position,
4868
4882
// the separator; to allow datetime_fromisoformat to make the simplifying
4869
4883
// assumption that all valid strings can be encoded in UTF-8, this function
@@ -4874,7 +4888,8 @@ _sanitize_isoformat_str(PyObject *dtstr, int *needs_decref) {
4874
4888
}
4875
4889
4876
4890
* needs_decref = 0 ;
4877
- if (len <= 10 || !Py_UNICODE_IS_SURROGATE (PyUnicode_READ_CHAR (dtstr , 10 ))) {
4891
+ if (len <= 10 ||
4892
+ !Py_UNICODE_IS_SURROGATE (PyUnicode_READ_CHAR (dtstr , 10 ))) {
4878
4893
return dtstr ;
4879
4894
}
4880
4895
@@ -4893,11 +4908,13 @@ _sanitize_isoformat_str(PyObject *dtstr, int *needs_decref) {
4893
4908
}
4894
4909
4895
4910
static PyObject *
4896
- datetime_fromisoformat (PyObject * cls , PyObject * dtstr ) {
4911
+ datetime_fromisoformat (PyObject * cls , PyObject * dtstr )
4912
+ {
4897
4913
assert (dtstr != NULL );
4898
4914
4899
4915
if (!PyUnicode_Check (dtstr )) {
4900
- PyErr_SetString (PyExc_TypeError , "fromisoformat: argument must be str" );
4916
+ PyErr_SetString (PyExc_TypeError ,
4917
+ "fromisoformat: argument must be str" );
4901
4918
return NULL ;
4902
4919
}
4903
4920
@@ -4908,13 +4925,14 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
4908
4925
}
4909
4926
4910
4927
Py_ssize_t len ;
4911
- const char * dt_ptr = PyUnicode_AsUTF8AndSize (dtstr_clean , & len );
4928
+ const char * dt_ptr = PyUnicode_AsUTF8AndSize (dtstr_clean , & len );
4912
4929
4913
4930
if (dt_ptr == NULL ) {
4914
4931
if (PyErr_ExceptionMatches (PyExc_UnicodeEncodeError )) {
4915
4932
// Encoding errors are invalid string errors at this point
4916
4933
goto invalid_string_error ;
4917
- } else {
4934
+ }
4935
+ else {
4918
4936
goto error ;
4919
4937
}
4920
4938
}
@@ -4932,8 +4950,9 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
4932
4950
// In UTF-8, the length of multi-byte characters is encoded in the MSB
4933
4951
if ((p [10 ] & 0x80 ) == 0 ) {
4934
4952
p += 11 ;
4935
- } else {
4936
- switch (p [10 ] & 0xf0 ) {
4953
+ }
4954
+ else {
4955
+ switch (p [10 ] & 0xf0 ) {
4937
4956
case 0xe0 :
4938
4957
p += 13 ;
4939
4958
break ;
@@ -4947,15 +4966,14 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
4947
4966
}
4948
4967
4949
4968
len -= (p - dt_ptr );
4950
- rv = parse_isoformat_time (p , len ,
4951
- & hour , & minute , & second , & microsecond ,
4952
- & tzoffset , & tzusec );
4969
+ rv = parse_isoformat_time (p , len , & hour , & minute , & second ,
4970
+ & microsecond , & tzoffset , & tzusec );
4953
4971
}
4954
4972
if (rv < 0 ) {
4955
4973
goto invalid_string_error ;
4956
4974
}
4957
4975
4958
- PyObject * tzinfo = tzinfo_from_isoformat_results (rv , tzoffset , tzusec );
4976
+ PyObject * tzinfo = tzinfo_from_isoformat_results (rv , tzoffset , tzusec );
4959
4977
if (tzinfo == NULL ) {
4960
4978
goto error ;
4961
4979
}
@@ -4980,7 +4998,6 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
4980
4998
return NULL ;
4981
4999
}
4982
5000
4983
-
4984
5001
/*
4985
5002
* Destructor.
4986
5003
*/
0 commit comments