cdef class just_getattribute:
def __getattribute__(self,n):
- if n == 'bar':
+ if n == u'bar':
return n
else:
raise AttributeError
def __init__(self):
self.foo = 10
def __getattr__(self,n):
- if n == 'bar':
+ if n == u'bar':
return n
else:
raise AttributeError
def __init__(self):
self.foo = 10
def __getattribute__(self,n):
- if n == 'foo':
+ if n == u'foo':
return self.foo
else:
raise AttributeError
def __getattr__(self,n):
- if n == 'bar':
+ if n == u'bar':
return n
else:
raise AttributeError
>>> a = getattr_boring()
>>> a.boring_member
10
- >>> a.resolved_by
- 'getattr_boring'
+ >>> print(a.resolved_by)
+ getattr_boring
getattribute does.
>>> a = getattribute_boring()
>>> a.boring_member
Traceback (most recent call last):
AttributeError
- >>> a.resolved_by
- 'getattribute_boring'
+ >>> print(a.resolved_by)
+ getattribute_boring
Is inherited.
>>> a = boring_boring_getattribute()
>>> a.boring_boring_getattribute_member
Traceback (most recent call last):
AttributeError
- >>> a.resolved_by
- '_getattribute'
+ >>> print(a.resolved_by)
+ _getattribute
__getattribute__ is always tried first, then __getattr__, regardless of where
in the inheritance hiarchy they came from.
>>> a.foo
Traceback (most recent call last):
AttributeError
- >>> a.resolved_by
- 'getattribute_boring_boring_getattr'
+ >>> print(a.resolved_by)
+ getattribute_boring_boring_getattr
>>> a.getattribute_boring_boring_getattr
True
>>> a._getattr
>>> a.foo
Traceback (most recent call last):
AttributeError
- >>> a.resolved_by
- '_getattribute'
+ >>> print(a.resolved_by)
+ _getattribute
>>> a.getattr_boring_boring_getattribute
True
>>> a._getattribute
cdef class getattr_boring(boring):
def __getattr__(self,n):
- if n == 'resolved_by':
- return 'getattr_boring'
- elif n == 'getattr_boring':
+ if n == u'resolved_by':
+ return u'getattr_boring'
+ elif n == u'getattr_boring':
return True
else:
raise AttributeError
cdef class getattribute_boring(boring):
def __getattribute__(self,n):
- if n == 'resolved_by':
- return 'getattribute_boring'
- elif n == 'getattribute_boring':
+ if n == u'resolved_by':
+ return u'getattribute_boring'
+ elif n == u'getattribute_boring':
return True
else:
raise AttributeError
cdef class _getattr:
def __getattr__(self,n):
- if n == 'resolved_by':
- return '_getattr'
- elif n == '_getattr':
+ if n == u'resolved_by':
+ return u'_getattr'
+ elif n == u'_getattr':
return True
else:
raise AttributeError
cdef class _getattribute(boring):
def __getattribute__(self,n):
- if n == 'resolved_by':
- return '_getattribute'
- elif n == '_getattribute':
+ if n == u'resolved_by':
+ return u'_getattribute'
+ elif n == u'_getattribute':
return True
else:
raise AttributeError
cdef class getattribute_boring_boring_getattr(boring_boring_getattr):
def __getattribute__(self,n):
- if n == 'resolved_by':
- return 'getattribute_boring_boring_getattr'
- elif n == 'getattribute_boring_boring_getattr':
+ if n == u'resolved_by':
+ return u'getattribute_boring_boring_getattr'
+ elif n == u'getattribute_boring_boring_getattr':
return True
else:
raise AttributeError
cdef class getattr_boring_boring_getattribute(boring_boring_getattribute):
def __getattr__(self,n):
- if n == 'resolved_by':
- return 'getattr_boring_boring_getattribute'
- elif n == 'getattr_boring_boring_getattribute':
+ if n == u'resolved_by':
+ return u'getattr_boring_boring_getattribute'
+ elif n == u'getattr_boring_boring_getattribute':
return True
else:
raise AttributeError
-
tomatotomatotomatotomatotomatotomatotomato
"""
-spam = "eggs" * 4
-grail = 7 * "tomato"
-
+spam = u"eggs" * 4
+grail = 7 * u"tomato"
>>> len(constant())
2
- >>> constant()['parrot']
- 'resting'
- >>> constant()['answer']
+ >>> print(constant()['parrot'])
+ resting
+ >>> print(constant()['answer'])
42
"""
return d
def constant():
- d = {"parrot":"resting", "answer":42}
+ d = {u"parrot":u"resting", u"answer":42}
return d
-
>>> d
{}
- >>> d = {'arg' : 2}
+ >>> d = {'arg' : 2} # this should be u'arg', but Py2 can't handle it...
>>> test(**d)
{'arg': 3}
>>> d
{'arg': 2}
"""
+import sys
+
def test(**kw):
- kw['arg'] = 3
+ if sys.version_info[0] >= 3:
+ kw[u'arg'] = 3
+ else:
+ kw['arg'] = 3
return kw
>>> g
42
>>> x
- 'spam'
+ u'spam'
>>> y
- 'eggs'
+ u'eggs'
>>> z
- 'spameggs'
+ u'spameggs'
"""
+import sys
+if sys.version_info[0] >= 3:
+ __doc__ = __doc__.replace(u" u'", u" '")
+
def f():
pass
g = 42
-x = "spam"
-y = "eggs"
+x = u"spam"
+y = u"eggs"
if g:
z = x + y
-
print a
print a, b
print a, b,
- print 42, "spam"
-
+ print 42, u"spam"
def swallow(name = None, airspeed = None, coconuts = None):
if name is not None:
- print "This swallow is called", name
+ print u"This swallow is called", name
if airspeed is not None:
- print "This swallow is flying at", airspeed, "furlongs per fortnight"
+ print u"This swallow is flying at", airspeed, u"furlongs per fortnight"
if coconuts is not None:
- print "This swallow is carrying", coconuts, "coconuts"
+ print u"This swallow is carrying", coconuts, u"coconuts"
cdef class Fee(Foo):
def bof(self):
- print 'Fee bof', self.val
+ print u'Fee bof', self.val
cdef class Faa(Fee):
def bof(self):
- print 'Foo bof', self.val
+ print u'Foo bof', self.val
cdef Py_complex cval
def spam(complex c):
- print "Real:", c.cval.real
- print "Imag:", c.cval.imag
+ print u"Real:", c.cval.real
+ print u"Imag:", c.cval.imag
def eggs():
return complex(17, 42)
cdef class Swallow:
def __init__(self, name, airspeed, *args, **kwds):
- print "Name:", name
- print "Airspeed:", airspeed
- print "Extra args:", args
- print "Extra keywords:", kwds
+ print u"Name:", name
+ print u"Airspeed:", airspeed
+ print u"Extra args:", args
+ print u"Extra keywords:", kwds
def go():
for i in range(5):
- print "Spam!"
+ print u"Spam!"
cdef class Parrot:
cdef void describe(self):
- print "This parrot is resting."
+ print u"This parrot is resting."
def describe_python(self):
self.describe()
cdef class Norwegian(Parrot):
cdef void describe(self):
- print "Lovely plumage!"
+ print u"Lovely plumage!"
def test():
cdef Parrot p1, p2
"""
def frighten():
- print "NOBODY", "expects", "the Spanish Inquisition!"
+ print u"NOBODY", u"expects", u"the Spanish Inquisition!"
self.weight = w
def serve(self):
- print self.weight, "tons of spam!"
+ print self.weight, u"tons of spam!"
def order():
s = Spam(42)
def swallow(self, name = None, airspeed = None, coconuts = None):
if name is not None:
- print "This swallow is called", name
+ print u"This swallow is called", name
if airspeed is not None:
- print "This swallow is flying at", airspeed, "furlongs per fortnight"
+ print u"This swallow is flying at", airspeed, "furlongs per fortnight"
if coconuts is not None:
- print "This swallow is carrying", coconuts, "coconuts"
+ print u"This swallow is carrying", coconuts, "coconuts"
self.tons = 17
def __dealloc__(self):
- print self.tons, "tons of spam is history."
+ print self.tons, u"tons of spam is history."
def get_tons(self):
return self.tons
"""
def spam(a, b, c):
- print "Args:", a, b, c
+ print u"Args:", a, b, c
def eggs():
spam(*(1,2,3))
- spam(*["buckle","my","shoe"])
+ spam(*[u"buckle",u"my",u"shoe"])
import sys
if sys.version_info[0] >= 3:
- encoding = {'encoding' : 'ASCII'}
+ encoding = {u'encoding' : u'ASCII'}
else:
encoding = {}
__doc__ = __doc__.replace(u" b'", u" '")
u'S\xf8k ik\xfc\xd6\xe4abc'
>>> null
u'\x00'
-""".decode("ASCII") + """
+""".decode(u"ASCII") + """
>>> len(sa)
3
>>> len(ua)
12
>>> len(null)
1
-""".decode("ASCII") + u"""
+""".decode(u"ASCII") + u"""
>>> sa == 'abc'
True
>>> ua == u'abc'
e = u'\x03\x67\xf8\uf8d2Søk ik'
f = u'\xf8'
-add = u'Søk ik' + u'üÖä' + 'abc'
+add = u'Søk ik' + u'üÖä' + u'abc'
null = u'\x00'
u'S\xf8k ik\xfc\xd6\xe4abc'
>>> null
u'\x00'
-""".decode("ASCII") + """
+""".decode(u"ASCII") + """
>>> len(sa)
3
>>> len(ua)
12
>>> len(null)
1
-""".decode("ASCII") + u"""
+""".decode(u"ASCII") + u"""
>>> sa == 'abc'
True
>>> ua == u'abc'
e = u'\x03\x67\xf8\uf8d2Søk ik'
f = u'\xf8'
-add = u'Søk ik' + u'üÖä' + 'abc'
+add = u'Søk ik' + u'üÖä' + u'abc'
null = u'\x00'
u'S\xf8k ik\xfc\xd6\xe4abc'
>>> null
u'\x00'
-""".decode("ASCII") + """
+""".decode(u"ASCII") + """
>>> len(sa)
3
>>> len(ua)
12
>>> len(null)
1
-""".decode("ASCII") + u"""
+""".decode(u"ASCII") + u"""
>>> sa == 'abc'
True
>>> ua == u'abc'
e = u'\x03\x67\xf8\uf8d2Søk ik'
f = u'\xf8'
-add = u'Søk ik' + u'üÖä' + 'abc'
+add = u'Søk ik' + u'üÖä' + u'abc'
null = u'\x00'