Skip to content

Commit 38b3eaa

Browse files
larsmanswesm
authored andcommitted
don't declare static array in header
Replaced by a static const array in a C module. Prevents warnings and potential multiple inclusion hazards. Also, added a const qualifier to another static array.
1 parent ce1afeb commit 38b3eaa

File tree

6 files changed

+24
-21
lines changed

6 files changed

+24
-21
lines changed

pandas/src/datetime.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ cdef extern from "datetime/np_datetime.h":
9393
void pandas_datetime_to_datetimestruct(npy_datetime val,
9494
PANDAS_DATETIMEUNIT fr,
9595
pandas_datetimestruct *result)
96-
int _days_per_month_table[2][12]
96+
int days_per_month_table[2][12]
9797

9898
int dayofweek(int y, int m, int d)
9999
int is_leapyear(int64_t year)

pandas/src/datetime.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ def monthrange(int64_t year, int64_t month):
14191419
if month < 1 or month > 12:
14201420
raise ValueError("bad month number 0; must be 1-12")
14211421

1422-
days = _days_per_month_table[is_leapyear(year)][month-1]
1422+
days = days_per_month_table[is_leapyear(year)][month-1]
14231423

14241424
return (dayofweek(year, month, 1), days)
14251425

pandas/src/datetime/np_datetime.c

+15-9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
3737
#endif
3838

39+
const int days_per_month_table[2][12] = {
40+
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
41+
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
42+
};
43+
3944
/*
4045
* Returns 1 if the given year is a leap year, 0 otherwise.
4146
*/
@@ -52,7 +57,7 @@ int is_leapyear(npy_int64 year)
5257
int dayofweek(int y, int m, int d)
5358
{
5459
int day;
55-
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
60+
static const int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
5661
y -= m < 3;
5762
day = (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
5863
// convert to python day
@@ -97,12 +102,12 @@ add_minutes_to_datetimestruct(pandas_datetimestruct *dts, int minutes)
97102
dts->month = 12;
98103
}
99104
isleap = is_leapyear(dts->year);
100-
dts->day += _days_per_month_table[isleap][dts->month-1];
105+
dts->day += days_per_month_table[isleap][dts->month-1];
101106
}
102107
else if (dts->day > 28) {
103108
isleap = is_leapyear(dts->year);
104-
if (dts->day > _days_per_month_table[isleap][dts->month-1]) {
105-
dts->day -= _days_per_month_table[isleap][dts->month-1];
109+
if (dts->day > days_per_month_table[isleap][dts->month-1]) {
110+
dts->day -= days_per_month_table[isleap][dts->month-1];
106111
dts->month++;
107112
if (dts->month > 12) {
108113
dts->year++;
@@ -120,7 +125,7 @@ get_datetimestruct_days(const pandas_datetimestruct *dts)
120125
{
121126
int i, month;
122127
npy_int64 year, days = 0;
123-
int *month_lengths;
128+
const int *month_lengths;
124129

125130
year = dts->year - 1970;
126131
days = year * 365;
@@ -160,7 +165,7 @@ get_datetimestruct_days(const pandas_datetimestruct *dts)
160165
days += year / 400;
161166
}
162167

163-
month_lengths = _days_per_month_table[is_leapyear(dts->year)];
168+
month_lengths = days_per_month_table[is_leapyear(dts->year)];
164169
month = dts->month - 1;
165170

166171
/* Add the months */
@@ -250,10 +255,11 @@ add_seconds_to_datetimestruct(pandas_datetimestruct *dts, int seconds)
250255
static void
251256
set_datetimestruct_days(npy_int64 days, pandas_datetimestruct *dts)
252257
{
253-
int *month_lengths, i;
258+
const int *month_lengths;
259+
int i;
254260

255261
dts->year = days_to_yearsdays(&days);
256-
month_lengths = _days_per_month_table[is_leapyear(dts->year)];
262+
month_lengths = days_per_month_table[is_leapyear(dts->year)];
257263

258264
for (i = 0; i < 12; ++i) {
259265
if (days < month_lengths[i]) {
@@ -348,7 +354,7 @@ convert_pydatetime_to_datetimestruct(PyObject *obj, pandas_datetimestruct *out,
348354
}
349355
isleap = is_leapyear(out->year);
350356
if (out->day < 1 ||
351-
out->day > _days_per_month_table[isleap][out->month-1]) {
357+
out->day > days_per_month_table[isleap][out->month-1]) {
352358
goto invalid_date;
353359
}
354360

pandas/src/datetime/np_datetime.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ void pandas_datetime_to_datetimestruct(npy_datetime val, PANDAS_DATETIMEUNIT fr,
5757

5858
int dayofweek(int y, int m, int d);
5959

60-
static int _days_per_month_table[2][12] = {
61-
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
62-
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
63-
};
60+
extern const int days_per_month_table[2][12];
6461

6562
// stuff numpy-derived code needs in header
6663
// ----------------------------------------------------------------------------

pandas/src/datetime/np_datetime_strings.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef time_t NPY_TIME_T;
4646
/*// linux complains.*/
4747
/*static void _suppress_unused_variable_warning(void)*/
4848
/*{*/
49-
/* int x = _days_per_month_table[0][0];*/
49+
/* int x = days_per_month_table[0][0];*/
5050
/* x = x;*/
5151

5252
/* int y = _month_offset[0][0];*/
@@ -593,7 +593,7 @@ parse_iso_8601_datetime(char *str, int len,
593593
out->day = 10 * (substr[0] - '0') + (substr[1] - '0');
594594

595595
if (out->day < 1 ||
596-
out->day > _days_per_month_table[year_leap][out->month-1]) {
596+
out->day > days_per_month_table[year_leap][out->month-1]) {
597597
PyErr_Format(PyExc_ValueError,
598598
"Day out of range in datetime string \"%s\"", str);
599599
goto error;

pandas/src/offsets.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ cdef class MonthOffset(_Offset):
218218
self.m -= 12
219219
self.y += 1
220220
self.ly = is_leapyear(self.y)
221-
days += _days_per_month_table[self.ly][self.m]
221+
days += days_per_month_table[self.ly][self.m]
222222
self.m += 1
223223

224224
self.t += days * us_in_day
@@ -238,7 +238,7 @@ cdef class MonthOffset(_Offset):
238238
self.m += 12
239239
self.y -= 1
240240
self.ly = is_leapyear(self.y)
241-
days += _days_per_month_table[self.ly][self.m]
241+
days += days_per_month_table[self.ly][self.m]
242242

243243
self.t -= days * us_in_day
244244

@@ -286,7 +286,7 @@ cdef class DayOfMonthOffset(_Offset):
286286
cdef:
287287
int64_t tmp, days
288288

289-
days = _days_per_month_table[self.ly][self.m]
289+
days = days_per_month_table[self.ly][self.m]
290290
self.t += days * us_in_day
291291
self.dow = (self.dow + days) % 7
292292

@@ -300,7 +300,7 @@ cdef class DayOfMonthOffset(_Offset):
300300
cdef:
301301
int64_t tmp, days
302302

303-
days = _days_per_month_table[self.ly][(self.m - 1) % 12]
303+
days = days_per_month_table[self.ly][(self.m - 1) % 12]
304304
self.t -= days * us_in_day
305305
self.dow = (self.dow - days) % 7
306306

0 commit comments

Comments
 (0)