Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added support for serializing class methods. - Refs #21290. #1775

Closed
wants to merge 1 commit into from

2 participants

@loic
Collaborator

The new handling allows us to do away with the whitelisting that was
required to support date and datetime objects.

@loic loic Added support for serializing class methods. - Refs #21290.
The new handling allows us to do away with the whitelisting that was
required to support date and datetime objects.
b91e3fc
@timgraham timgraham commented on the diff
django/db/migrations/writer.py
@@ -146,18 +146,9 @@ def serialize(cls, value):
return cls.serialize_deconstructed(path, args, kwargs)
# Functions
elif isinstance(value, (types.FunctionType, types.BuiltinFunctionType)):
- # Special-cases, as these don't have im_class
- special_cases = [
- (datetime.datetime.now, "datetime.datetime.now", ["import datetime"]),
- (datetime.datetime.utcnow, "datetime.datetime.utcnow", ["import datetime"]),
- (datetime.date.today, "datetime.date.today", ["import datetime"]),
- ]
- for func, string, imports in special_cases:
- if func == value: # For some reason "utcnow is not utcnow"
- return string, set(imports)
- # Method?
- if hasattr(value, "im_class"):
- klass = value.im_class
+ # @classmethod?
+ if getattr(value, "__self__", None) and isinstance(value.__self__, type):
@timgraham Owner

getattr->hasattr?

@loic Collaborator
loic added a note

getattr works well here, since we both want to check if __self__ exists (which means that func is a method), and that it's not None, which would be the case for an unbound method on PY2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham
Owner

Thanks for the explanation, merged in 8d6953d.

@timgraham timgraham closed this
@loic
Collaborator

Thanks for the merge ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 18, 2013
  1. @loic

    Added support for serializing class methods. - Refs #21290.

    loic authored
    The new handling allows us to do away with the whitelisting that was
    required to support date and datetime objects.
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 12 deletions.
  1. +3 −12 django/db/migrations/writer.py
  2. +2 −0  tests/migrations/test_writer.py
View
15 django/db/migrations/writer.py
@@ -146,18 +146,9 @@ def serialize(cls, value):
return cls.serialize_deconstructed(path, args, kwargs)
# Functions
elif isinstance(value, (types.FunctionType, types.BuiltinFunctionType)):
- # Special-cases, as these don't have im_class
- special_cases = [
- (datetime.datetime.now, "datetime.datetime.now", ["import datetime"]),
- (datetime.datetime.utcnow, "datetime.datetime.utcnow", ["import datetime"]),
- (datetime.date.today, "datetime.date.today", ["import datetime"]),
- ]
- for func, string, imports in special_cases:
- if func == value: # For some reason "utcnow is not utcnow"
- return string, set(imports)
- # Method?
- if hasattr(value, "im_class"):
- klass = value.im_class
+ # @classmethod?
+ if getattr(value, "__self__", None) and isinstance(value.__self__, type):
@timgraham Owner

getattr->hasattr?

@loic Collaborator
loic added a note

getattr works well here, since we both want to check if __self__ exists (which means that func is a method), and that it's not None, which would be the case for an unbound method on PY2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ klass = value.__self__
module = klass.__module__
return "%s.%s.%s" % (module, klass.__name__, value.__name__), set(["import %s" % module])
elif hasattr(value, 'deconstruct'):
View
2  tests/migrations/test_writer.py
@@ -70,6 +70,8 @@ def test_serialize(self):
# Datetime stuff
self.assertSerializedEqual(datetime.datetime.utcnow())
self.assertSerializedEqual(datetime.datetime.utcnow)
+ self.assertSerializedEqual(datetime.datetime.today())
+ self.assertSerializedEqual(datetime.datetime.today)
self.assertSerializedEqual(datetime.date.today())
self.assertSerializedEqual(datetime.date.today)
# Django fields
Something went wrong with that request. Please try again.