├── LICENSE
├── README.md
├── unpyc3.py
└── unpyc3_tests.py
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | unpyc3
2 | ======
3 |
4 | Decompiler for Python 3.3 (forked from https://code.google.com/p/unpyc3)
5 |
6 | ## Original README
7 |
8 | The aim is to be able to recreate Python3 source code from code objects. Current version is able to decompile itself successfully :). It has been tested with Python 3.3 only.
9 |
10 | It currently reconstructs most of Python 3 constructs but probably needs to be tested more thoroughly. All feedback welcome.
11 |
12 | Example:
13 |
14 | ```python
15 | >>> from unpyc3 import decompile
16 | >>> def foo(x, y, z=3, *args):
17 | ... global g
18 | ... for i, j in zip(x, y):
19 | ... if z == i + j or args[i] == j:
20 | ... g = i, j
21 | ... return
22 | ...
23 | >>> print(decompile(foo))
24 | def foo(x, y, z=3, *args):
25 | global g
26 | for i, j in zip(x, y):
27 | if z == i + j or args[i] == j:
28 | g = i, j
29 | return
30 | >>>
31 | ```
32 |
33 | Unpyc3 is made of a single python module. Download [unpyc3.py](https://github.com/figment/unpyc3/raw/master/unpyc3.py) and try it now!
34 |
35 | the unpyc3 module is able de decompile itself! (try import unpyc3; unpyc3.decompile(unpyc3)) so theorically I could just distribute the .pyc file.
36 |
37 | TODO:
38 |
39 | * Support for keyword-only arguments
40 | * Handle assert statements
41 | * Show docstrings for functions and modules
42 | * Nice spacing between function/class declarations
43 |
44 | ## Addendum
45 |
46 | * I am not the original author and just hacked this enough to work with Python 3.3
47 | * I will not be maintaining this repository to any degree
48 | * Also thanks to MadeCoder at StackOverflow.com for their work
49 | * http://stackoverflow.com/questions/22096717/decompile-python-3-3-pyc-using-unpyc3
50 |
51 |
--------------------------------------------------------------------------------
/unpyc3.py:
--------------------------------------------------------------------------------
1 | """
2 | Decompiler for Python3.3.
3 | Decompile a module or a function using the decompile() function
4 |
5 | >>> from unpyc3 import decompile
6 | >>> def foo(x, y, z=3, *args):
7 | ... global g
8 | ... for i, j in zip(x, y):
9 | ... if z == i + j or args[i] == j:
10 | ... g = i, j
11 | ... return
12 | ...
13 | >>> print(decompile(foo))
14 |
15 | def foo(x, y, z=3, *args):
16 | global g
17 | for i, j in zip(x, y):
18 | if z == i + j or args[i] == j:
19 | g = i, j
20 | return
21 | >>>
22 | """
23 |
24 | __all__ = ['decompile']
25 |
26 | # TODO:
27 | # - Support for keyword-only arguments
28 | # - Handle assert statements better
29 | # - (Partly done) Nice spacing between function/class declarations
30 |
31 | import dis
32 | from array import array
33 | from opcode import opname, opmap, HAVE_ARGUMENT, cmp_op
34 | import imp
35 | import inspect
36 |
37 | # Masks for code object's co_flag attribute
38 | VARARGS = 4
39 | VARKEYWORDS = 8
40 |
41 | # Put opcode names in the global namespace
42 | for name, val in opmap.items():
43 | globals()[name] = val
44 |
45 | # These opcodes will generate a statement. This is used in the first
46 | # pass (in Code.find_else) to find which POP_JUMP_IF_* instructions
47 | # are jumps to the else clause of an if statement
48 | stmt_opcodes = {
49 | SETUP_LOOP, BREAK_LOOP, CONTINUE_LOOP,
50 | SETUP_FINALLY, END_FINALLY,
51 | SETUP_EXCEPT, POP_EXCEPT,
52 | SETUP_WITH,
53 | POP_BLOCK,
54 | STORE_FAST, DELETE_FAST,
55 | STORE_DEREF, DELETE_DEREF,
56 | STORE_GLOBAL, DELETE_GLOBAL,
57 | STORE_NAME, DELETE_NAME,
58 | STORE_ATTR, DELETE_ATTR,
59 | IMPORT_NAME, IMPORT_FROM,
60 | RETURN_VALUE, YIELD_VALUE,
61 | RAISE_VARARGS,
62 | POP_TOP,
63 | }
64 |
65 | # Conditional branching opcode that make up if statements and and/or
66 | # expressions
67 | pop_jump_if_opcodes = (POP_JUMP_IF_TRUE, POP_JUMP_IF_FALSE)
68 |
69 | # These opcodes indicate that a pop_jump_if_x to the address just
70 | # after them is an else-jump
71 | else_jump_opcodes = (
72 | JUMP_FORWARD, RETURN_VALUE, JUMP_ABSOLUTE,
73 | SETUP_LOOP, RAISE_VARARGS
74 | )
75 |
76 | # These opcodes indicate for loop rather than while loop
77 | for_jump_opcodes = (
78 | GET_ITER, FOR_ITER
79 | )
80 |
81 |
82 | def read_code(stream):
83 | # This helper is needed in order for the PEP 302 emulation to
84 | # correctly handle compiled files
85 | # Note: stream must be opened in "rb" mode
86 | import marshal
87 |
88 | magic = stream.read(4)
89 | if magic != imp.get_magic():
90 | print("*** Warning: file has wrong magic number ***")
91 | stream.read(4) # Skip timestamp
92 | stream.read(4) # Skip rawsize
93 | return marshal.load(stream)
94 |
95 |
96 | def dec_module(path):
97 | if path.endswith(".py"):
98 | path = imp.cache_from_source(path)
99 | elif not path.endswith(".pyc") and not path.endswith(".pyo"):
100 | raise ValueError("path must point to a .py or .pyc file")
101 | stream = open(path, "rb")
102 | code_obj = read_code(stream)
103 | code = Code(code_obj)
104 | return code.get_suite(include_declarations=False, look_for_docstring=True)
105 |
106 |
107 | def decompile(obj):
108 | """
109 | Decompile obj if it is a module object, a function or a
110 | code object. If obj is a string, it is assumed to be the path
111 | to a python module.
112 | """
113 | if isinstance(obj, str):
114 | return dec_module(obj)
115 | if inspect.iscode(obj):
116 | code = Code(obj)
117 | return code.get_suite()
118 | if inspect.isfunction(obj):
119 | code = Code(obj.__code__)
120 | defaults = obj.__defaults__
121 | kwdefaults = obj.__kwdefaults__
122 | return DefStatement(code, defaults, kwdefaults, obj.__closure__)
123 | elif inspect.ismodule(obj):
124 | return dec_module(obj.__file__)
125 | else:
126 | msg = "Object must be string, module, function or code object"
127 | raise TypeError(msg)
128 |
129 |
130 | class Indent:
131 | def __init__(self, indent_level=0, indent_step=4):
132 | self.level = indent_level
133 | self.step = indent_step
134 |
135 | def write(self, pattern, *args, **kwargs):
136 | if args or kwargs:
137 | pattern = pattern.format(*args, **kwargs)
138 | return self.indent(pattern)
139 |
140 | def __add__(self, indent_increase):
141 | return type(self)(self.level + indent_increase, self.step)
142 |
143 |
144 | class IndentPrint(Indent):
145 | def indent(self, string):
146 | print(" " * self.step * self.level + string)
147 |
148 |
149 | class IndentString(Indent):
150 | def __init__(self, indent_level=0, indent_step=4, lines=None):
151 | Indent.__init__(self, indent_level, indent_step)
152 | if lines is None:
153 | self.lines = []
154 | else:
155 | self.lines = lines
156 |
157 | def __add__(self, indent_increase):
158 | return type(self)(self.level + indent_increase, self.step, self.lines)
159 |
160 | def sep(self):
161 | if not self.lines or self.lines[-1]:
162 | self.lines.append("")
163 |
164 | def indent(self, string):
165 | self.lines.append(" " * self.step * self.level + string)
166 |
167 | def __str__(self):
168 | return "\n".join(self.lines)
169 |
170 |
171 | class Stack:
172 | def __init__(self):
173 | self._stack = []
174 | self._counts = {}
175 |
176 | def __bool__(self):
177 | return bool(self._stack)
178 |
179 | def __len__(self):
180 | return len(self._stack)
181 |
182 | def __contains__(self, val):
183 | return self.get_count(val) > 0
184 |
185 | def get_count(self, obj):
186 | return self._counts.get(id(obj), 0)
187 |
188 | def set_count(self, obj, val):
189 | if val:
190 | self._counts[id(obj)] = val
191 | else:
192 | del self._counts[id(obj)]
193 |
194 | def pop1(self):
195 | val = self._stack.pop() if self._stack else PyConst('ERROR')
196 | self.set_count(val, self.get_count(val) - 1)
197 | return val
198 |
199 | def pop(self, count=None):
200 | if count is None:
201 | return self.pop1()
202 | else:
203 | vals = [self.pop1() for i in range(count)]
204 | vals.reverse()
205 | return vals
206 |
207 | def push(self, *args):
208 | for val in args:
209 | self.set_count(val, self.get_count(val) + 1)
210 | self._stack.append(val)
211 |
212 | def peek(self, count=None):
213 | if count is None:
214 | return self._stack[-1]
215 | else:
216 | return self._stack[-count:]
217 |
218 |
219 | def code_walker(code):
220 | l = len(code)
221 | code = array('B', code)
222 | i = 0
223 | extended_arg = 0
224 | while i < l:
225 | op = code[i]
226 | if op >= HAVE_ARGUMENT:
227 | oparg = code[i + 1] + code[i + 2] * 256 + extended_arg
228 | extended_arg = 0
229 | if op == EXTENDED_ARG:
230 | extended_arg = oparg * 65536
231 | yield i, (op, oparg)
232 | i += 3
233 | else:
234 | yield i, (op, None)
235 | i += 1
236 |
237 |
238 | class Code:
239 | def __init__(self, code_obj, parent=None):
240 | self.code_obj = code_obj
241 | self.parent = parent
242 | self.derefnames = [PyName(v)
243 | for v in code_obj.co_cellvars + code_obj.co_freevars]
244 | self.consts = list(map(PyConst, code_obj.co_consts))
245 | self.names = list(map(PyName, code_obj.co_names))
246 | self.varnames = list(map(PyName, code_obj.co_varnames))
247 | self.instr_seq = list(code_walker(code_obj.co_code))
248 | self.instr_map = {addr: i for i, (addr, _) in enumerate(self.instr_seq)}
249 | self.name = code_obj.co_name
250 | self.globals = []
251 | self.nonlocals = []
252 | self.find_else()
253 |
254 | def __getitem__(self, instr_index):
255 | if 0 <= instr_index < len(self.instr_seq):
256 | return Address(self, instr_index)
257 |
258 | def __iter__(self):
259 | for i in range(len(self.instr_seq)):
260 | yield Address(self, i)
261 |
262 | def show(self):
263 | for addr in self:
264 | print(addr)
265 |
266 | def address(self, addr):
267 | return self[self.instr_map[addr]]
268 |
269 | def iscellvar(self, i):
270 | return i < len(self.code_obj.co_cellvars)
271 |
272 | def find_else(self):
273 | jumps = {}
274 | last_jump = None
275 | for addr in self:
276 | opcode, arg = addr
277 | if opcode in pop_jump_if_opcodes:
278 | jump_addr = self.address(arg)
279 | if (jump_addr[-1].opcode in else_jump_opcodes
280 | or jump_addr.opcode == FOR_ITER):
281 | last_jump = addr
282 | jumps[jump_addr] = addr
283 | elif opcode == JUMP_ABSOLUTE:
284 | # This case is to deal with some nested ifs such as:
285 | # if a:
286 | # if b:
287 | # f()
288 | # elif c:
289 | # g()
290 | jump_addr = self.address(arg)
291 | if jump_addr in jumps:
292 | jumps[addr] = jumps[jump_addr]
293 | elif opcode in stmt_opcodes and last_jump is not None:
294 | # This opcode will generate a statement, so it means
295 | # that the last POP_JUMP_IF_x was an else-jump
296 | jumps[addr] = last_jump
297 | self.else_jumps = set(jumps.values())
298 |
299 | def get_suite(self, include_declarations=True, look_for_docstring=False):
300 | dec = SuiteDecompiler(self[0])
301 | dec.run()
302 | first_stmt = dec.suite and dec.suite[0]
303 | # Change __doc__ = "docstring" to "docstring"
304 | if look_for_docstring and isinstance(first_stmt, AssignStatement):
305 | chain = first_stmt.chain
306 | if len(chain) == 2 and str(chain[0]) == "__doc__":
307 | dec.suite[0] = DocString(first_stmt.chain[1].val)
308 | if include_declarations and (self.globals or self.nonlocals):
309 | suite = Suite()
310 | if self.globals:
311 | stmt = "global " + ", ".join(map(str, self.globals))
312 | suite.add_statement(SimpleStatement(stmt))
313 | if self.nonlocals:
314 | stmt = "nonlocal " + ", ".join(map(str, self.nonlocals))
315 | suite.add_statement(SimpleStatement(stmt))
316 | for stmt in dec.suite:
317 | suite.add_statement(stmt)
318 | return suite
319 | else:
320 | return dec.suite
321 |
322 | def declare_global(self, name):
323 | """
324 | Declare name as a global. Called by STORE_GLOBAL and
325 | DELETE_GLOBAL
326 | """
327 | if name not in self.globals:
328 | self.globals.append(name)
329 |
330 | def ensure_global(self, name):
331 | """
332 | Declare name as global only if it is also a local variable
333 | name in one of the surrounding code objects. This is called
334 | by LOAD_GLOBAL
335 | """
336 | parent = self.parent
337 | while parent:
338 | if name in parent.varnames:
339 | return self.declare_global(name)
340 | parent = parent.parent
341 |
342 | def declare_nonlocal(self, name):
343 | """
344 | Declare name as nonlocal. Called by STORE_DEREF and
345 | DELETE_DEREF (but only when the name denotes a free variable,
346 | not a cell one).
347 | """
348 | if name not in self.nonlocals:
349 | self.nonlocals.append(name)
350 |
351 |
352 | class Address:
353 | def __init__(self, code, instr_index):
354 | self.code = code
355 | self.index = instr_index
356 | self.addr, (self.opcode, self.arg) = code.instr_seq[instr_index]
357 |
358 | def __eq__(self, other):
359 | return (isinstance(other, type(self))
360 | and self.code == other.code and self.index == other.index)
361 |
362 | def __lt__(self, other):
363 | return other is None or (isinstance(other, type(self))
364 | and self.code == other.code and self.index < other.index)
365 |
366 | def __str__(self):
367 | mark = "*" if self in self.code.else_jumps else " "
368 | return "{} {} {} {}".format(
369 | mark, self.addr,
370 | opname[self.opcode], self.arg or ""
371 | )
372 |
373 | def __add__(self, delta):
374 | return self.code.address(self.addr + delta)
375 |
376 | def __getitem__(self, index):
377 | return self.code[self.index + index]
378 |
379 | def __iter__(self):
380 | yield self.opcode
381 | yield self.arg
382 |
383 | def __hash__(self):
384 | return hash((self.code, self.index))
385 |
386 | def is_else_jump(self):
387 | return self in self.code.else_jumps
388 |
389 | def change_instr(self, opcode, arg=None):
390 | self.code.instr_seq[self.index] = (self.addr, (opcode, arg))
391 |
392 | def jump(self):
393 | opcode = self.opcode
394 | if opcode in dis.hasjrel:
395 | return self[1] + self.arg
396 | elif opcode in dis.hasjabs:
397 | return self.code.address(self.arg)
398 |
399 |
400 | class PyExpr:
401 | def wrap(self, condition=True):
402 | if condition:
403 | return "({})".format(self)
404 | else:
405 | return str(self)
406 |
407 | def store(self, dec, dest):
408 | chain = dec.assignment_chain
409 | chain.append(dest)
410 | if self not in dec.stack:
411 | chain.append(self)
412 | dec.suite.add_statement(AssignStatement(chain))
413 | dec.assignment_chain = []
414 |
415 | def on_pop(self, dec):
416 | dec.write(str(self))
417 |
418 |
419 | class PyConst(PyExpr):
420 | precedence = 100
421 |
422 | def __init__(self, val):
423 | self.val = val
424 |
425 | def __str__(self):
426 | return repr(self.val)
427 |
428 | def __iter__(self):
429 | return iter(self.val)
430 |
431 | def __eq__(self, other):
432 | return isinstance(other, PyConst) and self.val == other.val
433 |
434 |
435 | class PyTuple(PyExpr):
436 | precedence = 0
437 |
438 | def __init__(self, values):
439 | self.values = values
440 |
441 | def __str__(self):
442 | if not self.values:
443 | return "()"
444 | valstr = [val.wrap(val.precedence <= self.precedence)
445 | for val in self.values]
446 | if len(valstr) == 1:
447 | return '(' + valstr[0] + "," + ')'
448 | else:
449 | return '(' + ", ".join(valstr) + ')'
450 |
451 | def __iter__(self):
452 | return iter(self.values)
453 |
454 | def wrap(self, condition=True):
455 | return str(self)
456 |
457 | class PyList(PyExpr):
458 | precedence = 16
459 |
460 | def __init__(self, values):
461 | self.values = values
462 |
463 | def __str__(self):
464 | valstr = ", ".join(val.wrap(val.precedence <= 0)
465 | for val in self.values)
466 | return "[{}]".format(valstr)
467 |
468 | def __iter__(self):
469 | return iter(self.values)
470 |
471 |
472 | class PySet(PyExpr):
473 | precedence = 16
474 |
475 | def __init__(self, values):
476 | self.values = values
477 |
478 | def __str__(self):
479 | valstr = ", ".join(val.wrap(val.precedence <= 0)
480 | for val in self.values)
481 | return "{{{}}}".format(valstr)
482 |
483 | def __iter__(self):
484 | return iter(self.values)
485 |
486 |
487 | class PyDict(PyExpr):
488 | precedence = 16
489 |
490 | def __init__(self):
491 | self.items = []
492 |
493 | def set_item(self, key, val):
494 | self.items.append((key, val))
495 |
496 | def __str__(self):
497 | itemstr = ", ".join("{}: {}".format(*kv) for kv in self.items)
498 | return "{{{}}}".format(itemstr)
499 |
500 |
501 | class PyName(PyExpr):
502 | precedence = 100
503 |
504 | def __init__(self, name):
505 | self.name = name
506 |
507 | def __str__(self):
508 | return self.name
509 |
510 | def __eq__(self, other):
511 | return isinstance(other, type(self)) and self.name == other.name
512 |
513 |
514 | class PyUnaryOp(PyExpr):
515 | def __init__(self, operand):
516 | self.operand = operand
517 |
518 | def __str__(self):
519 | opstr = self.operand.wrap(self.operand.precedence < self.precedence)
520 | return self.pattern.format(opstr)
521 |
522 | @classmethod
523 | def instr(cls, stack):
524 | stack.push(cls(stack.pop()))
525 |
526 |
527 | class PyBinaryOp(PyExpr):
528 | def __init__(self, left, right):
529 | self.left = left
530 | self.right = right
531 |
532 | def wrap_left(self):
533 | return self.left.wrap(self.left.precedence < self.precedence)
534 |
535 | def wrap_right(self):
536 | return self.right.wrap(self.right.precedence <= self.precedence)
537 |
538 | def __str__(self):
539 | return self.pattern.format(self.wrap_left(), self.wrap_right())
540 |
541 | @classmethod
542 | def instr(cls, stack):
543 | right = stack.pop()
544 | left = stack.pop()
545 | stack.push(cls(left, right))
546 |
547 |
548 | class PySubscript(PyBinaryOp):
549 | precedence = 15
550 | pattern = "{}[{}]"
551 |
552 | def wrap_right(self):
553 | return str(self.right)
554 |
555 |
556 | class PySlice(PyExpr):
557 | precedence = 1
558 |
559 | def __init__(self, args):
560 | assert len(args) in (2, 3)
561 | if len(args) == 2:
562 | self.start, self.stop = args
563 | self.step = None
564 | else:
565 | self.start, self.stop, self.step = args
566 | if self.start == PyConst(None):
567 | self.start = ""
568 | if self.stop == PyConst(None):
569 | self.stop = ""
570 |
571 | def __str__(self):
572 | if self.step is None:
573 | return "{}:{}".format(self.start, self.stop)
574 | else:
575 | return "{}:{}:{}".format(self.start, self.stop, self.step)
576 |
577 |
578 | class PyCompare(PyExpr):
579 | precedence = 6
580 |
581 | def __init__(self, complist):
582 | self.complist = complist
583 |
584 | def __str__(self):
585 | return " ".join(x if i % 2 else x.wrap(x.precedence <= 0)
586 | for i, x in enumerate(self.complist))
587 |
588 | def extends(self, other):
589 | if not isinstance(other, PyCompare):
590 | return False
591 | else:
592 | return self.complist[0] == other.complist[-1]
593 |
594 | def chain(self, other):
595 | return PyCompare(self.complist + other.complist[1:])
596 |
597 |
598 | class PyBooleanAnd(PyBinaryOp):
599 | precedence = 4
600 | pattern = "{} and {}"
601 |
602 |
603 | class PyBooleanOr(PyBinaryOp):
604 | precedence = 3
605 | pattern = "{} or {}"
606 |
607 |
608 | class PyIfElse(PyExpr):
609 | precedence = 2
610 |
611 | def __init__(self, cond, true_expr, false_expr):
612 | self.cond = cond
613 | self.true_expr = true_expr
614 | self.false_expr = false_expr
615 |
616 | def __str__(self):
617 | p = self.precedence
618 | cond_str = self.cond.wrap(self.cond.precedence <= p)
619 | true_str = self.true_expr.wrap(self.cond.precedence <= p)
620 | false_str = self.false_expr.wrap(self.cond.precedence < p)
621 | return "{} if {} else {}".format(true_str, cond_str, false_str)
622 |
623 |
624 | class PyAttribute(PyExpr):
625 | precedence = 15
626 |
627 | def __init__(self, expr, attrname):
628 | self.expr = expr
629 | self.attrname = attrname
630 |
631 | def __str__(self):
632 | expr_str = self.expr.wrap(self.expr.precedence < self.precedence)
633 | return "{}.{}".format(expr_str, self.attrname)
634 |
635 |
636 | class PyCallFunction(PyExpr):
637 | precedence = 15
638 |
639 | def __init__(self, func, args, kwargs, varargs=None, varkw=None):
640 | self.func = func
641 | self.args = args
642 | self.kwargs = kwargs
643 | self.varargs = varargs
644 | self.varkw = varkw
645 |
646 | def __str__(self):
647 | funcstr = self.func.wrap(self.func.precedence < self.precedence)
648 | if len(self.args) == 1 and not (self.kwargs or self.varargs
649 | or self.varkw):
650 | arg = self.args[0]
651 | if isinstance(arg, PyGenExpr):
652 | # Only one pair of brackets arount a single arg genexpr
653 | return "{}{}".format(funcstr, arg)
654 | args = [x.wrap(x.precedence <= 0) for x in self.args]
655 | args.extend("{}={}".format(k.val, v.wrap(v.precedence <= 0))
656 | for k, v in self.kwargs)
657 | if self.varargs is not None:
658 | args.append("*{}".format(self.varargs))
659 | if self.varkw is not None:
660 | args.append("**{}".format(self.varkw))
661 | return "{}({})".format(funcstr, ", ".join(args))
662 |
663 |
664 | class FunctionDefinition:
665 | def __init__(self, code, defaults, kwdefaults, closure, paramobjs={}):
666 | self.code = code
667 | self.defaults = defaults
668 | self.kwdefaults = kwdefaults
669 | self.closure = closure
670 | self.paramobjs = paramobjs
671 |
672 | def getparams(self):
673 | code_obj = self.code.code_obj
674 | l = code_obj.co_argcount
675 | params = list(code_obj.co_varnames[:l])
676 | if self.defaults:
677 | for i, arg in enumerate(reversed(self.defaults)):
678 | name = params[-i - 1]
679 | if name in self.paramobjs:
680 | params[-i - 1] = "{}:{}={}".format(name, self.paramobjs[name], arg)
681 | else:
682 | params[-i - 1] = "{}={}".format(name, arg)
683 | kwcount = code_obj.co_kwonlyargcount
684 | kwparams = []
685 | if kwcount:
686 | for i in range(kwcount):
687 | name = code_obj.co_varnames[l + i]
688 | if name in self.kwdefaults and name in self.paramobjs:
689 | kwparams.append("{}:{}={}".format(name, self.paramobjs[name], self.kwdefaults[name]))
690 | elif name in self.kwdefaults:
691 | kwparams.append("{}={}".format(name, self.kwdefaults[name]))
692 | else:
693 | kwparams.append(name)
694 | l += kwcount
695 | if code_obj.co_flags & VARARGS:
696 | params.append("*" + code_obj.co_varnames[l])
697 | l += 1
698 | elif kwparams:
699 | params.append("*")
700 | params.extend(kwparams)
701 | if code_obj.co_flags & VARKEYWORDS:
702 | params.append("**" + code_obj.co_varnames[l])
703 |
704 | return params
705 |
706 | def getreturn(self):
707 | if self.paramobjs and 'return' in self.paramobjs:
708 | return self.paramobjs['return']
709 | return None
710 |
711 | class PyLambda(PyExpr, FunctionDefinition):
712 | precedence = 1
713 |
714 | def __str__(self):
715 | suite = self.code.get_suite()
716 | params = ", ".join(self.getparams())
717 | if len(suite.statements) > 0:
718 | def strip_return(val):
719 | return val[len("return "):] if val.startswith('return') else val
720 |
721 | if isinstance(suite[0], IfStatement) and len(suite.statements) == 2:
722 | expr = "return {} if {} else {}".format(
723 | strip_return(str(suite[0].true_suite)),
724 | str(suite[0].cond),
725 | strip_return(str(suite[1]))
726 | )
727 | else:
728 | expr = strip_return(str(suite[0]))
729 | else:
730 | expr = "None"
731 | return "lambda {}: {}".format(params, expr)
732 |
733 |
734 | class PyComp(PyExpr):
735 | """
736 | Abstraction for list, set, dict comprehensions and generator expressions
737 | """
738 | precedence = 16
739 |
740 | def __init__(self, code, defaults, kwdefaults, closure, paramobjs={}):
741 | assert not defaults and not kwdefaults
742 | self.code = code
743 | code[0].change_instr(NOP)
744 | last_i = len(code.instr_seq) - 1
745 | code[last_i].change_instr(NOP)
746 |
747 | def set_iterable(self, iterable):
748 | self.code.varnames[0] = iterable
749 |
750 | def __str__(self):
751 | suite = self.code.get_suite()
752 | return self.pattern.format(suite.gen_display())
753 |
754 |
755 | class PyListComp(PyComp):
756 | pattern = "[{}]"
757 |
758 |
759 | class PySetComp(PyComp):
760 | pattern = "{{{}}}"
761 |
762 |
763 | class PyKeyValue(PyBinaryOp):
764 | """This is only to create dict comprehensions"""
765 | precedence = 1
766 | pattern = "{}: {}"
767 |
768 |
769 | class PyDictComp(PyComp):
770 | pattern = "{{{}}}"
771 |
772 |
773 | class PyGenExpr(PyComp):
774 | precedence = 16
775 | pattern = "({})"
776 |
777 | def __init__(self, code, defaults, kwdefaults, closure, paramobjs={}):
778 | self.code = code
779 |
780 |
781 | class PyYield(PyExpr):
782 | precedence = 1
783 |
784 | def __init__(self, value):
785 | self.value = value
786 |
787 | def __str__(self):
788 | return "yield {}".format(self.value)
789 |
790 |
791 | class PyYieldFrom(PyExpr):
792 | precedence = 1
793 |
794 | def __init__(self, value):
795 | self.value = value
796 |
797 | def __str__(self):
798 | return "yield from {}".format(self.value)
799 |
800 |
801 | class PyStarred(PyExpr):
802 | """Used in unpacking assigments"""
803 | precedence = 15
804 |
805 | def __init__(self, expr):
806 | self.expr = expr
807 |
808 | def __str__(self):
809 | es = self.expr.wrap(self.expr.precedence < self.precedence)
810 | return "*{}".format(es)
811 |
812 |
813 | code_map = {
814 | '': PyLambda,
815 | '': PyListComp,
816 | '': PySetComp,
817 | '': PyDictComp,
818 | '': PyGenExpr,
819 | }
820 |
821 | unary_ops = [
822 | ('UNARY_POSITIVE', 'Positive', '+{}', 13),
823 | ('UNARY_NEGATIVE', 'Negative', '-{}', 13),
824 | ('UNARY_NOT', 'Not', 'not {}', 5),
825 | ('UNARY_INVERT', 'Invert', '~{}', 13),
826 | ]
827 |
828 | binary_ops = [
829 | ('POWER', 'Power', '{}**{}', 14, '{} **= {}'),
830 | ('MULTIPLY', 'Multiply', '{}*{}', 12, '{} *= {}'),
831 | ('FLOOR_DIVIDE', 'FloorDivide', '{}//{}', 12, '{} //= {}'),
832 | ('TRUE_DIVIDE', 'TrueDivide', '{}/{}', 12, '{} /= {}'),
833 | ('MODULO', 'Modulo', '{} % {}', 12, '{} %= {}'),
834 | ('ADD', 'Add', '{} + {}', 11, '{} += {}'),
835 | ('SUBTRACT', 'Subtract', '{} - {}', 11, '{} -= {}'),
836 | ('SUBSCR', 'Subscript', '{}[{}]', 15, None),
837 | ('LSHIFT', 'LeftShift', '{} << {}', 10, '{} <<= {}'),
838 | ('RSHIFT', 'RightShift', '{} >> {}', 10, '{} >>= {}'),
839 | ('AND', 'And', '{} & {}', 9, '{} &= {}'),
840 | ('XOR', 'Xor', '{} ^ {}', 8, '{} ^= {}'),
841 | ('OR', 'Or', '{} | {}', 7, '{} |= {}'),
842 | ]
843 |
844 |
845 | class PyStatement:
846 | def __str__(self):
847 | istr = IndentString()
848 | self.display(istr)
849 | return str(istr)
850 |
851 | def wrap(self, condition=True):
852 | if condition:
853 | assert not condition
854 | return "({})".format(self)
855 | else:
856 | return str(self)
857 |
858 | def on_pop(self, dec):
859 | # dec.write("#ERROR: Unexpected context 'on_pop': pop on statement: ")
860 | pass
861 |
862 |
863 | class DocString(PyStatement):
864 | def __init__(self, string):
865 | self.string = string
866 |
867 | def display(self, indent):
868 | if '\n' not in self.string:
869 | indent.write(repr(self.string))
870 | else:
871 | if "'''" not in self.string:
872 | fence = "'''"
873 | elif '"""' not in self.string:
874 | fence = '"""'
875 | else:
876 | raise NotImplemented
877 | lines = self.string.split('\n')
878 | text = '\n'.join(l.encode('unicode_escape').decode()
879 | for l in lines)
880 | docstring = "{0}{1}{0}".format(fence, text)
881 | indent.write(docstring)
882 |
883 |
884 | class AssignStatement(PyStatement):
885 | def __init__(self, chain):
886 | self.chain = chain
887 |
888 | def display(self, indent):
889 | indent.write(" = ".join(map(str, self.chain)))
890 |
891 |
892 | class InPlaceOp(PyStatement):
893 | def __init__(self, left, right):
894 | self.right = right
895 | self.left = left
896 |
897 | def store(self, dec, dest):
898 | # assert dest is self.left
899 | dec.suite.add_statement(self)
900 |
901 | def display(self, indent):
902 | indent.write(self.pattern, self.left, self.right)
903 |
904 |
905 | class Unpack:
906 | precedence = 50
907 |
908 | def __init__(self, val, length, star_index=None):
909 | self.val = val
910 | self.length = length
911 | self.star_index = star_index
912 | self.dests = []
913 |
914 | def store(self, dec, dest):
915 | if len(self.dests) == self.star_index:
916 | dest = PyStarred(dest)
917 | self.dests.append(dest)
918 | if len(self.dests) == self.length:
919 | dec.stack.push(self.val)
920 | dec.store(PyTuple(self.dests))
921 |
922 |
923 | class ImportStatement(PyStatement):
924 | alias = ""
925 | precedence = 100
926 |
927 | def __init__(self, name, level, fromlist):
928 | self.name = name
929 | self.alias = name
930 | self.level = level
931 | self.fromlist = fromlist
932 | self.aslist = []
933 |
934 | def store(self, dec, dest):
935 | self.alias = dest
936 | dec.suite.add_statement(self)
937 |
938 | def on_pop(self, dec):
939 | dec.suite.add_statement(self)
940 |
941 | def display(self, indent):
942 | if self.fromlist == PyConst(None):
943 | name = self.name.name
944 | alias = self.alias.name
945 | if name == alias or name.startswith(alias + "."):
946 | indent.write("import {}", name)
947 | else:
948 | indent.write("import {} as {}", name, alias)
949 | elif self.fromlist == PyConst(('*',)):
950 | indent.write("from {} import *", self.name.name)
951 | else:
952 | names = []
953 | for name, alias in zip(self.fromlist, self.aslist):
954 | if name == alias:
955 | names.append(name)
956 | else:
957 | names.append("{} as {}".format(name, alias))
958 | indent.write("from {} import {}", self.name, ", ".join(names))
959 |
960 |
961 | class ImportFrom:
962 | def __init__(self, name):
963 | self.name = name
964 |
965 | def store(self, dec, dest):
966 | imp = dec.stack.peek()
967 | assert isinstance(imp, ImportStatement)
968 | imp.aslist.append(dest.name)
969 |
970 |
971 | class SimpleStatement(PyStatement):
972 | def __init__(self, val):
973 | assert val is not None
974 | self.val = val
975 |
976 | def display(self, indent):
977 | indent.write(self.val)
978 |
979 | def gen_display(self, seq=()):
980 | return " ".join((self.val,) + seq)
981 |
982 |
983 | class IfStatement(PyStatement):
984 | def __init__(self, cond, true_suite, false_suite):
985 | self.cond = cond
986 | self.true_suite = true_suite
987 | self.false_suite = false_suite
988 |
989 | def display(self, indent, is_elif=False):
990 | ptn = "elif {}:" if is_elif else "if {}:"
991 | indent.write(ptn, self.cond)
992 | self.true_suite.display(indent + 1)
993 | if not self.false_suite:
994 | return
995 | if len(self.false_suite) == 1:
996 | stmt = self.false_suite[0]
997 | if isinstance(stmt, IfStatement):
998 | stmt.display(indent, is_elif=True)
999 | return
1000 | indent.write("else:")
1001 | self.false_suite.display(indent + 1)
1002 |
1003 | def gen_display(self, seq=()):
1004 | assert not self.false_suite
1005 | s = "if {}".format(self.cond)
1006 | return self.true_suite.gen_display(seq + (s,))
1007 |
1008 |
1009 | class ForStatement(PyStatement):
1010 | def __init__(self, iterable):
1011 | self.iterable = iterable
1012 |
1013 | def store(self, dec, dest):
1014 | self.dest = dest
1015 |
1016 | def display(self, indent):
1017 | indent.write("for {} in {}:", self.dest, self.iterable)
1018 | self.body.display(indent + 1)
1019 |
1020 | def gen_display(self, seq=()):
1021 | s = "for {} in {}".format(self.dest, self.iterable)
1022 | return self.body.gen_display(seq + (s,))
1023 |
1024 |
1025 | class WhileStatement(PyStatement):
1026 | def __init__(self, cond, body):
1027 | self.cond = cond
1028 | self.body = body
1029 |
1030 | def display(self, indent):
1031 | indent.write("while {}:", self.cond)
1032 | self.body.display(indent + 1)
1033 |
1034 |
1035 | class DecorableStatement(PyStatement):
1036 | def __init__(self):
1037 | self.decorators = []
1038 |
1039 | def display(self, indent):
1040 | indent.sep()
1041 | for f in reversed(self.decorators):
1042 | indent.write("@{}", f)
1043 | self.display_undecorated(indent)
1044 | indent.sep()
1045 |
1046 | def decorate(self, f):
1047 | self.decorators.append(f)
1048 |
1049 |
1050 | class DefStatement(FunctionDefinition, DecorableStatement):
1051 | def __init__(self, code, defaults, kwdefaults, closure, paramobjs={}):
1052 | FunctionDefinition.__init__(self, code, defaults, kwdefaults, closure, paramobjs)
1053 | DecorableStatement.__init__(self)
1054 |
1055 | def display_undecorated(self, indent):
1056 | paramlist = ", ".join(self.getparams())
1057 | result = self.getreturn()
1058 | if result:
1059 | indent.write("def {}({}) -> {}:", self.code.name, paramlist, result)
1060 | else:
1061 | indent.write("def {}({}):", self.code.name, paramlist)
1062 | # Assume that co_consts starts with None unless the function
1063 | # has a docstring, in which case it starts with the docstring
1064 | if self.code.consts[0] != PyConst(None):
1065 | docstring = self.code.consts[0].val
1066 | DocString(docstring).display(indent + 1)
1067 | self.code.get_suite().display(indent + 1)
1068 |
1069 | def store(self, dec, dest):
1070 | self.name = dest
1071 | dec.suite.add_statement(self)
1072 |
1073 |
1074 | class TryStatement(PyStatement):
1075 | def __init__(self, try_suite):
1076 | self.try_suite = try_suite
1077 | self.except_clauses = []
1078 |
1079 | def add_except_clause(self, type, suite):
1080 | self.except_clauses.append([type, None, suite])
1081 |
1082 | def store(self, dec, dest):
1083 | self.except_clauses[-1][1] = dest
1084 |
1085 | def display(self, indent):
1086 | indent.write("try:")
1087 | self.try_suite.display(indent + 1)
1088 | for type, name, suite in self.except_clauses:
1089 | if type is None:
1090 | indent.write("except:")
1091 | elif name is None:
1092 | indent.write("except {}:", type)
1093 | else:
1094 | indent.write("except {} as {}:", type, name)
1095 | suite.display(indent + 1)
1096 |
1097 |
1098 | class FinallyStatement(PyStatement):
1099 | def __init__(self, try_suite, finally_suite):
1100 | self.try_suite = try_suite
1101 | self.finally_suite = finally_suite
1102 |
1103 | def display(self, indent):
1104 | # Wrap the try suite in a TryStatement if necessary
1105 | try_stmt = None
1106 | if len(self.try_suite) == 1:
1107 | try_stmt = self.try_suite[0]
1108 | if not isinstance(try_stmt, TryStatement):
1109 | try_stmt = None
1110 | if try_stmt is None:
1111 | try_stmt = TryStatement(self.try_suite)
1112 | try_stmt.display(indent)
1113 | indent.write("finally:")
1114 | self.finally_suite.display(indent + 1)
1115 |
1116 |
1117 | class WithStatement(PyStatement):
1118 | def __init__(self, with_expr):
1119 | self.with_expr = with_expr
1120 | self.with_name = None
1121 |
1122 | def store(self, dec, dest):
1123 | self.with_name = dest
1124 |
1125 | def display(self, indent, args=None):
1126 | # args to take care of nested withs:
1127 | # with x as t:
1128 | # with y as u:
1129 | #
1130 | # --->
1131 | # with x as t, y as u:
1132 | #
1133 | if args is None:
1134 | args = []
1135 | if self.with_name is None:
1136 | args.append(str(self.with_expr))
1137 | else:
1138 | args.append("{} as {}".format(self.with_expr, self.with_name))
1139 | if len(self.suite) == 1 and isinstance(self.suite[0], WithStatement):
1140 | self.suite[0].display(indent, args)
1141 | else:
1142 | indent.write("with {}:", ", ".join(args))
1143 | self.suite.display(indent + 1)
1144 |
1145 |
1146 | class ClassStatement(DecorableStatement):
1147 | def __init__(self, func, name, parents, kwargs):
1148 | DecorableStatement.__init__(self)
1149 | self.func = func
1150 | self.parents = parents
1151 | self.kwargs = kwargs
1152 |
1153 | def store(self, dec, dest):
1154 | self.name = dest
1155 | dec.suite.add_statement(self)
1156 |
1157 | def display_undecorated(self, indent):
1158 | if self.parents or self.kwargs:
1159 | args = [str(x) for x in self.parents]
1160 | kwargs = ["{}={}".format(k.val, v) for k, v in self.kwargs]
1161 | all_args = ", ".join(args + kwargs)
1162 | indent.write("class {}({}):", self.name, all_args)
1163 | else:
1164 | indent.write("class {}:", self.name)
1165 | suite = self.func.code.get_suite(look_for_docstring=True)
1166 | if suite:
1167 | # TODO: find out why sometimes the class suite ends with
1168 | # "return __class__"
1169 | last_stmt = suite[-1]
1170 | if isinstance(last_stmt, SimpleStatement):
1171 | if last_stmt.val.startswith("return "):
1172 | suite.statements.pop()
1173 | suite.display(indent + 1)
1174 |
1175 |
1176 | class Suite:
1177 | def __init__(self):
1178 | self.statements = []
1179 |
1180 | def __bool__(self):
1181 | return bool(self.statements)
1182 |
1183 | def __len__(self):
1184 | return len(self.statements)
1185 |
1186 | def __getitem__(self, i):
1187 | return self.statements[i]
1188 |
1189 | def __setitem__(self, i, val):
1190 | self.statements[i] = val
1191 |
1192 | def __str__(self):
1193 | istr = IndentString()
1194 | self.display(istr)
1195 | return str(istr)
1196 |
1197 | def display(self, indent):
1198 | if self.statements:
1199 | for stmt in self.statements:
1200 | stmt.display(indent)
1201 | else:
1202 | indent.write("pass")
1203 |
1204 | def gen_display(self, seq=()):
1205 | assert len(self) == 1
1206 | return self[0].gen_display(seq)
1207 |
1208 | def add_statement(self, stmt):
1209 | self.statements.append(stmt)
1210 |
1211 |
1212 | class SuiteDecompiler:
1213 | # An instruction handler can return this to indicate to the run()
1214 | # function that it should return immediately
1215 | END_NOW = object()
1216 |
1217 | # This is put on the stack by LOAD_BUILD_CLASS
1218 | BUILD_CLASS = object()
1219 |
1220 | def __init__(self, start_addr, end_addr=None, stack=None):
1221 | self.start_addr = start_addr
1222 | self.end_addr = end_addr
1223 | self.code = start_addr.code
1224 | self.stack = Stack() if stack is None else stack
1225 | self.suite = Suite()
1226 | self.assignment_chain = []
1227 | self.popjump_stack = []
1228 |
1229 | def push_popjump(self, jtruthiness, jaddr, jcond):
1230 | stack = self.popjump_stack
1231 | if jaddr and jaddr[-1].is_else_jump():
1232 | # Increase jaddr to the 'else' address if it jumps to the 'then'
1233 | jaddr = jaddr[-1].jump()
1234 | while stack:
1235 | truthiness, addr, cond = stack[-1]
1236 | if jaddr == None: print("#ERROR: jaddr is None")
1237 | if jaddr == None or jaddr < addr or jaddr == addr:
1238 | break
1239 | stack.pop()
1240 | obj_maker = PyBooleanOr if truthiness else PyBooleanAnd
1241 | if isinstance(jcond, obj_maker):
1242 | # Use associativity of 'and' and 'or' to minimise the
1243 | # number of parentheses
1244 | jcond = obj_maker(obj_maker(cond, jcond.left), jcond.right)
1245 | else:
1246 | jcond = obj_maker(cond, jcond)
1247 | stack.append((jtruthiness, jaddr, jcond))
1248 |
1249 | def pop_popjump(self):
1250 | truthiness, addr, cond = self.popjump_stack.pop()
1251 | return cond
1252 |
1253 | def run(self):
1254 | addr, end_addr = self.start_addr, self.end_addr
1255 | while addr and addr < end_addr:
1256 | opcode, arg = addr
1257 | method = getattr(self, opname[opcode])
1258 | if arg is None:
1259 | new_addr = method(addr)
1260 | else:
1261 | new_addr = method(addr, arg)
1262 | if new_addr is self.END_NOW:
1263 | break
1264 | elif new_addr is None:
1265 | new_addr = addr[1]
1266 | addr = new_addr
1267 | return addr
1268 |
1269 | def write(self, template, *args):
1270 | def fmt(x):
1271 | if isinstance(x, int):
1272 | return self.stack.getval(x)
1273 | else:
1274 | return x
1275 |
1276 | if args:
1277 | line = template.format(*map(fmt, args))
1278 | else:
1279 | line = template
1280 | self.suite.add_statement(SimpleStatement(line))
1281 |
1282 | def store(self, dest):
1283 | val = self.stack.pop()
1284 | val.store(self, dest)
1285 |
1286 | def scan_to_first_jump_if(self, addr, end_addr):
1287 | i = 0
1288 | while 1:
1289 | cur_addr = addr[i]
1290 | if cur_addr == end_addr:
1291 | break
1292 | elif cur_addr.opcode in pop_jump_if_opcodes:
1293 | return cur_addr
1294 | elif cur_addr.opcode in else_jump_opcodes:
1295 | break
1296 | elif cur_addr.opcode in for_jump_opcodes:
1297 | break
1298 | i = i + 1
1299 | return None
1300 |
1301 | def scan_for_final_jump(self, start_addr, end_addr):
1302 | i = 0
1303 | while 1:
1304 | cur_addr = end_addr[i]
1305 | if cur_addr == start_addr:
1306 | break
1307 | elif cur_addr.opcode == JUMP_ABSOLUTE:
1308 | return cur_addr
1309 | elif cur_addr.opcode in else_jump_opcodes:
1310 | break
1311 | elif cur_addr.opcode in pop_jump_if_opcodes:
1312 | break
1313 | i = i - 1
1314 | return None
1315 |
1316 | #
1317 | # All opcode methods in CAPS below.
1318 | #
1319 |
1320 | def SETUP_LOOP(self, addr, delta):
1321 | jump_addr = addr[1] + delta
1322 | end_addr = jump_addr[-1]
1323 | if end_addr.opcode == JUMP_ABSOLUTE: # while 1 ???
1324 | d_body = SuiteDecompiler(addr[1], end_addr)
1325 | while_stmt = WhileStatement(PyConst(True), d_body.suite)
1326 | d_body.stack.push(while_stmt)
1327 | d_body.run()
1328 | while_stmt.body = d_body.suite
1329 | self.suite.add_statement(while_stmt)
1330 | return jump_addr
1331 | elif end_addr.opcode == POP_BLOCK: # assume conditional
1332 | # scan to first jump
1333 | end_cond = self.scan_to_first_jump_if(addr[1], end_addr)
1334 | if end_cond:
1335 | # scan for conditional
1336 | d_cond = SuiteDecompiler(addr[1], end_cond)
1337 | #
1338 | d_cond.run()
1339 | cond = d_cond.stack.pop()
1340 | if end_cond.opcode == POP_JUMP_IF_TRUE:
1341 | cond = PyNot(cond)
1342 | d_body = SuiteDecompiler(end_cond[1], end_addr)
1343 | while_stmt = WhileStatement(cond, d_body.suite)
1344 | d_body.stack.push(while_stmt)
1345 | d_body.run()
1346 | while_stmt.body = d_body.suite
1347 | self.suite.add_statement(while_stmt)
1348 | return jump_addr
1349 | return None
1350 |
1351 | def BREAK_LOOP(self, addr):
1352 | self.write("break")
1353 |
1354 | def CONTINUE_LOOP(self, addr, *argv):
1355 | self.write("continue")
1356 |
1357 | def SETUP_FINALLY(self, addr, delta):
1358 | start_finally = addr.jump()
1359 | d_try = SuiteDecompiler(addr[1], start_finally)
1360 | d_try.run()
1361 | d_finally = SuiteDecompiler(start_finally)
1362 | end_finally = d_finally.run()
1363 | self.suite.add_statement(FinallyStatement(d_try.suite, d_finally.suite))
1364 | return end_finally[1]
1365 |
1366 | def END_FINALLY(self, addr):
1367 | return self.END_NOW
1368 |
1369 | def SETUP_EXCEPT(self, addr, delta):
1370 | start_except = addr.jump()
1371 | end_try = start_except[-1]
1372 | d_try = SuiteDecompiler(addr[1], start_except[-1])
1373 | d_try.run()
1374 | if end_try.opcode == JUMP_FORWARD:
1375 | end_addr = end_try[1] + end_try.arg
1376 | elif end_try.opcode == JUMP_ABSOLUTE:
1377 | end_addr = end_try.arg
1378 | else:
1379 | # print(repr(end_try.opcode))
1380 | assert end_try.opcode == JUMP_FORWARD
1381 | stmt = TryStatement(d_try.suite)
1382 | while start_except.opcode != END_FINALLY:
1383 | if start_except.opcode == DUP_TOP:
1384 | # There's a new except clause
1385 | d_except = SuiteDecompiler(start_except[1])
1386 | d_except.stack.push(stmt)
1387 | d_except.run()
1388 | start_except = stmt.next_start_except
1389 | elif start_except.opcode == POP_TOP:
1390 | # It's a bare except clause - it starts:
1391 | # POP_TOP
1392 | # POP_TOP
1393 | # POP_TOP
1394 | #
1395 | # POP_EXCEPT
1396 | d_except = SuiteDecompiler(start_except[3])
1397 | end_except = d_except.run()
1398 | stmt.add_except_clause(None, d_except.suite)
1399 | start_except = end_except[2]
1400 | assert start_except.opcode == END_FINALLY
1401 | self.suite.add_statement(stmt)
1402 | return start_except[1]
1403 |
1404 | def SETUP_WITH(self, addr, delta):
1405 | end_with = addr.jump()
1406 | with_stmt = WithStatement(self.stack.pop())
1407 | d_with = SuiteDecompiler(addr[1], end_with)
1408 | d_with.stack.push(with_stmt)
1409 | d_with.run()
1410 | with_stmt.suite = d_with.suite
1411 | self.suite.add_statement(with_stmt)
1412 | assert end_with.opcode == WITH_CLEANUP
1413 | assert end_with[1].opcode == END_FINALLY
1414 | return end_with[2]
1415 |
1416 | def POP_BLOCK(self, addr):
1417 | # print("** POP BLOCK:", addr)
1418 | pass
1419 |
1420 | def POP_EXCEPT(self, addr):
1421 | # print("** POP EXCEPT:", addr)
1422 | return self.END_NOW
1423 |
1424 | def NOP(self, addr):
1425 | return
1426 |
1427 | def COMPARE_OP(self, addr, opname):
1428 | left, right = self.stack.pop(2)
1429 | if opname != 10: # 10 is exception match
1430 | self.stack.push(PyCompare([left, cmp_op[opname], right]))
1431 | else:
1432 | # It's an exception match
1433 | # left is a TryStatement
1434 | # right is the exception type to be matched
1435 | # It goes:
1436 | # COMPARE_OP 10
1437 | # POP_JUMP_IF_FALSE
1438 | # POP_TOP
1439 | # POP_TOP or STORE_FAST (if the match is named)
1440 | # POP_TOP
1441 | # SETUP_FINALLY if the match was named
1442 | assert addr[1].opcode == POP_JUMP_IF_FALSE
1443 | left.next_start_except = addr[1].jump()
1444 | assert addr[2].opcode == POP_TOP
1445 | assert addr[4].opcode == POP_TOP
1446 | if addr[5].opcode == SETUP_FINALLY:
1447 | except_start = addr[6]
1448 | except_end = addr[5].jump()
1449 | else:
1450 | except_start = addr[5]
1451 | except_end = left.next_start_except[-1]
1452 | d_body = SuiteDecompiler(except_start, except_end)
1453 | d_body.run()
1454 | left.add_except_clause(right, d_body.suite)
1455 | if addr[3].opcode != POP_TOP:
1456 | # The exception is named
1457 | d_exc_name = SuiteDecompiler(addr[3], addr[4])
1458 | d_exc_name.stack.push(left)
1459 | # This will store the name in left:
1460 | d_exc_name.run()
1461 | # We're done with this except clause
1462 | return self.END_NOW
1463 |
1464 | #
1465 | # Stack manipulation
1466 | #
1467 |
1468 | def POP_TOP(self, addr):
1469 | self.stack.pop().on_pop(self)
1470 |
1471 | def ROT_TWO(self, addr):
1472 | tos1, tos = self.stack.pop(2)
1473 | self.stack.push(tos, tos1)
1474 |
1475 | def ROT_THREE(self, addr):
1476 | tos2, tos1, tos = self.stack.pop(3)
1477 | self.stack.push(tos, tos2, tos1)
1478 |
1479 | def DUP_TOP(self, addr):
1480 | self.stack.push(self.stack.peek())
1481 |
1482 | def DUP_TOP_TWO(self, addr):
1483 | self.stack.push(*self.stack.peek(2))
1484 |
1485 | #
1486 | # LOAD / STORE / DELETE
1487 | #
1488 |
1489 | # FAST
1490 |
1491 | def LOAD_FAST(self, addr, var_num):
1492 | name = self.code.varnames[var_num]
1493 | self.stack.push(name)
1494 |
1495 |
1496 | def STORE_FAST(self, addr, var_num):
1497 | name = self.code.varnames[var_num]
1498 | self.store(name)
1499 |
1500 |
1501 | def DELETE_FAST(self, addr, var_num):
1502 | name = self.code.varnames[var_num]
1503 | self.write("del {}", name)
1504 |
1505 | # DEREF
1506 |
1507 | def LOAD_DEREF(self, addr, i):
1508 | name = self.code.derefnames[i]
1509 | self.stack.push(name)
1510 |
1511 | def STORE_DEREF(self, addr, i):
1512 | name = self.code.derefnames[i]
1513 | if not self.code.iscellvar(i):
1514 | self.code.declare_nonlocal(name)
1515 | self.store(name)
1516 |
1517 | def DELETE_DEREF(self, addr, i):
1518 | name = self.code.derefnames[i]
1519 | if not self.code.iscellvar(i):
1520 | self.code.declare_nonlocal(name)
1521 | self.write("del {}", name)
1522 |
1523 | # GLOBAL
1524 |
1525 | def LOAD_GLOBAL(self, addr, namei):
1526 | name = self.code.names[namei]
1527 | self.code.ensure_global(name)
1528 | self.stack.push(name)
1529 |
1530 | def STORE_GLOBAL(self, addr, namei):
1531 | name = self.code.names[namei]
1532 | self.code.declare_global(name)
1533 | self.store(name)
1534 |
1535 | def DELETE_GLOBAL(self, addr, namei):
1536 | name = self.code.names[namei]
1537 | self.declare_global(name)
1538 | self.write("del {}", name)
1539 |
1540 | # NAME
1541 |
1542 | def LOAD_NAME(self, addr, namei):
1543 | name = self.code.names[namei]
1544 | self.stack.push(name)
1545 |
1546 | def STORE_NAME(self, addr, namei):
1547 | name = self.code.names[namei]
1548 | self.store(name)
1549 |
1550 | def DELETE_NAME(self, addr, namei):
1551 | name = self.code.names[namei]
1552 | self.write("del {}", name)
1553 |
1554 | # ATTR
1555 |
1556 | def LOAD_ATTR(self, addr, namei):
1557 | expr = self.stack.pop()
1558 | attrname = self.code.names[namei]
1559 | self.stack.push(PyAttribute(expr, attrname))
1560 |
1561 |
1562 | def STORE_ATTR(self, addr, namei):
1563 | expr = self.stack.pop()
1564 | attrname = self.code.names[namei]
1565 | self.store(PyAttribute(expr, attrname))
1566 |
1567 | def DELETE_ATTR(self, addr, namei):
1568 | expr = self.stack.pop()
1569 | attrname = self.code.names[namei]
1570 | self.write("del {}.{}", expr, attrname)
1571 |
1572 | # SUBSCR
1573 |
1574 | def STORE_SUBSCR(self, addr):
1575 | expr, sub = self.stack.pop(2)
1576 | self.store(PySubscript(expr, sub))
1577 |
1578 | def DELETE_SUBSCR(self, addr):
1579 | expr, sub = self.stack.pop(2)
1580 | self.write("del {}[{}]", expr, sub)
1581 |
1582 | # CONST
1583 |
1584 | def LOAD_CONST(self, addr, consti):
1585 | const = self.code.consts[consti]
1586 | self.stack.push(const)
1587 |
1588 | #
1589 | # Import statements
1590 | #
1591 |
1592 | def IMPORT_NAME(self, addr, namei):
1593 | name = self.code.names[namei]
1594 | level, fromlist = self.stack.pop(2)
1595 | self.stack.push(ImportStatement(name, level, fromlist))
1596 | # special case check for import x.y.z as w syntax which uses
1597 | # attributes and assignments and is difficult to workaround
1598 | i = 1
1599 | while addr[i].opcode == LOAD_ATTR: i = i + 1
1600 | if i > 1 and addr[i].opcode in (STORE_FAST, STORE_NAME):
1601 | return addr[i]
1602 | return None
1603 |
1604 | def IMPORT_FROM(self, addr, namei):
1605 | name = self.code.names[namei]
1606 | self.stack.push(ImportFrom(name))
1607 |
1608 | def IMPORT_STAR(self, addr):
1609 | self.POP_TOP(addr)
1610 |
1611 | #
1612 | # Function call
1613 | #
1614 |
1615 | def STORE_LOCALS(self, addr):
1616 | self.stack.pop()
1617 | return addr[3]
1618 |
1619 | def LOAD_BUILD_CLASS(self, addr):
1620 | self.stack.push(self.BUILD_CLASS)
1621 |
1622 | def RETURN_VALUE(self, addr):
1623 | value = self.stack.pop()
1624 | if isinstance(value, PyConst) and value.val is None:
1625 | if addr[1] is not None:
1626 | self.write("return")
1627 | return
1628 | self.write("return {}", value)
1629 |
1630 | def YIELD_VALUE(self, addr):
1631 | if self.code.name == '':
1632 | return
1633 | value = self.stack.pop()
1634 | self.stack.push(PyYield(value))
1635 |
1636 | def YIELD_FROM(self, addr):
1637 | value = self.stack.pop() # TODO: from statement ?
1638 | value = self.stack.pop()
1639 | self.stack.push(PyYield(value))
1640 |
1641 | def CALL_FUNCTION(self, addr, argc, have_var=False, have_kw=False):
1642 | kw_argc = argc >> 8
1643 | pos_argc = argc & 0xFF
1644 | varkw = self.stack.pop() if have_kw else None
1645 | varargs = self.stack.pop() if have_var else None
1646 | kwargs_iter = iter(self.stack.pop(2 * kw_argc))
1647 | kwargs = list(zip(kwargs_iter, kwargs_iter))
1648 | posargs = self.stack.pop(pos_argc)
1649 | func = self.stack.pop()
1650 | if func is self.BUILD_CLASS:
1651 | # It's a class construction
1652 | # TODO: check the assert statement below is correct
1653 | assert not (have_var or have_kw)
1654 | func, name, *parents = posargs
1655 | self.stack.push(ClassStatement(func, name, parents, kwargs))
1656 | elif isinstance(func, PyComp):
1657 | # It's a list/set/dict comprehension or generator expression
1658 | assert not (have_var or have_kw)
1659 | assert len(posargs) == 1 and not kwargs
1660 | func.set_iterable(posargs[0])
1661 | self.stack.push(func)
1662 | elif posargs and isinstance(posargs[0], DecorableStatement):
1663 | # It's a decorator for a def/class statement
1664 | assert len(posargs) == 1 and not kwargs
1665 | defn = posargs[0]
1666 | defn.decorate(func)
1667 | self.stack.push(defn)
1668 | else:
1669 | # It's none of the above, so it must be a normal function call
1670 | func_call = PyCallFunction(func, posargs, kwargs, varargs, varkw)
1671 | self.stack.push(func_call)
1672 |
1673 | def CALL_FUNCTION_VAR(self, addr, argc):
1674 | self.CALL_FUNCTION(addr, argc, have_var=True)
1675 |
1676 | def CALL_FUNCTION_KW(self, addr, argc):
1677 | self.CALL_FUNCTION(addr, argc, have_kw=True)
1678 |
1679 | def CALL_FUNCTION_VAR_KW(self, addr, argc):
1680 | self.CALL_FUNCTION(addr, argc, have_var=True, have_kw=True)
1681 |
1682 | # a, b, ... = ...
1683 |
1684 | def UNPACK_SEQUENCE(self, addr, count):
1685 | unpack = Unpack(self.stack.pop(), count)
1686 | for i in range(count):
1687 | self.stack.push(unpack)
1688 |
1689 | def UNPACK_EX(self, addr, counts):
1690 | rcount = counts >> 8
1691 | lcount = counts & 0xFF
1692 | count = lcount + rcount + 1
1693 | unpack = Unpack(self.stack.pop(), count, lcount)
1694 | for i in range(count):
1695 | self.stack.push(unpack)
1696 |
1697 | # special case: x, y = z, t
1698 |
1699 | def ROT_TWO(self, addr):
1700 | val = PyTuple(self.stack.pop(2))
1701 | unpack = Unpack(val, 2)
1702 | self.stack.push(unpack)
1703 | self.stack.push(unpack)
1704 |
1705 | # Build operations
1706 |
1707 | def BUILD_SLICE(self, addr, argc):
1708 | assert argc in (2, 3)
1709 | self.stack.push(PySlice(self.stack.pop(argc)))
1710 |
1711 | def BUILD_TUPLE(self, addr, count):
1712 | values = [self.stack.pop() for i in range(count)]
1713 | values.reverse()
1714 | self.stack.push(PyTuple(values))
1715 |
1716 | def BUILD_LIST(self, addr, count):
1717 | values = [self.stack.pop() for i in range(count)]
1718 | values.reverse()
1719 | self.stack.push(PyList(values))
1720 |
1721 | def BUILD_SET(self, addr, count):
1722 | values = [self.stack.pop() for i in range(count)]
1723 | values.reverse()
1724 | self.stack.push(PySet(values))
1725 |
1726 | def BUILD_MAP(self, addr, count):
1727 | self.stack.push(PyDict())
1728 |
1729 | def STORE_MAP(self, addr):
1730 | v, k = self.stack.pop(2)
1731 | d = self.stack.peek()
1732 | d.set_item(k, v)
1733 |
1734 | # Comprehension operations - just create an expression statement
1735 |
1736 | def LIST_APPEND(self, addr, i):
1737 | self.POP_TOP(addr)
1738 |
1739 | def SET_ADD(self, addr, i):
1740 | self.POP_TOP(addr)
1741 |
1742 | def MAP_ADD(self, addr, i):
1743 | value, key = self.stack.pop(2)
1744 | self.stack.push(PyKeyValue(key, value))
1745 | self.POP_TOP(addr)
1746 |
1747 | # and operator
1748 |
1749 | def JUMP_IF_FALSE_OR_POP(self, addr, target):
1750 | end_addr = addr.jump()
1751 | self.push_popjump(True, end_addr, self.stack.pop())
1752 | left = self.pop_popjump()
1753 | if end_addr.opcode == ROT_TWO:
1754 | opc, arg = end_addr[-1]
1755 | if opc == JUMP_FORWARD and arg == 2:
1756 | end_addr = end_addr[2]
1757 | d = SuiteDecompiler(addr[1], end_addr, self.stack)
1758 | d.run()
1759 | right = self.stack.pop()
1760 | if isinstance(right, PyCompare) and right.extends(left):
1761 | py_and = left.chain(right)
1762 | else:
1763 | py_and = PyBooleanAnd(left, right)
1764 | self.stack.push(py_and)
1765 | return end_addr
1766 |
1767 | # This appears when there are chained comparisons, e.g. 1 <= x < 10
1768 |
1769 | def JUMP_FORWARD(self, addr, delta):
1770 | # print("*** JUMP FORWARD", addr)
1771 | ## if delta == 2 and addr[1].opcode == ROT_TWO and addr[2].opcode == POP_TOP:
1772 | ## # We're in the special case of chained comparisons
1773 | ## return addr[3]
1774 | ## else:
1775 | ## # I'm hoping its an unused JUMP in an if-else statement
1776 | ## return addr[1]
1777 | return addr.jump()
1778 |
1779 | # or operator
1780 |
1781 | def JUMP_IF_TRUE_OR_POP(self, addr, target):
1782 | end_addr = addr.jump()
1783 | self.push_popjump(True, end_addr, self.stack.pop())
1784 | left = self.pop_popjump()
1785 | d = SuiteDecompiler(addr[1], end_addr, self.stack)
1786 | d.run()
1787 | right = self.stack.pop()
1788 | self.stack.push(PyBooleanOr(left, right))
1789 | return end_addr
1790 |
1791 | #
1792 | # If-else statements/expressions and related structures
1793 | #
1794 |
1795 | def POP_JUMP_IF(self, addr, target, truthiness):
1796 | jump_addr = addr.jump()
1797 | if jump_addr.opcode == FOR_ITER:
1798 | # We are in a for-loop with nothing after the if-suite
1799 | # But take care: for-loops in generator expression do
1800 | # not end in POP_BLOCK, hence the test below.
1801 | jump_addr = jump_addr.jump()
1802 | elif jump_addr[-1].opcode == SETUP_LOOP:
1803 | # We are in a while-loop with nothing after the if-suite
1804 | jump_addr = jump_addr[-1].jump()[-1]
1805 | cond = self.stack.pop()
1806 | if not addr.is_else_jump():
1807 | self.push_popjump(truthiness, jump_addr, cond)
1808 | return
1809 | # Increase jump_addr to pop all previous jumps
1810 | self.push_popjump(truthiness, jump_addr[1], cond)
1811 | cond = self.pop_popjump()
1812 | end_true = jump_addr[-1]
1813 | if truthiness:
1814 | cond = PyNot(cond)
1815 | # - If the true clause ends in return, make sure it's included
1816 | # - If the true clause ends in RAISE_VARARGS, then it's an
1817 | # assert statement. For now I just write it as a raise within
1818 | # an if (see below)
1819 | if end_true.opcode in (RETURN_VALUE, RAISE_VARARGS):
1820 | # TODO: change
1821 | # if cond: raise AssertionError(x)
1822 | # to
1823 | # assert cond, x
1824 | d_true = SuiteDecompiler(addr[1], end_true[1])
1825 | d_true.run()
1826 | self.suite.add_statement(IfStatement(cond, d_true.suite, Suite()))
1827 | return jump_addr
1828 | d_true = SuiteDecompiler(addr[1], end_true)
1829 | d_true.run()
1830 | if jump_addr.opcode == POP_BLOCK:
1831 | # It's a while loop
1832 | stmt = WhileStatement(cond, d_true.suite)
1833 | self.suite.add_statement(stmt)
1834 | return jump_addr[1]
1835 | # It's an if-else (expression or statement)
1836 | if end_true.opcode == JUMP_FORWARD:
1837 | end_false = end_true.jump()
1838 | elif end_true.opcode == JUMP_ABSOLUTE:
1839 | end_false = end_true.jump()
1840 | if end_false.opcode == FOR_ITER:
1841 | # We are in a for-loop with nothing after the else-suite
1842 | end_false = end_false.jump()[-1]
1843 | elif end_false[-1].opcode == SETUP_LOOP:
1844 | # We are in a while-loop with nothing after the else-suite
1845 | end_false = end_false[-1].jump()[-1]
1846 | elif end_true.opcode == RETURN_VALUE:
1847 | # find the next RETURN_VALUE
1848 | end_false = jump_addr
1849 | while end_false.opcode != RETURN_VALUE:
1850 | end_false = end_false[1]
1851 | end_false = end_false[1]
1852 | elif end_true.opcode == BREAK_LOOP:
1853 | # likely in a loop in a try/except
1854 | end_false = jump_addr
1855 | else:
1856 | # normal statement
1857 | self.write("#ERROR: Unexpected statement: {} | {}\n".format(end_true, jump_addr, jump_addr[-1]))
1858 | # raise Unknown
1859 | jump_addr = end_true[-2]
1860 | stmt = IfStatement(cond, d_true.suite, None)
1861 | self.suite.add_statement(stmt)
1862 | return jump_addr or self.END_NOW
1863 | d_false = SuiteDecompiler(jump_addr, end_false)
1864 | d_false.run()
1865 | if d_true.stack and d_false.stack:
1866 | assert len(d_true.stack) == len(d_false.stack) == 1
1867 | # self.write("#ERROR: Unbalanced stacks {} != {}".format(len(d_true.stack),len(d_false.stack)))
1868 | assert not (d_true.suite or d_false.suite)
1869 | # this happens in specific if else conditions with assigments
1870 | true_expr = d_true.stack.pop()
1871 | false_expr = d_false.stack.pop()
1872 | self.stack.push(PyIfElse(cond, true_expr, false_expr))
1873 | else:
1874 | stmt = IfStatement(cond, d_true.suite, d_false.suite)
1875 | self.suite.add_statement(stmt)
1876 | return end_false or self.END_NOW
1877 |
1878 | def POP_JUMP_IF_FALSE(self, addr, target):
1879 | return self.POP_JUMP_IF(addr, target, truthiness=False)
1880 |
1881 | def POP_JUMP_IF_TRUE(self, addr, target):
1882 | return self.POP_JUMP_IF(addr, target, truthiness=True)
1883 |
1884 | def JUMP_ABSOLUTE(self, addr, target):
1885 | # print("*** JUMP ABSOLUTE ***", addr)
1886 | #return addr.jump()
1887 |
1888 | # TODO: print out continue if not final jump
1889 | jump_addr = addr.jump()
1890 | if jump_addr[-1].opcode == SETUP_LOOP:
1891 | end_addr = jump_addr + jump_addr[-1].arg
1892 | last_jump = self.scan_for_final_jump(jump_addr, end_addr[-1])
1893 | if last_jump != addr:
1894 | self.write("continue")
1895 | pass
1896 |
1897 | #
1898 | # For loops
1899 | #
1900 |
1901 | def GET_ITER(self, addr):
1902 | pass
1903 |
1904 | def FOR_ITER(self, addr, delta):
1905 | iterable = self.stack.pop()
1906 | jump_addr = addr.jump()
1907 | d_body = SuiteDecompiler(addr[1], jump_addr[-1])
1908 | for_stmt = ForStatement(iterable)
1909 | d_body.stack.push(for_stmt)
1910 | d_body.run()
1911 | for_stmt.body = d_body.suite
1912 | self.suite.add_statement(for_stmt)
1913 | return jump_addr
1914 |
1915 | # Function creation
1916 |
1917 | def MAKE_FUNCTION(self, addr, argc, is_closure=False):
1918 | testType = self.stack.pop().val
1919 | if isinstance(testType, str):
1920 | code = Code(self.stack.pop().val, self.code)
1921 | else:
1922 | code = Code(testType, self.code)
1923 | closure = self.stack.pop() if is_closure else None
1924 | # parameter annotation objects
1925 | paramobjs = {}
1926 | paramcount = (argc >> 16) & 0x7FFF
1927 | if paramcount:
1928 | paramobjs = dict(zip(self.stack.pop().val, self.stack.pop(paramcount - 1)))
1929 | # default argument objects in positional order
1930 | defaults = self.stack.pop(argc & 0xFF)
1931 | # pairs of name and default argument, with the name just below the object on the stack, for keyword-only parameters
1932 | kwdefaults = {}
1933 | for i in range((argc >> 8) & 0xFF):
1934 | k, v = self.stack.pop(2)
1935 | if hasattr(k, 'name'):
1936 | kwdefaults[k.name] = v
1937 | elif hasattr(k, 'val'):
1938 | kwdefaults[k.val] = v
1939 | else:
1940 | kwdefaults[str(k)] = v
1941 | func_maker = code_map.get(code.name, DefStatement)
1942 | self.stack.push(func_maker(code, defaults, kwdefaults, closure, paramobjs))
1943 |
1944 | def LOAD_CLOSURE(self, addr, i):
1945 | # Push the varname. It doesn't matter as it is not used for now.
1946 | self.stack.push(self.code.derefnames[i])
1947 |
1948 | def MAKE_CLOSURE(self, addr, argc):
1949 | self.MAKE_FUNCTION(addr, argc, is_closure=True)
1950 |
1951 | #
1952 | # Raising exceptions
1953 | #
1954 |
1955 | def RAISE_VARARGS(self, addr, argc):
1956 | # TODO: find out when argc is 2 or 3
1957 | # Answer: In Python 3, only 0, 1, or 2 argument (see PEP 3109)
1958 | if argc == 0:
1959 | self.write("raise")
1960 | elif argc == 1:
1961 | exception = self.stack.pop()
1962 | self.write("raise {}", exception)
1963 | elif argc == 2:
1964 | from_exc, exc = self.stack.pop(), self.stack.pop()
1965 | self.write("raise {} from {}".format(exc, from_exc))
1966 | else:
1967 | raise Unknown
1968 |
1969 | def EXTENDED_ARG(self, addr, ext):
1970 | # self.write("# ERROR: {} : {}".format(addr, ext) )
1971 | pass
1972 |
1973 | def WITH_CLEANUP(self, addr, *args, **kwargs):
1974 | # self.write("# ERROR: {} : {}".format(addr, args))
1975 | pass
1976 |
1977 | # Create unary operators types and opcode handlers
1978 | for op, name, ptn, prec in unary_ops:
1979 | name = 'Py' + name
1980 | tp = type(name, (PyUnaryOp,), dict(pattern=ptn, precedence=prec))
1981 | globals()[name] = tp
1982 |
1983 | def method(self, addr, tp=tp):
1984 | tp.instr(self.stack)
1985 |
1986 | setattr(SuiteDecompiler, op, method)
1987 |
1988 | # Create binary operators types and opcode handlers
1989 | for op, name, ptn, prec, inplace_ptn in binary_ops:
1990 | # Create the binary operator
1991 | tp_name = 'Py' + name
1992 | tp = globals().get(tp_name, None)
1993 | if tp is None:
1994 | tp = type(tp_name, (PyBinaryOp,), dict(pattern=ptn, precedence=prec))
1995 | globals()[tp_name] = tp
1996 |
1997 | def method(self, addr, tp=tp):
1998 | tp.instr(self.stack)
1999 |
2000 | setattr(SuiteDecompiler, 'BINARY_' + op, method)
2001 | # Create the in-place operation
2002 | if inplace_ptn is not None:
2003 | inplace_op = "INPLACE_" + op
2004 | tp_name = 'InPlace' + name
2005 | tp = type(tp_name, (InPlaceOp,), dict(pattern=inplace_ptn))
2006 | globals()[tp_name] = tp
2007 |
2008 | def method(self, addr, tp=tp):
2009 | left, right = self.stack.pop(2)
2010 | self.stack.push(tp(left, right))
2011 |
2012 | setattr(SuiteDecompiler, inplace_op, method)
2013 |
2014 | if __name__ == "__main__":
2015 | import sys
2016 |
2017 | if len(sys.argv) == 1:
2018 | print('USAGE: {} '.format(sys.argv[0]))
2019 | else:
2020 | print(decompile(sys.argv[1]))
--------------------------------------------------------------------------------
/unpyc3_tests.py:
--------------------------------------------------------------------------------
1 |
2 | class TestClass:
3 |
4 | def test_func1(x):
5 | x = x*(f(x) + 1 - x[1])
6 | x = (y, [z, t]), {1, 2, 3}
7 | t = {1:[x, y], 3:'x'}
8 | a.x.y, b[2] = b, a
9 | t = 1 <= x < 2 < y
10 | g(x, y + 1, x=12)
11 | x = a and (b or c)
12 | y = 1 if x else 2
13 | z = 1 if not x else 2
14 | a = b = 3
15 | x[y.z] = a, b = u
16 | if x:
17 | f(x)
18 | del x
19 | else:
20 | g(x)
21 | h[y] = 3
22 | if y:
23 | foo()
24 | if x:
25 | a()
26 | if z:
27 | a1()
28 | else:
29 | a2()
30 | b()
31 | elif y:
32 | b()
33 | else:
34 | c()
35 | x = a and b or c
36 | return "hello"
37 | def test_func2():
38 | if a and ((b and c and d) or e or f) and g: g()
39 | if a or (b and (c1 or c2) and d) or e: g()
40 | if a and b or c: g()
41 | if a or b and c: g()
42 | if a and (b or c): g()
43 | def test_func3():
44 | x = a and b or c
45 | x = a and (b1 or b2) and c or c
46 | x = (a and b) + (c or (not d and e))
47 | def test_func4():
48 | def f(x, y=2):
49 | return x + y if x else x - y
50 | g = lambda x: x + 1
51 | def test_func5(x):
52 | x += 2
53 | x[3] *= 10
54 | def test_func6(x):
55 | while f(x):
56 | if x and y:
57 | g(x)
58 | else:
59 | x + 2
60 | x += f(x, y=2)
61 | while a and b:
62 | while c and d:
63 | print(a, c)
64 | def test_func7(x):
65 | for i in x:
66 | print(i)
67 | for a, b in x:
68 | for c, (d, e) in a:
69 | print(a + c)
70 | def test_func8(x):
71 | for i in x:
72 | if i == 2:
73 | f()
74 | else:
75 | g()
76 | for i in x:
77 | if i:
78 | break
79 | while x:
80 | if x:
81 | f()
82 | def test_func9():
83 | try:
84 | x = 1
85 | except A:
86 | x = 2
87 | except B as b:
88 | x = 3
89 | try:
90 | x = 2
91 | y = 3
92 | except A:
93 | x = 5
94 | finally:
95 | z = 2
96 | try:
97 | frobz()
98 | except:
99 | bar()
100 | finally:
101 | frobn()
102 | def test_func10(fname):
103 | with open(fname) as f:
104 | for line in f:
105 | print(line)
106 | with x as y, s as t:
107 | bar()
108 | def test_func11():
109 | l = [x for x in y for z in x]
110 | l1 = [x for x in y if f(x)]
111 | s = {x + 1 for x, y in T}
112 | d = {x: y for x, y in f(a)}
113 | def test_func12():
114 | class A:
115 | def f(self): return 1
116 | class B(A, metaclass=MyType):
117 | bar = 12
118 | def __init__(self, x):
119 | self.x = x
120 | def test_func13():
121 | g = (x for x in y)
122 | f(y - 2 for x in S for y in f(x))
123 | def test_func14():
124 | def g(x):
125 | for i in x:
126 | yield f(i) + 2
127 | a = yield 5
128 | b = 1 + (yield 12)
129 | def test_func15(x, y):
130 | def f(z):
131 | return z + x
132 | def g(z):
133 | global x
134 | return z + x
135 | def h(z):
136 | nonlocal x
137 | x = 12
138 | def test_func16():
139 | if a:
140 | return
141 | if b:
142 | foo()
143 | if c:
144 | return
145 | #foo = SuiteDecompiler.POP_JUMP_IF
146 | def test_func17():
147 | if a:
148 | if b:
149 | f()
150 | elif c:
151 | g()
152 | def test_func18():
153 | if a:
154 | if b:
155 | f()
156 | elif c:
157 | g()
158 | def test_func19():
159 | assert a, b
160 | def test_func20():
161 | assert a
162 | def test_func21():
163 | raise
164 | def test_func22():
165 | @decorate
166 | def f(): pass
167 | @foo
168 | @bar.baz(3)
169 | class A: pass
170 | def test_func23():
171 | class B(A):
172 | def foo(): pass
173 | def bar(): pass
174 |
175 | def test_func24():
176 | c = 2
177 | while 1:
178 | if a:
179 | break
180 | if b:
181 | continue
182 | c = 1
183 |
184 | def test_func25():
185 | c = 2
186 | while not 1:
187 | if a:
188 | break
189 | if b:
190 | continue
191 | c = 1
192 |
193 | def test_func26():
194 | c = 2
195 | while c:
196 | if a:
197 | break
198 | if b:
199 | continue
200 | c = 1
201 |
202 | def test_func27():
203 | c = 2
204 | while c:
205 | if a:
206 | break
207 | if b:
208 | continue
209 | c = 1
210 |
211 | def test_func28():
212 | c = 2
213 | while 1:
214 | if a:
215 | break
216 | if b:
217 | continue
218 | if c == '\b':
219 | pw = pw[:-1]
220 | else:
221 | pw = pw + c
222 | #c = 1
223 |
224 | def test_func29():
225 | c = 2
226 | while 1:
227 | if a:
228 | break
229 | if b:
230 | continue
231 | if c == '\b':
232 | pw = pw[:-1]
233 | else:
234 | pw = pw + c
235 | c = 1
236 |
237 | def test_func30():
238 | result_set.update(((self._res_id_group_map.get(r, 0), r) for r in res_dict))
239 |
240 | if __name__ == "__main__":
241 | import unpyc3
242 | import sys
243 |
244 | if len(sys.argv) == 1:
245 | import types
246 | import difflib
247 | # run through and compile all functions
248 | for k,func in TestClass.__dict__.items():
249 | if isinstance(func,types.FunctionType):
250 | code = unpyc3.Code(func.__code__)
251 | source = str(code.get_suite(include_declarations=False, look_for_docstring=True))
252 | #compiled = compile(source, '', 'exec')
253 | # now
254 | import unpyc3_tests
255 | code = unpyc3.decompile(unpyc3_tests)
256 | compiled = compile(str(code), '', 'exec')
257 | code2 = unpyc3.decompile(compiled)
258 | # d = difflib.Differ()
259 | # result = list(d.compare(str(code), str(code2)))
260 | # from pprint import pprint
261 | # pprint(result)
262 | diff = difflib.unified_diff(str(code), str(code2), fromfile='original', tofile='converted')
263 | sys.stdout.writelines(diff)
264 | else:
265 | pass
266 | #compiled2 = compile(str(code2), '', 'exec')
267 |
268 |
--------------------------------------------------------------------------------