summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2013-12-25 23:26:23 -0600
committerBenjamin Peterson <benjamin@python.org>2013-12-25 23:26:23 -0600
commite6079eeb0711b9947431113835ca8e4cd6ebe6c0 (patch)
tree78576e99e441cf463c6bae3433e2f79523f77e75
parent3ed7bdb0af69fe162bd5c05795d8977905f337b3 (diff)
parent6de380126ab7f3add2ec36cfdbaf2dd82289c649 (diff)
downloadsix-e6079eeb0711b9947431113835ca8e4cd6ebe6c0.tar.gz
Merged in msabramo/six/dont_fail_tests_if_dont_have_gdbm (pull request #25)
Don't fail test if gdbm is not available
-rw-r--r--CHANGES7
-rw-r--r--README2
-rw-r--r--documentation/index.rst13
-rw-r--r--six.py53
-rw-r--r--test_six.py32
5 files changed, 85 insertions, 22 deletions
diff --git a/CHANGES b/CHANGES
index 1e9eec5..e6f7b4e 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,13 @@ This file lists the changes in each six version.
Development version
-------------------
+- Pull request #24: Add __dir__ special method to six.moves modules.
+
+- Issue #47: Fix add_metaclass on classes with a string for the __slots__
+ variable.
+
+- Issue #44: Fix interpretation of backslashes on Python 2 in the u() function.
+
- Pull request #21: Add import mapping for urllib's proxy_bypass function.
- Issue #43: Add import mapping for the Python 2 xmlrpclib module.
diff --git a/README b/README
index 99bd3a6..4de73fa 100644
--- a/README
+++ b/README
@@ -3,7 +3,7 @@ for smoothing over the differences between the Python versions with the goal of
writing Python code that is compatible on both Python versions. See the
documentation for more information on what is provided.
-Six supports every Python version since 2.4. It is contained in only one Python
+Six supports every Python version since 2.5. It is contained in only one Python
file, so it can be easily copied into your project. (The copyright and license
notice must be retained.)
diff --git a/documentation/index.rst b/documentation/index.rst
index 394fb9c..5c7860d 100644
--- a/documentation/index.rst
+++ b/documentation/index.rst
@@ -137,27 +137,26 @@ functions and methods is the stdlib :mod:`py3:inspect` module.
Get the closure (list of cells) associated with *func*. This is equivalent
to ``func.__closure__`` on Python 2.6+ and ``func.func_closure`` on Python
- 2.4 and 2.5.
+ 2.5.
.. function:: get_function_code(func)
Get the code object associated with *func*. This is equivalent to
- ``func.__code__`` on Python 2.6+ and ``func.func_code`` on Python 2.4 and
- 2.5.
+ ``func.__code__`` on Python 2.6+ and ``func.func_code`` on Python 2.5.
.. function:: get_function_defaults(func)
Get the defaults tuple associated with *func*. This is equivalent to
- ``func.__defaults__`` on Python 2.6+ and ``func.func_defaults`` on Python 2.4
- and 2.5.
+ ``func.__defaults__`` on Python 2.6+ and ``func.func_defaults`` on Python
+ 2.5.
.. function:: get_function_globals(func)
Get the globals of *func*. This is equivalent to ``func.__globals__`` on
- Python 2.6+ and ``func.func_globals`` on Python 2.4 and 2.5.
+ Python 2.6+ and ``func.func_globals`` on Python 2.5.
.. function:: next(it)
@@ -307,7 +306,7 @@ Python 2 and 3.
on Python 2.
Note that class decorators require Python 2.6. However, the effect of the
- decorator can be emulated on Python 2.4 and 2.5 like so::
+ decorator can be emulated on Python 2.5 like so::
class MyClass(object):
pass
diff --git a/six.py b/six.py
index aa6c4d5..d197d00 100644
--- a/six.py
+++ b/six.py
@@ -105,6 +105,21 @@ class MovedModule(_LazyDescr):
return _import_module(self.mod)
+class _LazyModule(types.ModuleType):
+
+ def __init__(self, name):
+ super(_LazyModule, self).__init__(name)
+ self.__doc__ = self.__class__.__doc__
+
+ def __dir__(self):
+ attrs = ["__doc__", "__name__"]
+ attrs += [attr.name for attr in self._moved_attributes]
+ return attrs
+
+ # Subclasses should override this
+ _moved_attributes = []
+
+
class MovedAttribute(_LazyDescr):
def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):
@@ -131,7 +146,7 @@ class MovedAttribute(_LazyDescr):
-class _MovedItems(types.ModuleType):
+class _MovedItems(_LazyModule):
"""Lazy loading of moved objects"""
@@ -198,11 +213,13 @@ for attr in _moved_attributes:
setattr(_MovedItems, attr.name, attr)
del attr
+_MovedItems._moved_attributes = _moved_attributes
+
moves = sys.modules[__name__ + ".moves"] = _MovedItems(__name__ + ".moves")
-class Module_six_moves_urllib_parse(types.ModuleType):
+class Module_six_moves_urllib_parse(_LazyModule):
"""Lazy loading of moved objects in six.moves.urllib_parse"""
@@ -226,11 +243,13 @@ for attr in _urllib_parse_moved_attributes:
setattr(Module_six_moves_urllib_parse, attr.name, attr)
del attr
+Module_six_moves_urllib_parse._moved_attributes = _urllib_parse_moved_attributes
+
sys.modules[__name__ + ".moves.urllib_parse"] = Module_six_moves_urllib_parse(__name__ + ".moves.urllib_parse")
sys.modules[__name__ + ".moves.urllib.parse"] = Module_six_moves_urllib_parse(__name__ + ".moves.urllib.parse")
-class Module_six_moves_urllib_error(types.ModuleType):
+class Module_six_moves_urllib_error(_LazyModule):
"""Lazy loading of moved objects in six.moves.urllib_error"""
@@ -243,11 +262,13 @@ for attr in _urllib_error_moved_attributes:
setattr(Module_six_moves_urllib_error, attr.name, attr)
del attr
+Module_six_moves_urllib_error._moved_attributes = _urllib_error_moved_attributes
+
sys.modules[__name__ + ".moves.urllib_error"] = Module_six_moves_urllib_error(__name__ + ".moves.urllib_error")
sys.modules[__name__ + ".moves.urllib.error"] = Module_six_moves_urllib_error(__name__ + ".moves.urllib.error")
-class Module_six_moves_urllib_request(types.ModuleType):
+class Module_six_moves_urllib_request(_LazyModule):
"""Lazy loading of moved objects in six.moves.urllib_request"""
@@ -290,11 +311,13 @@ for attr in _urllib_request_moved_attributes:
setattr(Module_six_moves_urllib_request, attr.name, attr)
del attr
+Module_six_moves_urllib_request._moved_attributes = _urllib_request_moved_attributes
+
sys.modules[__name__ + ".moves.urllib_request"] = Module_six_moves_urllib_request(__name__ + ".moves.urllib_request")
sys.modules[__name__ + ".moves.urllib.request"] = Module_six_moves_urllib_request(__name__ + ".moves.urllib.request")
-class Module_six_moves_urllib_response(types.ModuleType):
+class Module_six_moves_urllib_response(_LazyModule):
"""Lazy loading of moved objects in six.moves.urllib_response"""
@@ -308,11 +331,13 @@ for attr in _urllib_response_moved_attributes:
setattr(Module_six_moves_urllib_response, attr.name, attr)
del attr
+Module_six_moves_urllib_response._moved_attributes = _urllib_response_moved_attributes
+
sys.modules[__name__ + ".moves.urllib_response"] = Module_six_moves_urllib_response(__name__ + ".moves.urllib_response")
sys.modules[__name__ + ".moves.urllib.response"] = Module_six_moves_urllib_response(__name__ + ".moves.urllib.response")
-class Module_six_moves_urllib_robotparser(types.ModuleType):
+class Module_six_moves_urllib_robotparser(_LazyModule):
"""Lazy loading of moved objects in six.moves.urllib_robotparser"""
@@ -323,6 +348,8 @@ for attr in _urllib_robotparser_moved_attributes:
setattr(Module_six_moves_urllib_robotparser, attr.name, attr)
del attr
+Module_six_moves_urllib_robotparser._moved_attributes = _urllib_robotparser_moved_attributes
+
sys.modules[__name__ + ".moves.urllib_robotparser"] = Module_six_moves_urllib_robotparser(__name__ + ".moves.urllib_robotparser")
sys.modules[__name__ + ".moves.urllib.robotparser"] = Module_six_moves_urllib_robotparser(__name__ + ".moves.urllib.robotparser")
@@ -335,6 +362,9 @@ class Module_six_moves_urllib(types.ModuleType):
response = sys.modules[__name__ + ".moves.urllib_response"]
robotparser = sys.modules[__name__ + ".moves.urllib_robotparser"]
+ def __dir__(self):
+ return ['parse', 'error', 'request', 'response', 'robotparser']
+
sys.modules[__name__ + ".moves.urllib"] = Module_six_moves_urllib(__name__ + ".moves.urllib")
@@ -468,8 +498,9 @@ if PY3:
else:
def b(s):
return s
+ # Workaround for standalone backslash
def u(s):
- return unicode(s, "unicode_escape")
+ return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape")
unichr = unichr
int2byte = chr
def byte2int(bs):
@@ -580,7 +611,11 @@ def add_metaclass(metaclass):
orig_vars = cls.__dict__.copy()
orig_vars.pop('__dict__', None)
orig_vars.pop('__weakref__', None)
- for slots_var in orig_vars.get('__slots__', ()):
- orig_vars.pop(slots_var)
+ slots = orig_vars.get('__slots__')
+ if slots is not None:
+ if isinstance(slots, str):
+ slots = [slots]
+ for slots_var in slots:
+ orig_vars.pop(slots_var)
return metaclass(cls.__name__, cls.__bases__, orig_vars)
return wrapper
diff --git a/test_six.py b/test_six.py
index fee58e2..34613ad 100644
--- a/test_six.py
+++ b/test_six.py
@@ -110,6 +110,7 @@ def test_move_items(item_name):
if item_name.startswith("dbm_gnu") and not have_gdbm:
py.test.skip("requires gdbm")
raise
+ assert item_name in dir(six.moves)
@py.test.mark.parametrize("item_name",
@@ -120,6 +121,8 @@ def test_move_items_urllib_parse(item_name):
py.test.skip("ParseResult is only found on 2.5+")
if item_name in ("parse_qs", "parse_qsl") and sys.version_info < (2, 6):
py.test.skip("parse_qs[l] is new in 2.6")
+ if sys.version_info[:2] >= (2, 6):
+ assert item_name in dir(six.moves.urllib.parse)
getattr(six.moves.urllib.parse, item_name)
@@ -127,6 +130,8 @@ def test_move_items_urllib_parse(item_name):
[item.name for item in six._urllib_error_moved_attributes])
def test_move_items_urllib_error(item_name):
"""Ensure that everything loads correctly."""
+ if sys.version_info[:2] >= (2, 6):
+ assert item_name in dir(six.moves.urllib.error)
getattr(six.moves.urllib.error, item_name)
@@ -134,6 +139,8 @@ def test_move_items_urllib_error(item_name):
[item.name for item in six._urllib_request_moved_attributes])
def test_move_items_urllib_request(item_name):
"""Ensure that everything loads correctly."""
+ if sys.version_info[:2] >= (2, 6):
+ assert item_name in dir(six.moves.urllib.request)
getattr(six.moves.urllib.request, item_name)
@@ -141,6 +148,8 @@ def test_move_items_urllib_request(item_name):
[item.name for item in six._urllib_response_moved_attributes])
def test_move_items_urllib_response(item_name):
"""Ensure that everything loads correctly."""
+ if sys.version_info[:2] >= (2, 6):
+ assert item_name in dir(six.moves.urllib.response)
getattr(six.moves.urllib.response, item_name)
@@ -148,6 +157,8 @@ def test_move_items_urllib_response(item_name):
[item.name for item in six._urllib_robotparser_moved_attributes])
def test_move_items_urllib_robotparser(item_name):
"""Ensure that everything loads correctly."""
+ if sys.version_info[:2] >= (2, 6):
+ assert item_name in dir(six.moves.urllib.robotparser)
getattr(six.moves.urllib.robotparser, item_name)
@@ -396,9 +407,9 @@ if six.PY3:
def test_u():
- s = six.u("hi")
+ s = six.u("hi \u0439 \U00000439 \\ \\\\ \n")
assert isinstance(s, str)
- assert s == "hi"
+ assert s == "hi \u0439 \U00000439 \\ \\\\ \n"
else:
@@ -410,9 +421,9 @@ else:
def test_u():
- s = six.u("hi")
+ s = six.u("hi \u0439 \U00000439 \\ \\\\ \n")
assert isinstance(s, unicode)
- assert s == "hi"
+ assert s == "hi \xd0\xb9 \xd0\xb9 \\ \\\\ \n".decode("utf8")
def test_u_escapes():
@@ -632,7 +643,7 @@ def test_add_metaclass():
assert instance.b == Base.b
assert instance.x == X.x
- # test a class with slots
+ # Test a class with slots.
class MySlots(object):
__slots__ = ["a", "b"]
MySlots = six.add_metaclass(Meta1)(MySlots)
@@ -641,3 +652,14 @@ def test_add_metaclass():
instance = MySlots()
instance.a = "foo"
py.test.raises(AttributeError, setattr, instance, "c", "baz")
+
+ # Test a class with string for slots.
+ class MyStringSlots(object):
+ __slots__ = "ab"
+ MyStringSlots = six.add_metaclass(Meta1)(MyStringSlots)
+ assert MyStringSlots.__slots__ == "ab"
+ instance = MyStringSlots()
+ instance.ab = "foo"
+ py.test.raises(AttributeError, setattr, instance, "a", "baz")
+ py.test.raises(AttributeError, setattr, instance, "b", "baz")
+