Skip to content

Commit d809d71

Browse files
committed
PEP 7 fixes to from_isoformat
1 parent 4e54bd9 commit d809d71

File tree

1 file changed

+75
-58
lines changed

1 file changed

+75
-58
lines changed

Modules/_datetimemodule.c

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,8 @@ set_date_fields(PyDateTime_Date *self, int y, int m, int d)
668668
* String parsing utilities and helper functions
669669
*/
670670

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)
673673
{
674674
for (size_t i = 0; i < num_digits; ++i) {
675675
unsigned int tmp = (unsigned int)(*(ptr++) - '0');
@@ -683,15 +683,16 @@ parse_digits(const char* ptr, int* var, size_t num_digits)
683683
return ptr;
684684
}
685685

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+
{
688689
/* 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+
*/
695696
const char *p = dtstr;
696697
p = parse_digits(p, year, 4);
697698
if (NULL == p) {
@@ -720,8 +721,9 @@ static int parse_isoformat_date(const char *dtstr,
720721
}
721722

722723
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+
{
725727
const char *p = tstr;
726728
const char *p_end = tstr_end;
727729
int *vals[3] = {hour, minute, second};
@@ -736,12 +738,15 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end,
736738
char c = *(p++);
737739
if (p >= p_end) {
738740
return c != '\0';
739-
} else if (c == ':') {
741+
}
742+
else if (c == ':') {
740743
continue;
741-
} else if (c == '.') {
744+
}
745+
else if (c == '.') {
742746
break;
743-
} else {
744-
return -4; // Malformed time separator
747+
}
748+
else {
749+
return -4; // Malformed time separator
745750
}
746751
}
747752

@@ -765,9 +770,10 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end,
765770
}
766771

767772
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+
{
771777
// Parse the time portion of a datetime.isoformat() string
772778
//
773779
// Return codes:
@@ -785,19 +791,21 @@ parse_isoformat_time(const char *dtstr, size_t dtlen,
785791
if (*tzinfo_pos == '+' || *tzinfo_pos == '-') {
786792
break;
787793
}
788-
} while(++tzinfo_pos < p_end);
794+
} while (++tzinfo_pos < p_end);
789795

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);
792798

793799
if (rv < 0) {
794800
return rv;
795-
} else if (tzinfo_pos == p_end) {
801+
}
802+
else if (tzinfo_pos == p_end) {
796803
// We know that there's no time zone, so if there's stuff at the
797804
// end of the string it's an error.
798805
if (rv == 1) {
799806
return -5;
800-
} else {
807+
}
808+
else {
801809
return 0;
802810
}
803811
}
@@ -812,19 +820,18 @@ parse_isoformat_time(const char *dtstr, size_t dtlen,
812820
return -5;
813821
}
814822

815-
int tzsign = (*tzinfo_pos == '-')?-1:1;
823+
int tzsign = (*tzinfo_pos == '-') ? -1 : 1;
816824
tzinfo_pos++;
817825
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);
820828

821829
*tzoffset = tzsign * ((tzhour * 3600) + (tzminute * 60) + tzsecond);
822830
*tzmicrosecond *= tzsign;
823831

824-
return rv?-5:1;
832+
return rv ? -5 : 1;
825833
}
826834

827-
828835
/* ---------------------------------------------------------------------------
829836
* Create various objects, mostly without range checking.
830837
*/
@@ -839,30 +846,33 @@ new_date_ex(int year, int month, int day, PyTypeObject *type)
839846
return NULL;
840847
}
841848

842-
self = (PyDateTime_Date *) (type->tp_alloc(type, 0));
849+
self = (PyDateTime_Date *)(type->tp_alloc(type, 0));
843850
if (self != NULL)
844851
set_date_fields(self, year, month, day);
845-
return (PyObject *) self;
852+
return (PyObject *)self;
846853
}
847854

848855
#define new_date(year, month, day) \
849856
new_date_ex(year, month, day, &PyDateTime_DateType)
850857

851858
// 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 *);
854861

855862
/* Create date instance with no range checking, or call subclass constructor */
856863
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+
{
858866
PyObject *result;
859867
// We have "fast path" constructors for two subclasses: date and datetime
860868
if ((PyTypeObject *)cls == &PyDateTime_DateType) {
861869
result = new_date_ex(year, month, day, (PyTypeObject *)cls);
862-
} else if ((PyTypeObject *)cls == &PyDateTime_DateTimeType) {
870+
}
871+
else if ((PyTypeObject *)cls == &PyDateTime_DateTimeType) {
863872
result = new_datetime_ex(year, month, day, 0, 0, 0, 0, Py_None,
864873
(PyTypeObject *)cls);
865-
} else {
874+
}
875+
else {
866876
result = PyObject_CallFunction(cls, "iii", year, month, day);
867877
}
868878

@@ -1281,7 +1291,8 @@ append_keyword_fold(PyObject *repr, int fold)
12811291
}
12821292

12831293
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+
{
12851296
PyObject *tzinfo;
12861297
if (rv == 1) {
12871298
// 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) {
12961307
}
12971308
tzinfo = new_timezone(delta, NULL);
12981309
Py_DECREF(delta);
1299-
} else {
1310+
}
1311+
else {
13001312
tzinfo = Py_None;
13011313
Py_INCREF(Py_None);
13021314
}
@@ -2886,17 +2898,19 @@ date_fromordinal(PyObject *cls, PyObject *args)
28862898

28872899
/* Return the new date from a string as generated by date.isoformat() */
28882900
static PyObject *
2889-
date_fromisoformat(PyObject *cls, PyObject *dtstr) {
2901+
date_fromisoformat(PyObject *cls, PyObject *dtstr)
2902+
{
28902903
assert(dtstr != NULL);
28912904

28922905
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");
28942908
return NULL;
28952909
}
28962910

28972911
Py_ssize_t len;
28982912

2899-
const char * dt_ptr = PyUnicode_AsUTF8AndSize(dtstr, &len);
2913+
const char *dt_ptr = PyUnicode_AsUTF8AndSize(dtstr, &len);
29002914
if (dt_ptr == NULL) {
29012915
goto invalid_string_error;
29022916
}
@@ -2906,7 +2920,8 @@ date_fromisoformat(PyObject *cls, PyObject *dtstr) {
29062920
int rv;
29072921
if (len == 10) {
29082922
rv = parse_isoformat_date(dt_ptr, &year, &month, &day);
2909-
} else {
2923+
}
2924+
else {
29102925
rv = -1;
29112926
}
29122927

@@ -2917,12 +2932,10 @@ date_fromisoformat(PyObject *cls, PyObject *dtstr) {
29172932
return new_date_subclass_ex(year, month, day, cls);
29182933

29192934
invalid_string_error:
2920-
PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R",
2921-
dtstr);
2935+
PyErr_Format(PyExc_ValueError, "Invalid isoformat string: %R", dtstr);
29222936
return NULL;
29232937
}
29242938

2925-
29262939
/*
29272940
* Date arithmetic.
29282941
*/
@@ -4863,7 +4876,8 @@ datetime_combine(PyObject *cls, PyObject *args, PyObject *kw)
48634876
}
48644877

48654878
static PyObject *
4866-
_sanitize_isoformat_str(PyObject *dtstr, int *needs_decref) {
4879+
_sanitize_isoformat_str(PyObject *dtstr, int *needs_decref)
4880+
{
48674881
// `fromisoformat` allows surrogate characters in exactly one position,
48684882
// the separator; to allow datetime_fromisoformat to make the simplifying
48694883
// 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) {
48744888
}
48754889

48764890
*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))) {
48784893
return dtstr;
48794894
}
48804895

@@ -4893,11 +4908,13 @@ _sanitize_isoformat_str(PyObject *dtstr, int *needs_decref) {
48934908
}
48944909

48954910
static PyObject *
4896-
datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
4911+
datetime_fromisoformat(PyObject *cls, PyObject *dtstr)
4912+
{
48974913
assert(dtstr != NULL);
48984914

48994915
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");
49014918
return NULL;
49024919
}
49034920

@@ -4908,13 +4925,14 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
49084925
}
49094926

49104927
Py_ssize_t len;
4911-
const char * dt_ptr = PyUnicode_AsUTF8AndSize(dtstr_clean, &len);
4928+
const char *dt_ptr = PyUnicode_AsUTF8AndSize(dtstr_clean, &len);
49124929

49134930
if (dt_ptr == NULL) {
49144931
if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
49154932
// Encoding errors are invalid string errors at this point
49164933
goto invalid_string_error;
4917-
} else {
4934+
}
4935+
else {
49184936
goto error;
49194937
}
49204938
}
@@ -4932,8 +4950,9 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
49324950
// In UTF-8, the length of multi-byte characters is encoded in the MSB
49334951
if ((p[10] & 0x80) == 0) {
49344952
p += 11;
4935-
} else {
4936-
switch(p[10] & 0xf0) {
4953+
}
4954+
else {
4955+
switch (p[10] & 0xf0) {
49374956
case 0xe0:
49384957
p += 13;
49394958
break;
@@ -4947,15 +4966,14 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
49474966
}
49484967

49494968
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);
49534971
}
49544972
if (rv < 0) {
49554973
goto invalid_string_error;
49564974
}
49574975

4958-
PyObject* tzinfo = tzinfo_from_isoformat_results(rv, tzoffset, tzusec);
4976+
PyObject *tzinfo = tzinfo_from_isoformat_results(rv, tzoffset, tzusec);
49594977
if (tzinfo == NULL) {
49604978
goto error;
49614979
}
@@ -4980,7 +4998,6 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
49804998
return NULL;
49814999
}
49825000

4983-
49845001
/*
49855002
* Destructor.
49865003
*/

0 commit comments

Comments
 (0)