Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updated bundled version of six.

  • Loading branch information...
commit e11ccc76d325b6bbbf101f510a91299f507f0745 1 parent 46f4b19
Aymeric Augustin authored March 17, 2013
59  django/utils/six.py
... ...
@@ -1,6 +1,6 @@
1 1
 """Utilities for writing code that runs on Python 2 and 3"""
2 2
 
3  
-# Copyright (c) 2010-2012 Benjamin Peterson
  3
+# Copyright (c) 2010-2013 Benjamin Peterson
4 4
 #
5 5
 # Permission is hereby granted, free of charge, to any person obtaining a copy of
6 6
 # this software and associated documentation files (the "Software"), to deal in
@@ -209,22 +209,28 @@ def remove_move(name):
209 209
     _meth_func = "__func__"
210 210
     _meth_self = "__self__"
211 211
 
  212
+    _func_closure = "__closure__"
212 213
     _func_code = "__code__"
213 214
     _func_defaults = "__defaults__"
  215
+    _func_globals = "__globals__"
214 216
 
215 217
     _iterkeys = "keys"
216 218
     _itervalues = "values"
217 219
     _iteritems = "items"
  220
+    _iterlists = "lists"
218 221
 else:
219 222
     _meth_func = "im_func"
220 223
     _meth_self = "im_self"
221 224
 
  225
+    _func_closure = "func_closure"
222 226
     _func_code = "func_code"
223 227
     _func_defaults = "func_defaults"
  228
+    _func_globals = "func_globals"
224 229
 
225 230
     _iterkeys = "iterkeys"
226 231
     _itervalues = "itervalues"
227 232
     _iteritems = "iteritems"
  233
+    _iterlists = "iterlists"
228 234
 
229 235
 
230 236
 try:
@@ -235,14 +241,18 @@ def advance_iterator(it):
235 241
 next = advance_iterator
236 242
 
237 243
 
  244
+try:
  245
+    callable = callable
  246
+except NameError:
  247
+    def callable(obj):
  248
+        return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
  249
+
  250
+
238 251
 if PY3:
239 252
     def get_unbound_function(unbound):
240 253
         return unbound
241 254
 
242 255
     Iterator = object
243  
-
244  
-    def callable(obj):
245  
-        return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
246 256
 else:
247 257
     def get_unbound_function(unbound):
248 258
         return unbound.im_func
@@ -259,21 +269,27 @@ def next(self):
259 269
 
260 270
 get_method_function = operator.attrgetter(_meth_func)
261 271
 get_method_self = operator.attrgetter(_meth_self)
  272
+get_function_closure = operator.attrgetter(_func_closure)
262 273
 get_function_code = operator.attrgetter(_func_code)
263 274
 get_function_defaults = operator.attrgetter(_func_defaults)
  275
+get_function_globals = operator.attrgetter(_func_globals)
264 276
 
265 277
 
266  
-def iterkeys(d):
  278
+def iterkeys(d, **kw):
267 279
     """Return an iterator over the keys of a dictionary."""
268  
-    return iter(getattr(d, _iterkeys)())
  280
+    return iter(getattr(d, _iterkeys)(**kw))
269 281
 
270  
-def itervalues(d):
  282
+def itervalues(d, **kw):
271 283
     """Return an iterator over the values of a dictionary."""
272  
-    return iter(getattr(d, _itervalues)())
  284
+    return iter(getattr(d, _itervalues)(**kw))
273 285
 
274  
-def iteritems(d):
  286
+def iteritems(d, **kw):
275 287
     """Return an iterator over the (key, value) pairs of a dictionary."""
276  
-    return iter(getattr(d, _iteritems)())
  288
+    return iter(getattr(d, _iteritems)(**kw))
  289
+
  290
+def iterlists(d, **kw):
  291
+    """Return an iterator over the (key, [values]) pairs of a dictionary."""
  292
+    return iter(getattr(d, _iterlists)(**kw))
277 293
 
278 294
 
279 295
 if PY3:
@@ -317,17 +333,17 @@ def reraise(tp, value, tb=None):
317 333
     del builtins
318 334
 
319 335
 else:
320  
-    def exec_(code, globs=None, locs=None):
  336
+    def exec_(_code_, _globs_=None, _locs_=None):
321 337
         """Execute code in a namespace."""
322  
-        if globs is None:
  338
+        if _globs_ is None:
323 339
             frame = sys._getframe(1)
324  
-            globs = frame.f_globals
325  
-            if locs is None:
326  
-                locs = frame.f_locals
  340
+            _globs_ = frame.f_globals
  341
+            if _locs_ is None:
  342
+                _locs_ = frame.f_locals
327 343
             del frame
328  
-        elif locs is None:
329  
-            locs = globs
330  
-        exec("""exec code in globs, locs""")
  344
+        elif _locs_ is None:
  345
+            _locs_ = _globs_
  346
+        exec("""exec _code_ in _globs_, _locs_""")
331 347
 
332 348
 
333 349
     exec_("""def reraise(tp, value, tb=None):
@@ -391,12 +407,10 @@ def with_metaclass(meta, base=object):
391 407
 ### Additional customizations for Django ###
392 408
 
393 409
 if PY3:
394  
-    _iterlists = "lists"
395 410
     _assertRaisesRegex = "assertRaisesRegex"
396 411
     _assertRegex = "assertRegex"
397 412
     memoryview = memoryview
398 413
 else:
399  
-    _iterlists = "iterlists"
400 414
     _assertRaisesRegex = "assertRaisesRegexp"
401 415
     _assertRegex = "assertRegexpMatches"
402 416
     # memoryview and buffer are not stricly equivalent, but should be fine for
@@ -404,11 +418,6 @@ def with_metaclass(meta, base=object):
404 418
     memoryview = buffer
405 419
 
406 420
 
407  
-def iterlists(d):
408  
-    """Return an iterator over the values of a MultiValueDict."""
409  
-    return getattr(d, _iterlists)()
410  
-
411  
-
412 421
 def assertRaisesRegex(self, *args, **kwargs):
413 422
     return getattr(self, _assertRaisesRegex)(*args, **kwargs)
414 423
 
7  docs/topics/python3.txt
@@ -391,12 +391,7 @@ function.
391 391
 Customizations of six
392 392
 ---------------------
393 393
 
394  
-The version of six bundled with Django includes one extra function:
395  
-
396  
-.. function:: iterlists(MultiValueDict)
397  
-
398  
-    Returns an iterator over the lists of values of a ``MultiValueDict``. This
399  
-    replaces ``iterlists()`` on Python 2 and ``lists()`` on Python 3.
  394
+The version of six bundled with Django includes a few extras.
400 395
 
401 396
 .. function:: assertRaisesRegex(testcase, *args, **kwargs)
402 397
 

0 notes on commit e11ccc7

Please sign in to comment.
Something went wrong with that request. Please try again.