You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
5.7 KiB

Issue #24400: Introduce a distinct type for 'async def' coroutines. Summary of changes: 1. Coroutines now have a distinct, separate from generators type at the C level: PyGen_Type, and a new typedef PyCoroObject. PyCoroObject shares the initial segment of struct layout with PyGenObject, making it possible to reuse existing generators machinery. The new type is exposed as 'types.CoroutineType'. As a consequence of having a new type, CO_GENERATOR flag is no longer applied to coroutines. 2. Having a separate type for coroutines made it possible to add an __await__ method to the type. Although it is not used by the interpreter (see details on that below), it makes coroutines naturally (without using __instancecheck__) conform to collections.abc.Coroutine and collections.abc.Awaitable ABCs. [The __instancecheck__ is still used for generator-based coroutines, as we don't want to add __await__ for generators.] 3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to allow passing native coroutines to the YIELD_FROM opcode. Before this change, 'yield from o' expression was compiled to: (o) GET_ITER LOAD_CONST YIELD_FROM Now, we use GET_YIELD_FROM_ITER instead of GET_ITER. The reason for adding a new opcode is that GET_ITER is used in some contexts (such as 'for .. in' loops) where passing a coroutine object is invalid. 4. Add two new introspection functions to the inspec module: getcoroutinestate(c) and getcoroutinelocals(c). 5. inspect.iscoroutine(o) is updated to test if 'o' is a native coroutine object. Before this commit it used abc.Coroutine, and it was requested to update inspect.isgenerator(o) to use abc.Generator; it was decided, however, that inspect functions should really be tailored for checking for native types. 6. sys.set_coroutine_wrapper(w) API is updated to work with only native coroutines. Since types.coroutine decorator supports any type of callables now, it would be confusing that it does not work for all types of coroutines. 7. Exceptions logic in generators C implementation was updated to raise clearer messages for coroutines: Before: TypeError("generator raised StopIteration") After: TypeError("coroutine raised StopIteration")
11 years ago
Merged revisions 59541-59561 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r59544 | raymond.hettinger | 2007-12-18 01:13:45 +0100 (Tue, 18 Dec 2007) | 1 line Add more namedtuple() test cases. Neaten the code and comments. ........ r59545 | christian.heimes | 2007-12-18 04:38:03 +0100 (Tue, 18 Dec 2007) | 3 lines Fixed for #1601: IDLE not working correctly on Windows (Py30a2/IDLE30a1) Amaury's ideas works great. Should we build the Python core with WINVER=0x0500 and _WIN32_WINNT=0x0500, too? ........ r59546 | christian.heimes | 2007-12-18 10:00:13 +0100 (Tue, 18 Dec 2007) | 1 line Make it a bit easier to test Tcl/Tk and idle from a build dir. ........ r59547 | christian.heimes | 2007-12-18 10:12:10 +0100 (Tue, 18 Dec 2007) | 1 line Removed several unused files from the PCbuild9 directory. They are relics from the past. ........ r59548 | raymond.hettinger | 2007-12-18 19:26:18 +0100 (Tue, 18 Dec 2007) | 29 lines Speed-up dictionary constructor by about 10%. New opcode, STORE_MAP saves the compiler from awkward stack manipulations and specializes for dicts using PyDict_SetItem instead of PyObject_SetItem. Old disassembly: 0 BUILD_MAP 0 3 DUP_TOP 4 LOAD_CONST 1 (1) 7 ROT_TWO 8 LOAD_CONST 2 ('x') 11 STORE_SUBSCR 12 DUP_TOP 13 LOAD_CONST 3 (2) 16 ROT_TWO 17 LOAD_CONST 4 ('y') 20 STORE_SUBSCR New disassembly: 0 BUILD_MAP 0 3 LOAD_CONST 1 (1) 6 LOAD_CONST 2 ('x') 9 STORE_MAP 10 LOAD_CONST 3 (2) 13 LOAD_CONST 4 ('y') 16 STORE_MAP ........ r59549 | thomas.heller | 2007-12-18 20:00:34 +0100 (Tue, 18 Dec 2007) | 2 lines Issue #1642: Fix segfault in ctypes when trying to delete attributes. ........ r59551 | guido.van.rossum | 2007-12-18 21:10:42 +0100 (Tue, 18 Dec 2007) | 2 lines Issue #1645 by Alberto Bertogli. Fix a comment. ........ r59553 | raymond.hettinger | 2007-12-18 22:24:09 +0100 (Tue, 18 Dec 2007) | 12 lines Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary. Allows dictionaries to be pre-sized (upto 255 elements) saving time lost to re-sizes with their attendant mallocs and re-insertions. Has zero effect on small dictionaries (5 elements or fewer), a slight benefit for dicts upto 22 elements (because they had to resize once anyway), and more benefit for dicts upto 255 elements (saving multiple resizes during the build-up and reducing the number of collisions on the first insertions). Beyond 255 elements, there is no addional benefit. ........ r59554 | christian.heimes | 2007-12-18 22:56:09 +0100 (Tue, 18 Dec 2007) | 1 line Fixed #1649: IDLE error: dictionary changed size during iteration ........ r59557 | raymond.hettinger | 2007-12-18 23:21:27 +0100 (Tue, 18 Dec 2007) | 1 line Simplify and speedup _asdict() for named tuples. ........ r59558 | christian.heimes | 2007-12-19 00:22:54 +0100 (Wed, 19 Dec 2007) | 3 lines Applied patch #1635: Float patch for inf and nan on Windows (and other platforms). The patch unifies float("inf") and repr(float("inf")) on all platforms. ........ r59559 | raymond.hettinger | 2007-12-19 00:51:15 +0100 (Wed, 19 Dec 2007) | 1 line Users demand iterable input for named tuples. The author capitulates. ........ r59560 | raymond.hettinger | 2007-12-19 01:21:06 +0100 (Wed, 19 Dec 2007) | 1 line Beef-up tests for dict literals ........ r59561 | raymond.hettinger | 2007-12-19 01:27:21 +0100 (Wed, 19 Dec 2007) | 1 line Zap a duplicate line ........
18 years ago
  1. """
  2. opcode module - potentially shared between dis and other modules which
  3. operate on bytecodes (e.g. peephole optimizers).
  4. """
  5. __all__ = ["cmp_op", "hasconst", "hasname", "hasjrel", "hasjabs",
  6. "haslocal", "hascompare", "hasfree", "opname", "opmap",
  7. "HAVE_ARGUMENT", "EXTENDED_ARG", "hasnargs"]
  8. # It's a chicken-and-egg I'm afraid:
  9. # We're imported before _opcode's made.
  10. # With exception unheeded
  11. # (stack_effect is not needed)
  12. # Both our chickens and eggs are allayed.
  13. # --Larry Hastings, 2013/11/23
  14. try:
  15. from _opcode import stack_effect
  16. __all__.append('stack_effect')
  17. except ImportError:
  18. pass
  19. cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
  20. 'is not', 'exception match', 'BAD')
  21. hasconst = []
  22. hasname = []
  23. hasjrel = []
  24. hasjabs = []
  25. haslocal = []
  26. hascompare = []
  27. hasfree = []
  28. hasnargs = [] # unused
  29. opmap = {}
  30. opname = ['<%r>' % (op,) for op in range(256)]
  31. def def_op(name, op):
  32. opname[op] = name
  33. opmap[name] = op
  34. def name_op(name, op):
  35. def_op(name, op)
  36. hasname.append(op)
  37. def jrel_op(name, op):
  38. def_op(name, op)
  39. hasjrel.append(op)
  40. def jabs_op(name, op):
  41. def_op(name, op)
  42. hasjabs.append(op)
  43. # Instruction opcodes for compiled code
  44. # Blank lines correspond to available opcodes
  45. def_op('POP_TOP', 1)
  46. def_op('ROT_TWO', 2)
  47. def_op('ROT_THREE', 3)
  48. def_op('DUP_TOP', 4)
  49. def_op('DUP_TOP_TWO', 5)
  50. def_op('NOP', 9)
  51. def_op('UNARY_POSITIVE', 10)
  52. def_op('UNARY_NEGATIVE', 11)
  53. def_op('UNARY_NOT', 12)
  54. def_op('UNARY_INVERT', 15)
  55. def_op('BINARY_MATRIX_MULTIPLY', 16)
  56. def_op('INPLACE_MATRIX_MULTIPLY', 17)
  57. def_op('BINARY_POWER', 19)
  58. def_op('BINARY_MULTIPLY', 20)
  59. def_op('BINARY_MODULO', 22)
  60. def_op('BINARY_ADD', 23)
  61. def_op('BINARY_SUBTRACT', 24)
  62. def_op('BINARY_SUBSCR', 25)
  63. def_op('BINARY_FLOOR_DIVIDE', 26)
  64. def_op('BINARY_TRUE_DIVIDE', 27)
  65. def_op('INPLACE_FLOOR_DIVIDE', 28)
  66. def_op('INPLACE_TRUE_DIVIDE', 29)
  67. def_op('GET_AITER', 50)
  68. def_op('GET_ANEXT', 51)
  69. def_op('BEFORE_ASYNC_WITH', 52)
  70. def_op('INPLACE_ADD', 55)
  71. def_op('INPLACE_SUBTRACT', 56)
  72. def_op('INPLACE_MULTIPLY', 57)
  73. def_op('INPLACE_MODULO', 59)
  74. def_op('STORE_SUBSCR', 60)
  75. def_op('DELETE_SUBSCR', 61)
  76. def_op('BINARY_LSHIFT', 62)
  77. def_op('BINARY_RSHIFT', 63)
  78. def_op('BINARY_AND', 64)
  79. def_op('BINARY_XOR', 65)
  80. def_op('BINARY_OR', 66)
  81. def_op('INPLACE_POWER', 67)
  82. def_op('GET_ITER', 68)
  83. def_op('GET_YIELD_FROM_ITER', 69)
  84. def_op('PRINT_EXPR', 70)
  85. def_op('LOAD_BUILD_CLASS', 71)
  86. def_op('YIELD_FROM', 72)
  87. def_op('GET_AWAITABLE', 73)
  88. def_op('INPLACE_LSHIFT', 75)
  89. def_op('INPLACE_RSHIFT', 76)
  90. def_op('INPLACE_AND', 77)
  91. def_op('INPLACE_XOR', 78)
  92. def_op('INPLACE_OR', 79)
  93. def_op('BREAK_LOOP', 80)
  94. def_op('WITH_CLEANUP_START', 81)
  95. def_op('WITH_CLEANUP_FINISH', 82)
  96. def_op('RETURN_VALUE', 83)
  97. def_op('IMPORT_STAR', 84)
  98. def_op('SETUP_ANNOTATIONS', 85)
  99. def_op('YIELD_VALUE', 86)
  100. def_op('POP_BLOCK', 87)
  101. def_op('END_FINALLY', 88)
  102. def_op('POP_EXCEPT', 89)
  103. HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
  104. name_op('STORE_NAME', 90) # Index in name list
  105. name_op('DELETE_NAME', 91) # ""
  106. def_op('UNPACK_SEQUENCE', 92) # Number of tuple items
  107. jrel_op('FOR_ITER', 93)
  108. def_op('UNPACK_EX', 94)
  109. name_op('STORE_ATTR', 95) # Index in name list
  110. name_op('DELETE_ATTR', 96) # ""
  111. name_op('STORE_GLOBAL', 97) # ""
  112. name_op('DELETE_GLOBAL', 98) # ""
  113. def_op('LOAD_CONST', 100) # Index in const list
  114. hasconst.append(100)
  115. name_op('LOAD_NAME', 101) # Index in name list
  116. def_op('BUILD_TUPLE', 102) # Number of tuple items
  117. def_op('BUILD_LIST', 103) # Number of list items
  118. def_op('BUILD_SET', 104) # Number of set items
  119. def_op('BUILD_MAP', 105) # Number of dict entries (upto 255)
  120. name_op('LOAD_ATTR', 106) # Index in name list
  121. def_op('COMPARE_OP', 107) # Comparison operator
  122. hascompare.append(107)
  123. name_op('IMPORT_NAME', 108) # Index in name list
  124. name_op('IMPORT_FROM', 109) # Index in name list
  125. jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
  126. jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code
  127. jabs_op('JUMP_IF_TRUE_OR_POP', 112) # ""
  128. jabs_op('JUMP_ABSOLUTE', 113) # ""
  129. jabs_op('POP_JUMP_IF_FALSE', 114) # ""
  130. jabs_op('POP_JUMP_IF_TRUE', 115) # ""
  131. name_op('LOAD_GLOBAL', 116) # Index in name list
  132. jabs_op('CONTINUE_LOOP', 119) # Target address
  133. jrel_op('SETUP_LOOP', 120) # Distance to target address
  134. jrel_op('SETUP_EXCEPT', 121) # ""
  135. jrel_op('SETUP_FINALLY', 122) # ""
  136. def_op('LOAD_FAST', 124) # Local variable number
  137. haslocal.append(124)
  138. def_op('STORE_FAST', 125) # Local variable number
  139. haslocal.append(125)
  140. def_op('DELETE_FAST', 126) # Local variable number
  141. haslocal.append(126)
  142. name_op('STORE_ANNOTATION', 127) # Index in name list
  143. def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
  144. def_op('CALL_FUNCTION', 131) # #args
  145. def_op('MAKE_FUNCTION', 132) # Flags
  146. def_op('BUILD_SLICE', 133) # Number of items
  147. def_op('LOAD_CLOSURE', 135)
  148. hasfree.append(135)
  149. def_op('LOAD_DEREF', 136)
  150. hasfree.append(136)
  151. def_op('STORE_DEREF', 137)
  152. hasfree.append(137)
  153. def_op('DELETE_DEREF', 138)
  154. hasfree.append(138)
  155. def_op('CALL_FUNCTION_KW', 141) # #args + #kwargs
  156. def_op('CALL_FUNCTION_EX', 142) # Flags
  157. jrel_op('SETUP_WITH', 143)
  158. def_op('LIST_APPEND', 145)
  159. def_op('SET_ADD', 146)
  160. def_op('MAP_ADD', 147)
  161. def_op('LOAD_CLASSDEREF', 148)
  162. hasfree.append(148)
  163. def_op('EXTENDED_ARG', 144)
  164. EXTENDED_ARG = 144
  165. def_op('BUILD_LIST_UNPACK', 149)
  166. def_op('BUILD_MAP_UNPACK', 150)
  167. def_op('BUILD_MAP_UNPACK_WITH_CALL', 151)
  168. def_op('BUILD_TUPLE_UNPACK', 152)
  169. def_op('BUILD_SET_UNPACK', 153)
  170. jrel_op('SETUP_ASYNC_WITH', 154)
  171. def_op('FORMAT_VALUE', 155)
  172. def_op('BUILD_CONST_KEY_MAP', 156)
  173. def_op('BUILD_STRING', 157)
  174. def_op('BUILD_TUPLE_UNPACK_WITH_CALL', 158)
  175. del def_op, name_op, jrel_op, jabs_op