Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1b01ccd7
Commit
1b01ccd7
authored
May 30, 2009
by
Martin v. Löwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #5562: Use wcsftime for time.strftime where available.
parent
3ad05763
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
12 deletions
+65
-12
Lib/test/test_time.py
Lib/test/test_time.py
+18
-3
Misc/NEWS
Misc/NEWS
+2
-0
Modules/timemodule.c
Modules/timemodule.c
+42
-9
PC/pyconfig.h
PC/pyconfig.h
+3
-0
No files found.
Lib/test/test_time.py
View file @
1b01ccd7
from
test
import
support
import
time
import
unittest
import
locale
class
TimeTestCase
(
unittest
.
TestCase
):
...
...
@@ -223,9 +223,24 @@ class TimeTestCase(unittest.TestCase):
t1
=
time
.
mktime
(
lt1
)
self
.
assert_
(
0
<=
(
t1
-
t0
)
<
0.2
)
def
test_main
():
support
.
run_unittest
(
TimeTestCase
)
class
TestLocale
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
oldloc
=
locale
.
setlocale
(
locale
.
LC_ALL
)
def
tearDown
(
self
):
locale
.
setlocale
(
locale
.
LC_ALL
,
self
.
oldloc
)
def
test_bug_5562
(
self
):
try
:
tmp
=
locale
.
setlocale
(
locale
.
LC_ALL
,
"fr_FR"
)
except
locale
.
Error
:
# skip this test
return
# This should not cause an exception
time
.
strftime
(
"%B"
,
(
2009
,
2
,
1
,
0
,
0
,
0
,
0
,
0
,
0
))
def
test_main
():
support
.
run_unittest
(
TimeTestCase
,
TestLocale
)
if
__name__
==
"__main__"
:
test_main
()
Misc/NEWS
View file @
1b01ccd7
...
...
@@ -111,6 +111,8 @@ Installation
Extension Modules
-----------------
- Issue #5562: Use wcsftime for time.strftime where available.
- Issue #4873: Fix resource leaks in error cases of pwd and grp.
- Issue #6093: Fix off-by-one error in locale.strxfrm.
...
...
Modules/timemodule.c
View file @
1b01ccd7
...
...
@@ -417,15 +417,25 @@ gettmarg(PyObject *args, struct tm *p)
}
#ifdef HAVE_STRFTIME
#ifdef HAVE_WCSFTIME
#define time_char wchar_t
#define format_time wcsftime
#define time_strlen wcslen
#else
#define time_char char
#define format_time strftime
#define time_strlen strlen
#endif
static
PyObject
*
time_strftime
(
PyObject
*
self
,
PyObject
*
args
)
{
PyObject
*
tup
=
NULL
;
struct
tm
buf
;
const
char
*
fmt
;
PyObject
*
format
;
const
time_
char
*
fmt
;
PyObject
*
format
,
*
tmpfmt
;
size_t
fmtlen
,
buflen
;
char
*
outbuf
=
0
;
time_
char
*
outbuf
=
0
;
size_t
i
;
memset
((
void
*
)
&
buf
,
'\0'
,
sizeof
(
buf
));
...
...
@@ -508,22 +518,38 @@ time_strftime(PyObject *self, PyObject *args)
return
NULL
;
}
#ifdef HAVE_WCSFTIME
tmpfmt
=
PyBytes_FromStringAndSize
(
NULL
,
sizeof
(
wchar_t
)
*
(
PyUnicode_GetSize
(
format
)
+
1
));
if
(
!
tmpfmt
)
return
NULL
;
/* This assumes that PyUnicode_AsWideChar doesn't do any UTF-16
expansion. */
if
(
PyUnicode_AsWideChar
((
PyUnicodeObject
*
)
format
,
(
wchar_t
*
)
PyBytes_AS_STRING
(
tmpfmt
),
PyUnicode_GetSize
(
format
)
+
1
)
==
(
size_t
)
-
1
)
/* This shouldn't fail. */
Py_FatalError
(
"PyUnicode_AsWideChar failed"
);
format
=
tmpfmt
;
fmt
=
(
wchar_t
*
)
PyBytes_AS_STRING
(
format
);
#else
/* Convert the unicode string to an ascii one */
format
=
PyUnicode_AsEncodedString
(
format
,
TZNAME_ENCODING
,
NULL
);
if
(
format
==
NULL
)
return
NULL
;
fmt
=
PyBytes_AS_STRING
(
format
);
#endif
#ifdef MS_WINDOWS
/* check that the format string contains only valid directives */
for
(
outbuf
=
strchr
(
fmt
,
'%'
);
for
(
outbuf
=
wcschr
(
fmt
,
L
'%'
);
outbuf
!=
NULL
;
outbuf
=
strchr
(
outbuf
+
2
,
'%'
))
outbuf
=
wcschr
(
outbuf
+
2
,
L
'%'
))
{
if
(
outbuf
[
1
]
==
'#'
)
++
outbuf
;
/* not documented by python, */
if
(
outbuf
[
1
]
==
'\0'
||
!
strchr
(
"aAbBcdfHIjmMpSUwWxXyYzZ%"
,
outbuf
[
1
]))
!
wcschr
(
L
"aAbBcdfHIjmMpSUwWxXyYzZ%"
,
outbuf
[
1
]))
{
PyErr_SetString
(
PyExc_ValueError
,
"Invalid format string"
);
return
0
;
...
...
@@ -531,18 +557,18 @@ time_strftime(PyObject *self, PyObject *args)
}
#endif
fmtlen
=
strlen
(
fmt
);
fmtlen
=
time_
strlen
(
fmt
);
/* I hate these functions that presume you know how big the output
* will be ahead of time...
*/
for
(
i
=
1024
;
;
i
+=
i
)
{
outbuf
=
(
char
*
)
PyMem_Malloc
(
i
);
outbuf
=
(
time_char
*
)
PyMem_Malloc
(
i
*
sizeof
(
time_char
)
);
if
(
outbuf
==
NULL
)
{
Py_DECREF
(
format
);
return
PyErr_NoMemory
();
}
buflen
=
strf
time
(
outbuf
,
i
,
fmt
,
&
buf
);
buflen
=
format_
time
(
outbuf
,
i
,
fmt
,
&
buf
);
if
(
buflen
>
0
||
i
>=
256
*
fmtlen
)
{
/* If the buffer is 256 times as long as the format,
it's probably not failing for lack of room!
...
...
@@ -550,8 +576,12 @@ time_strftime(PyObject *self, PyObject *args)
e.g. an empty format, or %Z when the timezone
is unknown. */
PyObject
*
ret
;
#ifdef HAVE_WCSFTIME
ret
=
PyUnicode_FromWideChar
(
outbuf
,
buflen
);
#else
ret
=
PyUnicode_Decode
(
outbuf
,
buflen
,
TZNAME_ENCODING
,
NULL
);
#endif
PyMem_Free
(
outbuf
);
Py_DECREF
(
format
);
return
ret
;
...
...
@@ -568,6 +598,9 @@ time_strftime(PyObject *self, PyObject *args)
}
}
#undef time_char
#undef format_time
PyDoc_STRVAR
(
strftime_doc
,
"strftime(format[, tuple]) -> string
\n
\
\n
\
...
...
PC/pyconfig.h
View file @
1b01ccd7
...
...
@@ -637,6 +637,9 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
/* Define if you have waitpid. */
/* #undef HAVE_WAITPID */
/* Define to 1 if you have the `wcsftime' function. */
#define HAVE_WCSFTIME 1
/* Define to 1 if you have the `wcscoll' function. */
#ifndef MS_WINCE
#define HAVE_WCSCOLL 1
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment