Source

2to3 / example.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/python
	# comment indented by tab

"""Docstring.

Here are some doctest exampes:

>>> print 42
42

 >>> d = {1: 1, 2: 2, 2: 2}
 >>> d.keys().sort()
 >>> print d
 {1: 1, 2: 2}

  >>> for i in d.keys():
  ...     print i, d[i]

And a tricky one:

>>> class X(Structure):
...     _fields_ = [("x", c_int), ("y", c_int), ("array", c_char_p * 5)]
...
>>> x = X()
>>> print x._objects
None
>>>

"""

import sys

def unicode_examples():
    a = unicode(b)
    a = u"xxx"
    a = U"""xxx"""
    a = ur'xxx'
    a = UR'''xxx'''
    a = Ur"xxx"
    a = uR"""xxx"""
    b = u"..." u'...'

def ne_examples():
    if x <> y:
        pass
    if x<>y:
        pass
    if x<>y<>z:
        pass

def has_key_examples():
    #
    x = d.has_key("x") or d.has_key("y")
    #
    x = a.b.c.d.has_key("x") ** 3
    #
    x = a.b.has_key(1 + 2).__repr__()
    #
    x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4
    #
    x = a.has_key(f or g)
    #
    x = a + b.has_key(c)
    #
    x = a.has_key(lambda: 12)
    #
    x = a.has_key(a for a in b)
    #
    if not a.has_key(b): pass
    #
    if not a.has_key(b).__repr__(): pass
    #
    if not a.has_key(b) ** 2: pass

def foo():
	pass # body indented by tab

def test_ws_comma():
    yield 1,2 ,3
    f(1,2 ,3)
    `a ,b`
    def f(a,b ,c): pass
    { a:b,c:d , e : f }

def apply_examples():
    x = apply(f, g + h)
    y = apply(f, g, h)
    z = apply(fs[0], g or h, h or g)
    # Hello
    apply(f, (x, y) + t)
    apply(f, args,)
    apply(f, args, kwds,)
    # Test that complex functions are parenthesized
    x = apply(f+g, args)
    x = apply(f*g, args)
    x = apply(f**g, args)
    # But dotted names etc. not
    x = apply(f.g, args)
    x = apply(f[x], args)
    x = apply(f(), args)
    # Extreme case
    x = apply(a.b.c.d.e.f, args, kwds)
    # XXX Comments in weird places still get lost
    apply(   # foo
          f, # bar
          args)

def bad_apply_examples():
    # These should *not* be touched
    apply()
    apply(f)
    apply(f,)
    apply(f, args, kwds, extras)
    apply(f, *args, **kwds)
    apply(f, *args)
    apply(func=f, args=args, kwds=kwds)
    apply(f, args=args, kwds=kwds)
    apply(f, args, kwds=kwds)

def metaclass_examples():
    class X:
        __metaclass__ = Meta

    class X(b1, b2):
        bar = 23 # Comment on me!
        __metaclass__ = Meta
        spam = 27.23 # Laughable

    class X:
        __metaclass__ = Meta; x = 23; y = 34 # Yes, I can handle this, too.

def intern_examples():
    #
    # These should be refactored:
    #
    x = intern(a)
    #
    y = intern("b" # test
              )
    #
    z = intern(a+b+c.d,)
    #
    intern("y%s" % 5).replace("y", "")
    #
    # These not:
    #
    intern(a=1)
    #
    intern(f, g)
    #
    intern(*h)
    #
    intern(**i)

def print_examples():
    # plain vanilla
    print 1, 1+1, 1+1+1
    #
    print 1, 2
    #
    print 1

    print

    # trailing commas
    print 1, 2, 3,
    #
    print 1, 2,
    #
    print 1,
    #
    print

    # >> stuff
    print >>sys.stderr, 1, 2, 3    # no trailing comma
    #
    print >>sys.stdder, 1, 2,      # trailing comma
    #
    print >>sys.stderr, 1+1        # no trailing comma
    #
    print >>  sys.stderr           # spaces before sys.stderr

def exec_examples():
    #
    exec code
    #
    exec code in ns
    #
    exec code in ns1, ns2
    #
    exec (a.b()) in ns
    #
    exec a.b() + c in ns
    #
    # These should not be touched:
    #
    exec(code)
    #
    exec (code)
    #
    exec(code, ns)
    #
    exec(code, ns1, ns2)

def repr_examples():
    x = `1 + 2`
    #
    y = `x`
    #
    z = `y`.__repr__()
    #
    x = `1, 2, 3`
    #
    x = `1 + `2``
    #
    x = `1, 2 + `3, 4``

def except_examples():
    try:
        pass
    except Exception, (f, e):
        pass
    except ImportError, e:
        print e.args
    #
    try:
        pass
    except (RuntimeError, ImportError), e:
        pass
    #
    try:
        pass
    except Exception, (a, b):
        pass
    #
    try:
        pass
    except Exception, d[5]:
        pass
    #
    try:
        pass
    except Exception, a.foo:
        pass
    #
    try:
        pass
    except Exception, a().foo:
        pass
    #
    # These should not be touched:
    #
    try:
        pass
    except:
        pass
    #
    try:
        pass
    except Exception:
        pass
    #
    try:
        pass
    except (Exception, SystemExit):
        pass

def raise_examples():
    raise Exception, 5
    #
    raise Exception,5
    #
    raise Exception, (5, 6, 7)
    #
    # These should not be touched
    #
    raise Exception
    #
    raise Exception(5, 6)
    #
    # These should produce a warning
    # TODO: convert "raise E, V, T" to
    #  "e = E(V); e.__traceback__ = T; raise e;"
    #
    raise Exception, 5, 6
    #
    raise Exception,5,6
    #
    raise Exception, (5, 6, 7), 6

def long_examples():
    x = long(x)
    y = isinstance(x, long)
    z = type(x) in (int, long)
    a = 12L
    b = 0x12l
    # unchanged:
    a = 12
    b = 0x12
    c = 3.14

def dict_examples():
    #
    # Plain method calls
    #
    print d.keys()
    print d.items()
    print d.values()
    #
    # Plain method calls in special contexts
    #
    print iter(e.keys())
    for i in e.keys(): print i
    [i for i in e.keys()]
    (i for i in e.keys())
    #
    # Iterator method calls
    #
    print f.iterkeys()
    print f.iteritems()
    print f.itervalues()
    #
    # Iterator method calls in special contexts
    #
    print list(g.iterkeys())
    print sorted(g.iterkeys())
    print iter(g.iterkeys())
    for i in g.iterkeys(): print i
    [i for i in g.iterkeys()]
    (i for i in g.iterkeys())
    #
    # Examples with a "tail"; these are never "special"
    #
    print h.iterkeys().next()
    print h.keys()[0]
    print list(h.iterkeys().next())
    for x in h.keys()[0]: print x
    #
    # Examples with dict views
    #
    print d.viewkeys()
    print d.viewitems()
    print d.viewvalues()

def dict_negative_examples():
    #
    # These should all remain unchanged:
    #
    print list(h.keys())
    print sorted(h.keys())

def xrange_examples():
    for i in xrange(100): print i
    for i in xrange(0, 100): print i
    for i in xrange(0, 100, 10): print i

def input_examples():
    a = input()
    b = input(str(a))

def raw_input_examples():
    a = raw_input()
    b = raw_input(a.rstrip())

def filter_examples():
    filter(os.unlink, filenames)
    filter(None, "whatever")
    filter(lambda x: not x, range(4))

def map_examples():
    map(None, foo.bar)
    map(None, foo.bar,)
    map(None, foo, bar)
    map(f, foo.bar)
    map(lambda x: x+1, range(10))

def basestring_examples():
    if isinstance(x, basestring): pass

def buffer_examples():
    x = buffer(y)

def sys_exc_examples():
    print sys.exc_type, sys.exc_value, sys.exc_traceback

def operator_examples():
    import operator
    operator.isCallable(foo)
    operator.sequenceIncludes(foo, bar)

    from operator import isCallable, sequenceIncludes
    # These should produce warnings.
    isCallable(foo)
    sequenceIncludes(foo, bar)

class X:
    def maximum(self):
        return max(self.data.values())
    def total(self):
        return sum(self.data.values())


# This is the last line.