Author: Alex Gaynor <[email protected]>
Branch: py3k
Changeset: r48883:705745d3509c
Date: 2011-11-07 15:23 -0500
http://bitbucket.org/pypy/pypy/changeset/705745d3509c/
Log: merged upstream
diff --git a/lib-python/conftest.py b/lib-python/conftest.py
--- a/lib-python/conftest.py
+++ b/lib-python/conftest.py
@@ -61,7 +61,7 @@
usemodules = '',
skip=None):
self.basename = basename
- self._usemodules = usemodules.split() + ['signal']
+ self._usemodules = usemodules.split() + ['signal', '_warnings']
self._compiler = compiler
self.core = core
self.skip = skip
@@ -237,7 +237,7 @@
RegrTest('test_global.py', core=True),
RegrTest('test_grammar.py', core=True),
RegrTest('test_grp.py', skip=skip_win32),
- RegrTest('test_gzip.py'),
+ RegrTest('test_gzip.py', usemodules='zlib'),
RegrTest('test_hash.py', core=True),
RegrTest('test_hashlib.py', core=True),
RegrTest('test_heapq.py', core=True),
diff --git a/pypy/module/__builtin__/compiling.py
b/pypy/module/__builtin__/compiling.py
--- a/pypy/module/__builtin__/compiling.py
+++ b/pypy/module/__builtin__/compiling.py
@@ -30,6 +30,8 @@
if space.is_true(space.isinstance(w_source, w_ast_type)):
ast_node = space.interp_w(ast.mod, w_source)
ast_node.sync_app_attrs(space)
+ elif space.isinstance_w(w_source, space.w_bytes):
+ source_str = space.bytes_w(w_source)
else:
source_str = space.str_w(w_source)
# This flag tells the parser to reject any coding cookies it sees.
diff --git a/pypy/module/__builtin__/interp_memoryview.py
b/pypy/module/__builtin__/interp_memoryview.py
--- a/pypy/module/__builtin__/interp_memoryview.py
+++ b/pypy/module/__builtin__/interp_memoryview.py
@@ -72,7 +72,7 @@
return space.wrap(self.buf)
def descr_tobytes(self, space):
- return space.wrap(self.as_str())
+ return space.wrapbytes(self.as_str())
def descr_tolist(self, space):
buf = self.buf
diff --git a/pypy/module/__builtin__/operation.py
b/pypy/module/__builtin__/operation.py
--- a/pypy/module/__builtin__/operation.py
+++ b/pypy/module/__builtin__/operation.py
@@ -215,7 +215,7 @@
table of interned strings whose purpose is to speed up dictionary lookups.
Return the string itself or the previously interned string object with the
same value."""
- if space.is_w(space.type(w_str), space.w_str):
+ if space.is_w(space.type(w_str), space.w_unicode):
return space.new_interned_w_str(w_str)
raise OperationError(space.w_TypeError, space.wrap("intern() argument must
be string."))
diff --git a/pypy/module/__builtin__/test/test_abstractinst.py
b/pypy/module/__builtin__/test/test_abstractinst.py
--- a/pypy/module/__builtin__/test/test_abstractinst.py
+++ b/pypy/module/__builtin__/test/test_abstractinst.py
@@ -195,10 +195,12 @@
"""Implement issubclass(sub, cls)."""
candidates = cls.__dict__.get("__subclass__", set()) |
set([cls])
return any(c in candidates for c in sub.mro())
- class Integer:
- __metaclass__ = ABC
+ # Equivalent to::
+ # class Integer(metaclass=ABC):
+ # __subclass__ = set([int])
+ # But with a syntax compatible with 2.x
+ Integer = ABC('Integer', (), dict(__subclass__=set([int])))
- __subclass__ = set([int])
assert issubclass(int, Integer)
assert issubclass(int, (Integer,))
diff --git a/pypy/module/__builtin__/test/test_buffer.py
b/pypy/module/__builtin__/test/test_buffer.py
--- a/pypy/module/__builtin__/test/test_buffer.py
+++ b/pypy/module/__builtin__/test/test_buffer.py
@@ -12,21 +12,21 @@
if sys.maxunicode == 65535: # UCS2 build
assert len(b) == 4
if sys.byteorder == "big":
- assert b[0:4] == "\x00a\x00b"
+ assert b[0:4] == b"\x00a\x00b"
else:
- assert b[0:4] == "a\x00b\x00"
+ assert b[0:4] == b"a\x00b\x00"
else: # UCS4 build
assert len(b) == 8
if sys.byteorder == "big":
- assert b[0:8] == "\x00\x00\x00a\x00\x00\x00b"
+ assert b[0:8] == b"\x00\x00\x00a\x00\x00\x00b"
else:
- assert b[0:8] == "a\x00\x00\x00b\x00\x00\x00"
+ assert b[0:8] == b"a\x00\x00\x00b\x00\x00\x00"
def test_array_buffer(self):
import array
b = buffer(array.array("B", [1, 2, 3]))
assert len(b) == 3
- assert b[0:3] == "\x01\x02\x03"
+ assert b[0:3] == b"\x01\x02\x03"
def test_nonzero(self):
assert buffer('\x00')
@@ -36,68 +36,68 @@
assert not buffer(array.array("B", []))
def test_str(self):
- assert str(buffer('hello')) == 'hello'
+ assert str(buffer(b'hello')) == 'hello'
def test_repr(self):
# from 2.5.2 lib tests
- assert repr(buffer('hello')).startswith('<read-only buffer for 0x')
+ assert repr(buffer(b'hello')).startswith('<read-only buffer for 0x')
def test_add(self):
- assert buffer('abc') + 'def' == 'abcdef'
+ assert buffer(b'abc') + b'def' == b'abcdef'
import array
- assert buffer('abc') + array.array('c', 'def') == 'abcdef'
+ assert buffer(b'abc') + array.array('b', b'def') == b'abcdef'
def test_cmp(self):
- assert buffer('ab') != 'ab'
- assert not ('ab' == buffer('ab'))
- assert buffer('ab') == buffer('ab')
- assert not (buffer('ab') != buffer('ab'))
- assert not (buffer('ab') < buffer('ab'))
- assert buffer('ab') <= buffer('ab')
- assert not (buffer('ab') > buffer('ab'))
- assert buffer('ab') >= buffer('ab')
- assert buffer('ab') != buffer('abc')
- assert buffer('ab') < buffer('abc')
- assert buffer('ab') <= buffer('ab')
- assert buffer('ab') > buffer('aa')
- assert buffer('ab') >= buffer('ab')
+ assert buffer(b'ab') != b'ab'
+ assert not (b'ab' == buffer(b'ab'))
+ assert buffer(b'ab') == buffer(b'ab')
+ assert not (buffer(b'ab') != buffer(b'ab'))
+ assert not (buffer(b'ab') < buffer(b'ab'))
+ assert buffer(b'ab') <= buffer(b'ab')
+ assert not (buffer(b'ab') > buffer(b'ab'))
+ assert buffer(b'ab') >= buffer(b'ab')
+ assert buffer(b'ab') != buffer(b'abc')
+ assert buffer(b'ab') < buffer(b'abc')
+ assert buffer(b'ab') <= buffer(b'ab')
+ assert buffer(b'ab') > buffer(b'aa')
+ assert buffer(b'ab') >= buffer(b'ab')
def test_hash(self):
- assert hash(buffer('hello')) == hash('hello')
+ assert hash(buffer(b'hello')) == hash(b'hello')
def test_mul(self):
- assert buffer('ab') * 5 == 'ababababab'
- assert buffer('ab') * (-2) == ''
- assert 5 * buffer('ab') == 'ababababab'
- assert (-2) * buffer('ab') == ''
+ assert buffer(b'ab') * 5 == b'ababababab'
+ assert buffer(b'ab') * (-2) == b''
+ assert 5 * buffer(b'ab') == b'ababababab'
+ assert (-2) * buffer(b'ab') == b''
def test_offset_size(self):
- b = buffer('hello world', 6)
+ b = buffer(b'hello world', 6)
assert len(b) == 5
- assert b[0] == 'w'
- assert b[:] == 'world'
+ assert b[0] == b'w'
+ assert b[:] == b'world'
raises(IndexError, 'b[5]')
b = buffer(b, 2)
assert len(b) == 3
- assert b[0] == 'r'
- assert b[:] == 'rld'
+ assert b[0] == b'r'
+ assert b[:] == b'rld'
raises(IndexError, 'b[3]')
- b = buffer('hello world', 1, 8)
+ b = buffer(b'hello world', 1, 8)
assert len(b) == 8
- assert b[0] == 'e'
- assert b[:] == 'ello wor'
+ assert b[0] == b'e'
+ assert b[:] == b'ello wor'
raises(IndexError, 'b[8]')
b = buffer(b, 2, 3)
assert len(b) == 3
- assert b[2] == ' '
- assert b[:] == 'lo '
+ assert b[2] == b' '
+ assert b[:] == b'lo '
raises(IndexError, 'b[3]')
b = buffer('hello world', 55)
assert len(b) == 0
- assert b[:] == ''
- b = buffer('hello world', 6, 999)
+ assert b[:] == b''
+ b = buffer(b'hello world', 6, 999)
assert len(b) == 5
- assert b[:] == 'world'
+ assert b[:] == b'world'
raises(ValueError, buffer, "abc", -1)
raises(ValueError, buffer, "abc", 0, -2)
@@ -105,17 +105,17 @@
def test_rw_offset_size(self):
import array
- a = array.array("c", 'hello world')
+ a = array.array("b", b'hello world')
b = buffer(a, 6)
assert len(b) == 5
- assert b[0] == 'w'
- assert b[:] == 'world'
+ assert b[0] == b'w'
+ assert b[:] == b'world'
raises(IndexError, 'b[5]')
- b[0] = 'W'
- assert str(b) == 'World'
- assert a.tostring() == 'hello World'
- b[:] = '12345'
- assert a.tostring() == 'hello 12345'
+ b[0] = b'W'
+ assert str(b) == b'World'
+ assert a.tostring() == b'hello World'
+ b[:] = b'12345'
+ assert a.tostring() == b'hello 12345'
raises(IndexError, 'b[5] = "."')
b = buffer(b, 2)
@@ -161,7 +161,7 @@
def test_slice(self):
# Test extended slicing by comparing with list slicing.
- s = "".join(chr(c) for c in list(range(255, -1, -1)))
+ s = bytes(c for c in list(range(255, -1, -1)))
b = buffer(s)
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
for start in indices:
@@ -172,8 +172,8 @@
class AppTestMemoryView:
def test_basic(self):
- v = memoryview("abc")
- assert v.tobytes() == "abc"
+ v = memoryview(b"abc")
+ assert v.tobytes() == b"abc"
assert len(v) == 3
assert list(v) == ['a', 'b', 'c']
assert v.tolist() == [97, 98, 99]
@@ -186,17 +186,17 @@
assert len(w) == 2
def test_rw(self):
- data = bytearray('abcefg')
+ data = bytearray(b'abcefg')
v = memoryview(data)
assert v.readonly is False
- v[0] = 'z'
+ v[0] = b'z'
assert data == bytearray(eval("b'zbcefg'"))
- v[1:4] = '123'
+ v[1:4] = b'123'
assert data == bytearray(eval("b'z123fg'"))
raises((ValueError, TypeError), "v[2] = 'spam'")
def test_memoryview_attrs(self):
- v = memoryview("a"*100)
+ v = memoryview(b"a"*100)
assert v.format == "B"
assert v.itemsize == 1
assert v.shape == (100,)
@@ -204,13 +204,13 @@
assert v.strides == (1,)
def test_suboffsets(self):
- v = memoryview("a"*100)
+ v = memoryview(b"a"*100)
assert v.suboffsets == None
- v = memoryview(buffer("a"*100, 2))
+ v = memoryview(buffer(b"a"*100, 2))
assert v.shape == (98,)
assert v.suboffsets == None
def test_compare(self):
- assert memoryview("abc") == "abc"
- assert memoryview("abc") == bytearray("abc")
- assert memoryview("abc") != 3
+ assert memoryview(b"abc") == b"abc"
+ assert memoryview(b"abc") == bytearray(b"abc")
+ assert memoryview(b"abc") != 3
diff --git a/pypy/module/__builtin__/test/test_builtin.py
b/pypy/module/__builtin__/test/test_builtin.py
--- a/pypy/module/__builtin__/test/test_builtin.py
+++ b/pypy/module/__builtin__/test/test_builtin.py
@@ -27,8 +27,8 @@
cls.w_safe_runtimerror = cls.space.wrap(sys.version_info < (2, 6))
def test_bytes_alias(self):
- assert bytes is str
- assert isinstance(eval("b'hi'"), str)
+ assert bytes is not str
+ assert isinstance(eval("b'hi'"), bytes)
def test_import(self):
m = __import__('pprint')
@@ -73,7 +73,7 @@
def test_globals(self):
d = {"foo":"bar"}
- exec "def f(): return globals()" in d
+ exec("def f(): return globals()", d)
d2 = d["f"]()
assert d2 is d
@@ -157,7 +157,7 @@
assert format(10, "o") == "12"
assert format(10, "#o") == "0o12"
assert format("hi") == "hi"
- assert isinstance(format(4, u""), unicode)
+ assert isinstance(format(4, u""), str)
def test_vars(self):
def f():
@@ -208,10 +208,10 @@
def test_iter_sequence(self):
raises(TypeError,iter,3)
x = iter(['a','b','c'])
- assert x.next() =='a'
- assert x.next() =='b'
- assert x.next() =='c'
- raises(StopIteration,x.next)
+ assert next(x) =='a'
+ assert next(x) =='b'
+ assert next(x) =='c'
+ raises(StopIteration, next, x)
def test_iter___iter__(self):
# This test assumes that dict.keys() method returns keys in
@@ -235,16 +235,16 @@
#self.assertRaises(TypeError,iter,[],5)
#self.assertRaises(TypeError,iter,{},5)
x = iter(count(),3)
- assert x.next() ==1
- assert x.next() ==2
- raises(StopIteration,x.next)
+ assert next(x) ==1
+ assert next(x) ==2
+ raises(StopIteration, next, x)
def test_enumerate(self):
seq = range(2,4)
enum = enumerate(seq)
- assert enum.next() == (0, 2)
- assert enum.next() == (1, 3)
- raises(StopIteration, enum.next)
+ assert next(enum) == (0, 2)
+ assert next(enum) == (1, 3)
+ raises(StopIteration, next, enum)
raises(TypeError, enumerate, 1)
raises(TypeError, enumerate, None)
enum = enumerate(range(5), 2)
@@ -262,7 +262,7 @@
class Counter:
def __init__(self):
self.count = 0
- def next(self):
+ def __next__(self):
self.count += 1
return self.count
x = Counter()
@@ -297,17 +297,17 @@
def test_range_up(self):
x = range(2)
iter_x = iter(x)
- assert iter_x.next() == 0
- assert iter_x.next() == 1
- raises(StopIteration, iter_x.next)
+ assert next(iter_x) == 0
+ assert next(iter_x) == 1
+ raises(StopIteration, next, iter_x)
def test_range_down(self):
x = range(4,2,-1)
iter_x = iter(x)
- assert iter_x.next() == 4
- assert iter_x.next() == 3
- raises(StopIteration, iter_x.next)
+ assert next(iter_x) == 4
+ assert next(iter_x) == 3
+ raises(StopIteration, next, iter_x)
def test_range_has_type_identity(self):
assert type(range(1)) == type(range(1))
@@ -315,13 +315,12 @@
def test_range_len(self):
x = range(33)
assert len(x) == 33
- x = range(33.2)
- assert len(x) == 33
+ raises(TypeError, range, 33.2)
x = range(33,0,-1)
assert len(x) == 33
x = range(33,0)
assert len(x) == 0
- x = range(33,0.2)
+ raises(TypeError, range, 33, 0.2)
assert len(x) == 0
x = range(0,33)
assert len(x) == 33
@@ -495,7 +494,7 @@
assert eval(co) == 3
compile("from __future__ import with_statement", "<test>", "exec")
raises(SyntaxError, compile, '-', '?', 'eval')
- raises(ValueError, compile, '"\\xt"', '?', 'eval')
+ raises(SyntaxError, compile, '"\\xt"', '?', 'eval')
raises(ValueError, compile, '1+2', '?', 'maybenot')
raises(ValueError, compile, "\n", "<string>", "exec", 0xff)
raises(TypeError, compile, '1+2', 12, 34)
@@ -510,10 +509,14 @@
code = u"# -*- coding: utf-8 -*-\npass\n"
raises(SyntaxError, compile, code, "tmp", "exec")
+ def test_bytes_compile(self):
+ code = b"# -*- coding: utf-8 -*-\npass\n"
+ compile(code, "tmp", "exec")
+
def test_recompile_ast(self):
import _ast
# raise exception when node type doesn't match with compile mode
- co1 = compile('print 1', '<string>', 'exec', _ast.PyCF_ONLY_AST)
+ co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
raises(TypeError, compile, co1, '<ast>', 'eval')
co2 = compile('1+1', '<string>', 'eval', _ast.PyCF_ONLY_AST)
compile(co2, '<ast>', 'eval')
@@ -589,39 +592,39 @@
assert firstlineno == 2
def test_print_function(self):
- import __builtin__
+ import builtins
import sys
- import StringIO
- pr = getattr(__builtin__, "print")
+ import io
+ pr = getattr(builtins, "print")
save = sys.stdout
- out = sys.stdout = StringIO.StringIO()
+ out = sys.stdout = io.StringIO()
try:
pr("Hello,", "person!")
finally:
sys.stdout = save
assert out.getvalue() == "Hello, person!\n"
- out = StringIO.StringIO()
+ out = io.StringIO()
pr("Hello,", "person!", file=out)
assert out.getvalue() == "Hello, person!\n"
- out = StringIO.StringIO()
+ out = io.StringIO()
pr("Hello,", "person!", file=out, end="")
assert out.getvalue() == "Hello, person!"
- out = StringIO.StringIO()
+ out = io.StringIO()
pr("Hello,", "person!", file=out, sep="X")
assert out.getvalue() == "Hello,Xperson!\n"
- out = StringIO.StringIO()
+ out = io.StringIO()
pr(u"Hello,", u"person!", file=out)
result = out.getvalue()
- assert isinstance(result, unicode)
+ assert isinstance(result, str)
assert result == u"Hello, person!\n"
pr("Hello", file=None) # This works.
- out = StringIO.StringIO()
+ out = io.StringIO()
pr(None, file=out)
assert out.getvalue() == "None\n"
def test_print_exceptions(self):
- import __builtin__
- pr = getattr(__builtin__, "print")
+ import builtins
+ pr = getattr(builtins, "print")
raises(TypeError, pr, x=3)
raises(TypeError, pr, end=3)
raises(TypeError, pr, sep=42)
diff --git a/pypy/module/__builtin__/test/test_descriptor.py
b/pypy/module/__builtin__/test/test_descriptor.py
--- a/pypy/module/__builtin__/test/test_descriptor.py
+++ b/pypy/module/__builtin__/test/test_descriptor.py
@@ -342,7 +342,7 @@
except ZeroDivisionError:
pass
else:
- raise Exception, "expected ZeroDivisionError from bad property"
+ raise Exception("expected ZeroDivisionError from bad property")
def test_property_subclass(self):
class P(property):
diff --git a/pypy/module/__builtin__/test/test_filter.py
b/pypy/module/__builtin__/test/test_filter.py
--- a/pypy/module/__builtin__/test/test_filter.py
+++ b/pypy/module/__builtin__/test/test_filter.py
@@ -16,22 +16,10 @@
raises(TypeError, filter, lambda x: x>3, [1], [2])
def test_filter_no_function_list(self):
- assert filter(None, [1, 2, 3]) == [1, 2, 3]
-
- def test_filter_no_function_tuple(self):
- assert filter(None, (1, 2, 3)) == (1, 2, 3)
-
- def test_filter_no_function_string(self):
- assert filter(None, 'mystring') == 'mystring'
+ assert list(filter(None, [1, 2, 3])) == [1, 2, 3]
def test_filter_no_function_with_bools(self):
- assert filter(None, (True, False, True)) == (True, True)
+ assert tuple(filter(None, (True, False, True))) == (True, True)
def test_filter_list(self):
- assert filter(lambda x: x>3, [1, 2, 3, 4, 5]) == [4, 5]
-
- def test_filter_tuple(self):
- assert filter(lambda x: x>3, (1, 2, 3, 4, 5)) == (4, 5)
-
- def test_filter_string(self):
- assert filter(lambda x: x>'a', 'xyzabcd') == 'xyzbcd'
+ assert list(filter(lambda x: x>3, [1, 2, 3, 4, 5])) == [4, 5]
diff --git a/pypy/module/__builtin__/test/test_functional.py
b/pypy/module/__builtin__/test/test_functional.py
--- a/pypy/module/__builtin__/test/test_functional.py
+++ b/pypy/module/__builtin__/test/test_functional.py
@@ -70,7 +70,7 @@
class B(object):
def __init__(self, n):
self.n = n
- def next(self):
+ def __next__(self):
self.n -= 1
if self.n == 0: raise StopIteration
return self.n
@@ -126,12 +126,12 @@
x = range(2, 9, 3)
it = iter(x)
assert iter(it) is it
- assert it.next() == 2
- assert it.next() == 5
- assert it.next() == 8
- raises(StopIteration, it.next)
+ assert it.__next__() == 2
+ assert it.__next__() == 5
+ assert it.__next__() == 8
+ raises(StopIteration, it.__next__)
# test again, to make sure that range() is not its own iterator
- assert iter(x).next() == 2
+ assert iter(x).__next__() == 2
def test_range_object_with___int__(self):
class A(object):
@@ -143,7 +143,7 @@
assert list(range(0, 10, A())) == [0, 5]
def test_range_float(self):
- assert list(range(0.1, 2.0, 1.1)) == [0, 1]
+ raises(TypeError, range(0.1, 2.0, 1.1))
def test_range_long(self):
import sys
@@ -162,12 +162,12 @@
def test_reversed(self):
r = reversed("hello")
assert iter(r) is r
- assert r.next() == "o"
- assert r.next() == "l"
- assert r.next() == "l"
- assert r.next() == "e"
- assert r.next() == "h"
- raises(StopIteration, r.next)
+ assert r.__next__() == "o"
+ assert r.__next__() == "l"
+ assert r.__next__() == "l"
+ assert r.__next__() == "e"
+ assert r.__next__() == "h"
+ raises(StopIteration, r.__next__)
assert list(reversed(list(reversed("hello")))) == ['h','e','l','l','o']
raises(TypeError, reversed, reversed("hello"))
diff --git a/pypy/module/__builtin__/test/test_rawinput.py
b/pypy/module/__builtin__/test/test_rawinput.py
--- a/pypy/module/__builtin__/test/test_rawinput.py
+++ b/pypy/module/__builtin__/test/test_rawinput.py
@@ -1,30 +1,30 @@
+from __future__ import print_function
import autopath
class AppTestRawInput():
def test_input_and_raw_input(self):
- import sys, StringIO
- for prompt, expected in [("def:", "abc/ def:/ghi\n"),
- ("", "abc/ /ghi\n"),
- (42, "abc/ 42/ghi\n"),
- (None, "abc/ None/ghi\n"),
- (Ellipsis, "abc/ /ghi\n")]:
+ import sys, io
+ for prompt, expected in [("def:", "abc/def:/ghi\n"),
+ ("", "abc//ghi\n"),
+ (42, "abc/42/ghi\n"),
+ (None, "abc/None/ghi\n"),
+ (Ellipsis, "abc//ghi\n")]:
for inputfn, inputtext, gottext in [
- (raw_input, "foo\nbar\n", "foo"),
- (input, "40+2\n", 42)]:
+ (input, "foo\nbar\n", "foo")]:
save = sys.stdin, sys.stdout
try:
- sys.stdin = StringIO.StringIO(inputtext)
- out = sys.stdout = StringIO.StringIO()
- print "abc", # softspace = 1
+ sys.stdin = io.StringIO(inputtext)
+ out = sys.stdout = io.StringIO()
+ print("abc", end='')
out.write('/')
if prompt is Ellipsis:
got = inputfn()
else:
got = inputfn(prompt)
out.write('/')
- print "ghi"
+ print("ghi")
finally:
sys.stdin, sys.stdout = save
assert out.getvalue() == expected
@@ -32,9 +32,9 @@
def test_softspace(self):
import sys
- import StringIO
- fin = StringIO.StringIO()
- fout = StringIO.StringIO()
+ import io
+ fin = io.StringIO()
+ fout = io.StringIO()
fin.write("Coconuts\n")
fin.seek(0)
@@ -45,20 +45,20 @@
sys.stdin = fin
sys.stdout = fout
- print "test",
- raw_input("test")
+ print("test", end='')
+ input("test")
sys.stdin = sys_stdin_orig
sys.stdout = sys_stdout_orig
fout.seek(0)
- assert fout.read() == "test test"
+ assert fout.read() == "testtest"
def test_softspace_carryover(self):
import sys
- import StringIO
- fin = StringIO.StringIO()
- fout = StringIO.StringIO()
+ import io
+ fin = io.StringIO()
+ fout = io.StringIO()
fin.write("Coconuts\n")
fin.seek(0)
@@ -69,12 +69,12 @@
sys.stdin = fin
sys.stdout = fout
- print "test",
- raw_input("test")
- print "test",
+ print("test", end='')
+ input("test")
+ print("test", end='')
sys.stdin = sys_stdin_orig
sys.stdout = sys_stdout_orig
fout.seek(0)
- assert fout.read() == "test testtest"
+ assert fout.read() == "testtesttest"
diff --git a/pypy/module/_sre/interp_sre.py b/pypy/module/_sre/interp_sre.py
--- a/pypy/module/_sre/interp_sre.py
+++ b/pypy/module/_sre/interp_sre.py
@@ -78,8 +78,7 @@
return space.newtuple(grps)
def import_re(space):
- w_builtin = space.getbuiltinmodule('__builtin__')
- w_import = space.getattr(w_builtin, space.wrap("__import__"))
+ w_import = space.getattr(space.builtin, space.wrap("__import__"))
return space.call_function(w_import, space.wrap("re"))
def matchcontext(space, ctx):
diff --git a/pypy/module/posix/interp_posix.py
b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -56,7 +56,7 @@
self.w_obj = w_obj
def as_bytes(self):
- return self.space.str_w(self.w_obj)
+ return self.space.bytes_w(self.w_obj)
def as_unicode(self):
space = self.space
@@ -71,7 +71,7 @@
fname = FileEncoder(space, w_fname)
return func(fname, *args)
else:
- fname = space.str_w(w_fname)
+ fname = space.bytes_w(w_fname)
return func(fname, *args)
return dispatch
@@ -512,19 +512,17 @@
for key, value in os.environ.items():
space.setitem(w_env, space.wrapbytes(key), space.wrapbytes(value))
-@unwrap_spec(name=str, value=str)
-def putenv(space, name, value):
+def putenv(space, w_name, w_value):
"""Change or add an environment variable."""
try:
- os.environ[name] = value
+ dispatch_filename_2(rposix.putenv)(space, w_name, w_value)
except OSError, e:
raise wrap_oserror(space, e)
-@unwrap_spec(name=str)
-def unsetenv(space, name):
+def unsetenv(space, w_name):
"""Delete an environment variable."""
try:
- del os.environ[name]
+ dispatch_filename(rposix.unsetenv)(space, w_name)
except KeyError:
pass
except OSError, e:
diff --git a/pypy/module/rctime/app_time.py b/pypy/module/rctime/app_time.py
--- a/pypy/module/rctime/app_time.py
+++ b/pypy/module/rctime/app_time.py
@@ -24,7 +24,7 @@
(same as strftime())."""
import _strptime # from the CPython standard library
- return _strptime._strptime(string, format)[0]
+ return _strptime._strptime_time(string, format)
__doc__ = """This module provides various functions to manipulate time values.
diff --git a/pypy/module/rctime/test/test_rctime.py
b/pypy/module/rctime/test/test_rctime.py
--- a/pypy/module/rctime/test/test_rctime.py
+++ b/pypy/module/rctime/test/test_rctime.py
@@ -199,7 +199,7 @@
# rely on it.
if org_TZ is not None:
os.environ['TZ'] = org_TZ
- elif os.environ.has_key('TZ'):
+ elif 'TZ' in os.environ:
del os.environ['TZ']
rctime.tzset()
@@ -279,7 +279,7 @@
'j', 'm', 'M', 'p', 'S',
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
format = ' %' + directive
- print format
+ print(format)
rctime.strptime(rctime.strftime(format, tt), format)
def test_pickle(self):
diff --git a/pypy/module/zlib/interp_zlib.py b/pypy/module/zlib/interp_zlib.py
--- a/pypy/module/zlib/interp_zlib.py
+++ b/pypy/module/zlib/interp_zlib.py
@@ -1,7 +1,7 @@
import sys
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.baseobjspace import Wrappable
-from pypy.interpreter.typedef import TypeDef, interp_attrproperty
+from pypy.interpreter.typedef import TypeDef, interp_attrproperty_bytes
from pypy.interpreter.error import OperationError
from pypy.rlib.rarithmetic import intmask, r_uint
from pypy.rlib.objectmodel import keepalive_until_here
@@ -84,7 +84,7 @@
rzlib.deflateEnd(stream)
except rzlib.RZlibError, e:
raise zlib_error(space, e.msg)
- return space.wrap(result)
+ return space.wrapbytes(result)
@unwrap_spec(string='bufferstr', wbits=int, bufsize=int)
@@ -106,7 +106,7 @@
rzlib.inflateEnd(stream)
except rzlib.RZlibError, e:
raise zlib_error(space, e.msg)
- return space.wrap(result)
+ return space.wrapbytes(result)
class ZLibObject(Wrappable):
@@ -179,7 +179,7 @@
self.unlock()
except rzlib.RZlibError, e:
raise zlib_error(self.space, e.msg)
- return self.space.wrap(result)
+ return self.space.wrapbytes(result)
@unwrap_spec(mode=int)
@@ -209,7 +209,7 @@
self.unlock()
except rzlib.RZlibError, e:
raise zlib_error(self.space, e.msg)
- return self.space.wrap(result)
+ return self.space.wrapbytes(result)
@unwrap_spec(level=int, method=int, wbits=int, memLevel=int, strategy=int)
@@ -302,11 +302,11 @@
assert unused_start >= 0
tail = data[unused_start:]
if finished:
- self.unconsumed_tail = ''
+ self.unconsumed_tail = b''
self.unused_data = tail
else:
self.unconsumed_tail = tail
- return self.space.wrap(string)
+ return self.space.wrapbytes(string)
@unwrap_spec(length=int)
@@ -324,7 +324,7 @@
# however CPython's zlib module does not behave like that.
# I could not figure out a case in which flush() in CPython
# doesn't simply return an empty string without complaining.
- return self.space.wrap("")
+ return self.space.wrapbytes("")
@unwrap_spec(wbits=int)
@@ -343,8 +343,8 @@
__new__ = interp2app(Decompress___new__),
decompress = interp2app(Decompress.decompress),
flush = interp2app(Decompress.flush),
- unused_data = interp_attrproperty('unused_data', Decompress),
- unconsumed_tail = interp_attrproperty('unconsumed_tail', Decompress),
+ unused_data = interp_attrproperty_bytes('unused_data', Decompress),
+ unconsumed_tail = interp_attrproperty_bytes('unconsumed_tail', Decompress),
__doc__ = """decompressobj([wbits]) -- Return a decompressor object.
Optional arg wbits is the window buffer size.
diff --git a/pypy/module/zlib/test/test_zlib.py
b/pypy/module/zlib/test/test_zlib.py
--- a/pypy/module/zlib/test/test_zlib.py
+++ b/pypy/module/zlib/test/test_zlib.py
@@ -34,9 +34,9 @@
import zlib
return zlib
""")
- expanded = 'some bytes which will be compressed'
- cls.w_expanded = cls.space.wrap(expanded)
- cls.w_compressed = cls.space.wrap(zlib.compress(expanded))
+ expanded = b'some bytes which will be compressed'
+ cls.w_expanded = cls.space.wrapbytes(expanded)
+ cls.w_compressed = cls.space.wrapbytes(zlib.compress(expanded))
def test_error(self):
@@ -52,9 +52,9 @@
return it as a signed 32 bit integer. On 64-bit machines too
(it is a bug in CPython < 2.6 to return unsigned values in this case).
"""
- assert self.zlib.crc32('') == 0
- assert self.zlib.crc32('\0') == -771559539
- assert self.zlib.crc32('hello, world.') == -936931198
+ assert self.zlib.crc32(b'') == 0
+ assert self.zlib.crc32(b'\0') == -771559539
+ assert self.zlib.crc32(b'hello, world.') == -936931198
def test_crc32_start_value(self):
@@ -62,29 +62,29 @@
When called with a string and an integer, zlib.crc32 should compute the
CRC32 of the string using the integer as the starting value.
"""
- assert self.zlib.crc32('', 42) == 42
- assert self.zlib.crc32('\0', 42) == 163128923
- assert self.zlib.crc32('hello, world.', 42) == 1090960721
- hello = 'hello, '
+ assert self.zlib.crc32(b'', 42) == 42
+ assert self.zlib.crc32(b'\0', 42) == 163128923
+ assert self.zlib.crc32(b'hello, world.', 42) == 1090960721
+ hello = b'hello, '
hellocrc = self.zlib.crc32(hello)
- world = 'world.'
+ world = b'world.'
helloworldcrc = self.zlib.crc32(world, hellocrc)
assert helloworldcrc == self.zlib.crc32(hello + world)
def test_crc32_negative_start(self):
- v = self.zlib.crc32('', -1)
+ v = self.zlib.crc32(b'', -1)
assert v == -1
def test_crc32_negative_long_start(self):
- v = self.zlib.crc32('', -1L)
+ v = self.zlib.crc32(b'', -1L)
assert v == -1
- assert self.zlib.crc32('foo', -99999999999999999999999) == 1611238463
+ assert self.zlib.crc32(b'foo', -99999999999999999999999) == 1611238463
def test_crc32_long_start(self):
import sys
- v = self.zlib.crc32('', sys.maxint*2)
+ v = self.zlib.crc32(b'', sys.maxint*2)
assert v == -2
- assert self.zlib.crc32('foo', 99999999999999999999999) == 1635107045
+ assert self.zlib.crc32(b'foo', 99999999999999999999999) == 1635107045
def test_adler32(self):
"""
@@ -93,10 +93,10 @@
On 64-bit machines too
(it is a bug in CPython < 2.6 to return unsigned values in this case).
"""
- assert self.zlib.adler32('') == 1
- assert self.zlib.adler32('\0') == 65537
- assert self.zlib.adler32('hello, world.') == 571147447
- assert self.zlib.adler32('x' * 23) == -2122904887
+ assert self.zlib.adler32(b'') == 1
+ assert self.zlib.adler32(b'\0') == 65537
+ assert self.zlib.adler32(b'hello, world.') == 571147447
+ assert self.zlib.adler32(b'x' * 23) == -2122904887
def test_adler32_start_value(self):
@@ -105,18 +105,18 @@
the adler 32 checksum of the string using the integer as the starting
value.
"""
- assert self.zlib.adler32('', 42) == 42
- assert self.zlib.adler32('\0', 42) == 2752554
- assert self.zlib.adler32('hello, world.', 42) == 606078176
- assert self.zlib.adler32('x' * 23, 42) == -2061104398
- hello = 'hello, '
+ assert self.zlib.adler32(b'', 42) == 42
+ assert self.zlib.adler32(b'\0', 42) == 2752554
+ assert self.zlib.adler32(b'hello, world.', 42) == 606078176
+ assert self.zlib.adler32(b'x' * 23, 42) == -2061104398
+ hello = b'hello, '
hellosum = self.zlib.adler32(hello)
- world = 'world.'
+ world = b'world.'
helloworldsum = self.zlib.adler32(world, hellosum)
assert helloworldsum == self.zlib.adler32(hello + world)
- assert self.zlib.adler32('foo', -1) == 45547858
- assert self.zlib.adler32('foo', 99999999999999999999999) == -114818734
+ assert self.zlib.adler32(b'foo', -1) == 45547858
+ assert self.zlib.adler32(b'foo', 99999999999999999999999) == -114818734
def test_invalidLevel(self):
@@ -171,7 +171,7 @@
Try to feed garbage to zlib.decompress().
"""
raises(self.zlib.error, self.zlib.decompress, self.compressed[:-2])
- raises(self.zlib.error, self.zlib.decompress, 'foobar')
+ raises(self.zlib.error, self.zlib.decompress, b'foobar')
def test_unused_data(self):
@@ -180,21 +180,21 @@
It should show up in the unused_data attribute.
"""
d = self.zlib.decompressobj()
- s = d.decompress(self.compressed + 'extrastuff')
+ s = d.decompress(self.compressed + b'extrastuff')
assert s == self.expanded
- assert d.unused_data == 'extrastuff'
+ assert d.unused_data == b'extrastuff'
# try again with several decompression steps
d = self.zlib.decompressobj()
s1 = d.decompress(self.compressed[:10])
- assert d.unused_data == ''
+ assert d.unused_data == b''
s2 = d.decompress(self.compressed[10:-3])
- assert d.unused_data == ''
- s3 = d.decompress(self.compressed[-3:] + 'spam' * 100)
- assert d.unused_data == 'spam' * 100
+ assert d.unused_data == b''
+ s3 = d.decompress(self.compressed[-3:] + b'spam' * 100)
+ assert d.unused_data == b'spam' * 100
assert s1 + s2 + s3 == self.expanded
- s4 = d.decompress('egg' * 50)
- assert d.unused_data == 'egg' * 50
- assert s4 == ''
+ s4 = d.decompress(b'egg' * 50)
+ assert d.unused_data == b'egg' * 50
+ assert s4 == b''
def test_max_length(self):
@@ -215,8 +215,8 @@
"""
We should be able to pass buffer objects instead of strings.
"""
- assert self.zlib.crc32(buffer('hello, world.')) == -936931198
- assert self.zlib.adler32(buffer('hello, world.')) == 571147447
+ assert self.zlib.crc32(buffer(b'hello, world.')) == -936931198
+ assert self.zlib.adler32(buffer(b'hello, world.')) == 571147447
compressor = self.zlib.compressobj()
bytes = compressor.compress(buffer(self.expanded))
diff --git a/pypy/objspace/std/bytearraytype.py
b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -93,18 +93,13 @@
return val - 87
return -1
-def descr_fromhex(space, w_type, w_hexstring):
- "bytearray.fromhex(string) -> bytearray\n\nCreate a bytearray object "
- "from a string of hexadecimal numbers.\nSpaces between two numbers are "
- "accepted.\nExample: bytearray.fromhex('B9 01EF') -> "
- "bytearray(b'\\xb9\\x01\\xef')."
- hexstring = space.unicode_w(w_hexstring)
+def _hexstring_to_array(space, s):
data = []
- length = len(hexstring)
+ length = len(s)
i = -2
while True:
i += 2
- while i < length and hexstring[i] == ' ':
+ while i < length and s[i] == ' ':
i += 1
if i >= length:
break
@@ -112,16 +107,28 @@
raise OperationError(space.w_ValueError, space.wrap(
"non-hexadecimal number found in fromhex() arg at position %d"
% i))
- top = _hex_digit_to_int(hexstring[i])
+ top = _hex_digit_to_int(s[i])
if top == -1:
raise OperationError(space.w_ValueError, space.wrap(
"non-hexadecimal number found in fromhex() arg at position %d"
% i))
- bot = _hex_digit_to_int(hexstring[i+1])
+ bot = _hex_digit_to_int(s[i+1])
if bot == -1:
raise OperationError(space.w_ValueError, space.wrap(
"non-hexadecimal number found in fromhex() arg at position %d"
% (i+1,)))
data.append(chr(top*16 + bot))
+ return data
+def descr_fromhex(space, w_type, w_hexstring):
+ "bytearray.fromhex(string) -> bytearray\n\nCreate a bytearray object "
+ "from a string of hexadecimal numbers.\nSpaces between two numbers are "
+ "accepted.\nExample: bytearray.fromhex('B9 01EF') -> "
+ "bytearray(b'\\xb9\\x01\\xef')."
+ if not space.is_w(space.type(w_hexstring), space.w_unicode):
+ raise OperationError(space.w_TypeError, space.wrap(
+ "must be str, not %s" % space.type(w_hexstring).name))
+ hexstring = space.unicode_w(w_hexstring)
+
+ data = _hexstring_to_array(space, hexstring)
# in CPython bytearray.fromhex is a staticmethod, so
# we ignore w_type and always return a bytearray
return new_bytearray(space, space.w_bytearray, data)
diff --git a/pypy/objspace/std/dictproxyobject.py
b/pypy/objspace/std/dictproxyobject.py
--- a/pypy/objspace/std/dictproxyobject.py
+++ b/pypy/objspace/std/dictproxyobject.py
@@ -20,7 +20,7 @@
def getitem(self, w_dict, w_key):
space = self.space
w_lookup_type = space.type(w_key)
- if space.is_w(w_lookup_type, space.w_str):
+ if space.is_w(w_lookup_type, space.w_unicode):
return self.getitem_str(w_dict, space.str_w(w_key))
else:
return None
@@ -30,7 +30,7 @@
def setitem(self, w_dict, w_key, w_value):
space = self.space
- if space.is_w(space.type(w_key), space.w_str):
+ if space.is_w(space.type(w_key), space.w_unicode):
self.setitem_str(w_dict, self.space.str_w(w_key), w_value)
else:
raise OperationError(space.w_TypeError, space.wrap("cannot add
non-string keys to dict of a type"))
@@ -60,7 +60,7 @@
def delitem(self, w_dict, w_key):
space = self.space
w_key_type = space.type(w_key)
- if space.is_w(w_key_type, space.w_str):
+ if space.is_w(w_key_type, space.w_unicode):
key = self.space.str_w(w_key)
if not self.unerase(w_dict.dstorage).deldictvalue(space, key):
raise KeyError
diff --git a/pypy/objspace/std/stringtype.py b/pypy/objspace/std/stringtype.py
--- a/pypy/objspace/std/stringtype.py
+++ b/pypy/objspace/std/stringtype.py
@@ -342,14 +342,43 @@
W_StringObject.__init__(w_obj, value)
return w_obj
+def descr_fromhex(space, w_type, w_hexstring):
+ "bytes.fromhex(string) -> bytes\n"
+ "\n"
+ "Create a bytes object from a string of hexadecimal numbers.\n"
+ "Spaces between two numbers are accepted.\n"
+ "Example: bytes.fromhex('B9 01EF') -> bytes(b'\\xb9\\x01\\xef')."
+ from pypy.objspace.std.bytearraytype import _hexstring_to_array
+ if not space.is_w(space.type(w_hexstring), space.w_unicode):
+ raise OperationError(space.w_TypeError, space.wrap(
+ "must be str, not %s" % space.type(w_hexstring).name))
+ hexstring = space.unicode_w(w_hexstring)
+ chars = ''.join(_hexstring_to_array(space, hexstring))
+ if space.config.objspace.std.withrope:
+ from pypy.objspace.std.ropeobject import rope, W_RopeObject
+ w_obj = space.allocate_instance(W_RopeObject, w_type)
+ W_RopeObject.__init__(w_obj, rope.LiteralStringNode(chars))
+ return w_obj
+ else:
+ from pypy.objspace.std.stringobject import W_StringObject
+ w_obj = space.allocate_instance(W_StringObject, w_type)
+ W_StringObject.__init__(w_obj, chars)
+ return w_obj
+
# ____________________________________________________________
str_typedef = StdTypeDef("bytes",
__new__ = gateway.interp2app(descr__new__),
- __doc__ = '''str(object) -> string
-
-Return a nice string representation of the object.
-If the argument is a string, the return value is the same object.'''
+ __doc__ = 'bytes(iterable_of_ints) -> bytes\n'
+ 'bytes(string, encoding[, errors]) -> bytes\n'
+ 'bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer\n'
+ 'bytes(memory_view) -> bytes\n\n'
+ 'Construct an immutable array of bytes from:\n'
+ ' - an iterable yielding integers in range(256)\n'
+ ' - a text string encoded using the specified encoding\n'
+ ' - a bytes or a buffer object\n'
+ ' - any object implementing the buffer API.',
+ fromhex = gateway.interp2app(descr_fromhex, as_classmethod=True)
)
str_typedef.registermethods(globals())
diff --git a/pypy/objspace/std/test/test_stringobject.py
b/pypy/objspace/std/test/test_stringobject.py
--- a/pypy/objspace/std/test/test_stringobject.py
+++ b/pypy/objspace/std/test/test_stringobject.py
@@ -99,6 +99,14 @@
import operator
raises(TypeError, operator.mod, b"%s", (1,))
+ def test_fromhex(self):
+ assert bytes.fromhex("abcd") == b'\xab\xcd'
+ assert b''.fromhex("abcd") == b'\xab\xcd'
+ assert bytes.fromhex("ab cd ef") == b'\xab\xcd\xef'
+ raises(TypeError, bytes.fromhex, b"abcd")
+ raises(TypeError, bytes.fromhex, True)
+ raises(ValueError, bytes.fromhex, "hello world")
+
def test_split(self):
assert b"".split() == []
assert b"".split(b'x') == [b'']
diff --git a/pypy/objspace/std/unicodetype.py b/pypy/objspace/std/unicodetype.py
--- a/pypy/objspace/std/unicodetype.py
+++ b/pypy/objspace/std/unicodetype.py
@@ -340,7 +340,7 @@
unicode_typedef = StdTypeDef("str",
__new__ = gateway.interp2app(descr_new_),
- __doc__ = '''unicode(string [, encoding[, errors]]) -> object
+ __doc__ = '''str(string [, encoding[, errors]]) -> object
Create a new Unicode object from the given encoded string.
encoding defaults to the current default string encoding.
diff --git a/pypy/rlib/rposix.py b/pypy/rlib/rposix.py
--- a/pypy/rlib/rposix.py
+++ b/pypy/rlib/rposix.py
@@ -163,3 +163,18 @@
return nt._getfullpathname(path)
else:
return nt._getfullpathname(path.as_bytes())
+
[email protected](0, 1)
+def putenv(name, value):
+ if isinstance(name, str):
+ os.environ[name] = value
+ else:
+ os.environ[name.as_bytes()] = value.as_bytes()
+
[email protected](0)
+def unsetenv(name):
+ if isinstance(name, str):
+ del os.environ[name]
+ else:
+ del os.environ[name.as_bytes()]
+
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit