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
dfca49ec
Commit
dfca49ec
authored
Jun 23, 2010
by
Alexander Belopolsky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #9051: Instances of timezone class can now be pickled.
parent
1a72a4bd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
16 deletions
+32
-16
Lib/test/test_datetime.py
Lib/test/test_datetime.py
+19
-14
Modules/datetimemodule.c
Modules/datetimemodule.c
+11
-0
Python/Python-ast.c
Python/Python-ast.c
+2
-2
No files found.
Lib/test/test_datetime.py
View file @
dfca49ec
...
...
@@ -19,8 +19,8 @@ from datetime import timezone
from
datetime
import
date
,
datetime
import
time
as
_time
pickle_choices
=
[(
pickle
,
pickle
,
proto
)
for
proto
in
range
(
3
)]
assert
len
(
pickle_choices
)
==
3
pickle_choices
=
[(
pickle
,
pickle
,
proto
)
for
proto
in
range
(
4
)]
assert
len
(
pickle_choices
)
==
4
# An arbitrary collection of objects of non-datetime types, for testing
# mixed-type comparisons.
...
...
@@ -122,18 +122,23 @@ class TestTZInfo(unittest.TestCase):
def
test_pickling_subclass
(
self
):
# Make sure we can pickle/unpickle an instance of a subclass.
offset
=
timedelta
(
minutes
=-
300
)
orig
=
PicklableFixedOffset
(
offset
,
'cookie'
)
self
.
assertIsInstance
(
orig
,
tzinfo
)
self
.
assertTrue
(
type
(
orig
)
is
PicklableFixedOffset
)
self
.
assertEqual
(
orig
.
utcoffset
(
None
),
offset
)
self
.
assertEqual
(
orig
.
tzname
(
None
),
'cookie'
)
for
pickler
,
unpickler
,
proto
in
pickle_choices
:
green
=
pickler
.
dumps
(
orig
,
proto
)
derived
=
unpickler
.
loads
(
green
)
self
.
assertIsInstance
(
derived
,
tzinfo
)
self
.
assertTrue
(
type
(
derived
)
is
PicklableFixedOffset
)
self
.
assertEqual
(
derived
.
utcoffset
(
None
),
offset
)
self
.
assertEqual
(
derived
.
tzname
(
None
),
'cookie'
)
for
otype
,
args
in
[
(
PicklableFixedOffset
,
(
offset
,
'cookie'
)),
(
timezone
,
(
offset
,)),
(
timezone
,
(
offset
,
"EST"
))]:
orig
=
otype
(
*
args
)
oname
=
orig
.
tzname
(
None
)
self
.
assertIsInstance
(
orig
,
tzinfo
)
self
.
assertIs
(
type
(
orig
),
otype
)
self
.
assertEqual
(
orig
.
utcoffset
(
None
),
offset
)
self
.
assertEqual
(
orig
.
tzname
(
None
),
oname
)
for
pickler
,
unpickler
,
proto
in
pickle_choices
:
green
=
pickler
.
dumps
(
orig
,
proto
)
derived
=
unpickler
.
loads
(
green
)
self
.
assertIsInstance
(
derived
,
tzinfo
)
self
.
assertIs
(
type
(
derived
),
otype
)
self
.
assertEqual
(
derived
.
utcoffset
(
None
),
offset
)
self
.
assertEqual
(
derived
.
tzname
(
None
),
oname
)
class
TestTimeZone
(
unittest
.
TestCase
):
...
...
Modules/datetimemodule.c
View file @
dfca49ec
...
...
@@ -3469,6 +3469,14 @@ timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt)
return
add_datetime_timedelta
(
dt
,
(
PyDateTime_Delta
*
)
self
->
offset
,
1
);
}
static
PyObject
*
timezone_getinitargs
(
PyDateTime_TimeZone
*
self
)
{
if
(
self
->
name
==
NULL
)
return
Py_BuildValue
(
"(O)"
,
self
->
offset
);
return
Py_BuildValue
(
"(OO)"
,
self
->
offset
,
self
->
name
);
}
static
PyMethodDef
timezone_methods
[]
=
{
{
"tzname"
,
(
PyCFunction
)
timezone_tzname
,
METH_O
,
PyDoc_STR
(
"If name is specified when timezone is created, returns the name."
...
...
@@ -3483,6 +3491,9 @@ static PyMethodDef timezone_methods[] = {
{
"fromutc"
,
(
PyCFunction
)
timezone_fromutc
,
METH_O
,
PyDoc_STR
(
"datetime in UTC -> datetime in local time."
)},
{
"__getinitargs__"
,
(
PyCFunction
)
timezone_getinitargs
,
METH_NOARGS
,
PyDoc_STR
(
"pickle support"
)},
{
NULL
,
NULL
}
};
...
...
Python/Python-ast.c
View file @
dfca49ec
...
...
@@ -2,7 +2,7 @@
/*
__version__
73626
.
__version__
82163
.
This module must be committed separately after each AST grammar change;
The __version__ number is set to the revision number of the commit
...
...
@@ -6773,7 +6773,7 @@ PyInit__ast(void)
NULL
;
if
(
PyModule_AddIntConstant
(
m
,
"PyCF_ONLY_AST"
,
PyCF_ONLY_AST
)
<
0
)
return
NULL
;
if
(
PyModule_AddStringConstant
(
m
,
"__version__"
,
"
73626
"
)
<
0
)
if
(
PyModule_AddStringConstant
(
m
,
"__version__"
,
"
82163
"
)
<
0
)
return
NULL
;
if
(
PyDict_SetItemString
(
d
,
"mod"
,
(
PyObject
*
)
mod_type
)
<
0
)
return
NULL
;
...
...
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