shedskin-0.9.4/0000775000175000017500000000000012157270661013261 5ustar srepmubsrepmubshedskin-0.9.4/LICENSE0000664000175000017500000011721412157270661014274 0ustar srepmubsrepmub This package is comprised of two parts: - the Shed Skin compiler (shedskin/*.py); License GNU GPL version 3 - a C++ implementation of the Python builtins and libraries (everything under shedskin/lib/); License Expat (with the exception of shedskin/lib/random.?pp, license BSD-3, see below) Concerning the Shed Skin compiler (shedskin/*.py): GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . Concerning the C++ implementation of Python builtins and libraries (everything under shedskin/lib/, with some exceptions, see below): Expat License Copyright (c) 2005-2011 Mark Dufour and contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Concerning the C++ implementation of Python itertools and heapq libraries (shedskin/lib/(itertools|heapq).?pp): Expat License Copyright (c) 2009 Jérémie Roquet Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Concerning the C++ implementation of the Python random library (shedskin/lib/random.?pp): BSD-3 License Copyright 1997-2002 Makoto Matsumoto, Takuji Nishimura =========================== Source Notes ============================== Translated by Guido van Rossum from C source provided by Adrian Baddeley. Adapted by Raymond Hettinger for use with the Mersenne Twister core generator. Adapted by Jeff Miller for compatibility with the Shed Skin Python-to-C++ compiler. Mersenne Twister was converted to Python by Jeff Miller 2007-02-03 jwmillerusa (at) gmail (dot) com http://millerideas.com Below are the original comments from the authors' C source file. ======================================================================= A C-program for MT19937, with initialization improved 2002/1/26. Coded by Takuji Nishimura and Makoto Matsumoto. Before using, initialize the state by using init_genrand(seed) or init_by_array(init_key, key_length). Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Any feedback is very welcome. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) shedskin-0.9.4/scripts/0000775000175000017500000000000012157270661014750 5ustar srepmubsrepmubshedskin-0.9.4/scripts/shedskin0000644000175000017500000000006612157270661016503 0ustar srepmubsrepmub#!/usr/bin/env python import shedskin shedskin.main() shedskin-0.9.4/shedskin/0000775000175000017500000000000012157270661015071 5ustar srepmubsrepmubshedskin-0.9.4/shedskin/python.py0000664000175000017500000003062112157270661016766 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) ''' import imp import os import re import sys from compiler import parse from compiler.ast import AssTuple, AssList, List, Tuple, CallFunc, Name, \ Const, UnaryAdd, UnarySub, Getattr class Module(object): def __init__(self, name, filename, relative_filename, builtin, node): #set name and its dependent fields self.name = name self.name_list = name.split('.') self.ident = self.name_list[-1] #set filename and its dependent fields self.filename = filename self.path = os.path.dirname(filename) self.relative_filename = relative_filename self.relative_path = os.path.dirname(relative_filename) #set the rest self.builtin = builtin self.node = node self.prop_includes = set() self.import_order = 0 def full_path(self): return '__' + '__::__'.join(self.name_list) + '__' def include_path(self): if self.relative_filename.endswith('__init__.py'): return os.path.join(self.relative_path, '__init__.hpp') else: filename_without_ext = os.path.splitext(self.relative_filename)[0] return filename_without_ext + '.hpp' def in_globals(self, ident): return ident in self.mv.globals \ or ident in self.mv.funcs \ or ident in self.mv.ext_funcs \ or ident in self.mv.classes \ or ident in self.mv.ext_classes def __repr__(self): return 'Module ' + self.ident class Class(object): def __init__(self, gx, node, mv): self.gx = gx self.node = node self.mv = mv self.ident = node.name self.bases = [] self.children = [] self.dcpa = 1 self.vars = {} self.funcs = {} self.virtuals = {} # 'virtually' called methods self.virtualvars = {} # 'virtual' variables self.properties = {} self.staticmethods = [] self.typenr = self.gx.nrcltypes self.gx.nrcltypes += 1 self.splits = {} # contour: old contour (used between iterations) self.has_copy = self.has_deepcopy = False self.def_order = self.gx.class_def_order self.gx.class_def_order += 1 def ancestors(self, inclusive=False): # XXX attribute (faster) a = set(self.bases) changed = 1 while changed: changed = 0 for cl in a.copy(): if set(cl.bases) - a: changed = 1 a.update(cl.bases) if inclusive: a.add(self) return a def ancestors_upto(self, other): a = self result = [] while a != other: result.append(a) if not a.bases: break a = a.bases[0] return result def descendants(self, inclusive=False): # XXX attribute (faster) a = set() if inclusive: a.add(self) for cl in self.children: a.add(cl) a.update(cl.descendants()) return a def tvar_names(self): if self.mv.module.builtin: if self.ident in ['list', 'tuple', 'frozenset', 'set', 'frozenset', 'deque', '__iter', 'pyseq', 'pyiter', 'pyset', 'array']: return ['unit'] elif self.ident in ['dict', 'defaultdict']: return ['unit', 'value'] elif self.ident == 'tuple2': return ['first', 'second'] return [] def __repr__(self): return 'class ' + self.ident class StaticClass(object): def __init__(self, cl, mv): self.vars = {} self.static_nodes = [] self.funcs = {} self.ident = cl.ident self.parent = None self.mv = mv self.module = cl.module def __repr__(self): return 'static class ' + self.ident class Function(object): def __init__(self, gx, node=None, parent=None, inherited_from=None, mv=None): self.gx = gx self.node = node self.inherited_from = inherited_from if node: ident = node.name if inherited_from and ident in parent.funcs: ident += inherited_from.ident + '__' # XXX ugly self.ident = ident self.formals = node.argnames self.flags = node.flags self.doc = node.doc self.returnexpr = [] self.retnode = None self.lambdanr = None self.lambdawrapper = False self.parent = parent self.constraints = set() self.vars = {} self.globals = [] self.mv = mv self.lnodes = [] self.nodes = set() self.nodes_ordered = [] self.defaults = [] self.misses = set() self.cp = {} self.xargs = {} self.largs = None self.listcomp = False self.isGenerator = False self.yieldNodes = [] self.tvars = set() self.ftypes = [] # function is called via a virtual call: arguments may have to be cast self.inherited = None if node: self.gx.allfuncs.add(self) self.retvars = [] self.invisible = False self.fakeret = None self.declared = False self.registered = [] self.registered_temp_vars = [] def __repr__(self): if self.parent: return 'Function ' + repr((self.parent, self.ident)) return 'Function ' + self.ident class Variable(object): def __init__(self, name, parent): self.name = name self.parent = parent self.invisible = False # not in C++ output self.formal_arg = False self.imported = False self.registered = False self.looper = None self.wopper = None self.const_assign = [] def masks_global(self): if isinstance(self.parent, Class): mv = self.parent.mv if not mv.module.builtin and mv.module.in_globals(self.name): return True return False def __repr__(self): if self.parent: return repr((self.parent, self.name)) return self.name def clear_block(m): return m.string.count('\n', m.start(), m.end()) * '\n' def parse_file(name): # Convert block comments into strings which will be duely ignored. pat = re.compile(r"#{.*?#}[^\r\n]*$", re.MULTILINE | re.DOTALL) filebuf = re.sub(pat, clear_block, ''.join(open(name, 'U').readlines())) try: return parse(filebuf) except SyntaxError, s: print '*ERROR* %s:%d: %s' % (name, s.lineno, s.msg) sys.exit(1) def find_module(gx, name, paths): if '.' in name: name, module_name = name.rsplit('.', 1) name_as_path = name.replace('.', os.path.sep) import_paths = [os.path.join(path, name_as_path) for path in paths] else: module_name = name import_paths = paths _, filename, description = imp.find_module(module_name, import_paths) filename = os.path.splitext(filename)[0] absolute_import_paths = gx.libdirs + [os.getcwd()] absolute_import_path = next( path for path in absolute_import_paths if filename.startswith(path) ) relative_filename = os.path.relpath(filename, absolute_import_path) absolute_name = relative_filename.replace(os.path.sep, '.') builtin = absolute_import_path in gx.libdirs is_a_package = description[2] == imp.PKG_DIRECTORY if is_a_package: filename = os.path.join(filename, '__init__.py') relative_filename = os.path.join(relative_filename, '__init__.py') else: filename = filename + '.py' relative_filename = relative_filename + '.py' return absolute_name, filename, relative_filename, builtin # XXX ugly: find ancestor class that implements function 'ident' def lookup_implementor(cl, ident): while cl: if ident in cl.funcs and not cl.funcs[ident].inherited: return cl.ident if cl.bases: cl = cl.bases[0] else: break return None def lookup_class_module(objexpr, mv, parent): if isinstance(objexpr, Name): # XXX Getattr? var = lookup_var(objexpr.name, parent, mv=mv) if var and not var.imported: # XXX cl? return None, None return lookup_class(objexpr, mv), lookup_module(objexpr, mv) def lookup_func(node, mv): # XXX lookup_var first? if isinstance(node, Name): if node.name in mv.funcs: return mv.funcs[node.name] elif node.name in mv.ext_funcs: return mv.ext_funcs[node.name] else: return None elif isinstance(node, Getattr): module = lookup_module(node.expr, mv) if module and node.attrname in module.mv.funcs: return module.mv.funcs[node.attrname] def lookup_class(node, mv): # XXX lookup_var first? if isinstance(node, Name): if node.name in mv.classes: return mv.classes[node.name] elif node.name in mv.ext_classes: return mv.ext_classes[node.name] else: return None elif isinstance(node, Getattr): module = lookup_module(node.expr, mv) if module and node.attrname in module.mv.classes: return module.mv.classes[node.attrname] def lookup_module(node, mv): path = [] imports = mv.imports while isinstance(node, Getattr): path = [node.attrname] + path node = node.expr if isinstance(node, Name): path = [node.name] + path # --- search import chain for ident in path: if ident in imports: module = imports[ident] imports = module.mv.imports else: return None return module def def_class(gx, name, mv=None): if mv is None: mv = gx.modules['builtin'].mv if name in mv.classes: return mv.classes[name] elif name in mv.ext_classes: return mv.ext_classes[name] def lookup_var(name, parent, local=False, mv=None): if not local and isinstance(parent, Class) and name in parent.parent.vars: # XXX return parent.parent.vars[name] elif parent and name in parent.vars: return parent.vars[name] elif not (parent and local): # recursive lookup chain = [] while isinstance(parent, Function): if name in parent.vars: for ancestor in chain: if isinstance(ancestor, Function): # XXX optimize ancestor.misses.add(name) return parent.vars[name] chain.append(parent) parent = parent.parent # not found: global if name in mv.globals: return mv.globals[name] def subclass(a, b): if b in a.bases: return True else: return a.bases and subclass(a.bases[0], b) # XXX mult inh def is_property_setter(dec): return isinstance(dec, Getattr) and isinstance(dec.expr, Name) and dec.attrname == 'setter' def is_literal(node): if isinstance(node, (UnarySub, UnaryAdd)): node = node.expr return isinstance(node, Const) and isinstance(node.value, (int, float)) def is_fastfor(node): return isinstance(node.list, CallFunc) and isinstance(node.list.node, Name) and node.list.node.name in ['range', 'xrange'] def is_method(parent): return isinstance(parent, Function) and isinstance(parent.parent, Class) def is_enum(node): return isinstance(node.list, CallFunc) and isinstance(node.list.node, Name) and node.list.node.name == 'enumerate' and len(node.list.args) == 1 and isinstance(node.assign, (AssList, AssTuple)) def is_zip2(node): return isinstance(node.list, CallFunc) and isinstance(node.list.node, Name) and node.list.node.name == 'zip' and len(node.list.args) == 2 and isinstance(node.assign, (AssList, AssTuple)) # --- recursively determine (lvalue, rvalue) pairs in assignment expressions def assign_rec(left, right): if isinstance(left, (AssTuple, AssList)) and isinstance(right, (Tuple, List)): pairs = [] for (lvalue, rvalue) in zip(left.getChildNodes(), right.getChildNodes()): pairs += assign_rec(lvalue, rvalue) return pairs else: return [(left, right)] def aug_msg(node, msg): if hasattr(node, 'augment'): return '__i' + msg + '__' return '__' + msg + '__' shedskin-0.9.4/shedskin/makefile.py0000664000175000017500000001562112157270661017225 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) makefile.py: generate makefile ''' import os import sys from distutils import sysconfig def generate_makefile(gx): if sys.platform == 'win32': pyver = '%d%d' % sys.version_info[:2] prefix = sysconfig.get_config_var('prefix') else: pyver = sysconfig.get_config_var('VERSION') or sysconfig.get_python_version() includes = '-I' + sysconfig.get_python_inc() + ' ' if not gx.pypy: includes += '-I' + sysconfig.get_python_inc(plat_specific=True) if sys.platform == 'darwin': ldflags = sysconfig.get_config_var('BASECFLAGS') else: ldflags = (sysconfig.get_config_var('LIBS') or '') + ' ' ldflags += (sysconfig.get_config_var('SYSLIBS') or '') + ' ' if not gx.pypy: ldflags += '-lpython' + pyver if not sysconfig.get_config_var('Py_ENABLE_SHARED'): ldflags += ' -L' + sysconfig.get_config_var('LIBPL') ident = gx.main_module.ident if gx.extension_module: if sys.platform == 'win32': ident += '.pyd' else: ident += '.so' makefile = file(gx.makefile_name, 'w') if gx.msvc: esc_space = '/ ' def env_var(name): return '$(%s)' % name else: esc_space = '\ ' def env_var(name): return '${%s}' % name libdirs = [d.replace(' ', esc_space) for d in gx.libdirs] print >>makefile, 'SHEDSKIN_LIBDIR=%s' % (libdirs[-1]) filenames = [] modules = gx.modules.values() for module in modules: filename = os.path.splitext(module.filename)[0] # strip .py filename = filename.replace(' ', esc_space) # make paths valid filename = filename.replace(libdirs[-1], env_var('SHEDSKIN_LIBDIR')) filenames.append(filename) cppfiles = [fn + '.cpp' for fn in filenames] hppfiles = [fn + '.hpp' for fn in filenames] for always in ('re',): repath = os.path.join(env_var('SHEDSKIN_LIBDIR'), always) if not repath in filenames: cppfiles.append(repath + '.cpp') hppfiles.append(repath + '.hpp') cppfiles.sort(reverse=True) hppfiles.sort(reverse=True) cppfiles = ' \\\n\t'.join(cppfiles) hppfiles = ' \\\n\t'.join(hppfiles) # import flags if gx.flags: flags = gx.flags elif os.path.isfile('FLAGS'): flags = 'FLAGS' elif os.path.isfile('/etc/shedskin/FLAGS'): flags = '/etc/shedskin/FLAGS' elif gx.msvc: flags = os.path.join(gx.sysdir, 'FLAGS.msvc') elif sys.platform == 'win32': flags = os.path.join(gx.sysdir, 'FLAGS.mingw') elif sys.platform == 'darwin': flags = os.path.join(gx.sysdir, 'FLAGS.osx') else: flags = os.path.join(gx.sysdir, 'FLAGS') for line in file(flags): line = line[:-1] variable = line[:line.find('=')].strip() if variable == 'CCFLAGS': line += ' -I. -I%s' % env_var('SHEDSKIN_LIBDIR') line += ''.join(' -I' + libdir for libdir in libdirs[:-1]) if sys.platform == 'darwin' and os.path.isdir('/usr/local/include'): line += ' -I/usr/local/include' # XXX if sys.platform == 'darwin' and os.path.isdir('/opt/local/include'): line += ' -I/opt/local/include' # XXX if not gx.wrap_around_check: line += ' -D__SS_NOWRAP' if not gx.bounds_checking: line += ' -D__SS_NOBOUNDS' if gx.fast_random: line += ' -D__SS_FASTRANDOM' if gx.gc_cleanup: line += ' -D__SS_GC_CLEANUP' if not gx.assertions: line += ' -D__SS_NOASSERT' if gx.fast_hash: line += ' -D__SS_FASTHASH' if gx.longlong: line += ' -D__SS_LONG' if gx.backtrace: line += ' -D__SS_BACKTRACE -rdynamic -fno-inline' if gx.pypy: line += ' -D__SS_PYPY' if not gx.gcwarns: line += ' -D__SS_NOGCWARNS' if gx.extension_module: if sys.platform == 'win32': line += ' -I%s\\include -D__SS_BIND' % prefix else: line += ' -g -fPIC -D__SS_BIND ' + includes elif variable == 'LFLAGS': if sys.platform == 'darwin' and os.path.isdir('/opt/local/lib'): # XXX line += ' -L/opt/local/lib' if sys.platform == 'darwin' and os.path.isdir('/usr/local/lib'): # XXX line += ' -L/usr/local/lib' if gx.extension_module: if gx.msvc: line += ' /dll /libpath:%s\\libs ' % prefix elif sys.platform == 'win32': line += ' -shared -L%s\\libs -lpython%s' % (prefix, pyver) elif sys.platform == 'darwin': line += ' -bundle -undefined dynamic_lookup ' + ldflags elif sys.platform == 'sunos5': line += ' -shared -Xlinker ' + ldflags else: line += ' -shared -Xlinker -export-dynamic ' + ldflags if 'socket' in (m.ident for m in modules): if sys.platform == 'win32': line += ' -lws2_32' elif sys.platform == 'sunos5': line += ' -lsocket -lnsl' if 'os' in (m.ident for m in modules): if sys.platform not in ['win32', 'darwin', 'sunos5']: line += ' -lutil' if 'hashlib' in (m.ident for m in modules): line += ' -lcrypto' print >>makefile, line print >>makefile print >>makefile, 'CPPFILES=%s\n' % cppfiles print >>makefile, 'HPPFILES=%s\n' % hppfiles print >>makefile, 'all:\t' + ident + '\n' # executable (normal, debug, profile) or extension module _out = '-o ' _ext = '' targets = [('', '')] if not gx.extension_module: targets += [('_prof', '-pg -ggdb'), ('_debug', '-g -ggdb')] if gx.msvc: _out = '/out:' _ext = '' targets = [('', '')] if not gx.extension_module: _ext = '.exe' for suffix, options in targets: print >>makefile, ident + suffix + ':\t$(CPPFILES) $(HPPFILES)' print >>makefile, '\t$(CC) ' + options + ' $(CCFLAGS) $(CPPFILES) $(LFLAGS) ' + _out + ident + suffix + _ext + '\n' # clean ext = '' if sys.platform == 'win32' and not gx.extension_module: ext = '.exe' print >>makefile, 'clean:' targets = [ident + ext] if not gx.extension_module: if not gx.msvc: targets += [ident + '_prof' + ext, ident + '_debug' + ext] print >>makefile, '\trm -f %s\n' % ' '.join(targets) # phony print >>makefile, '.PHONY: all clean\n' makefile.close() shedskin-0.9.4/shedskin/FLAGS.msvc0000664000175000017500000000064212157270661016621 0ustar srepmubsrepmubCC=cl CCFLAGS=$(CPPFLAGS) /MP /O2 /EHsc /TP /MD /Zi /nologo /W0 /Oi /Oy- /GL /Gm- /GS /fp:precise /Zc:wchar_t /Zc:forScope /D "and"="&&" /D "or"="||" /D "not"="!" /D "WIN32" /D "_CONSOLE" /D "ALL_INTERIOR_POINTERS" /D "GC_NOT_DLL" /D "_UNICODE" /D "PCRE_STATIC" /I$(EXTRA_INCLUDE) LFLAGS= $(LDFLAGS) /link /libpath:$(EXTRA_LIB) gc.lib pcre.lib user32.lib /NODEFAULTLIB:"libcmt" /subsystem:console /OPT:REF /OPT:ICF shedskin-0.9.4/shedskin/virtual.py0000664000175000017500000001216412157270661017135 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) virtual.py: virtual methods and variables ''' from compiler.ast import CallFunc, Name import infer from python import subclass, Class from typestr import lowest_common_parents, typestr, polymorphic_t def virtuals(self, cl, declare): if not cl.virtuals: return for ident, subclasses in cl.virtuals.items(): if not subclasses: continue if ident in cl.funcs and infer.called(cl.funcs[ident]): subclasses = subclasses.copy() subclasses.add(cl) # --- merge arg/return types formals = [] retexpr = False for subcl in subclasses: if ident not in subcl.funcs: continue func = subcl.funcs[ident] sig_types = [] if func.returnexpr: retexpr = True if func.retnode.thing in self.mergeinh: sig_types.append(self.mergeinh[func.retnode.thing]) # XXX mult returns; some targets with return some without.. else: sig_types.append(set()) # XXX for name in func.formals[1:]: var = func.vars[name] sig_types.append(self.mergeinh[var]) formals.append(sig_types) merged = [] for z in zip(*formals): merge = set() for _types in z: merge.update(_types) merged.append(merge) formals = [] subcl0 = list(subclasses)[0] if ident in subcl0.funcs: formals = list(subclasses)[0].funcs[ident].formals[1:] ftypes = [] for m in merged: ts = typestr(self.gx, m, mv=self.mv) if not ts.endswith('*'): ftypes.append(ts + ' ') else: ftypes.append(ts) # --- prepare for having to cast back arguments (virtual function call means multiple targets) for subcl in subclasses: if ident in subcl.funcs: subcl.funcs[ident].ftypes = ftypes # --- virtual function declaration if declare: self.start('virtual ') if retexpr and ftypes: self.append(ftypes[0]) ftypes = ftypes[1:] else: self.append('void ') self.append(self.cpp_name(ident) + '(') self.append(', '.join(t + f for (t, f) in zip(ftypes, formals))) if ident in cl.funcs and self.inhcpa(cl.funcs[ident]): self.eol(')') else: if merged: self.eol(') { return %s; }' % self.nothing(merged[0])) # XXX msvc needs return statement else: self.eol(') { }') # XXX merged may be empty because of dynamic typing if ident in cl.funcs: cl.funcs[ident].declared = True # --- determine virtual methods and variables def analyze_virtuals(gx): for node in gx.merged_inh: # --- for every message if isinstance(node, CallFunc) and not infer.inode(gx, node).mv.module.builtin: # ident == 'builtin': objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func = infer.analyze_callfunc(gx, node, merge=gx.merged_inh) if not method_call or objexpr not in gx.merged_inh: continue # XXX # --- determine abstract receiver class classes = polymorphic_t(gx, gx.merged_inh[objexpr]) classes = [cl for cl in classes if isinstance(cl, Class)] if not classes: continue if isinstance(objexpr, Name) and objexpr.name == 'self' and infer.inode(gx, objexpr).parent: abstract_cl = infer.inode(gx, objexpr).parent.parent upgrade_cl(gx, abstract_cl, node, ident, classes) lcp = lowest_common_parents(classes) if lcp: upgrade_cl(gx, lcp[0], node, ident, classes) def upgrade_cl(gx, abstract_cl, node, ident, classes): if not abstract_cl or not isinstance(abstract_cl, Class): return subclasses = [cl for cl in classes if subclass(cl, abstract_cl)] # --- register virtual method if not ident.startswith('__'): redefined = False for concrete_cl in classes: if [cl for cl in concrete_cl.ancestors_upto(abstract_cl) if ident in cl.funcs and not cl.funcs[ident].inherited]: redefined = True if redefined: abstract_cl.virtuals.setdefault(ident, set()).update(subclasses) # --- register virtual var elif ident in ['__getattr__', '__setattr__'] and subclasses: var = infer.default_var(gx, node.args[0].value, abstract_cl) for subcl in subclasses: if var.name in subcl.vars and subcl.vars[var.name] in gx.merged_inh: gx.types.setdefault(gx.cnode[var, 0, 0], set()).update(gx.merged_inh[subcl.vars[var.name]]) # XXX shouldn't this be merged automatically already? abstract_cl.virtualvars.setdefault(node.args[0].value, set()).update(subclasses) shedskin-0.9.4/shedskin/lib/0000775000175000017500000000000012157270661015637 5ustar srepmubsrepmubshedskin-0.9.4/shedskin/lib/glob.hpp0000664000175000017500000000123512157270661017274 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __GLOB_HPP #define __GLOB_HPP #include "builtin.hpp" #include "os/path.hpp" #include "fnmatch.hpp" #include "re.hpp" #include "os/__init__.hpp" using namespace __shedskin__; namespace __glob__ { extern str *const_0, *const_2, *const_3; extern str *__name__; extern __re__::re_object *magic_check; list *glob(str *pathname); __iter *iglob(str *pathname); list *glob1(str *dirname, str *pattern); list *glob0(str *dirname, str *basename); __ss_bool has_magic(str *s); void __init(void); } // module namespace #endif shedskin-0.9.4/shedskin/lib/cStringIO.cpp0000664000175000017500000000234412157270661020207 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "cStringIO.hpp" #include namespace __cStringIO__ { str* StringI::read(int n) { __check_closed(); str *result; if(n < 0) { result = s->__slice__(1, pos, 0, 0); pos = len(s); } else { result = s->__slice__(3, pos, pos + n, 0); pos = std::min(pos + n, len(s)); } return result; } str * StringI::readline(int n) { __check_closed(); if(__eof()) return new str(""); size_t nl = s->unit.find('\n', pos); if(nl != std::string::npos) { int tbr = nl - pos + 1; return read(n < 0 ? tbr : std::min(tbr, n)); } else { return read(n); } } void *StringI::seek(__ss_int i, __ss_int w) { __check_closed(); if(w==0) pos = i; else if(w==1) pos += i; else pos = len(s)+i; return NULL; } void *StringI::write(str *data) { __check_closed(); if(data) { const size_t size = data->unit.size(); s->unit.insert(pos, data->unit); pos += size; s->unit.erase(pos, size); } return 0; } StringI *StringIO(str *s) { return (new StringI(s)); } void __init() { } } // module namespace shedskin-0.9.4/shedskin/lib/heapq.hpp0000664000175000017500000002103312157270661017445 0ustar srepmubsrepmub/* Copyright (c) 2009 Jérémie Roquet ; License Expat (See LICENSE) */ #ifndef HEAPQ_HPP #define HEAPQ_HPP #include "builtin.hpp" #include using namespace __shedskin__; namespace __heapq__ { /* Local helpers */ template struct Cmp { inline __ss_int operator()(T& first, T& second) const { return __cmp(first, second); } }; template struct InvCmp { inline __ss_int operator()(T& first, T& second) const { return -__cmp(first, second); } }; template struct CmpSecond { inline __ss_int operator()(T& first, T& second) const { return __cmp(first.second, second.second); } }; template class X, template class Cmp> inline void _siftdown(X& heap, __ss_int startpos, __ss_int pos) { assert((size_t)startpos < heap.size()); assert((size_t)pos < heap.size()); Cmp cmp; T item = heap[pos]; while (pos > startpos) { __ss_int parentpos = (pos - 1) / 2; T parent = heap[parentpos]; if (cmp(item, parent) >= 0) { break; } heap[pos] = parent; pos = parentpos; } heap[pos] = item; } template class X> inline void _siftdown(X& heap, __ss_int startpos, __ss_int pos) { _siftdown(heap, startpos, pos); } template inline void _siftdown(list *heap, __ss_int startpos, __ss_int pos) { _siftdown(heap->units, startpos, pos); } template class X, template class Cmp> inline void _siftup(X& heap, __ss_int pos) { assert((size_t)pos < heap.size()); Cmp cmp; __ss_int startpos = pos; __ss_int endpos = heap.size(); T item = heap[pos]; for (;;) { __ss_int leftsonpos = 2 * pos + 1; __ss_int rightsonpos = leftsonpos + 1; if (leftsonpos >= endpos) { break; } else if (rightsonpos < endpos) { if (cmp(heap[leftsonpos], heap[rightsonpos]) >= 0) { leftsonpos = rightsonpos; } } heap[pos] = heap[leftsonpos]; pos = leftsonpos; } heap[pos] = item; _siftdown(heap, startpos, pos); } template class X> inline void _siftup(X& heap, __ss_int pos) { _siftup(heap, pos); } template inline void _siftup(list *heap, __ss_int pos) { _siftup(heap->units, pos); } /* Basic operations */ template class X, template class Cmp> inline void heappush(X& heap, T item) { heap.push_back(item); _siftdown(heap, 0, heap.size() - 1); } template class X> inline void heappush(X& heap, T item) { heappush(heap, item); } template inline void heappush(list *heap, T item) { heappush(heap->units, item); } template class X, template class Cmp> T heappop(X& heap) { T item = heap.front(); heap[0] = heap.back(); _siftup(heap, 0); heap.pop_back(); return item; } template class X> T heappop(X& heap) { return heappop(heap); } template inline T heappop(list *heap) { return heappop(heap->units); } template class X, template class Cmp> T heappushpop(X& heap, T item) { Cmp cmp; if (!heap.size() || cmp(item, heap.front()) < 0) { return item; } T item2 = heap[0]; heap[0] = item; _siftup(heap, 0); return item2; } template class X> T heappushpop(X& heap, T item) { return heappushpop(heap, item); } template inline T heappushpop(list *heap, T item) { return heappushpop(heap->units, item); } template class X, template class Cmp> inline void heapify(X& heap) { for (__ss_int i = heap.size() / 2 - 1; i > -1; --i) { _siftup(heap, i); } } template class X> inline void heapify(X& heap) { heapify(heap); } template inline void heapify(list *heap) { heapify(heap->units); } template class X, template class Cmp> T heapreplace(X& heap, T item) { T item2 = heap[0]; heap[0] = item; _siftup(heap, 0); return item2; } template class X> T heapreplace(X& heap, T item) { return heapreplace(heap, item); } template inline T heapreplace(list *heap, T item) { return heapreplace(heap->units, item); } /* Advanced operations */ template class mergeiter; template inline mergeiter *merge(__ss_int iterable_count, pyiter *iterable, ...); template class mergeiter : public __iter { public: typedef std::pair<__ss_int, T> iter_heap; typedef std::allocator iter_heapallocator; bool exhausted; __GC_VECTOR(__iter *) iters; std::vector heap; mergeiter(); mergeiter(pyiter *iterable); void push_iter(pyiter *iterable); T next(); friend mergeiter *merge(__ss_int iterable_count, pyiter *iterable, ...); }; template inline mergeiter::mergeiter() { this->exhausted = true; } template inline mergeiter::mergeiter(pyiter *iterable) { this->exhausted = false; this->push_iter(iterable); } template void mergeiter::push_iter(pyiter *iterable) { this->iters.push_back(iterable->__iter__()); } template T mergeiter::next() { if (this->exhausted) { throw new StopIteration(); } if (!this->heap.size()) { for (size_t i = 0; i < this->iters.size(); ++i) { try { heappush(this->heap, iter_heap(i, this->iters[i]->next())); } catch (StopIteration *) { } } if (!this->heap.size()) { this->exhausted = true; throw new StopIteration(); } } iter_heap it = heappop(this->heap); try { heappush(this->heap, iter_heap(it.first, this->iters[it.first]->next())); } catch (StopIteration *) { if (!this->heap.size()) { this->exhausted = true; } } return it.second; } inline mergeiter *merge(__ss_int /* iterable_count */) { return new mergeiter(); } template inline mergeiter *merge(__ss_int iterable_count, pyiter *iterable, ...) { mergeiter *iter = new mergeiter(iterable); va_list ap; va_start(ap, iterable); while (--iterable_count) { iter->push_iter(va_arg(ap, pyiter *)); } va_end(ap); return iter; } template class Cmp> class nheapiter : public __iter { public: __ss_int index; std::vector values; nheapiter(); nheapiter(__ss_int n, pyiter *iterable); T next(); }; template class Cmp> inline nheapiter::nheapiter() { this->index = 0; } template class Cmp> inline nheapiter::nheapiter(__ss_int n, pyiter *iterable) { __iter *iter = iterable->__iter__(); std::vector heap; try { for (__ss_int i = 0; i < n; ++i) heappush, std::vector, Cmp>(heap, iter->next()); for (; ; ) { heappushpop, std::vector, Cmp>(heap, iter->next()); } } catch (StopIteration *) { while (!heap.empty()) this->values.push_back(heappop, std::vector, Cmp>(heap)); } this->index = values.size(); } template class Cmp> T nheapiter::next() { if (!this->index) { throw new StopIteration(); } return this->values[--this->index]; } template nheapiter *nlargest(__ss_int n, pyiter *iterable) { return new nheapiter(n, iterable); } template nheapiter *nsmallest(__ss_int n, pyiter *iterable) { return new nheapiter(n, iterable); } void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/select.py0000664000175000017500000000021612157270661017467 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) def select(rFDs, wFDs, xFDs, timeout=-1.0): return ([1],) shedskin-0.9.4/shedskin/lib/struct.py0000664000175000017500000000032612157270661017536 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) class error(Exception): pass def pack(fmt, *vals): return '' def unpack(fmt, s): pass def calcsize(fmt): return 1 shedskin-0.9.4/shedskin/lib/mmap.py0000664000175000017500000000320412157270661017142 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) PAGESIZE = 0x1000 ALLOCATIONGRANULARITY = 0x10000 MAP_SHARED, MAP_PRIVATE, MAP_ANON, MAP_ANONYMOUS = (1, 2, 32, 32) PROT_READ, PROT_WRITE, PROT_EXEC = (1, 2, 4) ACCESS_READ, ACCESS_WRITE, ACCESS_COPY = (1, 2, 3) class mmap: def __init__(self, fileno, length, flags=MAP_SHARED, prot=PROT_READ | PROT_WRITE, access=0, offset=0): pass def __win32__init__(self, fileno, length, tagname='', access=0, offset=0): pass def close(self): pass def flush(self, offset=0, size=-1): return 0 def find(self, string, start=-1, end=-1): return -1 def move(self, destination, source, count): pass def read(self, size): return '' def read_byte(self): return '' def readline(self): return '' def resize(self, newsize): pass def rfind(self, string, start=-1, end=-1): return -1 def seek(self, offset, whence=0): pass def size(self): return 0 def tell(self): return 0 def write(self, string): pass def write_byte(self, string): pass def __contains(self, string): return False def __iter__(self): return __mmapiter() def __len__(self): return 0 def __getitem__(self, index): return '' def __setitem__(self, index, value): pass def __slice__(self, kind, lower, upper, step=1): return '' def __setslice__(self, kind, lower, upper, step, sequence): pass class __mmapiter: def next(self): return '' shedskin-0.9.4/shedskin/lib/sys.py0000664000175000017500000000053512157270661017032 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) argv = [''] stdin, stdout, stderr = file('stdin'), file('stdout'), file('stderr') version = '' version_info = (0,) copyright = '' platform = '' byteorder = '' hexversion = 0 maxint = 0 maxsize = 0 def setrecursionlimit(limit): pass def exit(code=0): pass shedskin-0.9.4/shedskin/lib/builtin.cpp0000664000175000017500000002275512157270661020024 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "builtin.hpp" #include "re.hpp" #include #include #include #include #include #include #include namespace __shedskin__ { class_ *cl_class_, *cl_none, *cl_str_, *cl_int_, *cl_bool, *cl_float_, *cl_complex, *cl_list, *cl_tuple, *cl_dict, *cl_set, *cl_object, *cl_rangeiter, *cl_xrange; class_ *cl_stopiteration, *cl_assertionerror, *cl_eoferror, *cl_floatingpointerror, *cl_keyerror, *cl_indexerror, *cl_typeerror, *cl_ioerror, *cl_valueerror, *cl_zerodivisionerror, *cl_keyboardinterrupt, *cl_memoryerror, *cl_nameerror, *cl_notimplementederror, *cl_oserror, *cl_overflowerror, *cl_runtimeerror, *cl_syntaxerror, *cl_systemerror, *cl_systemexit; str *sp, *nl, *__fmt_s, *__fmt_H, *__fmt_d; __GC_STRING ws, __fmtchars; __GC_VECTOR(str *) __char_cache; __ss_bool True; __ss_bool False; list *__join_cache, *__mod5_cache; list *__print_cache; char __str_cache[4000]; file *__ss_stdin, *__ss_stdout, *__ss_stderr; #ifdef __SS_BIND dict *__ss_proxy; #endif void gc_warning_handler(char *msg, GC_word arg) { #ifndef __SS_NOGCWARNS printf(msg, arg); printf("(use a 64-bit system to possibly avoid GC warnings, or use shedskin -g to disable them)\n"); #endif } void __init() { GC_INIT(); GC_set_warn_proc(gc_warning_handler); #ifdef __SS_BIND #ifndef __SS_PYPY Py_Initialize(); #endif __ss_proxy = new dict(); #endif cl_class_ = new class_ ("class"); cl_none = new class_("None"); cl_str_ = new class_("str"); cl_int_ = new class_("int"); cl_float_ = new class_("float"); cl_list = new class_("list"); cl_tuple = new class_("tuple"); cl_dict = new class_("dict"); cl_set = new class_("set"); cl_object = new class_("object"); cl_rangeiter = new class_("rangeiter"); cl_complex = new class_("complex"); cl_xrange = new class_("xrange"); True.value = 1; False.value = 0; ws = " \n\r\t\f\v"; __fmtchars = "#*-+ .0123456789hlL"; sp = new str(" "); nl = new str("\n"); __fmt_s = new str("%s"); __fmt_H = new str("%H"); __fmt_d = new str("%d"); for(int i=0;i<256;i++) { char c = i; str *charstr = new str(&c, 1); charstr->charcache = 1; __char_cache.push_back(charstr); } __join_cache = new list(); __print_cache = new list(); __mod5_cache = new list(); for(int i=0; i<1000; i++) { __str_cache[4*i] = '0' + (i % 10); __str_cache[4*i+1] = '0' + ((i/10) % 10); __str_cache[4*i+2] = '0' + ((i/100) % 10); } __ss_stdin = new file(stdin); __ss_stdin->name = new str(""); __ss_stdout = new file(stdout); __ss_stdout->name = new str(""); __ss_stderr = new file(stderr); __ss_stderr->name = new str(""); cl_stopiteration = new class_("StopIteration"); cl_assertionerror = new class_("AssertionError"); cl_eoferror = new class_("EOFError"); cl_floatingpointerror = new class_("FloatingPointError"); cl_keyerror = new class_("KeyError"); cl_indexerror = new class_("IndexError"); cl_typeerror = new class_("TypeError"); cl_ioerror = new class_("IOError"); cl_valueerror = new class_("ValueError"); cl_zerodivisionerror = new class_("ZeroDivisionError"); cl_keyboardinterrupt = new class_("KeyboardInterrupt"); cl_memoryerror = new class_("MemoryError"); cl_nameerror = new class_("NameError"); cl_notimplementederror = new class_("NotImplementedError"); cl_oserror = new class_("OSError"); cl_overflowerror = new class_("OverflowError"); cl_runtimeerror = new class_("RuntimeError"); cl_syntaxerror = new class_("SyntaxError"); cl_systemerror = new class_("SystemError"); cl_systemexit = new class_("SystemExit"); } class_::class_(const char *name) { this->__name__ = new str(name); } str *class_::__repr__() { return (new str("class "))->__add__(__name__); } __ss_bool class_::__eq__(pyobj *c) { return __mbool(c == this); } #include "builtin/file.cpp" #include "builtin/math.cpp" #include "builtin/bool.cpp" #include "builtin/complex.cpp" #include "builtin/str.cpp" #include "builtin/exception.cpp" #include "builtin/function.cpp" #include "builtin/format.cpp" void __add_missing_newline() { if(__ss_stdout->options.lastchar != '\n') __ss_stdout->write(new str("\n")); } /* print traceback for uncaught exception, may only work for GCC */ void terminate_handler() { int code = 0; static bool terminating = false; if(terminating) abort(); terminating = true; try { // rethrow to detect uncaught exception, will recursively // call terminate() if no exception is active (which is // detected above). throw; } catch (SystemExit *s) { __add_missing_newline(); /* XXX s->message -> stderr? */ if(s->show_message) print2(__ss_stderr, 0, 1, s->message); code = s->code; } catch (BaseException *e) { __add_missing_newline(); #ifndef WIN32 #ifdef __SS_BACKTRACE print_traceback(stdout); #endif #endif str *s = __str(e); if(___bool(s)) print2(NULL, 0, 1, __add_strs(3, e->__class__->__name__, new str(": "), s)); else print2(NULL, 0, 1, e->__class__->__name__); code = 1; } std::exit(code); } /* starting and stopping */ void __start(void (*initfunc)()) { std::set_terminate(terminate_handler); initfunc(); std::exit(0); } void __ss_exit(int code) { throw new SystemExit(code); } /* glue */ #ifdef __SS_BIND #ifdef __SS_LONG template<> PyObject *__to_py(__ss_int i) { return PyLong_FromLongLong(i); } #endif template<> PyObject *__to_py(int i) { return PyInt_FromLong(i); } template<> PyObject *__to_py(long i) { return PyInt_FromLong(i); } template<> PyObject *__to_py(__ss_bool i) { return PyBool_FromLong(i.value); } template<> PyObject *__to_py(double d) { return PyFloat_FromDouble(d); } template<> PyObject *__to_py(void *v) { Py_INCREF(Py_None); return Py_None; } void throw_exception() { PyObject *ptype, *pvalue, *ptraceback; PyErr_Fetch(&ptype, &pvalue, &ptraceback); char *pStrErrorMessage = PyString_AsString(pvalue); throw new TypeError(new str(pStrErrorMessage)); } #ifdef __SS_LONG template<> __ss_int __to_ss(PyObject *p) { if(PyLong_Check(p) || PyInt_Check(p)) { __ss_int result = PyLong_AsLongLong(p); if (result == -1 && PyErr_Occurred() != NULL) { throw_exception(); } return result; } throw new TypeError(new str("error in conversion to Shed Skin (integer expected)")); } #endif template<> int __to_ss(PyObject *p) { if(PyLong_Check(p) || PyInt_Check(p)) { int result = PyInt_AsLong(p); if (result == -1 && PyErr_Occurred() != NULL) { throw_exception(); } return result; } throw new TypeError(new str("error in conversion to Shed Skin (integer expected)")); } template<> __ss_bool __to_ss(PyObject *p) { if(!PyBool_Check(p)) throw new TypeError(new str("error in conversion to Shed Skin (boolean expected)")); return (p==Py_True)?(__mbool(true)):(__mbool(false)); } template<> double __to_ss(PyObject *p) { if(!PyInt_Check(p) and !PyFloat_Check(p)) throw new TypeError(new str("error in conversion to Shed Skin (float or int expected)")); return PyFloat_AsDouble(p); } template<> void * __to_ss(PyObject *p) { if(p!=Py_None) throw new TypeError(new str("error in conversion to Shed Skin (None expected)")); return NULL; } #endif template <> void *myallocate<__ss_int>(int n) { return GC_MALLOC_ATOMIC(n); } template <> void *myallocate<__ss_int, __ss_int>(int n) { return GC_MALLOC_ATOMIC(n); } template<> int __none() { throw new TypeError(new str("mixing None with int")); } template<> double __none() { throw new TypeError(new str("mixing None with float")); } list *> *__zip(int) { return new list *>(); } /* pyobj */ str *pyobj::__str__() { return __repr__(); } str *pyobj::__repr__() { return __add_strs(3, new str("<"), __class__->__name__, new str(" instance>")); } long pyobj::__hash__() { return (intptr_t)this; } __ss_int pyobj::__cmp__(pyobj *p) { return __cmp(this, p); } __ss_bool pyobj::__eq__(pyobj *p) { return __mbool(this == p); } __ss_bool pyobj::__ne__(pyobj *p) { return __mbool(!__eq__(p)); } __ss_bool pyobj::__gt__(pyobj *p) { return __mbool(__cmp__(p) == 1); } __ss_bool pyobj::__lt__(pyobj *p) { return __mbool(__cmp__(p) == -1); } __ss_bool pyobj::__ge__(pyobj *p) { return __mbool(__cmp__(p) != -1); } __ss_bool pyobj::__le__(pyobj *p) { return __mbool(__cmp__(p) != 1); } pyobj *pyobj::__copy__() { return this; } pyobj *pyobj::__deepcopy__(dict *) { return this; } __ss_int pyobj::__len__() { return 1; } /* XXX exceptions? */ __ss_int pyobj::__int__() { return 0; } __ss_bool pyobj::__nonzero__() { return __mbool(__len__() != 0); } __ss_int pyobj::__index__() { throw new TypeError(new str("no such method: '__index__'")); } /* object */ object::object() { this->__class__ = cl_object; } #ifdef __SS_BIND PyObject *__ss__newobj__(PyObject *, PyObject *args, PyObject *kwargs) { PyObject *cls = PyTuple_GetItem(args, 0); PyObject *__new__ = PyObject_GetAttrString(cls, "__new__"); return PyObject_Call(__new__, args, kwargs); } #endif } // namespace __shedskin__ shedskin-0.9.4/shedskin/lib/csv.hpp0000664000175000017500000001546612157270661017157 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __CSV_HPP #define __CSV_HPP #include "builtin.hpp" using namespace __shedskin__; namespace __csv__ { extern tuple2 *const_3; extern list *const_0; extern str *const_1, *const_10, *const_11, *const_12, *const_13, *const_14, *const_15, *const_16, *const_17, *const_18, *const_19, *const_2, *const_20, *const_21, *const_22, *const_23, *const_24, *const_25, *const_26, *const_27, *const_4, *const_5, *const_6, *const_7, *const_8, *const_9; class Error; class Excel; class reader; class writer; class DictReader; class DictWriter; extern str *__name__; extern list<__ss_int> *__0, *__1; extern __ss_int EAT_CRNL, ESCAPED_CHAR, ESCAPE_IN_QUOTED_FIELD, IN_FIELD, IN_QUOTED_FIELD, QUOTE_ALL, QUOTE_IN_QUOTED_FIELD, QUOTE_MINIMAL, QUOTE_NONE, QUOTE_NONNUMERIC, START_FIELD, START_RECORD, _field_limit; extern OSError *__exception; extern class_ *cl_Error; class Error : public Exception { public: Error() {} Error(str *msg) { this->__class__ = cl_Error; __init__(msg); } }; extern class_ *cl_Excel; class Excel : public pyobj { public: str *lineterminator; __ss_int skipinitialspace; __ss_int quoting; __ss_int strict; str *delimiter; str *escapechar; str *quotechar; __ss_int doublequote; Excel() {} Excel(__ss_int) { this->__class__ = cl_Excel; __init__(); } void *__init__(); }; class __csviter : public __iter *> { public: reader *r; __csviter(reader *reader); list *next(); }; extern class_ *cl_reader; class reader : public pyiter *> { public: Excel *dialect; __ss_int line_num; list *fields; list *field; __ss_int state; __ss_int numeric_field; file *input_iter; reader() {} reader(file *input_iter, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict) { this->__class__ = cl_reader; __init__(input_iter, dialect, delimiter, quotechar, doublequote, skipinitialspace, lineterminator, quoting, escapechar, strict); } void *parse_process_char(str *c); void *parse_reset(); list *next(); __csviter *__iter__(); void *__init__(file *input_iter, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict); void *parse_save_field(); void *parse_add_char(str *c); }; extern class_ *cl_writer; class writer : public pyobj { public: Excel *dialect; __ss_int num_fields; file *output_file; list *rec; writer() {} writer(file *output_file, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict) { this->__class__ = cl_writer; __init__(output_file, dialect, delimiter, quotechar, doublequote, skipinitialspace, lineterminator, quoting, escapechar, strict); } __ss_int join_append_data(str *field, __ss_int quote_empty, __ss_int quoted); void *writerow(list *seq); void *join_reset(); void *writerows(list *> *seqs); __ss_int join_append(str *field, __ss_int quoted, __ss_int quote_empty); void *__init__(file *output_file, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict); }; class __driter : public __iter *> { public: DictReader *r; __driter(DictReader *reader); dict *next(); }; extern class_ *cl_DictReader; class DictReader : public pyiter *> { public: str *restval; str *dialect; __ss_int line_num; str *restkey; list *_fieldnames; reader *_reader; DictReader() {} DictReader(file *f, list *fieldnames, str *restkey, str *restval, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict) { this->__class__ = cl_DictReader; __init__(f, fieldnames, restkey, restval, dialect, delimiter, quotechar, doublequote, skipinitialspace, lineterminator, quoting, escapechar, strict); } void *setfieldnames(list *value); dict *next(); __driter *__iter__(); list *getfieldnames(); void *__init__(file *f, list *fieldnames, str *restkey, str *restval, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict); }; extern class_ *cl_DictWriter; class DictWriter : public pyobj { public: str *restval; writer *_writer; list *fieldnames; str *extrasaction; DictWriter() {} DictWriter(file *f, list *fieldnames, str *restval, str *extrasaction, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict) { this->__class__ = cl_DictWriter; __init__(f, fieldnames, restval, extrasaction, dialect, delimiter, quotechar, doublequote, skipinitialspace, lineterminator, quoting, escapechar, strict); } list *_dict_to_list(dict *rowdict); void *writerow(dict *rowdict); void *writerows(list *> *rowdicts); void *__init__(file *f, list *fieldnames, str *restval, str *extrasaction, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict); }; extern void * default_9; extern void * default_14; extern void * default_16; extern void * default_21; extern void * default_23; extern str * default_18; extern void * default_25; extern void * default_0; extern void * default_2; extern void * default_6; extern void * default_3; extern void * default_8; extern void * default_10; extern void * default_11; extern void * default_13; extern void * default_15; extern void * default_12; extern void * default_17; extern void * default_24; extern str * default_19; extern str * default_20; extern void * default_22; extern void * default_7; extern void * default_1; extern void * default_5; extern void * default_4; void __init(); list *list_dialects(); Excel *_get_dialect(str *name, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict); __ss_int field_size_limit(__ss_int new_limit); } // module namespace #endif shedskin-0.9.4/shedskin/lib/stat.cpp0000664000175000017500000000471612157270661017326 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "stat.hpp" #include #include #if defined( _MSC_VER ) #include #include #else #include #endif namespace __stat__ { __ss_int __ss_ST_MODE, __ss_ST_INO, __ss_ST_DEV, __ss_ST_NLINK, __ss_ST_UID, __ss_ST_GID, __ss_ST_SIZE, __ss_ST_ATIME, __ss_ST_MTIME, __ss_ST_CTIME, __ss_S_IFDIR, __ss_S_IFCHR, __ss_S_IFBLK, __ss_S_IFREG, __ss_S_IFIFO, __ss_S_IFLNK, __ss_S_IFSOCK, __ss_S_ISUID, __ss_S_ISGID, __ss_S_ENFMT, __ss_S_ISVTX, __ss_S_IREAD, __ss_S_IWRITE, __ss_S_IEXEC, __ss_S_IRWXU, __ss_S_IRUSR, __ss_S_IWUSR, __ss_S_IXUSR, __ss_S_IRWXG, __ss_S_IRGRP, __ss_S_IWGRP, __ss_S_IXGRP, __ss_S_IRWXO, __ss_S_IROTH, __ss_S_IWOTH, __ss_S_IXOTH; void __init() { __ss_ST_MODE = 0; /* XXX */ __ss_ST_INO = 1; __ss_ST_DEV = 2; __ss_ST_NLINK = 3; __ss_ST_UID = 4; __ss_ST_GID = 5; __ss_ST_SIZE = 6; __ss_ST_ATIME = 7; __ss_ST_MTIME = 8; __ss_ST_CTIME = 9; #if !defined( _MSC_VER ) __ss_S_IFDIR = S_IFDIR; __ss_S_IFCHR = S_IFCHR; __ss_S_IFBLK = S_IFBLK; __ss_S_IFREG = S_IFREG; __ss_S_IFIFO = S_IFIFO; __ss_S_IREAD = S_IREAD; __ss_S_IWRITE = S_IWRITE; __ss_S_IEXEC = S_IEXEC; __ss_S_IRWXU = S_IRWXU; __ss_S_IRUSR = S_IRUSR; __ss_S_IWUSR = S_IWUSR; __ss_S_IXUSR = S_IXUSR; #endif #ifndef WIN32 __ss_S_ISUID = S_ISUID; __ss_S_ISGID = S_ISGID; __ss_S_ENFMT = S_ISGID; __ss_S_ISVTX = S_ISVTX; __ss_S_IFLNK = S_IFLNK; __ss_S_IFSOCK = S_IFSOCK; __ss_S_IRWXG = S_IRWXG; __ss_S_IRGRP = S_IRGRP; __ss_S_IWGRP = S_IWGRP; __ss_S_IXGRP = S_IXGRP; __ss_S_IRWXO = S_IRWXO; __ss_S_IROTH = S_IROTH; __ss_S_IWOTH = S_IWOTH; __ss_S_IXOTH = S_IXOTH; #endif } #if !defined( _MSC_VER ) __ss_int __ss_S_IMODE(__ss_int mode) { return (mode&4095); /* XXX */ } __ss_int __ss_S_IFMT(__ss_int mode) { return (mode&61440); /* XXX */ } __ss_int __ss_S_ISDIR(__ss_int mode) { return S_ISDIR(mode); } __ss_int __ss_S_ISCHR(__ss_int mode) { return S_ISCHR(mode); } __ss_int __ss_S_ISBLK(__ss_int mode) { return S_ISBLK(mode); } __ss_int __ss_S_ISREG(__ss_int mode) { return S_ISREG(mode); } __ss_int __ss_S_ISFIFO(__ss_int mode) { return S_ISFIFO(mode); } #endif #ifndef WIN32 __ss_int __ss_S_ISLNK(__ss_int mode) { return S_ISLNK(mode); } __ss_int __ss_S_ISSOCK(__ss_int mode) { return S_ISSOCK(mode); } #endif } // module namespace shedskin-0.9.4/shedskin/lib/stat.py0000664000175000017500000000350712157270661017171 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) # copied from pypy: # https://codespeak.net/viewvc/pypy/dist/lib-python/2.4.1/stat.py?revision=16842&view=markup """Constants/functions for interpreting results of os.stat() and os.lstat(). Suggested usage: from stat import * """ # XXX Strictly spoken, this module may have to be adapted for each POSIX # implementation; in practice, however, the numeric constants used by # stat() are almost universal (even for stat() emulations on non-UNIX # systems like MS-DOS). # Indices for stat struct members in tuple returned by os.stat() ST_MODE = 0 ST_INO = 1 ST_DEV = 2 ST_NLINK = 3 ST_UID = 4 ST_GID = 5 ST_SIZE = 6 ST_ATIME = 7 ST_MTIME = 8 ST_CTIME = 9 # Extract bits from the mode def S_IMODE(mode): return mode & 07777 def S_IFMT(mode): return mode & 0170000 # Constants used as S_IFMT() for various file types # (not all are implemented on all systems) S_IFDIR = 0040000 S_IFCHR = 0020000 S_IFBLK = 0060000 S_IFREG = 0100000 S_IFIFO = 0010000 S_IFLNK = 0120000 S_IFSOCK = 0140000 # Functions to test for each file type def S_ISDIR(mode): return S_IFMT(mode) == S_IFDIR def S_ISCHR(mode): return S_IFMT(mode) == S_IFCHR def S_ISBLK(mode): return S_IFMT(mode) == S_IFBLK def S_ISREG(mode): return S_IFMT(mode) == S_IFREG def S_ISFIFO(mode): return S_IFMT(mode) == S_IFIFO def S_ISLNK(mode): return S_IFMT(mode) == S_IFLNK def S_ISSOCK(mode): return S_IFMT(mode) == S_IFSOCK # Names for permission bits S_ISUID = 04000 S_ISGID = 02000 S_ENFMT = S_ISGID S_ISVTX = 01000 S_IREAD = 00400 S_IWRITE = 00200 S_IEXEC = 00100 S_IRWXU = 00700 S_IRUSR = 00400 S_IWUSR = 00200 S_IXUSR = 00100 S_IRWXG = 00070 S_IRGRP = 00040 S_IWGRP = 00020 S_IXGRP = 00010 S_IRWXO = 00007 S_IROTH = 00004 S_IWOTH = 00002 S_IXOTH = 00001 shedskin-0.9.4/shedskin/lib/random.py0000664000175000017500000000536412157270661017501 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) import math import time NV_MAGICCONST = 1.0 LOG4 = 1.0 SG_MAGICCONST = 1.0 BPF = 1 MAXWIDTH = 1 MAXINT = 1 N = 1 M = 1 MATRIX_A = 1 UPPER = 1 LOWER = 1 class Random: def __init__(self, a=-1): return 1 def seed(self, a=None): a.__hash__() def random(self): return 1.0 def _genrand_res53(self): return 1.0 def _genrand_int32(self): return 1 def _init_genrand(self, s): return 1 def _init_by_array(self, init_key): return 1 def getstate(self): return [1.0,1.0,1.0] def setstate(self, state): pass def getrandbits(self, k): return 1 def randrange(self, start, stop=1, step=1): return 1 def randint(self, a, b): return 1 def choice(self, seq): return seq[seq.__len__()] def shuffle(self, x): pass def sample(self, population, k): return [iter(population).next()] def uniform(self, a, b): return 1.0 def triangular(self, low=0.0, high=1.0, mode=None): return 1.0 def normalvariate(self, mu, sigma): return 1.0 def lognormvariate(self, mu, sigma): return 1.0 def cunifvariate(self, mean, arc): return 1.0 def expovariate(self, lambd): return 1.0 def vonmisesvariate(self, mu, kappa): return 1.0 def gammavariate(self, alpha, beta): return 1.0 def stdgamma(self, alpha, ainv, bbb, ccc): return 1.0 def gauss(self, mu, sigma): return 1.0 def betavariate(self, alpha, beta): return 1.0 def paretovariate(self, alpha): return 1.0 def weibullvariate(self, alpha, beta): return 1.0 class WichmannHill(Random): def __init__(self, a=-1): return 1 def seed(self, a=-1): pass def random(self): return 1.0 def getstate(self): return [1.0,1.0,1.0] def setstate(self, state): pass def jumpahead(self, n): return 1 def __whseed(self, x=0, y=0, z=0): pass def whseed(self, a=-1): pass _inst = Random() def seed(a=None): _inst.seed(a) def random(): return 1.0 def getstate(): return [1.0,1.0,1.0] def setstate(state): pass def randrange(start, stop=1, step=1): return 1 def randint(a, b): return 1 def choice(seq): return seq[seq.__len__()] def shuffle(x): pass def sample(population, k): return [iter(population).next()] def uniform(a, b): return 1.0 def triangular(low=0.0, high=1.0, mode=None): return 1.0 def normalvariate(mu, sigma): return 1.0 def lognormvariate(mu, sigma): return 1.0 def cunifvariate(mean, arc): return 1.0 def expovariate(lambd): return 1.0 def vonmisesvariate(mu, kappa): return 1.0 def gammavariate(alpha, beta): return 1.0 def stdgamma(alpha, ainv, bbb, ccc): return 1.0 def gauss(mu, sigma): return 1.0 def betavariate(alpha, beta): return 1.0 def paretovariate(alpha): return 1.0 def weibullvariate(alpha, beta): return 1.0 def getrandbits(k): return 1 shedskin-0.9.4/shedskin/lib/socket.cpp0000664000175000017500000004472012157270661017642 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* * Implementation of the Python 2.5.1 socket module for Shed Skin * by: Michael Elkins * February 25, 2008 * * Current Issues: * - unix domain sockets are not implemented * - can't call socket.settimeout(None) */ #include "socket.hpp" #include #include #include #ifdef WIN32 #define CLOSE closesocket #define EINPROGRESS WSAEINPROGRESS #define SOCKOPT_CAST (char*) typedef long tv_sec_type; typedef long tv_usec_type; #define ERRNO WSAGetLastError() #else /* ! WIN32 */ #include #include #include #include #include #include #define CLOSE close #define SOCKET_ERROR -1 #define SOCKOPT_CAST #define ERRNO errno typedef time_t tv_sec_type; typedef suseconds_t tv_usec_type; #endif /* WIN32 */ #ifndef HOST_NAME_MAX #define HOST_NAME_MAX 256 #endif #include namespace __socket__ { str *__name__; str *invalid_address; str *timed_out; str *host_not_found; __ss_int default_0; __ss_int default_1; /** class error */ class_ *cl_error; /** class herror */ class_ *cl_herror; /** class gaierror */ class_ *cl_gaierror; /** class timeout */ class_ *cl_timeout; /** class socket */ class_ *cl_socket; __ss_int __ss_AF_INET6 = AF_INET6; __ss_int __ss_AF_INET = AF_INET; __ss_int __ss_AF_UNIX = AF_UNIX; __ss_int __ss_SOCK_STREAM = SOCK_STREAM; __ss_int __ss_SOCK_DGRAM = SOCK_DGRAM; #ifndef WIN32 __ss_int __ss_AI_PASSIVE = AI_PASSIVE; #ifndef __APPLE__ #ifndef __sun #ifndef __FreeBSD__ __ss_int __ss_SOL_IP = SOL_IP; #endif #endif #endif __ss_int __ss_IP_TOS = IP_TOS; __ss_int __ss_IP_TTL = IP_TTL; #endif __ss_int __ss_SOL_SOCKET = SOL_SOCKET; __ss_int __ss_SO_REUSEADDR = SO_REUSEADDR; __ss_int __ss_INADDR_ANY = INADDR_ANY; __ss_int __ss_INADDR_LOOPBACK = INADDR_LOOPBACK; #ifndef __sun __ss_int __ss_INADDR_NULL = INADDR_NONE; #endif __ss_int __ss_INADDR_BROADCAST = INADDR_BROADCAST; __ss_int __ss_SOMAXCONN = SOMAXCONN; double __ss_default_timeout = -1.0; __ss_int socket::__ss_fileno() { return this->_fd; } #ifdef WIN32 //not exactly the correct definition, but we only use it with ostringstream std::string strerror(int e) { std::ostringstream os; os << "socket error " << e; return os.str(); } #endif str* make_errstring(const char *prefix) { std::ostringstream os; os << prefix << ": " << strerror(ERRNO) << " (errno " << ERRNO << ")"; return new str( os.str().c_str() ); } str *socket::getsockopt(__ss_int level, __ss_int optname, __ss_int value) { socklen_t buflen = value; std::vector buf(buflen); if (::getsockopt(_fd, level, optname, buf.data(), &buflen) == SOCKET_ERROR) throw new error(make_errstring("getsockopt")); return new str(buf.data(), buflen); } file *socket::makefile(str *mode) { if(!mode) mode = new str("r"); #ifdef WIN32 intptr_t fd; #else int fd; #endif FILE *fp; #ifdef WIN32 if (((fd = _open_osfhandle(_fd, O_BINARY)) < 0) || ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode->unit.c_str())) == NULL)) #else if ((fd = dup(_fd)) < 0 || (fp = fdopen(fd, mode->unit.c_str())) == NULL) #endif { /*if (fd >= 0) SOCKETCLOSE(fd); return s->errorhandler(); */ throw new error(make_errstring("makefile")); } file *f = new file(fp); f->name = new str(""); f->mode = mode; return f; } socket *socket::bind(const sockaddr *sa, socklen_t salen) { if (::bind(_fd, sa, salen) == SOCKET_ERROR) { throw new error(make_errstring("bind")); } return this; } // python supports two special strings static unsigned long int string_to_addr(const char *s) { if (!*s) return INADDR_ANY; if (strcmp(s, "") == 0) return INADDR_BROADCAST; #ifdef WIN32 /* winsock doesn't have inet_aton() so we are forced to use inet_addr(). * however, since python has the special form we can use * -1 as the error check here. */ unsigned long int addr = inet_addr(s); if (addr != (unsigned long int)-1) return addr; #else struct in_addr addr; if (::inet_aton(s, &addr)) return addr.s_addr; // ip address #endif /* try looking up the address in dns */ struct hostent *he = ::gethostbyname(s); if (!he) throw new herror(host_not_found); return * reinterpret_cast( he->h_addr_list[0] ); } // conver the python version of a address to the bsd socket variety static void tuple_to_sin_addr(sockaddr_in *dst, socket::inet_address src) { memset(dst, 0, sizeof(sockaddr_in)); dst->sin_family = AF_INET; const char *host = src->first->unit.c_str(); dst->sin_addr.s_addr = string_to_addr(host); dst->sin_port = htons(src->second); } socket *socket::bind(socket::inet_address address) { if (family != AF_INET) throw new ValueError(invalid_address); sockaddr_in sin; tuple_to_sin_addr(&sin, address); return bind(reinterpret_cast(&sin), sizeof(sin)); } socket *socket::setsockopt(__ss_int level, __ss_int optname, __ss_int value) { if (::setsockopt(_fd, level, optname, SOCKOPT_CAST &value, sizeof(value)) == SOCKET_ERROR) throw new error(make_errstring("setsockopt")); return this; } socket *socket::connect(socket::inet_address address) { if (family != AF_INET) throw new ValueError(invalid_address); const char *host = address->first->unit.c_str(); int port = address->second; sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = family; sin.sin_port = htons(port); sin.sin_addr.s_addr = string_to_addr(host); return connect(reinterpret_cast(&sin), sizeof(sin)); } #ifndef WIN32 socket *socket::connect(pyseq *address) { if (family != AF_UNIX) throw new ValueError(invalid_address); sockaddr_un smup; smup.sun_family = AF_UNIX; const str* __0 = address->__getitem__(0); strcpy(smup.sun_path, __0->unit.c_str()); return connect(reinterpret_cast(&smup), sizeof(smup)); } #endif /* ! WIN32 */ static void set_blocking(socket_type fd) { #ifdef WIN32 u_long flag = 0; if (ioctlsocket(fd, FIONBIO, &flag) == SOCKET_ERROR) #else //FIXME should probably only clear the O_NONBLOCKING flag if (::fcntl(fd, F_SETFL, 0) == SOCKET_ERROR) #endif { throw new error(make_errstring("fcntl")); } } static void set_nonblocking(socket_type fd) { #ifdef WIN32 u_long flag = 1; if (ioctlsocket(fd, FIONBIO, &flag) == SOCKET_ERROR) #else if (::fcntl(fd, F_SETFL, O_NONBLOCK) == SOCKET_ERROR) #endif { throw new error(make_errstring("fcntl")); } } socket *socket::connect(const sockaddr *sa, socklen_t salen) { if (_blocking && _timeout > 0) { // temporarily set the socket to nonblocking set_nonblocking(_fd); } if (::connect(_fd, sa, salen) == SOCKET_ERROR) { if (ERRNO != EINPROGRESS) { if (_blocking && _timeout > 0) set_blocking(_fd); // turn blocking back on throw new error(make_errstring("connect")); } } if (_blocking && _timeout > 0) { fd_set s; FD_ZERO(&s); FD_SET(_fd, &s); timeval to; to.tv_sec = static_cast(_timeout); to.tv_usec = static_cast(1000000 * (_timeout - (double)to.tv_sec)); if (::select(_fd+1, 0, &s, 0, &to) == SOCKET_ERROR) { set_blocking(_fd); // turn blocking back on throw new error(make_errstring("select")); } if (! FD_ISSET(_fd, &s)) { set_blocking(_fd); // turn blocking back on throw new timeout(timed_out); } // get connection status int err = 0; socklen_t errsize = sizeof(err); if (::getsockopt(_fd, SOL_SOCKET, SO_ERROR, SOCKOPT_CAST &err, &errsize) == SOCKET_ERROR) { set_blocking(_fd); // turn blocking back on throw new error(make_errstring("getsockopt")); } set_blocking(_fd); // turn blocking back on if (err != 0) { std::ostringstream os; os << "connect: " << strerror(err) << " (errno " << err << ")"; const std::string& s = os.str(); throw new error(new str( s.c_str() )); } } return this; } socket *socket::setblocking(__ss_int flag) { if (flag) { //blocking mode _blocking = true; _timeout = __ss_default_timeout; // use default value set by socket.setdefaulttimeout() set_blocking(_fd); } else { //non-blocking set_nonblocking(_fd); _blocking = false; } return this; } socket *socket::settimeout(double val) { if (val < 0) throw new ValueError(new str("Timeout value out of range")); if (val == 0) { // s.settimeout(0.0) is equivalent to s.setblocking(0) set_nonblocking(_fd); _blocking = false; } else { set_blocking(_fd); _blocking = true; _timeout = val; } return this; } socket *socket::shutdown(__ss_int how) { if (::shutdown(_fd, how) == SOCKET_ERROR) throw new error(make_errstring("shutdown")); return this; } void socket::write_wait() { if (_blocking && _timeout >= 0) { fd_set s; FD_ZERO(&s); FD_SET(_fd, &s); timeval to; to.tv_sec = static_cast(_timeout); to.tv_usec = static_cast(1000000 * (_timeout - (double)to.tv_sec)); if (::select(_fd+1, 0, &s, 0, &to) == SOCKET_ERROR) throw new error(make_errstring("select")); if (! FD_ISSET(_fd, &s)) throw new timeout(timed_out); } } int socket::send(const char *s, size_t len, int flags) { write_wait(); ssize_t r = ::send(_fd, s, len, flags); if (r == SOCKET_ERROR) throw new error(make_errstring("send")); return r; } __ss_int socket::send(str *string, __ss_int flags) { const char *s = string->unit.c_str(); return send( s, strlen(s), flags ); } __ss_int socket::sendall(str *string, __ss_int flags) { const char *s = string->unit.c_str(); size_t offset = 0; size_t len = string->__len__(); //FIXME is this guaranteed to be the same as the C string length, even if we are dealing with wide/unicode? while (offset < len) offset += send(s + offset, len - offset, flags); return len; } __ss_int socket::sendto(str* msg, __ss_int flags, socket::inet_address addr) { write_wait(); const char *buf = msg->unit.c_str(); size_t buflen = strlen(buf); sockaddr *sa; socklen_t salen; //FIXME hardcoded for AF_INET sockaddr_in sin; sa = reinterpret_cast(&sin); salen = sizeof(sin); tuple_to_sin_addr(&sin, addr); ssize_t len = ::sendto(_fd, buf, buflen, flags, sa, salen); if (len == SOCKET_ERROR) throw new error(make_errstring("sendto")); return len; } __ss_int socket::sendto(str* msg, socket::inet_address addr) { return sendto(msg, 0, addr); } socket *socket::close() { if (::CLOSE(_fd) == SOCKET_ERROR) #define STRINGIFY(x) #x throw new error(make_errstring(STRINGIFY(CLOSE))); #undef STRINGIFY return this; } void socket::read_wait() { if (_blocking && _timeout >= 0) { fd_set s; FD_ZERO(&s); FD_SET(_fd, &s); timeval to; to.tv_sec = static_cast(_timeout); to.tv_usec = static_cast(1000000 * (_timeout - (double)to.tv_sec)); if (::select(_fd+1, &s, 0, 0, &to) == SOCKET_ERROR) throw new error(make_errstring("select")); if (! FD_ISSET(_fd, &s)) throw new timeout(timed_out); } } str *socket::recv(__ss_int bufsize, __ss_int flags) { read_wait(); std::vector buf(bufsize); ssize_t len = ::recv(_fd, buf.data(), bufsize, flags); if (len == SOCKET_ERROR) throw new error(make_errstring("recv")); return new str(buf.data(), len); } #ifdef WIN32 void inet_ntop(int proto, const in_addr *addr, char *dst, size_t len) { int v = ntohl(addr->s_addr); sprintf(dst, "%d.%d.%d.%d", ((v>> 24) & 0xff) ,((v >> 16) & 0xff) ,((v >> 8) & 0xff) ,((v) & 0xff)); } #endif static socket::inet_address sin_addr_to_tuple(const sockaddr_in *sin) { char ip[sizeof("xxx.xxx.xxx.xxx")]; inet_ntop(AF_INET, &sin->sin_addr, ip, sizeof(ip)); socket::inet_address addr = new tuple2(2, new str(ip), static_cast<__ss_int>(ntohs(sin->sin_port))); return addr; } ssize_t socket::recvfrom(char *buf, size_t bufsize, int flags, sockaddr *sa, socklen_t *salen) { read_wait(); ssize_t len = ::recvfrom(_fd, buf, bufsize, flags, sa, salen); if (len == SOCKET_ERROR) throw new error(make_errstring("recvfrom")); return len; } tuple2 *socket::recvfrom(__ss_int bufsize, __ss_int flags) { std::vector buf(bufsize); struct sockaddr_in sin; socklen_t salen = sizeof(sin); ssize_t len = recvfrom(buf.data(), bufsize, flags, reinterpret_cast(&sin), &salen); return new tuple2(2, new str(buf.data(), len), sin_addr_to_tuple(&sin)); } socket::socket(__ss_int family, __ss_int type, __ss_int proto) { this->__class__ = cl_socket; this->family = family; this->type = type; this->proto = proto; _fd = ::socket(family, type, proto); if (_fd == SOCKET_ERROR) throw new error(make_errstring("socket")); _timeout = __ss_default_timeout; _blocking = true; } socket::~socket() { ::CLOSE(_fd); // ignore errror since we can't throw } socket *socket::listen(__ss_int backlog) { if(::listen(_fd, backlog) == SOCKET_ERROR) throw new error(make_errstring("listen")); return this; } socket* socket::accept(sockaddr *sa, socklen_t *salen) { if (_blocking && _timeout >= 0) { fd_set s; FD_ZERO(&s); FD_SET(_fd, &s); timeval to; to.tv_sec = static_cast(_timeout); to.tv_usec = static_cast(1000000 * (_timeout - (double)to.tv_sec)); if (::select(_fd+1, &s, 0, 0, &to) == SOCKET_ERROR) throw new error(make_errstring("select")); if (! FD_ISSET(_fd, &s)) throw new timeout(timed_out); } int r; if ((r = ::accept(_fd, sa, salen)) == SOCKET_ERROR) { throw new error(make_errstring("accept")); } socket *sock = new socket(); sock->family = family; sock->proto = proto; sock->type = type; sock->_fd = r; return sock; } #if 0 // UNIX sockets tuple2 *> *socket::accept() { sockaddr_un smup; socklen_t sunsize = sizeof(smup); socket *sock = accept(reinterpret_cast(&smup), &sunsize); str* addr = new str(smup.sun_path); return new tuple2 *>(2, sock, addr); } #endif // INET sockets tuple2 *socket::accept() { sockaddr_in sin; socklen_t sinsize = sizeof(sin); socket *sock = accept(reinterpret_cast(&sin), &sinsize); return new tuple2( 2, sock, sin_addr_to_tuple(&sin)); } #ifndef WIN32 socket *socket::bind(pyseq *address) { if (family != AF_UNIX) throw new ValueError(invalid_address); sockaddr_un smup; smup.sun_family = AF_UNIX; const str* __0 = address->__getitem__(0); strcpy(smup.sun_path, __0->unit.c_str()); return bind(reinterpret_cast(&smup), sizeof(smup)); } #endif /* ! WIN32 */ socket::inet_address socket::getpeername() { struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); if (::getpeername(_fd, reinterpret_cast(&addr), &addrlen) == SOCKET_ERROR) throw new error(make_errstring("getpeername")); return sin_addr_to_tuple(&addr); } socket::inet_address socket::getsockname() { struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); if (::getsockname(_fd, reinterpret_cast(&addr), &addrlen) == SOCKET_ERROR) throw new error(make_errstring("getsockname")); return sin_addr_to_tuple(&addr); } str *gethostname() { char name[HOST_NAME_MAX]; if (::gethostname(name, sizeof(name)) == -1) throw new herror(make_errstring("gethostname")); return new str(name); } __ss_int _ss_htonl(__ss_int x) { return htonl(x); } __ss_int _ss_htons(__ss_int x) { return htons(x); } __ss_int _ss_ntohl(__ss_int x) { return ntohl(x); } __ss_int _ss_ntohs(__ss_int x) { return ntohs(x); } //FIXME this should return None when no timeout is set double getdefaulttimeout() { if (__ss_default_timeout < 0) throw new error(new str("no timeout is set")); return __ss_default_timeout; } // FIXME this should allow the argument to be None void *setdefaulttimeout(double x) { if (x < 0) throw new ValueError(new str("Timeout value out of range")); __ss_default_timeout = x; return NULL; } void __init() { __name__ = new str("socket"); cl_socket = new class_("socket"); cl_herror = new class_("herror"); cl_gaierror = new class_("gaierror"); cl_timeout = new class_("timeout"); cl_error = new class_("error"); default_0 = __ss_AF_INET; default_1 = __ss_SOCK_STREAM; // string constants used by this module invalid_address = new str("invalid address"); timed_out = new str("timed out"); host_not_found = new str("host not found"); #ifdef WIN32 int iResult; WSADATA wsaData; // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { throw new error(new str("WSAStartup failed")); } #endif /* WIN32 */ } void __exit() { #ifdef WIN32 //FIXME // should call winsock finalization routine, but __exit() doesn't get called // except for the builtin module #endif } str *gethostbyname(str *hostname) { hostent *he = ::gethostbyname(hostname->unit.c_str()); if (!he) throw new herror(host_not_found); char ip[sizeof("xxx.xxx.xxx.xxx")]; int addr = htonl( *((int *) he->h_addr_list[0]) ); sprintf(ip, "%d.%d.%d.%d", ((addr >> 24) & 0xff), ((addr >> 16) & 0xff), ((addr >> 8) & 0xff), (addr & 0xff)); return new str(ip); } str *inet_aton(str *x) { int addr = string_to_addr(x->unit.c_str()); return new str((char *) &addr, 4); } str *inet_ntoa(str *x) { const char *s = x->unit.c_str(); int addr = *((int *) s); char ip[sizeof("xxx.xxx.xxx.xxx")]; sprintf(ip, "%d.%d.%d.%d", ((addr >> 24) & 0xff), ((addr >> 16) & 0xff), ((addr >> 8) & 0xff), (addr & 0xff)); return new str(ip); } __ss_bool has_ipv6() { return False; } } // module namespace shedskin-0.9.4/shedskin/lib/random.cpp0000664000175000017500000007442712157270661017641 0ustar srepmubsrepmub/* Copyright 1997-2002 Makoto Matsumoto, Takuji Nishimura, License BSD-3 (See LICENSE) */ #include "random.hpp" /** Random variable generators. integers -------- uniform within range sequences --------- pick random element pick random sample generate random permutation distributions on the real line: ------------------------------ uniform normal (Gaussian) lognormal negative exponential gamma beta pareto Weibull distributions on the circle (angles 0 to 2pi) --------------------------------------------- circular uniform von Mises General notes on the underlying Mersenne Twister core generator: * The period is 2**19937-1. * It is one of the most extensively tested generators in existence * Without a direct way to compute N steps forward, the semantics of jumpahead(n) are weakened to simply jump to another distant state and rely on the large period to avoid overlapping sequences. Note: The jumpahead method is implemented for WichmannHill, but not yet for Random (Mersenne Twister). */ namespace __random__ { str *const_0, *const_1, *const_10, *const_11, *const_12, *const_13, *const_14, *const_15, *const_16, *const_17, *const_18, *const_19, *const_2, *const_20, *const_21, *const_22, *const_23, *const_24, *const_25, *const_26, *const_27, *const_28, *const_29, *const_3, *const_30, *const_31, *const_32, *const_33, *const_34, *const_35, *const_36, *const_4, *const_7, *const_8, *const_9; list *__all__; list *mag01; double LOG4, NV_MAGICCONST, SG_MAGICCONST; int BPF, LOWER, M, MATRIX_A, MAXBITS, __ss_MAXINT, MAXWIDTH, N, UPPER; str *__name__; Random *_inst; static inline list *list_comp_0(list *__108) { int s, __25; list *result = new list(); list::for_in_loop __123; result->resize(len(__108)); FOR_IN(s,__108,108,25,123) result->units[__25] = __float(s); END_FOR return result; } static inline list *list_comp_1(list *__108) { double s; list *result = new list(); list::for_in_loop __123; int __28; result->resize(len(__108)); FOR_IN(s,__108,108,28,123) result->units[__28] = __int(s); END_FOR return result; } /** class Random */ class_ *cl_Random; double Random::paretovariate(double alpha) { /** Pareto distribution. alpha is the shape parameter. */ double u; u = (1.0-this->random()); return (1.0/__power(u, (1.0/alpha))); } int Random::randrange(int stop) { return this->randrange(0, stop, 1); } int Random::randrange(int start, int stop) { return this->randrange(start, stop, 1); } int Random::randrange(int start, int stop, int step) { /** Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want. Do not supply the 'int', 'default', and 'maxwidth' arguments. */ int istart, istep, istop, n, width; istart = __int(start); if ((istart!=start)) { throw (new ValueError(const_0)); } istop = __int(stop); if ((istop!=stop)) { throw (new ValueError(const_1)); } width = (istop-istart); if (((step==1) && (width>0))) { return __int((istart+__int((this->random()*width)))); } if ((step==1)) { throw (new ValueError(const_2)); } istep = __int(step); if ((istep!=step)) { throw (new ValueError(const_3)); } if ((istep>0)) { n = (((width+istep)-1)/istep); } else if ((istep<0)) { n = (((width+istep)+1)/istep); } else { throw (new ValueError(const_4)); } if ((n<=0)) { throw (new ValueError(const_2)); } return (istart+(istep*__int((this->random()*n)))); } double Random::betavariate(double alpha, double beta) { /** Beta distribution. Conditions on the parameters are alpha > -1 and beta} > -1. Returned values range between 0 and 1. */ double y; y = this->gammavariate(alpha, 1.0); if ((y==0)) { return 0.0; } else { return (y/(y+this->gammavariate(beta, 1.0))); } return 0; } double Random::random() { /** Generate a random number on [0,1)-real-interval. */ #ifdef __SS_FASTRANDOM return rand() / ((double)RAND_MAX+1); #else return this->_genrand_res53(); #endif } double Random::normalvariate(double mu, double sigma) { /** Normal distribution. mu is the mean, and sigma is the standard deviation. */ double u1, u2, z, zz; while(1) { u1 = this->random(); u2 = (1.0-this->random()); z = ((NV_MAGICCONST*(u1-0.5))/u2); zz = ((z*z)/4.0); if ((zz<=-__math__::log(u2))) { break; } } return (mu+(z*sigma)); } double Random::_genrand_res53() { /** Generate a random number on [0,1) with 53-bit resolution. */ int a, b; a = ((this->_genrand_int32()>>5)&~(-1<<(32-5))); b = ((this->_genrand_int32()>>6)&~(-1<<(32-6))); // return (((a*67108864.0)+b)*(1.0/9.00719925474e+15)); return (((a*67108864.0)+b)*(1.0/9007199254740992.0)); return 0; } double Random::weibullvariate(double alpha, double beta) { /** Weibull distribution. alpha is the scale parameter and beta is the shape parameter. */ double u; u = (1.0-this->random()); return (alpha*__power(-__math__::log(u), (1.0/beta))); } Random::Random() { this->__class__ = cl_Random; this->mt = ((new list(1, 0)))->__mul__(N); this->mti = (N+1); this->gauss_next = 0.0; this->gauss_switch = 0; this->seed((void *)NULL); this->VERSION = 2; } Random::Random(int a) { /** Initialize an instance. Optional argument a controls seeding, as for Random.seed(). The seed, a, must be an integer. */ this->__class__ = cl_Random; this->mt = ((new list(1, 0)))->__mul__(N); this->mti = (N+1); this->gauss_next = 0.0; this->gauss_switch = 0; this->seed(a); this->VERSION = 2; } int Random::_init_by_array(list *init_key) { /** Seed the random number generator with a list of numbers. */ list *__14, *__15, *__16, *__19, *__20, *__21, *__22; int __12, __13, __17, __18, i, j, k, key_length; key_length = len(init_key); this->_init_genrand(19650218); i = 1; j = 0; k = ___max(2, 0, N, key_length); FAST_FOR(k,k,0,-1,12,13) __14 = this->mt; __14->__setitem__(i, (((this->mt)->__getfast__(i)^(((this->mt)->__getfast__((i-1))^(((this->mt)->__getfast__((i-1))>>30)&3))*1664525))+init_key->__getfast__(j))+j); __15 = this->mt; __15->__setitem__(i, __15->__getfast__(i) & 4294967295u); i += 1; j += 1; if ((i>=N)) { __16 = this->mt; __16->__setitem__(0, (this->mt)->__getfast__((N-1))); i = 1; } if ((j>=key_length)) { j = 0; } END_FOR FAST_FOR(k,(N-1),0,-1,17,18) __19 = this->mt; __19->__setitem__(i, (((this->mt)->__getfast__(i)^(((this->mt)->__getfast__((i-1))^(((this->mt)->__getfast__((i-1))>>30)&3))*1566083941))-i)); __20 = this->mt; __20->__setitem__(i, __20->__getfast__(i) & 4294967295u); i += 1; if ((i>=N)) { __21 = this->mt; __21->__setitem__(0, (this->mt)->__getfast__((N-1))); i = 1; } END_FOR __22 = this->mt; __22->__setitem__(0, 2147483648u); return 0; } int Random::randint(int a, int b) { /** Return random integer in range [a, b], including both end points. */ return this->randrange(a, (b+1), 1); } double Random::vonmisesvariate(double mu, double kappa) { /** Circular data distribution. mu is the mean angle, expressed in radians between 0 and 2*pi, and kappa is the concentration parameter, which must be greater than or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to 2*pi. */ double d, f, q, r, s, theta, u1, u2, u3, z, TWOPI; __ss_bool __0, __1; TWOPI = 2*__math__::pi; if ((kappa<=1e-06)) { return (TWOPI*this->random()); } s = (0.5/kappa); r = (s+__math__::sqrt((1.0+(s*s)))); while (1) { u1 = this->random(); z = __math__::cos((__math__::pi*u1)); d = (z/(r+z)); u2 = this->random(); if (((u2<(1.0-(d*d))) or (u2<=((1.0-d)*__math__::exp(d))))) { break; } } q = (1.0/r); f = ((q+z)/(1.0+(q*z))); u3 = this->random(); if ((u3>0.5)) { theta = __mods((mu+__math__::acos(f)), TWOPI); } else { theta = __mods((mu-__math__::acos(f)), TWOPI); } return theta; } double Random::gammavariate(double alpha, double beta) { /** Gamma distribution. Not the gamma function! Conditions on the parameters are alpha > 0 and beta > 0. */ double ainv, b, bbb, ccc, p, r, u, u1, u2, v, x, z; if (((alpha<=0.0) || (beta<=0.0))) { throw (new ValueError(const_7)); } if ((alpha>1.0)) { ainv = __math__::sqrt(((2.0*alpha)-1.0)); bbb = (alpha-LOG4); ccc = (alpha+ainv); while(1) { u1 = this->random(); if ((!((1e-07random()); v = (__math__::log((u1/(1.0-u1)))/ainv); x = (alpha*__math__::exp(v)); z = ((u1*u1)*u2); r = ((bbb+(ccc*v))-x); if (((((r+SG_MAGICCONST)-(4.5*z))>=0.0) || (r>=__math__::log(z)))) { return (x*beta); } } } else if ((alpha==1.0)) { u = this->random(); while((u<=1e-07)) { u = this->random(); } return (-__math__::log(u)*beta); } else { while(1) { u = this->random(); b = ((__math__::e+alpha)/__math__::e); p = (b*u); if ((p<=1.0)) { x = __power(p, (1.0/alpha)); } else { x = -__math__::log(((b-p)/alpha)); } u1 = this->random(); if ((!(((p<=1.0) && (u1>__math__::exp(-x))) || ((p>1) && (u1>__power(x, (alpha-1.0))))))) { break; } } return (x*beta); } return 0; } double Random::uniform(double a, double b) { /** Get a random number in the range [a, b). */ return (a+((b-a)*this->random())); } static inline double __triangular(double low, double high, double u, double c) { double __0, __1; if ((u>c)) { u = (1.0-u); c = (1.0-c); __0 = high; __1 = low; low = __0; high = __1; } return (low+((high-low)*__power((u*c), 0.5))); } double Random::triangular(double low, double high, double mode) { /** Triangular distribution. Continuous distribution bounded by given lower and upper limits, and having a given mode value in-between. http://en.wikipedia.org/wiki/Triangular_distribution */ return __triangular(low, high, this->random(), ((mode-low)/(high-low))); } double Random::triangular(double low, double high, __ss_int mode) { return __triangular(low, high, this->random(), (double)mode); } double Random::triangular(double low, double high, void *) { return __triangular(low, high, this->random(), 0.5); } double Random::stdgamma(double alpha, double, double, double) { return this->gammavariate(alpha, 1.0); } double Random::expovariate(double lambd) { /** Exponential distribution. lambd is 1.0 divided by the desired mean. (The parameter would be called "lambda", but that is a reserved word in Python.) Returned values range from 0 to positive infinity. */ /* python 2.6 behaviour, changed in 2.7: double u; u = this->random(); while((u<=1e-07)) { u = this->random(); } return (-__math__::log(u)/lambd); */ return (-__math__::log(1.0 - this->random())/lambd); } int Random::getrandbits(int k) { /** getrandbits(k) -> x. Generates an int with k random bits. */ if ((k<=0)) { throw (const_8); } if ((k>MAXBITS)) { throw (new ValueError(const_9)); } return ((this->_genrand_int32()>>(32-k))&~(-1< *state) { /** Restore internal state from object returned by getstate(). */ int version; version = __int(state->__getfast__(0)); if ((version!=2)) { throw ((new ValueError(__modct(const_10, 2, ___box(version), ___box(this->VERSION))))); } this->mti = __int(state->__getfast__(1)); this->gauss_switch = __int(state->__getfast__(2)); this->mt = list_comp_1(state->__slice__(3, 3, -1, 0)); this->gauss_next = state->__getfast__(-1); return NULL; } double Random::lognormvariate(double mu, double sigma) { /** Log normal distribution. If you take the natural logarithm of this distribution, you'll get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero. */ return __math__::exp(this->normalvariate(mu, sigma)); } int Random::_init_genrand(int s) { /** Seed the random number generator. */ list *__10, *__11, *__7; int __8, __9; __7 = this->mt; __7->__setitem__(0, (s&4294967295u)); FAST_FOR(this->mti,1,N,1,8,9) __10 = this->mt; __10->__setitem__(this->mti, ((1812433253*((this->mt)->__getfast__((this->mti-1))^(((this->mt)->__getfast__((this->mti-1))>>30)&3)))+this->mti)); __11 = this->mt; __11->__setitem__(this->mti, __11->__getfast__(this->mti) & 4294967295u); END_FOR this->mti += 1; return 0; } double Random::gauss(double mu, double sigma) { /** Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function. Not thread-safe without a lock around calls. */ double g2rad, x2pi, z; if ((this->gauss_switch==1)) { z = this->gauss_next; this->gauss_switch = 0; } else { x2pi = ((this->random()*2)*__math__::pi); g2rad = __math__::sqrt((-2.0*__math__::log((1.0-this->random())))); z = (__math__::cos(x2pi)*g2rad); this->gauss_next = (__math__::sin(x2pi)*g2rad); this->gauss_switch = 1; } return (mu+(z*sigma)); } int Random::_genrand_int32() { /** Generate a random number on [0,0xffffffff]-interval. */ list *__2, *__5, *__6; int __0, __1, __3, __4, kk, y; kk = 0; if ((this->mti>=N)) { if ((this->mti==(N+1))) { this->_init_genrand(5489); } FAST_FOR(kk,0,(N-M),1,0,1) y = (((this->mt)->__getfast__(kk)&UPPER)|((this->mt)->__getfast__((kk+1))&LOWER)); __2 = this->mt; __2->__setitem__(kk, ((this->mt)->__getfast__((kk+M))^(((y>>1)&LOWER)^mag01->__getfast__((y&1))))); END_FOR FAST_FOR(kk,(kk+1),(N-1),1,3,4) y = (((this->mt)->__getfast__(kk)&UPPER)|((this->mt)->__getfast__((kk+1))&LOWER)); __5 = this->mt; __5->__setitem__(kk, ((this->mt)->__getfast__((kk+(M-N)))^(((y>>1)&LOWER)^mag01->__getfast__((y&1))))); END_FOR y = (((this->mt)->__getfast__((N-1))&UPPER)|((this->mt)->__getfast__(0)&LOWER)); __6 = this->mt; __6->__setitem__(N-1, ((this->mt)->__getfast__((M-1))^(((y>>1)&LOWER)^mag01->__getfast__((y&1))))); this->mti = 0; } y = (this->mt)->__getfast__(this->mti); this->mti += 1; y ^= ((y>>11)&~(-1<<(32-11))); y ^= ((y<<7)&2636928640u); y ^= ((y<<15)&4022730752u); y ^= ((y>>18)&~(-1<<(32-18))); return y; } list *Random::getstate() { /** Return internal state; can be passed to setstate() later. */ list *x; x = list_comp_0(__add((new list(3, this->VERSION, this->mti, this->gauss_switch)), this->mt)); return __add(x, (new list(1, this->gauss_next))); } double Random::cunifvariate(double mean, double arc) { return __math__::fmod((mean+(arc*(this->random()-0.5))), __math__::pi); } /** class WichmannHill */ class_ *cl_WichmannHill; void *WichmannHill::__whseed(int x, int y, int z) { /** Set the Wichmann-Hill seed from (x, y, z). These must be integers in the range [0, 256). */ tuple2 *__59, *__60, *__61; int __62, __63, __64, secs, t, usec; double hophop; if ((!(((0<=x)&&(x<256)) && ((0<=y)&&(y<256)) && ((0<=z)&&(z<256))))) { throw ((new ValueError(const_11))); } if (((0==x)&&(x==y)&&(y==z))) { hophop = __time__::time(); secs = __int(hophop); usec = __int((1000000*(hophop-__int(hophop)))); t = ((__mods(secs, (__ss_MAXINT/1000000))*1000000)|usec); __59 = divmod(t, 256); t = __59->__getfirst__(); x = __59->__getsecond__(); __60 = divmod(t, 256); t = __60->__getfirst__(); y = __60->__getsecond__(); __61 = divmod(t, 256); t = __61->__getfirst__(); z = __61->__getsecond__(); } if ((x==0)) { x = 1; } if ((y==0)) { y = 1; } if ((z==0)) { z = 1; } __62 = x; __63 = y; __64 = z; this->_seed = (new tuple2(3, __62, __63, __64)); this->gauss_next = 0.0; this->gauss_switch = 0; return NULL; } double WichmannHill::random() { /** Get the next random number in the range [0.0, 1.0). */ tuple2 *__46; int __47, __48, __49, x, y, z; __46 = this->_seed; x = __46->__getfast__(0); y = __46->__getfast__(1); z = __46->__getfast__(2); x = __mods((171*x), 30269); y = __mods((172*y), 30307); z = __mods((170*z), 30323); __47 = x; __48 = y; __49 = z; this->_seed = (new tuple2(3, __47, __48, __49)); return __math__::fmod((((__float(x)/30269.0)+(__float(y)/30307.0))+(__float(z)/30323.0)), 1.0); } void *WichmannHill::seed() { return this->seed(-1); } void *WichmannHill::seed(int a) { /** Initialize internal state from hashable object. If provided, the seed, a, should be a non-negative integer. If no argument is provided, current time is used for seeding. Distinct values between 0 and 27814431486575L inclusive are guaranteed to yield distinct internal states (this guarantee is specific to the default Wichmann-Hill generator). */ tuple2 *__40, *__41, *__42; int __43, __44, __45, secs, usec, x, y, z; double hophop; if ((a==-1)) { hophop = __time__::time(); secs = __int(hophop); usec = __int((1000000*(hophop-__int(hophop)))); a = ((__mods(secs, (__ss_MAXINT/1000000))*1000000)|usec); } __40 = divmod(a, 30268); a = __40->__getfirst__(); x = __40->__getsecond__(); __41 = divmod(a, 30306); a = __41->__getfirst__(); y = __41->__getsecond__(); __42 = divmod(a, 30322); a = __42->__getfirst__(); z = __42->__getsecond__(); __43 = (__int(x)+1); __44 = (__int(y)+1); __45 = (__int(z)+1); this->_seed = (new tuple2(3, __43, __44, __45)); this->gauss_next = 0.0; this->gauss_switch = 0; return NULL; } WichmannHill::WichmannHill() { this->__class__ = cl_WichmannHill; this->seed(-1); this->gauss_next = 0.0; this->gauss_switch = 0; this->VERSION = 1; } WichmannHill::WichmannHill(int a) { this->__class__ = cl_WichmannHill; this->seed(a); this->gauss_next = 0.0; this->gauss_switch = 0; this->VERSION = 1; } void *WichmannHill::whseed() { return this->whseed(-1); } void *WichmannHill::whseed(int a) { /** Seed from current time or non-negative integer argument. If no argument is provided, current time is used for seeding. This is obsolete, provided for compatibility with the seed routine used prior to Python 2.1. Use the .seed() method instead. */ tuple2 *__65, *__66, *__67; int x, y, z; if ((a==-1)) { this->__whseed(((int )(0)), ((int )(0)), ((int )(0))); return NULL; } __65 = divmod(a, 256); a = __65->__getfirst__(); x = __65->__getsecond__(); __66 = divmod(a, 256); a = __66->__getfirst__(); y = __66->__getsecond__(); __67 = divmod(a, 256); a = __67->__getfirst__(); z = __67->__getsecond__(); x = __mods((x+a), 256); y = __mods((y+a), 256); z = __mods((z+a), 256); if ((x==0)) { x = 1; } if ((y==0)) { y = 1; } if ((z==0)) { z = 1; } this->__whseed(x, y, z); return NULL; } void *WichmannHill::setstate(list *state) { /** Restore internal state from object returned by getstate(). */ double xf, yf, zf; list *__51; int __52, __53, __54, version; version = __int(state->__getfast__(0)); if ((version==1)) { __51 = state->__slice__(3, 1, 4, 0); xf = __51->__getfast__(0); yf = __51->__getfast__(1); zf = __51->__getfast__(2); __52 = __int(xf); __53 = __int(yf); __54 = __int(zf); this->_seed = (new tuple2(3, __52, __53, __54)); this->gauss_switch = __int(state->__getfast__(4)); this->gauss_next = state->__getfast__(5); } else { throw ((new ValueError(__modct(const_10, 2, ___box(version), ___box(this->VERSION))))); } return NULL; } int WichmannHill::jumpahead(int n) { /** Act as if n calls to random() were made, but quickly. n is an int, greater than or equal to 0. Example use: If you have 2 threads and know that each will consume no more than a million random numbers, create two Random objects r1 and r2, then do r2.setstate(r1.getstate()) r2.jumpahead(1000000) Then r1 and r2 will use guaranteed-disjoint segments of the full period. */ tuple2 *__55; int __56, __57, __58, x, y, z; if ((!(n>=0))) { throw ((new ValueError(const_12))); } __55 = this->_seed; x = __55->__getfast__(0); y = __55->__getfast__(1); z = __55->__getfast__(2); x = __mods(__int((x*__power(171, n, 30269))), (__ss_int)30269); y = __mods(__int((y*__power(172, n, 30307))), (__ss_int)30307); z = __mods(__int((z*__power(170, n, 30323))), (__ss_int)30323); __56 = x; __57 = y; __58 = z; this->_seed = (new tuple2(3, __56, __57, __58)); return 0; } list *WichmannHill::getstate() { /** Return internal state; can be passed to setstate() later. */ tuple2 *__50; int x, y, z; __50 = this->_seed; x = __50->__getfast__(0); y = __50->__getfast__(1); z = __50->__getfast__(2); return (new list(6, __float(this->VERSION), __float(x), __float(y), __float(z), __float(this->gauss_switch), this->gauss_next)); } void __init() { const_0 = new str("non-integer arg 1 for randrange()"); const_1 = new str("non-integer stop for randrange()"); const_2 = new str("empty range for randrange()"); const_3 = new str("non-integer step for randrange()"); const_4 = new str("zero step for randrange()"); const_7 = new str("gammavariate: alpha and beta must be > 0.0"); const_8 = new str("number of bits must be greater than zero for getrandbits(k)"); const_9 = new str("k exceeds size of int for getrandbits(k)"); const_10 = new str("state with version %s passed to Random.setstate() of version %s"); const_11 = new str("seeds must be in range(0, 256)"); const_12 = new str("n must be >= 0"); const_13 = new str("Random"); const_14 = new str("seed"); const_15 = new str("random"); const_16 = new str("uniform"); const_17 = new str("randint"); const_18 = new str("choice"); const_19 = new str("sample"); const_20 = new str("randrange"); const_21 = new str("shuffle"); const_22 = new str("normalvariate"); const_23 = new str("lognormvariate"); const_24 = new str("cunifvariate"); const_25 = new str("expovariate"); const_26 = new str("vonmisesvariate"); const_27 = new str("gammavariate"); const_28 = new str("stdgamma"); const_29 = new str("gauss"); const_30 = new str("betavariate"); const_31 = new str("paretovariate"); const_32 = new str("weibullvariate"); const_33 = new str("getstate"); const_34 = new str("setstate"); const_35 = new str("jumpahead"); const_36 = new str("WichmannHill"); __name__ = new str("random"); cl_WichmannHill = new class_("WichmannHill"); cl_Random = new class_("Random"); /** =========================== Source Notes ============================== Translated by Guido van Rossum from C source provided by Adrian Baddeley. Adapted by Raymond Hettinger for use with the Mersenne Twister core generator. Adapted by Jeff Miller for compatibility with the Shed Skin Python-to-C++ compiler. Mersenne Twister was converted to Python by Jeff Miller 2007-02-03 jwmillerusa (at) gmail (dot) com http://millerideas.com Below are the original comments from the authors' C source file. ======================================================================= A C-program for MT19937, with initialization improved 2002/1/26. Coded by Takuji Nishimura and Makoto Matsumoto. Before using, initialize the state by using init_genrand(seed) or init_by_array(init_key, key_length). Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Any feedback is very welcome. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) */ __all__ = (new list(24, const_13, const_14, const_15, const_16, const_17, const_18, const_19, const_20, const_21, const_22, const_23, const_24, const_25, const_26, const_27, const_28, const_29, const_30, const_31, const_32, const_33, const_34, const_35, const_36)); NV_MAGICCONST = ((4*__math__::exp(-0.5))/__math__::sqrt(2.0)); LOG4 = __math__::log(4.0); SG_MAGICCONST = (1.0+__math__::log(4.5)); BPF = 53; MAXWIDTH = (1<(2, 0, MATRIX_A)); _inst = (new Random()); } double random() { return _inst->random(); } list *getstate() { return _inst->getstate(); } void *setstate(list *state) { return _inst->setstate(state); } int randrange(int stop) { return _inst->randrange(0, stop, 1); } int randrange(int start, int stop) { return _inst->randrange(start, stop, 1); } int randrange(int start, int stop, int step) { return _inst->randrange(start, stop, step); } int randint(int a, int b) { return _inst->randint(a, b); } double uniform(double a, double b) { return _inst->uniform(a, b); } double normalvariate(double mu, double sigma) { return _inst->normalvariate(mu, sigma); } double lognormvariate(double mu, double sigma) { return _inst->lognormvariate(mu, sigma); } double cunifvariate(double mean, double arc) { return _inst->cunifvariate(mean, arc); } double expovariate(double lambd) { return _inst->expovariate(lambd); } double vonmisesvariate(double mu, double kappa) { return _inst->vonmisesvariate(mu, kappa); } double gammavariate(double alpha, double beta) { return _inst->gammavariate(alpha, beta); } double stdgamma(double alpha, double ainv, double bbb, double ccc) { return _inst->stdgamma(alpha, ainv, bbb, ccc); } double gauss(double mu, double sigma) { return _inst->gauss(mu, sigma); } double betavariate(double alpha, double beta) { return _inst->betavariate(alpha, beta); } double paretovariate(double alpha) { return _inst->paretovariate(alpha); } double weibullvariate(double alpha, double beta) { return _inst->weibullvariate(alpha, beta); } int getrandbits(int k) { return _inst->getrandbits(k); } double triangular(double low, double high, double mode) { return _inst->triangular(low, high, mode); } double triangular(double low, double high, __ss_int mode) { return _inst->triangular(low, high, mode); } double triangular(double low, double high, void *mode) { return _inst->triangular(low, high, mode); } } // module namespace shedskin-0.9.4/shedskin/lib/getopt.py0000664000175000017500000000120212157270661017506 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) import os class GetoptError(Exception): def __init__(self, msg, opt=''): self.msg = msg self.opt = opt class error(GetoptError): pass def getopt(args, shortopts, longopts = []): return ([('',)], ['']) def gnu_getopt(args, shortopts, longopts = []): return ([('',)], ['']) def do_longs(opts, opt, longopts, args): return ([('',)], ['']) def long_has_args(opt, longopts): return True, '' def do_shorts(opts, optstring, shortopts, args): return ([('',)], ['']) def short_has_arg(opt, shortopts): return True shedskin-0.9.4/shedskin/lib/getopt.hpp0000664000175000017500000000343712157270661017661 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __GETOPT_HPP #define __GETOPT_HPP #include "builtin.hpp" #include "sys.hpp" #include "os/__init__.hpp" using namespace __shedskin__; namespace __getopt__ { extern str *const_0, *const_1, *const_10, *const_11, *const_12, *const_2, *const_3, *const_4, *const_5, *const_6, *const_7, *const_8, *const_9; class GetoptError; extern str *__name__; extern __ss_int __18; extern class_ *cl_GetoptError; class GetoptError : public Exception { public: str *opt, *msg; GetoptError(str *message, str *opt=0); }; extern str * __name__; void __init(); tuple2 *> *, list *> *getopt(list *args, str *shortopts, pyiter *longopts); tuple2 *> *, list *> *getopt(list *args, str *shortopts, str *longopts); tuple2 *> *, list *> *getopt(list *args, str *shortopts); tuple2 *> *, list *> *gnu_getopt(list *args, str *shortopts, pyiter *longopts); tuple2 *> *, list *> *gnu_getopt(list *args, str *shortopts, str *longopts); tuple2 *> *, list *> *gnu_getopt(list *args, str *shortopts); tuple2 *> *, list *> *do_longs(list *> *opts, str *opt, pyiter *longopts, list *args); tuple2<__ss_bool, str *> *long_has_args(str *opt, pyiter *longopts); tuple2 *> *, list *> *do_shorts(list *> *opts, str *optstring, str *shortopts, list *args); __ss_bool short_has_arg(str *opt, str *shortopts); typedef GetoptError error; } // module namespace #endif shedskin-0.9.4/shedskin/lib/random.hpp0000664000175000017500000002152112157270661017631 0ustar srepmubsrepmub/* Copyright 1997-2002 Makoto Matsumoto, Takuji Nishimura, License BSD-3 (See LICENSE) */ #ifndef __RANDOM_HPP #define __RANDOM_HPP #include "builtin.hpp" #include "math.hpp" #include "time.hpp" using namespace __shedskin__; namespace __random__ { class Random; class WichmannHill; extern class_ *cl_Random; class Random : public pyobj { /** Random number generator base class used by bound module functions. Used to instantiate instances of Random to get generators that don't share state. Especially useful for multi-threaded programs, creating a different instance of Random for each thread, and using the jumpahead() method to ensure that the generated sequences seen by each thread don't overlap. Class Random can also be subclassed if you want to use a different basic generator of your own devising: in that case, override the following methods: random(), seed(), getstate(), setstate() and jumpahead(). */ public: int gauss_switch; int VERSION; double gauss_next; list *mt; int mti; Random(); Random(int a); virtual double random(); double paretovariate(double alpha); int randrange(int stop); int randrange(int start, int stop); int randrange(int start, int stop, int step); double betavariate(double alpha, double beta); double normalvariate(double mu, double sigma); double _genrand_res53(); template void *seed(A a); double weibullvariate(double alpha, double beta); int _init_by_array(list *init_key); int randint(int a, int b); double vonmisesvariate(double mu, double kappa); double gammavariate(double alpha, double beta); double uniform(double a, double b); double triangular(double low, double high, double mode); /* XXX template */ double triangular(double low, double high, __ss_int mode); double triangular(double low, double high, void *mode); double stdgamma(double alpha, double ainv, double bbb, double ccc); double expovariate(double lambd); int getrandbits(int k); virtual void *setstate(list *state); double lognormvariate(double mu, double sigma); int _init_genrand(int s); double gauss(double mu, double sigma); template A choice(pyseq *seq); template void *shuffle(list *x); template list *sample(pyiter *population, int k); template list *sample(pyseq *population, int k); int _genrand_int32(); virtual list *getstate(); double cunifvariate(double mean, double arc); }; extern class_ *cl_WichmannHill; class WichmannHill : public Random { public: tuple2 *_seed; void *__whseed(int x, int y, int z); double random(); void *seed(); void *seed(int a); WichmannHill(); WichmannHill(int a); void *whseed(); void *whseed(int a); void *setstate(list *state); int jumpahead(int n); list *getstate(); }; extern int UPPER; extern double LOG4; extern double SG_MAGICCONST; extern list * __all__; extern int BPF; extern Random * _inst; extern int MATRIX_A; extern int M; extern int LOWER; extern int N; extern int MAXWIDTH; extern int __ss_MAXINT; extern str * __name__; extern double NV_MAGICCONST; extern int MAXBITS; void __init(); double random(); list *getstate(); void *setstate(list *state); int randrange(int stop); int randrange(int start, int stop); int randrange(int start, int stop, int step); int randint(int a, int b); template A choice(pyseq *seq); template void *shuffle(list *x); template list *sample(pyiter *population, int k); template list *sample(pyseq *population, int k); double uniform(double a, double b); double triangular(double low, double high, double mode); double triangular(double low, double high, __ss_int mode); double triangular(double low, double high, void *mode); double normalvariate(double mu, double sigma); double lognormvariate(double mu, double sigma); double cunifvariate(double mean, double arc); double expovariate(double lambd); double vonmisesvariate(double mu, double kappa); double gammavariate(double alpha, double beta); double stdgamma(double alpha, double ainv, double bbb, double ccc); double gauss(double mu, double sigma); double betavariate(double alpha, double beta); double paretovariate(double alpha); double weibullvariate(double alpha, double beta); int getrandbits(int k); template A choice(pyseq *seq) { return _inst->choice(seq); } template void *shuffle(list *x) { return _inst->shuffle(x); } template list *sample(pyiter *population, int k) { return sample(new list(population), k); } template list *sample(pyseq *population, int k) { return _inst->sample(population, k); } template void *Random::shuffle(list *x) { /** x, random=random.random -> shuffle list x in place; return None. Note that for even rather small len(x), the total number of permutations of x is larger than the period of most random number generators; this implies that "most" permutations of a long sequence can never be generated. */ A __31, __32; int __29, __30, i, j; FAST_FOR(i,(len(x)-1),0,-1,29,30) j = __int((this->random()*(i+1))); __31 = x->__getitem__(j); __32 = x->__getitem__(i); x->__setitem__(i, __31); x->__setitem__(j, __32); END_FOR return NULL; } template list *Random::sample(pyiter *population, int k) { return sample(new list(population), k); } template list *Random::sample(pyseq *population, int k) { /** Chooses k unique random elements from a population sequence. Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices). Members of the population need not be hashable or unique. If the population contains repeats, then each occurrence is a possible selection in the sample. */ str *const_5, *const_6; const_5 = new str("sample larger than population"); const_6 = new str("population to sample has no members"); A __39; dict *selected; int __33, __34, __37, __38, i, j, n; list *pool, *result; n = len(population); if ((!((0<=k)&&(k<=n)))) { throw (new ValueError(const_5)); } if ((n==0)) { throw (new ValueError(const_6)); } result = ((new list(1, population->__getitem__(0))))->__mul__(k); if ((n<(6*k))) { pool = new list(population); FAST_FOR(i,0,k,1,33,34) j = __int((this->random()*(n-i))); result->__setitem__(i, pool->__getfast__(j)); pool->__setitem__(j, pool->__getfast__(((n-i)-1))); END_FOR } else { try { ((n>0) && ___bool((new tuple2(3, population->__getitem__(0), population->__getitem__(__floordiv(n, 2)), population->__getitem__((n-1)))))); } catch (TypeError *) { population = new tuple2(population); } catch (KeyError *) { population = new tuple2(population); } selected = (new dict()); FAST_FOR(i,0,k,1,37,38) j = __int((this->random()*n)); while(selected->__contains__(j)) { j = __int((this->random()*n)); } __39 = population->__getitem__(j); result->__setitem__(i, __39); selected->__setitem__(j, __39); END_FOR } return result; } template A Random::choice(pyseq *seq) { /** Choose a random element from a non-empty sequence. */ return seq->__getitem__(__int((this->random()*len(seq)))); } template void *Random::seed(A a) { /** Initialize the random number generator with a single seed number. If provided, the seed, a, must be an integer. If no argument is provided, current time is used for seeding. */ int h; if(__is_none(a)) { int secs, usec; double hophop = __time__::time(); secs = __int(hophop); usec = __int((1000000*(hophop-__int(hophop)))); h = ((__mods(secs, (__ss_MAXINT/1000000))*1000000)|usec); } else h = hasher(a); #ifdef __SS_FASTRANDOM srand(h); #else this->_init_by_array((new list(1, h))); this->gauss_next = 0.0; this->gauss_switch = 0; #endif return NULL; } template void *seed(A a) { return _inst->seed(a); } } // module namespace #endif shedskin-0.9.4/shedskin/lib/cStringIO.hpp0000664000175000017500000000142412157270661020212 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __CSTRINGIO_HPP #define __CSTRINGIO_HPP #include "builtin.hpp" using namespace __shedskin__; namespace __cStringIO__ { class StringI : public file { public: __ss_int pos; str *s; StringI(str *s=NULL) : file(), pos(0), s(s ? s : new str()) {} str * read(int n=-1); str * readline(int n=-1); void *seek(__ss_int i, __ss_int w=0); __ss_int tell() { return pos; } void *truncate(int size=-1) { s->unit.resize(size == -1 ? pos : size); return NULL; } void *write(str* data); bool __error() { return false; } bool __eof() { return (pos >= len(s)); } }; StringI *StringIO(str *s=0); void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/heapq.py0000664000175000017500000000145312157270661017312 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) # http://docs.python.org/dev/_sources/library/heapq.txt def heappush(heap, item): heap[0] = item cmp(item, item) def heappop(heap): return heap[0] def heappushpop(heap, item): heap[0] = item cmp(item, item) return heap[0] def heapify(x): cmp(x[0], x[0]) def heapreplace(heap, item): heap[0] = item cmp(item, item) return heap[0] def merge(*iterables): item = iter(iterables).next() cmp(item, item) yield item def nlargest(n, iterable): # TODO , key = None item = iter(iterable).next() cmp(item, item) #key(elem) yield item def nsmallest(n, iterable): # TODO , key = None item = iter(iterable).next() cmp(item, item) #key(elem) yield item shedskin-0.9.4/shedskin/lib/array.cpp0000664000175000017500000000671112157270661017466 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "array.hpp" namespace __array__ { str *__name__; void * default_0; class_ *cl_array; void *buffy; template<> str *array::__repr__() { return __add_strs(5, new str("array('"), typecode, new str("', "), repr(tostring()), new str(")")); } void __throw_no_char() { throw new TypeError(new str("array item must be char")); } template<> void *array::append(str *t) { if(t->unit.size() != 1) __throw_no_char(); units.push_back(t->unit[0]); return NULL; } template<> void *array::__setitem__(__ss_int i, str *t) { if(t->unit.size() != 1) __throw_no_char(); i = __wrap(this, i); units[i*itemsize] = t->unit[0]; return NULL; } unsigned int get_itemsize(char typechar) { switch(typechar) { case 'c': return sizeof(char); case 'b': return sizeof(signed char); case 'B': return sizeof(unsigned char); case 'h': return sizeof(signed short); case 'H': return sizeof(unsigned short); case 'i': return sizeof(signed int); case 'I': return sizeof(unsigned int); case 'l': return sizeof(signed long); case 'L': return sizeof(unsigned long); case 'f': return sizeof(float); case 'd': return sizeof(double); } throw new TypeError(new str("must be char, not str")); } template<> __ss_int array::count(str *t) { if(len(t) != 1) return 0; return std::count(this->units.begin(), this->units.end(), t->unit[0]); } template<> __ss_int array::index(str *t) { if(len(t) == 1) { size_t len = this->__len__(); char c = t->unit[0]; for(size_t i=0; i template<> void *array::extend(list<__ss_int> *l) { size_t len = l->__len__(); size_t pos = this->units.size(); this->units.resize(pos+len*itemsize); switch(typechar) { case 'b': for(size_t i=0; iunits[pos+i*itemsize])) = l->units[i]; break; case 'B': for(size_t i=0; iunits[pos+i*itemsize])) = l->units[i]; break; case 'h': for(size_t i=0; iunits[pos+i*itemsize])) = l->units[i]; break; case 'H': for(size_t i=0; iunits[pos+i*itemsize])) = l->units[i]; break; case 'i': for(size_t i=0; iunits[pos+i*itemsize])) = l->units[i]; break; case 'I': for(size_t i=0; iunits[pos+i*itemsize])) = l->units[i]; break; case 'l': for(size_t i=0; iunits[pos+i*itemsize])) = l->units[i]; break; case 'L': for(size_t i=0; iunits[pos+i*itemsize])) = l->units[i]; break; case 'f': for(size_t i=0; iunits[pos+i*itemsize])) = l->units[i]; break; case 'd': for(size_t i=0; iunits[pos+i*itemsize])) = l->units[i]; break; } return NULL; } template<> template<> void *array::extend(str *s) { fromstring(s); } void __init() { __name__ = new str("array"); cl_array = new class_("array"); buffy = malloc(8); default_0 = NULL; } } // module namespace shedskin-0.9.4/shedskin/lib/string.cpp0000664000175000017500000001060412157270661017652 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "string.hpp" #include #include namespace __string__ { str *const_0; str *ascii_letters, *ascii_uppercase, *ascii_lowercase, *uppercase, *lowercase, *digits, *hexdigits, *octdigits, *printable, *punctuation, *whitespace, *letters; char trans_table[256]; str *join(pyiter *a, str *b) { if(!b) b = const_0; return b->join(a); } str *join(pyseq *a, str *b) { if(!b) b = const_0; return b->join(a); } str *joinfields(pyiter *a, str *b) { return join(a, b); } str *joinfields(pyseq *a, str *b) { return join(a, b); } __ss_int count(str *a, str *b, __ss_int start) { return count(a, b, start, a->__len__()); } __ss_int count(str *a, str *b, __ss_int start, __ss_int end) { return a->count(b, start, end); } __ss_int find(str *s, str *t, __ss_int a) { return find(s, t, a, s->__len__()); } __ss_int find(str *s, str *t, __ss_int a, __ss_int b) { return s->find(t, a, b); } __ss_int rfind(str *s, str *t, __ss_int a) { return rfind(s, t, a, s->__len__()); } __ss_int rfind(str *s, str *t, __ss_int a, __ss_int b) { return s->rfind(t, a, b); } __ss_int index(str *s, str *t, __ss_int a) { return index(s, t, a, s->__len__()); } __ss_int index(str *s, str *t, __ss_int a, __ss_int b) { return s->index(t, a, b); } __ss_int rindex(str *s, str *t, __ss_int a) { return rindex(s, t, a, s->__len__()); } __ss_int rindex(str *s, str *t, __ss_int a, __ss_int b) { return s->rindex(t, a, b); } list *rsplit(str *s, str *sep, __ss_int maxsep) { return s->rsplit(sep, maxsep); } list *split(str *s, str *sep, __ss_int c) { return s->split(sep, c); } list *splitfields(str *s, str *sep, __ss_int c) { return s->split(sep, c); } str *replace(str *s, str *a, str *b, __ss_int c) { return s->replace(a, b, c); } str *translate(str *s, str *table, str *delchars) { return s->translate(table, delchars); } str *zfill(str *s, __ss_int width) { return s->zfill(width); } str *upper(str *s) { return s->upper(); } str *lower(str *s) { return s->lower(); } str *strip(str *s, str *chars) { return s->strip(chars); } str *lstrip(str *s, str *chars) { return s->lstrip(chars); } str *rstrip(str *s, str *chars) { return s->rstrip(chars); } str *ljust(str *s, __ss_int width, str *fchar) { return s->ljust(width, fchar); } str *rjust(str *s, __ss_int width, str *fchar) { return s->rjust(width, fchar); } str *expandtabs(str *s, __ss_int width) { return s->expandtabs(width); } str *maketrans(str *frm, str *to) { str *r = new str(trans_table, 256); __ss_int l = len(frm); if(l != len(to)) throw new ValueError(new str("maketrans arguments must have same length")); for(__ss_int i=0; iunit[(unsigned char)frm->unit[i]] = to->unit[i]; return r; } str *capitalize(str *s) { return s->capitalize(); } str *capwords(str *s, str *sep) { list *__3 = s->split(sep); list *result = new list(); result->resize(len(__3)); str *e; list::for_in_loop __4; int __2; list *__1; FOR_IN(e,__3,1,2,4) result->units[__2] = e->capitalize(); END_FOR if(!sep) sep = const_0; return sep->join(result); } str *swapcase(str *s) { return s->swapcase(); } str *center(str *s, __ss_int w, str *sep) { return s->center(w, sep); } __ss_int atoi(str *s, __ss_int base) { return __int(s, base); } __ss_int atol(str *s, __ss_int base) { char c = s->unit[len(s)-1]; if(c=='l' || c=='L') s = s->__slice__(7, 0, -1, 1); return __int(s, base); } double atof(str *s) { return __float(s); } str *__ctype_str(int (*cfunc)(int)) { str *s = new str(); for(__ss_int i=0; i<256; i++) if(cfunc(i)) s->unit += (char)i; return s; } void __init() { const_0 = new str(" "); lowercase = ascii_lowercase = __ctype_str(islower); uppercase = ascii_uppercase = __ctype_str(isupper); letters = ascii_letters = lowercase->__add__(uppercase); digits = new str("0123456789"); hexdigits = new str("0123456789abcdefABCDEF"); octdigits = new str("01234567"); punctuation = __ctype_str(ispunct); whitespace = __ctype_str(isspace); printable = __add_strs(5, digits, lowercase, uppercase, punctuation, new str(" \t\n\r\x0b\x0c")); for(__ss_int i=0; i<256; i++) trans_table[i] = i; } } // module namespace shedskin-0.9.4/shedskin/lib/itertools.py0000664000175000017500000000606612157270661020245 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) # http://docs.python.org/dev/_sources/library/itertools.txt # Infinite Iterators def count(start = 0, step = 1): yield start def cycle(iterable): yield iter(iterable).next() def repeat(object, times = 0): yield object # Iterators terminating on the shortest input iterableuence def chain(*iterables): yield iter(iterables).next() def compress(data, selectors): iter(selectors).next() yield iter(data).next() def __pred_elem(predicate, iterable): elem = iter(iterable).next() predicate(elem) return elem def dropwhile(predicate, iterable): yield __pred_elem(predicate, iterable) def groupby(iterable, key = lambda x: x): yield key(iter(iterable).next()), iter(iterable) def ifilter(predicate, iterable): yield __pred_elem(predicate, iterable) def ifilterfalse(predicate, iterable): yield __pred_elem(predicate, iterable) def takewhile(predicate, iterable): yield __pred_elem(predicate, iterable) def islice(iterable, start, stop = -1, step = -1): 'Known limitations: cannot distinguish between 0 and None for the stop argument' yield iter(iterable).next() def imap(function, *iterables): 'Known limitations: no more than 5 iterables are supported' yield function(*iter(iterables).next()) def __imap3(function, iter1, iter2): yield function(iter(iter1).next(), iter(iter2).next()) def __imap4(function, iter1, iter2, iter3): yield function(iter(iter1).next(), iter(iter2).next(), iter(iter3).next()) def __imap5(function, iter1, iter2, iter3, iter4): yield function(iter(iter1).next(), iter(iter2).next(), iter(iter3).next(), iter(iter4).next()) def __imap6(function, iter1, iter2, iter3, iter4, iter5): yield function(iter(iter1).next(), iter(iter2).next(), iter(iter3).next(), iter(iter4).next(), iter(iter5).next()) #def starmap(function, iterable): # yield func(*iterable[0]) def tee(iterable, n = 2): return iter(iterable), iter(iterable) def izip(*iterables): 'Known limitations: iterables must all be of the same type if they are more than two' yield iter(iterables).next(), def __izip2(iterable1, iterable2): yield iter(iterable1).next(), iter(iterable2).next() def izip_longest(__kw_fillvalue=None, *iterables): 'Known limitations: iterables must all be of the same type, cannot distinguish between 0 and None for the return value' yield iter(iterables).next(), def __izip_longest2(iterable1, iterable2, __kw_fillvalue=None): yield iter(iterable1).next(), iter(iterable2).next() # Combinatoric generators def product(__kw_repeat=1, *iterables): 'Known limitations: iterables must all be of the same type if they are more than two' yield iter(iterables).next(), def __product2(iterable1, iterable2, __kw_repeat=1): yield iter(iterable1).next(), iter(iterable2).next() def permutations(iterable, r = None): yield iter(iterable).next(), def combinations(iterable, r): yield iter(iterable).next(), def combinations_with_replacement(iterable, r): yield iter(iterable).next(), shedskin-0.9.4/shedskin/lib/sys.hpp0000664000175000017500000000120712157270661017166 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __SYS_HPP #define __SYS_HPP #include "builtin.hpp" using namespace __shedskin__; namespace __sys__ { void __init(int argc, char **argv); extern list *argv; extern str *version; extern tuple2<__ss_int, __ss_int> *version_info; extern str *__name__, *copyright, *platform, *byteorder; extern __ss_int hexversion, maxint, maxsize; extern file *__ss_stdin, *__ss_stdout, *__ss_stderr; void __ss_exit(); template void __ss_exit(T x) { throw new SystemExit(x); } void *setrecursionlimit(__ss_int limit); } // module namespace #endif shedskin-0.9.4/shedskin/lib/signal.py0000664000175000017500000000102512157270661017464 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) SIGABRT = 1 SIGALRM = 1 SIGBUS = 1 SIGCHLD = 1 SIGCLD = 1 SIGCONT = 1 SIGFPE = 1 SIGHUP = 1 SIGILL = 1 SIGINT = 1 SIGIO = 1 SIGIOT = 1 SIGKILL = 1 SIGPIPE = 1 SIGPOLL = 1 SIGPROF = 1 SIGPWR = 1 SIGQUIT = 1 SIGRTMAX = 1 SIGRTMIN = 1 SIGSEGV = 1 SIGSTOP = 1 SIGSYS = 1 SIGTERM = 1 SIGTRAP = 1 SIGTSTP = 1 SIGTTIN = 1 SIGTTOU = 1 SIGURG = 1 SIGUSR1 = 1 SIGUSR2 = 1 SIGVTALRM = 1 SIGWINCH = 1 SIGXCPU = 1 SIGXFSZ = 1 #SIG_DFL = 1 #SIG_IGN = 1 SIGBREAK = 1 shedskin-0.9.4/shedskin/lib/select.cpp0000664000175000017500000000050712157270661017624 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include #include #include #include #include "builtin.hpp" #include "select.hpp" namespace __select__ { str *__name__; void __init() { __name__ = new str("select"); } } // module namespace shedskin-0.9.4/shedskin/lib/signal.hpp0000664000175000017500000000146412157270661017632 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __SIGNAL_HPP #define __SIGNAL_HPP #include "builtin.hpp" using namespace __shedskin__; namespace __signal__ { extern str *__name__; extern __ss_int __ss_SIGABRT, __ss_SIGALRM, __ss_SIGBUS, __ss_SIGCHLD, __ss_SIGCLD, __ss_SIGCONT, __ss_SIGFPE, __ss_SIGHUP, __ss_SIGILL, __ss_SIGINT, __ss_SIGIO, __ss_SIGIOT, __ss_SIGKILL, __ss_SIGPIPE, __ss_SIGPOLL, __ss_SIGPROF, __ss_SIGPWR, __ss_SIGQUIT, __ss_SIGRTMAX, __ss_SIGRTMIN, __ss_SIGSEGV, __ss_SIGSTOP, __ss_SIGSYS, __ss_SIGTERM, __ss_SIGTRAP, __ss_SIGTSTP, __ss_SIGTTIN, __ss_SIGTTOU, __ss_SIGURG, __ss_SIGUSR1, __ss_SIGUSR2, __ss_SIGVTALRM, __ss_SIGWINCH, __ss_SIGXCPU, __ss_SIGXFSZ, __ss_SIGBREAK; //, __ss_SIG_DFL, __ss_SIG_IGN; void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/colorsys.py0000664000175000017500000000060512157270661020067 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) ONE_THIRD = 1.0 ONE_SIXTH = 1.0 TWO_THIRD = 1.0 def hls_to_rgb(a, b, c): return (1.0,) def rgb_to_hls(a, b, c): return (1.0,) def yiq_to_rgb(a, b, c): return (1.0,) def rgb_to_yiq(a, b, c): return (1.0,) def hsv_to_rgb(a, b, c): return (1.0,) def rgb_to_hsv(a, b, c): return (1.0,) shedskin-0.9.4/shedskin/lib/os/0000775000175000017500000000000012157270661016260 5ustar srepmubsrepmubshedskin-0.9.4/shedskin/lib/os/__init__.hpp0000664000175000017500000001756712157270661020550 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __OS_HPP #define __OS_HPP #include "builtin.hpp" using namespace __shedskin__; namespace __os__ { extern str *linesep, *name; extern dict *__ss_environ; extern dict *pathconf_names, *confstr_names, *sysconf_names; extern str *altsep, *curdir, *defpath, *devnull, *extsep, *pardir, *pathsep, *sep; extern __ss_int __ss_F_OK, __ss_R_OK, __ss_W_OK, __ss_X_OK, __ss_NGROUPS_MAX, __ss_TMP_MAX, __ss_WCONTINUED, __ss_WNOHANG, __ss_WUNTRACED, __ss_O_RDONLY, __ss_O_WRONLY, __ss_O_RDWR, __ss_O_NDELAY, __ss_O_NONBLOCK, __ss_O_APPEND, __ss_O_DSYNC, __ss_O_RSYNC, __ss_O_SYNC, __ss_O_NOCTTY, __ss_O_CREAT, __ss_O_EXCL, __ss_O_TRUNC, __ss_O_BINARY, __ss_O_TEXT, __ss_O_LARGEFILE, __ss_O_SHLOCK, __ss_O_EXLOCK, __ss_O_NOINHERIT, __ss__O_SHORT_LIVED, __ss_O_TEMPORARY, __ss_O_RANDOM, __ss_O_SEQUENTIAL, __ss_O_ASYNC, __ss_O_DIRECT, __ss_O_DIRECTORY, __ss_O_NOFOLLOW, __ss_O_NOATIME, __ss_EX_OK, __ss_EX_USAGE, __ss_EX_DATAERR, __ss_EX_NOINPUT, __ss_EX_NOUSER, __ss_EX_NOHOST, __ss_EX_UNAVAILABLE, __ss_EX_SOFTWARE, __ss_EX_OSERR, __ss_EX_OSFILE, __ss_EX_CANTCREAT, __ss_EX_IOERR, __ss_EX_TEMPFAIL, __ss_EX_PROTOCOL, __ss_EX_NOPERM, __ss_EX_CONFIG, __ss_EX_NOTFOUND, __ss_P_WAIT, __ss_P_NOWAIT, __ss_P_OVERLAY, __ss_P_NOWAITO, __ss_P_DETACH, __ss_SEEK_SET, __ss_SEEK_CUR, __ss_SEEK_END; typedef OSError error; class __cstat; list *listdir(str *path); str *getcwd(); void *chdir(str *dir); str *getenv(str *name, str *alternative=0); void *rename(str *a, str *b); void *remove(str *a); void *rmdir(str *a); void *removedirs(str *name); void *mkdir(str *path, __ss_int mode=0777); void *makedirs(str *name, __ss_int mode=0777); void *abort(); __ss_int system(str *c); class namedtuple : public pyobj { public: tuple2<__ss_int, __ss_int> *__slice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s); str *__repr__(); virtual __ss_int __len__() = 0; virtual __ss_int __getitem__(__ss_int i) = 0; }; extern class_ *cl___cstat; class __cstat : public namedtuple { public: __ss_int st_mode, st_ino, st_dev, st_rdev, st_nlink, st_uid, st_gid, st_size, st_blksize, st_blocks; __ss_int __ss_st_mtime, __ss_st_atime, __ss_st_ctime; __cstat(str *path, __ss_int t); __cstat(__ss_int fd); void fill_er_up(); __ss_int __len__(); __ss_int __getitem__(__ss_int i); }; __cstat *stat(str *path); __cstat *lstat(str *path); __cstat *fstat(__ss_int fd); __ss_bool stat_float_times(__ss_int newvalue=-1); str *strerror(__ss_int i); void *putenv(str* varname, str* value); __ss_int umask(__ss_int newmask); __ss_int chmod(str* path, __ss_int val); void *renames(str* old, str* _new); tuple2<__ss_int,__ss_int>* pipe(); __ss_int dup(__ss_int f1); void *dup2(__ss_int f1, __ss_int f2); void *close(__ss_int fd); __ss_int open(str *name, __ss_int flags); file* fdopen(__ss_int fd, str* mode=NULL, __ss_int bufsize=-1); str *read(__ss_int fd, __ss_int n); __ss_int write(__ss_int fd, str *s); class popen_pipe : public file { public: popen_pipe(str *name, str *mode=0); popen_pipe(FILE* pipe=0) : file(pipe) {} void *close(); }; popen_pipe* popen(str* cmd); popen_pipe* popen(str* cmd, str* mode); popen_pipe* popen(str* cmd, str* mode, __ss_int bufsize); void _exit(__ss_int code); void *utime(str *path, tuple2<__ss_int, __ss_int> *times); void *utime(str *path, tuple2<__ss_int, double> *times); void *utime(str *path, tuple2 *times); void *utime(str *path, tuple2 *times); #ifndef WIN32 __ss_int __ss_WCOREDUMP(__ss_int status); __ss_int __ss_WEXITSTATUS(__ss_int status); __ss_int __ss_WIFCONTINUED(__ss_int status); __ss_int __ss_WIFEXITED(__ss_int status); __ss_int __ss_WIFSIGNALED(__ss_int status); __ss_int __ss_WIFSTOPPED(__ss_int status); __ss_int __ss_WSTOPSIG(__ss_int status); __ss_int __ss_WTERMSIG(__ss_int status); void *execl(__ss_int n, str *file, ...); void *execlp(__ss_int n, str *file, ...); void *execle(__ss_int n, str *file, ...); void *execlpe(__ss_int n, str *file, ...); void *execv(str *file, list *args); void *execvp(str *file, list *args); void *execve(str *file, list *args, dict *env); void *execvpe(str *file, list *args, dict *env); __ss_int spawnl(__ss_int n, __ss_int mode, str *file, ...); __ss_int spawnlp(__ss_int n, __ss_int mode, str *file, ...); __ss_int spawnle(__ss_int n, __ss_int mode, str *file, ...); __ss_int spawnlpe(__ss_int n, __ss_int mode, str *file, ...); __ss_int spawnv(__ss_int mode, str *file, list *args); __ss_int spawnvp(__ss_int mode, str *file, list *args); __ss_int spawnve(__ss_int mode, str *file, list *args, dict *env); __ss_int spawnvpe(__ss_int mode, str *file, list *args, dict *env); void *unsetenv(str* var); __ss_int getpid(); tuple2* popen2(str* cmd); tuple2* popen2(str* cmd, str* mode, __ss_int bufsize); tuple2* popen2(pyiter *cmd, str *mode, __ss_int bufsize); tuple2* popen2(pyiter *cmd); tuple2* popen3(str* cmd); tuple2* popen3(str* cmd, str* mode, __ss_int bufsize); tuple2* popen4(str* cmd); tuple2* popen4(str* cmd, str* mode, __ss_int bufsize); extern class_ *cl___vfsstat; class __vfsstat : public namedtuple { public: __ss_int f_bsize, f_frsize, f_blocks, f_bfree, f_bavail, f_files, f_ffree, f_favail, f_flag, f_namemax; __vfsstat(str *path); __vfsstat(__ss_int fd); void fill_er_up(); __ss_int __len__(); __ss_int __getitem__(__ss_int i); }; __vfsstat *statvfs(str *path); __vfsstat *fstatvfs(__ss_int fd); __ss_int getuid(); void *setuid(__ss_int uid); __ss_int geteuid(); void *seteuid(__ss_int euid); __ss_int getgid(); void *setgid(__ss_int gid); __ss_int getegid(); void *setegid(__ss_int egid); __ss_int getpgid(__ss_int pid); void *setpgid(__ss_int pid, __ss_int pgrp); __ss_int getpgrp(); void *setpgrp(); __ss_int getppid(); void *setreuid(__ss_int ruid, __ss_int euid); void *setregid(__ss_int rgid, __ss_int egid); __ss_int getsid(__ss_int pid); __ss_int setsid(); __ss_int tcgetpgrp(__ss_int fd); void *tcsetpgrp(__ss_int fd, __ss_int pg); void *lchown(str *path, __ss_int uid, __ss_int gid); void *link(str *src, str *dst); void *symlink(str *src, str *dst); list<__ss_int> *getgroups(); void *setgroups(pyseq<__ss_int> *groups); str *getlogin(); str *readlink(str *path); void *fchdir(__ss_int f1); void *fdatasync(__ss_int f1); void *chown(str *path, __ss_int uid, __ss_int gid); void *chroot(str *path); str *ctermid(); __ss_bool isatty(__ss_int fd); str *ttyname(__ss_int fd); tuple2 *uname(); __ss_int fork(); tuple2<__ss_int, __ss_int> *forkpty(); tuple2<__ss_int, __ss_int> *openpty(); tuple2<__ss_int, __ss_int> *wait(); tuple2<__ss_int, __ss_int> *waitpid(__ss_int pid, __ss_int options); __ss_int nice(__ss_int n); void *kill(__ss_int pid, __ss_int sig); void *killpg(__ss_int pgid, __ss_int sig); __ss_int pathconf(str *path, str *name); __ss_int pathconf(str *path, __ss_int name); __ss_int fpathconf(__ss_int fd, str *name); __ss_int fpathconf(__ss_int fd, __ss_int name); str *confstr(str *name); str *confstr(__ss_int name); __ss_int sysconf(str *name); __ss_int sysconf(__ss_int name); void *ftruncate(__ss_int fd, __ss_int n); tuple2 *getloadavg(); void *mkfifo(str *path, __ss_int mode=438); void *unlink(str *path); void *fsync(__ss_int fd); void *lseek(__ss_int fd, __ss_int pos, __ss_int how); str *urandom(__ss_int n); __ss_bool access(str *path, __ss_int mode); tuple2 *times(); str *tmpnam(); file *tmpfile(); str *tempnam(str *dir, str *prefix=NULL); __ss_int __ss_makedev(__ss_int major, __ss_int minor); __ss_int __ss_major(__ss_int dev); __ss_int __ss_minor(__ss_int dev); void *mknod(str *filename, __ss_int mode=438, __ss_int device=0); #endif void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/os/__init__.cpp0000664000175000017500000020251012157270661020523 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "__init__.hpp" #include "path.hpp" #include #include #include #include #include #include #include #include #include #include #ifndef WIN32 #include #include #include #include #include #include #endif #include #ifdef WIN32 #include #endif #ifdef __FreeBSD__ #include #include #endif #ifdef __APPLE__ #include #include #include #define environ (*_NSGetEnviron()) #endif #ifdef __sun #include #include #include extern char **environ; #endif #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__sun) && !defined(WIN32) #include #endif namespace __os__ { str *const_0; str *linesep, *name; dict *__ss_environ; dict *pathconf_names, *confstr_names, *sysconf_names; struct stat sbuf; #ifndef WIN32 struct statvfs vbuf; #endif const __ss_int MAXENTRIES = 4096; /* XXX fix functions that use this */ str *altsep, *curdir, *defpath, *devnull, *extsep, *pardir, *pathsep, *sep; __ss_int __ss_F_OK, __ss_R_OK, __ss_W_OK, __ss_X_OK, __ss_NGROUPS_MAX, __ss_TMP_MAX, __ss_WCONTINUED, __ss_WNOHANG, __ss_WUNTRACED, __ss_O_RDONLY, __ss_O_WRONLY, __ss_O_RDWR, __ss_O_NDELAY, __ss_O_NONBLOCK, __ss_O_APPEND, __ss_O_DSYNC, __ss_O_RSYNC, __ss_O_SYNC, __ss_O_NOCTTY, __ss_O_CREAT, __ss_O_EXCL, __ss_O_TRUNC, __ss_O_BINARY, __ss_O_TEXT, __ss_O_LARGEFILE, __ss_O_SHLOCK, __ss_O_EXLOCK, __ss_O_NOINHERIT, __ss__O_SHORT_LIVED, __ss_O_TEMPORARY, __ss_O_RANDOM, __ss_O_SEQUENTIAL, __ss_O_ASYNC, __ss_O_DIRECT, __ss_O_DIRECTORY, __ss_O_NOFOLLOW, __ss_O_NOATIME, __ss_EX_OK, __ss_EX_USAGE, __ss_EX_DATAERR, __ss_EX_NOINPUT, __ss_EX_NOUSER, __ss_EX_NOHOST, __ss_EX_UNAVAILABLE, __ss_EX_SOFTWARE, __ss_EX_OSERR, __ss_EX_OSFILE, __ss_EX_CANTCREAT, __ss_EX_IOERR, __ss_EX_TEMPFAIL, __ss_EX_PROTOCOL, __ss_EX_NOPERM, __ss_EX_CONFIG, __ss_EX_NOTFOUND, __ss_P_WAIT, __ss_P_NOWAIT, __ss_P_OVERLAY, __ss_P_NOWAITO, __ss_P_DETACH, __ss_SEEK_SET, __ss_SEEK_CUR, __ss_SEEK_END; list *listdir(str *path) { list *r = new list(); DIR *dp; struct dirent *ep; dp = opendir(path->unit.c_str()); if (dp == 0) throw new OSError(path); while ((ep = readdir(dp))) if(strcmp(ep->d_name, ".") && strcmp(ep->d_name, "..")) r->append(new str(ep->d_name)); closedir (dp); return r; } str *getcwd() { str *r; char *d=::getcwd(0, 256); r = new str(d); free(d); return r; } void *chdir(str *dir) { if(::chdir(dir->unit.c_str()) == -1) throw new OSError(dir); return NULL; } str *strerror(__ss_int i) { return new str(::strerror(i)); } __ss_int system(str *c) { return std::system(c->unit.c_str()); } str *getenv(str *name, str *alternative) { const char *waba = name->unit.c_str(); if(std::getenv(waba)) return new str(std::getenv(waba)); return alternative; } void *rename(str *a, str *b) { if(std::rename(a->unit.c_str(), b->unit.c_str()) == -1) throw new OSError(a); return NULL; } void *remove(str *path) { if(std::remove(path->unit.c_str()) == -1) throw new OSError(path); return NULL; } void *unlink(str *path) { remove(path); return NULL; } void *rmdir(str *a) { if (::rmdir(a->unit.c_str()) == -1) throw new OSError(a); return NULL; } void *removedirs(str *name) { tuple2 *__0, *__1, *__5; str *__2, *head, *tail; rmdir(name); __0 = __path__::split(name); head = __0->__getfirst__(); tail = __0->__getsecond__(); if ((!___bool(tail))) { __1 = __path__::split(head); head = __1->__getfirst__(); tail = __1->__getsecond__(); } while(___bool(__AND(head, tail, 2))) { try { rmdir(head); } catch (OSError *) { break; } __5 = __path__::split(head); head = __5->__getfirst__(); tail = __5->__getsecond__(); } return NULL; } void *mkdir(str *path, __ss_int mode) { #ifdef WIN32 if (::mkdir(path->unit.c_str()) == -1) #else if (::mkdir(path->unit.c_str(), mode) == -1) #endif throw new OSError(path); return NULL; } void _exit(__ss_int code) { ::exit(code); } void *makedirs(str *name, __ss_int mode) { tuple2 *__0, *__1; str *head, *tail; __0 = __path__::split(name); head = __0->__getfirst__(); tail = __0->__getsecond__(); if ((!___bool(tail))) { __1 = __path__::split(head); head = __1->__getfirst__(); tail = __1->__getsecond__(); } if ((___bool(head) && ___bool(tail) && (!__path__::exists(head)))) { try { makedirs(head, mode); } catch (OSError *e) { if (e->__ss_errno != EEXIST) { throw (e); } } if (__eq(tail, __path__::curdir)) { return NULL; } } mkdir(name, mode); return NULL; } void *abort() { std::abort(); } /* class __cstat */ class_ *cl___cstat; __cstat::__cstat(str *path, __ss_int t) { this->__class__ = cl___cstat; if(t==1) { if(stat(path->unit.c_str(), &sbuf) == -1) throw new OSError(path); } else if (t==2) { #ifndef WIN32 if(lstat(path->unit.c_str(), &sbuf) == -1) #endif throw new OSError(path); } fill_er_up(); } __cstat::__cstat(__ss_int fd) { this->__class__ = cl___cstat; if(fstat(fd, &sbuf) == -1) throw new OSError(); fill_er_up(); } void __cstat::fill_er_up() { this->st_mode = sbuf.st_mode; this->st_ino = sbuf.st_ino; this->st_dev = sbuf.st_dev; this->st_rdev = sbuf.st_rdev; this->st_nlink = sbuf.st_nlink; this->__ss_st_atime = sbuf.st_atime; this->__ss_st_mtime = sbuf.st_mtime; this->__ss_st_ctime = sbuf.st_ctime; this->st_uid = sbuf.st_uid; this->st_gid = sbuf.st_gid; this->st_size = sbuf.st_size; #ifndef WIN32 this->st_blksize = sbuf.st_blksize; this->st_blocks = sbuf.st_blocks; #endif } __ss_int __cstat::__len__() { return 10; } __ss_int __cstat::__getitem__(__ss_int i) { i = __wrap(this, i); switch(i) { case 0: return st_mode; case 1: return st_ino; case 2: return st_dev; case 3: return st_nlink; case 4: return st_uid; case 5: return st_gid; case 6: return st_size; case 7: return __ss_st_atime; case 8: return __ss_st_mtime; case 9: return __ss_st_ctime; default: throw new IndexError(new str("tuple index out of range")); } return 0; } /* class namedtuple */ str *namedtuple::__repr__() { tuple2<__ss_int, __ss_int> *t = new tuple2<__ss_int, __ss_int>(); for(__ss_int i=0; i < __len__(); i++) t->units.push_back(__getitem__(i)); return repr(t); } tuple2<__ss_int, __ss_int> *namedtuple::__slice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s) { tuple2<__ss_int, __ss_int> *c = new tuple2<__ss_int, __ss_int>(); slicenr(x, l, u, s, __len__()); if(s > 0) for(__ss_int i=l; iunits.push_back(__getitem__(i)); else for(__ss_int i=l; i>u; i += s) c->units.push_back(__getitem__(i)); return c; } __cstat *stat(str *path) { return new __cstat(path, 1); } __cstat *lstat(str *path) { #ifndef WIN32 return new __cstat(path, 2); #else return new __cstat(path, 1); #endif } __cstat *fstat(__ss_int fd) { return new __cstat(fd); } __ss_bool stat_float_times(__ss_int newvalue) { if(newvalue==0) throw new TypeError(new str("os.stat_float_times: cannot change type")); return True; } void *putenv(str* varname, str* value) { std::stringstream ss; ss << varname->unit.c_str() << '=' << value->unit.c_str(); ::putenv(const_cast(ss.str().c_str())); return NULL; } __ss_int umask(__ss_int newmask) { return ::umask(newmask); } #ifndef WIN32 __ss_int chmod (str* path, __ss_int val) { #ifdef WIN32 DWORD attr; __ss_int res; attr = GetFileAttributesA(var->unit.c_str()); if (attr != 0xFFFFFFFF) { if (i & S_IWRITE) attr &= ~FILE_ATTRIBUTE_READONLY; else attr |= FILE_ATTRIBUTE_READONLY; res = SetFileAttributesA(var->unit.c_str(), attr); } else { res = 0; } if(!res) { throw new OSError("Chmod"); } return 0; #else return ::chmod(path->unit.c_str(), val); #endif } #endif void *renames(str* old, str* _new) { tuple2 *__0, *__1, *__5; str *__2, *head, *tail; __0 = __path__::split(_new); head = __0->__getfirst__(); tail = __0->__getsecond__(); if ((!___bool(tail))) { __1 = __path__::split(head); head = __1->__getfirst__(); tail = __1->__getsecond__(); } while(___bool(__AND(head, tail, 2)) && !__path__::exists(head)) { try { makedirs(head); } catch (OSError *) { break; } __5 = __path__::split(head); head = __5->__getfirst__(); tail = __5->__getsecond__(); } rename(old, _new); __0 = __path__::split(old); head = __0->__getfirst__(); tail = __0->__getsecond__(); if ((!___bool(tail))) { __1 = __path__::split(head); head = __1->__getfirst__(); tail = __1->__getsecond__(); } if(___bool(__AND(head,tail,2))) { removedirs(head); } return NULL; } popen_pipe::popen_pipe(str *cmd, str *flags) { if(flags == 0) flags = new str("r"); f = ::popen(cmd->unit.c_str(), flags->unit.c_str()); if(f == 0) throw new IOError(cmd); name = cmd; mode = flags; } void *popen_pipe::close() { pclose(f); closed = 1; return NULL; } popen_pipe* popen(str* cmd) { return popen(cmd, new str("r"), -1); } popen_pipe* popen(str* cmd, str* mode) { return popen(cmd, mode, -1); } popen_pipe* popen(str* cmd, str* mode, __ss_int) { FILE* fp = ::popen(cmd->unit.c_str(), mode->unit.c_str()); if(!fp) throw new OSError(cmd); return new popen_pipe(fp); } __ss_int dup(__ss_int f1) { __ss_int f2 = ::dup(f1); if (f2 == -1) throw new OSError(new str("os.dup failed")); return f2; } void *dup2(__ss_int f1, __ss_int f2) { if (::dup2(f1,f2) == -1) throw new OSError(new str("os.dup2 failed")); return NULL; } #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(WIN32) void *fdatasync(__ss_int f1) { if (::fdatasync(f1) == -1) throw new OSError(new str("os.fdatasync failed")); return NULL; } #endif __ss_int open(str *name, __ss_int flags) { /* XXX mode argument */ __ss_int fp = ::open(name->unit.c_str(), flags); if(fp == -1) throw new OSError(new str("os.open failed")); return fp; } file* fdopen(__ss_int fd, str* mode, __ss_int) { if(!mode) mode = new str("r"); /* XXX ValueError: mode string must begin with one of 'r', 'w', 'a' or 'U' */ FILE* fp = ::fdopen(fd, mode->unit.c_str()); if(fp == NULL) throw new OSError(new str("os.fdopen failed")); file* ret = new file(fp); ret->name = new str(""); return ret; } str *read(__ss_int fd, __ss_int n) { /* XXX slowness */ char c; str *s = new str(); __ss_int nr; for(__ss_int i=0; iunit += c; } return s; } __ss_int write(__ss_int fd, str *s) { __ss_int r; if((r=::write(fd, s->unit.c_str(), len(s))) == -1) throw new OSError(new str("os.write")); return r; } void *close(__ss_int fd) { if(::close(fd) < 0) throw new OSError(new str("os.close failed")); return NULL; } /* utime */ #ifdef WIN32 /* win32 implementation based on cpython */ static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ static void time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) { /* XXX endianness */ __int64 out; out = time_in + secs_between_epochs; out = out * 10000000 + nsec_in / 100; memcpy(out_ptr, &out, sizeof(out)); } void __utime_win32(str *path, FILETIME atime, FILETIME mtime) { HANDLE hFile; const char *apath = path->unit.c_str(); hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if (hFile == INVALID_HANDLE_VALUE) throw new OSError(new str("os.utime")); if (!SetFileTime(hFile, NULL, &atime, &mtime)) throw new OSError(new str("os.utime")); CloseHandle(hFile); } void __utime(str *path) { SYSTEMTIME now; FILETIME atime, mtime; GetSystemTime(&now); if (!SystemTimeToFileTime(&now, &mtime) || !SystemTimeToFileTime(&now, &atime)) { throw new OSError(new str("os.utime")); } __utime_win32(path, atime, mtime); } void __utime(str *path, double actime, double modtime) { time_t atimesec, mtimesec; FILETIME atime, mtime; atimesec = (time_t)actime; mtimesec = (time_t)modtime; time_t_to_FILE_TIME(atimesec, 0, &atime); /* XXX nanoseconds */ time_t_to_FILE_TIME(mtimesec, 0, &mtime); __utime_win32(path, atime, mtime); } #else void __utime(str *path, double actime, double modtime) { struct utimbuf buf; buf.actime = (time_t)actime; buf.modtime = (time_t)modtime; if(::utime(path->unit.c_str(), &buf) == -1) throw new OSError(new str("os.utime")); } void __utime(str *path) { if(::utime(path->unit.c_str(), NULL) == -1) throw new OSError(new str("os.utime")); } #endif #define HOPPA if (times) __utime(path, times->__getfirst__(), times->__getsecond__()); else __utime(path); return NULL; void *utime(str *path, tuple2<__ss_int, __ss_int> *times) { HOPPA } void *utime(str *path, tuple2<__ss_int, double> *times) { HOPPA } void *utime(str *path, tuple2 *times) { HOPPA } void *utime(str *path, tuple2 *times) { HOPPA } #undef HOPPA /* UNIX-only functionality */ #ifndef WIN32 __ss_int __ss_WCOREDUMP(__ss_int status) { return WCOREDUMP(status); } __ss_int __ss_WEXITSTATUS(__ss_int status) { return WEXITSTATUS(status); } __ss_int __ss_WIFCONTINUED(__ss_int status) { return WIFCONTINUED(status); } __ss_int __ss_WIFEXITED(__ss_int status) { return WIFEXITED(status); } __ss_int __ss_WIFSIGNALED(__ss_int status) { return WIFSIGNALED(status); } __ss_int __ss_WIFSTOPPED(__ss_int status) { return WIFSTOPPED(status); } __ss_int __ss_WSTOPSIG(__ss_int status) { return WSTOPSIG(status); } __ss_int __ss_WTERMSIG(__ss_int status) { return WTERMSIG(status); } void *fchdir(__ss_int f1) { if (::fchdir(f1) == -1) throw new OSError(new str("os.fchdir failed")); return NULL; } str *readlink(str *path) { __ss_int size = 255; str *r; while (1) { char *buffer = (char *) GC_malloc (size); __ss_int nchars = ::readlink(path->unit.c_str(), buffer, size); if (nchars == -1) { throw new OSError(path); } if (nchars < size) { buffer[nchars] = '\0'; r = new str(buffer); return r; } size *= 2; } } __ss_int getuid() { return ::getuid(); } void *setuid(__ss_int uid) { if(::setuid(uid) == -1) throw new OSError(new str("os.setuid")); return NULL; } __ss_int getgid() { return ::getgid(); } void *setgid(__ss_int gid) { if(::setgid(gid) == -1) throw new OSError(new str("os.setgid")); return NULL; } __ss_int geteuid() { return ::geteuid(); } void *seteuid(__ss_int euid) { if(::seteuid(euid) == -1) throw new OSError(new str("os.seteuid")); return NULL; } __ss_int getegid() { return ::getegid(); } void *setegid(__ss_int egid) { if(::setegid(egid) == -1) throw new OSError(new str("os.setegid")); return NULL; } __ss_int getppid() { return ::getppid(); } void *setreuid(__ss_int ruid, __ss_int euid) { if(::setreuid(ruid, euid) == -1) throw new OSError(new str("os.setreuid")); return NULL; } void *setregid(__ss_int rgid, __ss_int egid) { if(::setregid(rgid, egid) == -1) throw new OSError(new str("os.setregid")); return NULL; } __ss_int tcgetpgrp(__ss_int fd) { __ss_int nr; nr = ::tcgetpgrp(fd); if(nr == -1) throw new OSError(new str("os.tcgetpgrp")); return nr; } void *tcsetpgrp(__ss_int fd, __ss_int pg) { if(::tcsetpgrp(fd, pg) == -1) throw new OSError(new str("os.tcsetpgrp")); return NULL; } __ss_int fork() { __ss_int ret; if ((ret = ::fork()) == -1) throw new OSError(new str("os.fork")); return ret; } void *ftruncate(__ss_int fd, __ss_int n) { if (::ftruncate(fd, n) == -1) throw new OSError(new str("os.ftruncate")); return NULL; } #if !defined(__sun) tuple2<__ss_int, __ss_int> *forkpty() { __ss_int ret; int amaster; if ((ret = ::forkpty(&amaster, NULL, NULL, NULL)) == -1) throw new OSError(new str("os.forkpty")); return new tuple2<__ss_int, __ss_int>(2, ret, (__ss_int)amaster); } tuple2<__ss_int, __ss_int> *openpty() { int amaster, aslave; if (::openpty(&amaster, &aslave, NULL, NULL, NULL) == -1) throw new OSError(new str("os.openpty")); return new tuple2<__ss_int, __ss_int>(2, (__ss_int)amaster, (__ss_int)aslave); } #endif tuple2<__ss_int, __ss_int> *wait() { int pid, status; if((pid = ::wait(&status)) == -1) throw new OSError(new str("os.wait")); return new tuple2<__ss_int, __ss_int>(2, (__ss_int)pid, (__ss_int)status); } tuple2<__ss_int, __ss_int> *waitpid(__ss_int pid, __ss_int options) { int status; if((pid = ::waitpid(pid, &status, options)) == -1) throw new OSError(new str("os.waitpid")); return new tuple2<__ss_int, __ss_int>(2, pid, (__ss_int)status); } __ss_int nice(__ss_int n) { __ss_int m; if((m = ::nice(n)) == -1) throw new OSError(new str("os.nice")); return m; } void *kill(__ss_int pid, __ss_int sig) { if(::kill(pid, sig) == -1) throw new OSError(new str("os.kill")); return NULL; } void *killpg(__ss_int pgid, __ss_int sig) { if(::killpg(pgid, sig) == -1) throw new OSError(new str("os.killpg")); return NULL; } str *getlogin() { char *name = ::getlogin(); if(!name) throw new OSError(new str("os.getlogin")); return new str(name); } void *chown(str *path, __ss_int uid, __ss_int gid) { if (::chown(path->unit.c_str(), uid, gid) == -1) throw new OSError(path); return NULL; } void *lchown(str *path, __ss_int uid, __ss_int gid) { if (::lchown(path->unit.c_str(), uid, gid) == -1) throw new OSError(path); return NULL; } void *chroot(str *path) { if (::chroot(path->unit.c_str()) == -1) throw new OSError(path); return NULL; } str *ctermid() { char term[L_ctermid]; char *ptr = ::ctermid(term); return new str(ptr); } __ss_bool isatty(__ss_int fd) { return __mbool(::isatty(fd)); } str *ttyname(__ss_int fd) { char *name = ::ttyname(fd); if(!name) throw new OSError(new str("os.ttyname")); return new str(name); } tuple2 *uname() { struct utsname name; ::uname(&name); return new tuple2(5, new str(name.sysname), new str(name.nodename), new str(name.release), new str(name.version), new str(name.machine)); } list<__ss_int> *getgroups() { gid_t l[MAXENTRIES]; __ss_int nr = ::getgroups(MAXENTRIES, l); if(nr == -1) throw new OSError(new str("os.getgroups")); list<__ss_int> *r = new list<__ss_int>(); for(__ss_int i=0;iappend(l[i]); return r; } void *setgroups(pyseq<__ss_int> *groups) { gid_t l[MAXENTRIES]; for(__ss_int i=0; i__getitem__(i); if(::setgroups(len(groups), l) == -1) throw new OSError(new str("os.setgroups")); return NULL; } __ss_int getsid(__ss_int pid) { __ss_int nr = ::getsid(pid); if(nr == -1) throw new OSError(new str("os.getsid")); return nr; } __ss_int setsid() { __ss_int nr = ::setsid(); if(nr == -1) throw new OSError(new str("os.setsid")); return nr; } __ss_int getpgid(__ss_int pid) { __ss_int nr = ::getpgid(pid); if(nr == -1) throw new OSError(new str("os.getpgid")); return nr; } void *setpgid(__ss_int pid, __ss_int pgrp) { if(::setpgid(pid, pgrp) == -1) throw new OSError(new str("os.setpgid")); return NULL; } __ss_int getpgrp() { return getpgid(0); } void *setpgrp() { if(::setpgid(0, 0) == -1) throw new OSError(new str("os.setpgrp")); return NULL; } void *link(str *src, str *dst) { if(::link(src->unit.c_str(), dst->unit.c_str()) == -1) throw new OSError(new str("os.link")); return NULL; } void *symlink(str *src, str *dst) { if(::symlink(src->unit.c_str(), dst->unit.c_str()) == -1) throw new OSError(new str("os.symlink")); return NULL; } __ss_int pathconf(str *path, str *name) { if(!pathconf_names->__contains__(name)) throw new ValueError(new str("unrecognized configuration name")); return pathconf(path, pathconf_names->__getitem__(name)); /* XXX errors */ } __ss_int pathconf(str *path, __ss_int name) { __ss_int limit = ::pathconf(path->unit.c_str(), name); /* XXX errors */ return limit; } __ss_int fpathconf(__ss_int fd, str *name) { if(!pathconf_names->__contains__(name)) throw new ValueError(new str("unrecognized configuration name")); return fpathconf(fd, pathconf_names->__getitem__(name)); /* XXX errors */ } __ss_int fpathconf(__ss_int fd, __ss_int name) { __ss_int limit = ::fpathconf(fd, name); /* XXX errors */ return limit; } str *confstr(str *name) { if(!confstr_names->__contains__(name)) throw new ValueError(new str("unrecognized configuration name")); return confstr(confstr_names->__getitem__(name)); } str *confstr(__ss_int name) { char buf[MAXENTRIES]; __ss_int size = ::confstr(name, buf, MAXENTRIES); /* XXX errors */ if(size == -1) throw new OSError(new str("os.confstr")); return new str(buf); } __ss_int sysconf(str *name) { if(!sysconf_names->__contains__(name)) throw new ValueError(new str("unrecognized configuration name")); return sysconf(sysconf_names->__getitem__(name)); /* XXX errors */ } __ss_int sysconf(__ss_int name) { __ss_int limit = ::sysconf(name); /* XXX errors */ return limit; } tuple2 *getloadavg() { #ifdef __CYGWIN__ throw new NotImplementedError(); #else double load[3]; if(::getloadavg(load, 3) != 3) throw new OSError(new str("os.getloadavg")); return new tuple2(3, load[0], load[1], load[2]); #endif } void *mkfifo(str *path, __ss_int mode) { if(::mkfifo(path->unit.c_str(), mode) == -1) throw new OSError(new str("os.mkfifo")); return NULL; } /* class __vfsstat */ class_ *cl___vfsstat; __vfsstat::__vfsstat(str *path) { this->__class__ = cl___vfsstat; if(statvfs(path->unit.c_str(), &vbuf) == -1) throw new OSError(path); fill_er_up(); } __vfsstat::__vfsstat(__ss_int fd) { this->__class__ = cl___vfsstat; if(fstatvfs(fd, &vbuf) == -1) throw new OSError(__str(fd)); fill_er_up(); } void __vfsstat::fill_er_up() { this->f_bsize = vbuf.f_bsize; this->f_frsize = vbuf.f_frsize; this->f_blocks = vbuf.f_blocks; this->f_bfree = vbuf.f_bfree; this->f_bavail = vbuf.f_bavail; this->f_files = vbuf.f_files; this->f_ffree = vbuf.f_ffree; this->f_favail = vbuf.f_favail; this->f_flag = vbuf.f_flag; this->f_namemax = vbuf.f_namemax; } __ss_int __vfsstat::__len__() { return 10; } __ss_int __vfsstat::__getitem__(__ss_int i) { i = __wrap(this, i); switch(i) { case 0: return vbuf.f_bsize; case 1: return vbuf.f_frsize; case 2: return vbuf.f_blocks; case 3: return vbuf.f_bfree; case 4: return vbuf.f_bavail; case 5: return vbuf.f_files; case 6: return vbuf.f_ffree; case 7: return vbuf.f_favail; case 8: return vbuf.f_flag; case 9: return vbuf.f_namemax; default: throw new IndexError(new str("tuple index out of range")); } return 0; } __vfsstat *statvfs(str *path) { return new __vfsstat(path); } __vfsstat *fstatvfs(__ss_int fd) { return new __vfsstat(fd); } void *fsync(__ss_int fd) { if(::fsync(fd) == -1) throw new OSError(new str("os.fsync")); return NULL; } void *lseek(__ss_int fd, __ss_int pos, __ss_int how) { if(::lseek(fd, pos, how) == -1) throw new OSError(new str("os.lseek")); return NULL; } str *urandom(__ss_int n) { __ss_int fd = open(new str("/dev/urandom"), __ss_O_RDONLY); str *s = read(fd, n); close(fd); return s; } __ss_bool access(str *path, __ss_int mode) { return __mbool(::access(path->unit.c_str(), mode) == 0); } tuple2 *times() { struct tms buf; clock_t c; __ss_int ticks_per_second = ::sysconf(_SC_CLK_TCK); if((c = ::times(&buf)) == -1) throw new OSError(new str("os.utime")); return new tuple2(5, ((double)buf.tms_utime / ticks_per_second), ((double)buf.tms_stime / ticks_per_second), ((double)buf.tms_cutime / ticks_per_second), ((double)buf.tms_cstime / ticks_per_second), ((double)c / ticks_per_second)); } /* str *tmpnam() { XXX raises compiler warning char *buf; if((buf = ::tmpnam(NULL)) == NULL) throw new OSError(new str("os.tmpnam")); return new str(buf); } */ file *tmpfile() { FILE *f; if((f = ::tmpfile()) == NULL) throw new OSError(new str("os.tmpfile")); file *_file = new file(f); _file->name = new str(""); return _file; } /* str *tempnam(str *dir, str *prefix) { XXX raises compiler warning char *name; str *result; char *pfx = NULL; if(prefix) pfx = (char *)(prefix->unit.c_str()); if((name = ::tempnam(dir->unit.c_str(), pfx)) == NULL) throw new OSError(new str("os.tempnam")); result = new str(name); free(name); return result; } */ __ss_int __ss_makedev(__ss_int major, __ss_int minor) { return makedev(major, minor); } __ss_int __ss_major(__ss_int dev) { return major(dev); } __ss_int __ss_minor(__ss_int dev) { return minor(dev); } void *mknod(str *filename, __ss_int mode, __ss_int device) { if(::mknod(filename->unit.c_str(), mode, device) == -1) throw new OSError(new str("os.mknod")); return NULL; } char **__exec_argvlist(list *args) { char** argvlist = (char**)GC_malloc(sizeof(char*)*(args->__len__()+1)); for(__ss_int i = 0; i < args->__len__(); ++i) { argvlist[i] = (char *)(args->__getitem__(i)->unit.c_str()); } argvlist[args->__len__()] = NULL; return argvlist; } char **__exec_envplist(dict *env) { char** envplist = (char**)GC_malloc(sizeof(char*)*(env->__len__()+1)); list *> *items = env->items(); for(__ss_int i=0; i < items->__len__(); i++) { envplist[i] = (char *)(__add_strs(3, items->__getitem__(i)->__getfirst__(), new str("="), items->__getitem__(i)->__getsecond__())->unit.c_str()); } envplist[items->__len__()] = NULL; return envplist; } list *__exec_path() { str* envpath; if(__ss_environ->__contains__(new str("PATH"))) envpath = __ss_environ->get(new str("PATH")); else envpath = defpath; return envpath->split(pathsep); } void *execl(__ss_int n, str *file, ...) { list *vals = new list(); va_list args; va_start(args, file); for(__ss_int i=0; iappend(va_arg(args, str *)); /* XXX check str */ va_end(args); execv(file, vals); return NULL; } void *execlp(__ss_int n, str *file, ...) { list *vals = new list(); va_list args; va_start(args, file); for(__ss_int i=0; iappend(va_arg(args, str *)); /* XXX check str */ va_end(args); execvp(file, vals); return NULL; } void *execle(__ss_int n, str *file, ...) { list *vals = new list(); va_list args; va_start(args, file); for(__ss_int i=0; iappend(va_arg(args, str *)); /* XXX check str */ va_end(args); dict *env = (dict *)(va_arg(args, pyobj *)); /* XXX check */ execve(file, vals, env); return NULL; } void *execlpe(__ss_int n, str *file, ...) { list *vals = new list(); va_list args; va_start(args, file); for(__ss_int i=0; iappend(va_arg(args, str *)); /* XXX check str */ va_end(args); dict *env = (dict *)(va_arg(args, pyobj *)); /* XXX check */ execvpe(file, vals, env); return NULL; } void *execv(str* file, list* args) { ::execv(file->unit.c_str(), __exec_argvlist(args)); throw new OSError(new str("os.execv")); } void *execvp(str* file, list* args) { tuple2* h_t = __path__::split(file); if( ___bool(h_t->__getfirst__())) { execv(file,args); throw new OSError(new str("os.execvp")); } list *PATH = __exec_path(); for(__ss_int i = 0; i < PATH->__len__(); ++i) { str* dir = PATH->__getfast__(i); str* fullname = __path__::join(2, dir, file); if(__path__::exists(fullname)) { execv(fullname, args); } } throw new OSError(new str("os.execvp")); } void *execve(str* file, list* args, dict *env) { ::execve(file->unit.c_str(), __exec_argvlist(args), __exec_envplist(env)); throw new OSError(new str("os.execve")); } void *execvpe(str* file, list* args, dict *env) { tuple2* h_t = __path__::split(file); if( ___bool(h_t->__getfirst__())) { execve(file, args, env); throw new OSError(new str("os.execvpe")); } list *PATH = __exec_path(); for(__ss_int i = 0; i < PATH->__len__(); ++i) { str* dir = PATH->__getfast__(i); str* fullname = __path__::join(2, dir, file); if(__path__::exists(fullname)) execve(fullname, args, env); } throw new OSError(new str("os.execvpe")); } __ss_int spawnl(__ss_int n, __ss_int mode, str *file, ...) { list *vals = new list(); va_list args; va_start(args, file); for(__ss_int i=0; iappend(va_arg(args, str *)); /* XXX check str */ va_end(args); return spawnv(mode, file, vals); } __ss_int spawnlp(__ss_int n, __ss_int mode, str *file, ...) { list *vals = new list(); va_list args; va_start(args, file); for(__ss_int i=0; iappend(va_arg(args, str *)); /* XXX check str */ va_end(args); return spawnvp(mode, file, vals); } __ss_int spawnle(__ss_int n, __ss_int mode, str *file, ...) { list *vals = new list(); va_list args; va_start(args, file); for(__ss_int i=0; iappend(va_arg(args, str *)); /* XXX check str */ va_end(args); dict *env = (dict *)(va_arg(args, pyobj *)); /* XXX check */ return spawnve(mode, file, vals, env); } __ss_int spawnlpe(__ss_int n, __ss_int mode, str *file, ...) { list *vals = new list(); va_list args; va_start(args, file); for(__ss_int i=0; iappend(va_arg(args, str *)); /* XXX check str */ va_end(args); dict *env = (dict *)(va_arg(args, pyobj *)); /* XXX check */ return spawnvpe(mode, file, vals, env); } __ss_int spawnv(__ss_int mode, str *file, list *args) { __ss_int pid; tuple2<__ss_int, __ss_int> *t; if(!(pid = fork())) /* XXX no spawn* for C++..? */ execv(file, args); else if (mode == __ss_P_WAIT) { t = waitpid(pid, 0); return t->__getsecond__(); } return pid; } __ss_int spawnvp(__ss_int mode, str *file, list *args) { __ss_int pid; tuple2<__ss_int, __ss_int> *t; if(!(pid = fork())) /* XXX no spawn* for C++..? */ execvp(file, args); else if (mode == __ss_P_WAIT) { t = waitpid(pid, 0); return t->__getsecond__(); } return pid; } __ss_int spawnve(__ss_int mode, str *file, list *args, dict *env) { __ss_int pid; tuple2<__ss_int, __ss_int> *t; if(!(pid = fork())) /* XXX no spawn* for C++..? */ execve(file, args, env); else if (mode == __ss_P_WAIT) { t = waitpid(pid, 0); return t->__getsecond__(); } return pid; } __ss_int spawnvpe(__ss_int mode, str *file, list *args, dict *env) { __ss_int pid; tuple2<__ss_int, __ss_int> *t; if(!(pid = fork())) /* XXX no spawn* for C++..? */ execvpe(file, args, env); else if (mode == __ss_P_WAIT) { t = waitpid(pid, 0); return t->__getsecond__(); } return pid; } __ss_int getpid() { //return GetCurrentProcessId(); return ::getpid(); } void *unsetenv (str* var) { ::unsetenv(var->unit.c_str()); return NULL; } tuple2* popen2(str* cmd) { return popen2(cmd, new str("t"), -1); } tuple2* popen2(pyiter *cmd_l) { return popen2(cmd_l, new str("t"), -1); } tuple2* popen2(pyiter *cmd_i, str *, __ss_int) { list *cmd_l = new list(cmd_i); // TODO what if there is not even 1 element in cmd_l ? tuple2<__ss_int,__ss_int>* p2c = pipe(); tuple2<__ss_int,__ss_int>* c2p = pipe(); __ss_int pid = fork(); if(pid == 0) { dup2( p2c->__getfirst__(), 0); dup2( c2p->__getsecond__(), 1); for(__ss_int i = 3; i < MAXENTRIES; ++i) { try { close(i); } catch(OSError*) {} } execvp(cmd_l->__getitem__(0), cmd_l); /* XXX pass cmd_i? */ ::exit(1); } close(p2c->__getfirst__()); close(c2p->__getsecond__()); tuple2* ret = new tuple2(); ret->__init2__(fdopen(p2c->__getsecond__(),new str("w")), fdopen(c2p->__getfirst__(), new str("r"))); return ret; } tuple2* popen2(str* cmd, str*, __ss_int) { list* cmd_l = new list(3, new str("/bin/sh"), new str("-c"), cmd); return popen2(cmd_l); } tuple2* popen3(str* cmd) { return popen3(cmd, new str("t"), -1); } tuple2* popen3(str* cmd, str*, __ss_int) { tuple2<__ss_int,__ss_int>* p2c = pipe(); tuple2<__ss_int,__ss_int>* c2p = pipe(); tuple2<__ss_int,__ss_int>* erp = pipe(); __ss_int pid = fork(); if(pid == 0) { dup2( p2c->__getfirst__(), 0); dup2( c2p->__getsecond__(), 1); dup2( erp->__getsecond__(), 2); for(__ss_int i = 3; i < MAXENTRIES; ++i) { try { close(i); } catch(OSError*) {} } list* cmd_l = new list(3, new str("/bin/sh"), new str("-c"), cmd); execvp(new str("/bin/sh"), cmd_l); ::exit(1); } close(p2c->__getfirst__()); close(c2p->__getsecond__()); close(erp->__getsecond__()); return new tuple2(3,fdopen(p2c->__getsecond__(),new str("w")), fdopen(c2p->__getfirst__(), new str("r")), fdopen(erp->__getfirst__(), new str("r")) ); } tuple2* popen4(str* cmd) { return popen4(cmd, new str("t"), -1); } tuple2* popen4(str* cmd, str*, __ss_int) { tuple2<__ss_int,__ss_int>* p2c = pipe(); tuple2<__ss_int,__ss_int>* c2p = pipe(); __ss_int pid = fork(); if(pid == 0) { dup2( p2c->__getfirst__(), 0); dup2( c2p->__getsecond__(), 1); dup2( c2p->__getsecond__(), 2); for(__ss_int i = 3; i < MAXENTRIES; ++i) { try { close(i); } catch(OSError*) {} } list* cmd_l = new list(3, new str("/bin/sh"), new str("-c"), cmd); execvp(new str("/bin/sh"), cmd_l); ::exit(1); } close(p2c->__getfirst__()); close(c2p->__getsecond__()); tuple2* ret = new tuple2(); ret->__init2__(fdopen(p2c->__getsecond__(),new str("w")), fdopen(c2p->__getfirst__(), new str("r"))); return ret; } tuple2<__ss_int,__ss_int>* pipe() { int fds[2]; __ss_int ret; ret = ::pipe(fds); if(ret != 0) { str* s = new str("os.pipe failed"); throw new OSError(s); } return new tuple2<__ss_int,__ss_int>(2,(__ss_int)fds[0],(__ss_int)fds[1]); } #endif void __init() { const_0 = new str("(%d, %d, %d, %d, %d, %d, %d, %d, %d, %d)"); cl___cstat = new class_("__cstat"); linesep = new str("\n"); #ifdef WIN32 name = new str("nt"); #else name = new str("posix"); cl___vfsstat = new class_("__vfsstat"); #endif __ss_environ = new dict(); for (__ss_int n = 0; environ[n]; n++) { str *line = new str(environ[n]); __ss_int pos = line->find(new str("=")); __ss_environ->__setitem__(line->__slice__(2, 0, pos, 0), line->__slice__(1, (pos+1), 0, 0)); } __path__::__init(); /* ugh */ curdir = __path__::curdir; pardir = __path__::pardir; extsep = __path__::extsep; sep = __path__::sep; pathsep = __path__::pathsep; defpath = __path__::defpath; altsep = __path__::altsep; devnull = __path__::devnull; #ifdef F_OK __ss_F_OK = F_OK; #endif #ifdef R_OK __ss_R_OK = R_OK; #endif #ifdef W_OK __ss_W_OK = W_OK; #endif #ifdef X_OK __ss_X_OK = X_OK; #endif #ifdef NGROUPS_MAX __ss_NGROUPS_MAX = NGROUPS_MAX; #endif #ifdef TMP_MAX __ss_TMP_MAX = TMP_MAX; #endif #ifdef WCONTINUED __ss_WCONTINUED = WCONTINUED; #endif #ifdef WNOHANG __ss_WNOHANG = WNOHANG; #endif #ifdef WUNTRACED __ss_WUNTRACED = WUNTRACED; #endif #ifdef O_RDONLY __ss_O_RDONLY = O_RDONLY; #endif #ifdef O_WRONLY __ss_O_WRONLY = O_WRONLY; #endif #ifdef O_RDWR __ss_O_RDWR = O_RDWR; #endif #ifdef O_NDELAY __ss_O_NDELAY = O_NDELAY; #endif #ifdef O_NONBLOCK __ss_O_NONBLOCK = O_NONBLOCK; #endif #ifdef O_APPEND __ss_O_APPEND = O_APPEND; #endif #ifdef O_DSYNC __ss_O_DSYNC = O_DSYNC; #endif #ifdef O_RSYNC __ss_O_RSYNC = O_RSYNC; #endif #ifdef O_SYNC __ss_O_SYNC = O_SYNC; #endif #ifdef O_NOCTTY __ss_O_NOCTTY = O_NOCTTY; #endif #ifdef O_CREAT __ss_O_CREAT = O_CREAT; #endif #ifdef O_EXCL __ss_O_EXCL = O_EXCL; #endif #ifdef O_TRUNC __ss_O_TRUNC = O_TRUNC; #endif #ifdef O_BINARY __ss_O_BINARY = O_BINARY; #endif #ifdef O_TEXT __ss_O_TEXT = O_TEXT; #endif #ifdef O_LARGEFILE __ss_O_LARGEFILE = O_LARGEFILE; #endif #ifdef O_SHLOCK __ss_O_SHLOCK = O_SHLOCK; #endif #ifdef O_EXLOCK __ss_O_EXLOCK = O_EXLOCK; #endif #ifdef O_NOINHERIT __ss_O_NOINHERIT = O_NOINHERIT; #endif #ifdef _O_SHORT_LIVED __ss__O_SHORT_LIVED = _O_SHORT_LIVED; #endif #ifdef O_TEMPORARY __ss_O_TEMPORARY = O_TEMPORARY; #endif #ifdef O_RANDOM __ss_O_RANDOM = O_RANDOM; #endif #ifdef O_SEQUENTIAL __ss_O_SEQUENTIAL = O_SEQUENTIAL; #endif #ifdef O_ASYNC __ss_O_ASYNC = O_ASYNC; #endif #ifdef O_DIRECT __ss_O_DIRECT = O_DIRECT; #endif #ifdef O_DIRECTORY __ss_O_DIRECTORY = O_DIRECTORY; #endif #ifdef O_NOFOLLOW __ss_O_NOFOLLOW = O_NOFOLLOW; #endif #ifdef O_NOATIME __ss_O_NOATIME = O_NOATIME; #endif #ifdef EX_OK __ss_EX_OK = EX_OK; #endif #ifdef EX_USAGE __ss_EX_USAGE = EX_USAGE; #endif #ifdef EX_DATAERR __ss_EX_DATAERR = EX_DATAERR; #endif #ifdef EX_NOINPUT __ss_EX_NOINPUT = EX_NOINPUT; #endif #ifdef EX_NOUSER __ss_EX_NOUSER = EX_NOUSER; #endif #ifdef EX_NOHOST __ss_EX_NOHOST = EX_NOHOST; #endif #ifdef EX_UNAVAILABLE __ss_EX_UNAVAILABLE = EX_UNAVAILABLE; #endif #ifdef EX_SOFTWARE __ss_EX_SOFTWARE = EX_SOFTWARE; #endif #ifdef EX_OSERR __ss_EX_OSERR = EX_OSERR; #endif #ifdef EX_OSFILE __ss_EX_OSFILE = EX_OSFILE; #endif #ifdef EX_CANTCREAT __ss_EX_CANTCREAT = EX_CANTCREAT; #endif #ifdef EX_IOERR __ss_EX_IOERR = EX_IOERR; #endif #ifdef EX_TEMPFAIL __ss_EX_TEMPFAIL = EX_TEMPFAIL; #endif #ifdef EX_PROTOCOL __ss_EX_PROTOCOL = EX_PROTOCOL; #endif #ifdef EX_NOPERM __ss_EX_NOPERM = EX_NOPERM; #endif #ifdef EX_CONFIG __ss_EX_CONFIG = EX_CONFIG; #endif #ifdef EX_NOTFOUND __ss_EX_NOTFOUND = EX_NOTFOUND; #endif __ss_P_WAIT = 0; /* XXX */ __ss_P_NOWAIT = 1; __ss_P_NOWAITO = 1; __ss_P_OVERLAY = 2; __ss_P_DETACH = 3; #ifdef SEEK_CUR __ss_SEEK_CUR = SEEK_CUR; #endif #ifdef SEEK_END __ss_SEEK_END = SEEK_END; #endif #ifdef SEEK_SET __ss_SEEK_SET = SEEK_SET; #endif pathconf_names = new dict(); #ifdef _PC_ABI_AIO_XFER_MAX pathconf_names->__setitem__(new str("PC_ABI_AIO_XFER_MAX"), _PC_ABI_AIO_XFER_MAX); #endif #ifdef _PC_ABI_ASYNC_IO pathconf_names->__setitem__(new str("PC_ABI_ASYNC_IO"), _PC_ABI_ASYNC_IO); #endif #ifdef _PC_ASYNC_IO pathconf_names->__setitem__(new str("PC_ASYNC_IO"), _PC_ASYNC_IO); #endif #ifdef _PC_CHOWN_RESTRICTED pathconf_names->__setitem__(new str("PC_CHOWN_RESTRICTED"), _PC_CHOWN_RESTRICTED); #endif #ifdef _PC_FILESIZEBITS pathconf_names->__setitem__(new str("PC_FILESIZEBITS"), _PC_FILESIZEBITS); #endif #ifdef _PC_LAST pathconf_names->__setitem__(new str("PC_LAST"), _PC_LAST); #endif #ifdef _PC_LINK_MAX pathconf_names->__setitem__(new str("PC_LINK_MAX"), _PC_LINK_MAX); #endif #ifdef _PC_MAX_CANON pathconf_names->__setitem__(new str("PC_MAX_CANON"), _PC_MAX_CANON); #endif #ifdef _PC_MAX_INPUT pathconf_names->__setitem__(new str("PC_MAX_INPUT"), _PC_MAX_INPUT); #endif #ifdef _PC_NAME_MAX pathconf_names->__setitem__(new str("PC_NAME_MAX"), _PC_NAME_MAX); #endif #ifdef _PC_NO_TRUNC pathconf_names->__setitem__(new str("PC_NO_TRUNC"), _PC_NO_TRUNC); #endif #ifdef _PC_PATH_MAX pathconf_names->__setitem__(new str("PC_PATH_MAX"), _PC_PATH_MAX); #endif #ifdef _PC_PIPE_BUF pathconf_names->__setitem__(new str("PC_PIPE_BUF"), _PC_PIPE_BUF); #endif #ifdef _PC_PRIO_IO pathconf_names->__setitem__(new str("PC_PRIO_IO"), _PC_PRIO_IO); #endif #ifdef _PC_SOCK_MAXBUF pathconf_names->__setitem__(new str("PC_SOCK_MAXBUF"), _PC_SOCK_MAXBUF); #endif #ifdef _PC_SYNC_IO pathconf_names->__setitem__(new str("PC_SYNC_IO"), _PC_SYNC_IO); #endif #ifdef _PC_VDISABLE pathconf_names->__setitem__(new str("PC_VDISABLE"), _PC_VDISABLE); #endif confstr_names = new dict(); #ifdef _CS_ARCHITECTURE confstr_names->__setitem__(new str("CS_ARCHITECTURE"), _CS_ARCHITECTURE); #endif #ifdef _CS_HOSTNAME confstr_names->__setitem__(new str("CS_HOSTNAME"), _CS_HOSTNAME); #endif #ifdef _CS_HW_PROVIDER confstr_names->__setitem__(new str("CS_HW_PROVIDER"), _CS_HW_PROVIDER); #endif #ifdef _CS_HW_SERIAL confstr_names->__setitem__(new str("CS_HW_SERIAL"), _CS_HW_SERIAL); #endif #ifdef _CS_INITTAB_NAME confstr_names->__setitem__(new str("CS_INITTAB_NAME"), _CS_INITTAB_NAME); #endif #ifdef _CS_LFS64_CFLAGS confstr_names->__setitem__(new str("CS_LFS64_CFLAGS"), _CS_LFS64_CFLAGS); #endif #ifdef _CS_LFS64_LDFLAGS confstr_names->__setitem__(new str("CS_LFS64_LDFLAGS"), _CS_LFS64_LDFLAGS); #endif #ifdef _CS_LFS64_LIBS confstr_names->__setitem__(new str("CS_LFS64_LIBS"), _CS_LFS64_LIBS); #endif #ifdef _CS_LFS64_LINTFLAGS confstr_names->__setitem__(new str("CS_LFS64_LINTFLAGS"), _CS_LFS64_LINTFLAGS); #endif #ifdef _CS_LFS_CFLAGS confstr_names->__setitem__(new str("CS_LFS_CFLAGS"), _CS_LFS_CFLAGS); #endif #ifdef _CS_LFS_LDFLAGS confstr_names->__setitem__(new str("CS_LFS_LDFLAGS"), _CS_LFS_LDFLAGS); #endif #ifdef _CS_LFS_LIBS confstr_names->__setitem__(new str("CS_LFS_LIBS"), _CS_LFS_LIBS); #endif #ifdef _CS_LFS_LINTFLAGS confstr_names->__setitem__(new str("CS_LFS_LINTFLAGS"), _CS_LFS_LINTFLAGS); #endif #ifdef _CS_MACHINE confstr_names->__setitem__(new str("CS_MACHINE"), _CS_MACHINE); #endif #ifdef _CS_PATH confstr_names->__setitem__(new str("CS_PATH"), _CS_PATH); #endif #ifdef _CS_RELEASE confstr_names->__setitem__(new str("CS_RELEASE"), _CS_RELEASE); #endif #ifdef _CS_SRPC_DOMAIN confstr_names->__setitem__(new str("CS_SRPC_DOMAIN"), _CS_SRPC_DOMAIN); #endif #ifdef _CS_SYSNAME confstr_names->__setitem__(new str("CS_SYSNAME"), _CS_SYSNAME); #endif #ifdef _CS_VERSION confstr_names->__setitem__(new str("CS_VERSION"), _CS_VERSION); #endif #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS confstr_names->__setitem__(new str("CS_XBS5_ILP32_OFF32_CFLAGS"), _CS_XBS5_ILP32_OFF32_CFLAGS); #endif #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS confstr_names->__setitem__(new str("CS_XBS5_ILP32_OFF32_LDFLAGS"), _CS_XBS5_ILP32_OFF32_LDFLAGS); #endif #ifdef _CS_XBS5_ILP32_OFF32_LIBS confstr_names->__setitem__(new str("CS_XBS5_ILP32_OFF32_LIBS"), _CS_XBS5_ILP32_OFF32_LIBS); #endif #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS confstr_names->__setitem__(new str("CS_XBS5_ILP32_OFF32_LINTFLAGS"), _CS_XBS5_ILP32_OFF32_LINTFLAGS); #endif #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS confstr_names->__setitem__(new str("CS_XBS5_ILP32_OFFBIG_CFLAGS"), _CS_XBS5_ILP32_OFFBIG_CFLAGS); #endif #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS confstr_names->__setitem__(new str("CS_XBS5_ILP32_OFFBIG_LDFLAGS"), _CS_XBS5_ILP32_OFFBIG_LDFLAGS); #endif #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS confstr_names->__setitem__(new str("CS_XBS5_ILP32_OFFBIG_LIBS"), _CS_XBS5_ILP32_OFFBIG_LIBS); #endif #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS confstr_names->__setitem__(new str("CS_XBS5_ILP32_OFFBIG_LINTFLAGS"), _CS_XBS5_ILP32_OFFBIG_LINTFLAGS); #endif #ifdef _CS_XBS5_LP64_OFF64_CFLAGS confstr_names->__setitem__(new str("CS_XBS5_LP64_OFF64_CFLAGS"), _CS_XBS5_LP64_OFF64_CFLAGS); #endif #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS confstr_names->__setitem__(new str("CS_XBS5_LP64_OFF64_LDFLAGS"), _CS_XBS5_LP64_OFF64_LDFLAGS); #endif #ifdef _CS_XBS5_LP64_OFF64_LIBS confstr_names->__setitem__(new str("CS_XBS5_LP64_OFF64_LIBS"), _CS_XBS5_LP64_OFF64_LIBS); #endif #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS confstr_names->__setitem__(new str("CS_XBS5_LP64_OFF64_LINTFLAGS"), _CS_XBS5_LP64_OFF64_LINTFLAGS); #endif #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS confstr_names->__setitem__(new str("CS_XBS5_LPBIG_OFFBIG_CFLAGS"), _CS_XBS5_LPBIG_OFFBIG_CFLAGS); #endif #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS confstr_names->__setitem__(new str("CS_XBS5_LPBIG_OFFBIG_LDFLAGS"), _CS_XBS5_LPBIG_OFFBIG_LDFLAGS); #endif #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS confstr_names->__setitem__(new str("CS_XBS5_LPBIG_OFFBIG_LIBS"), _CS_XBS5_LPBIG_OFFBIG_LIBS); #endif #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS confstr_names->__setitem__(new str("CS_XBS5_LPBIG_OFFBIG_LINTFLAGS"), _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS); #endif #ifdef _MIPS_CS_AVAIL_PROCESSORS confstr_names->__setitem__(new str("MIPS_CS_AVAIL_PROCESSORS"), _MIPS_CS_AVAIL_PROCESSORS); #endif #ifdef _MIPS_CS_BASE confstr_names->__setitem__(new str("MIPS_CS_BASE"), _MIPS_CS_BASE); #endif #ifdef _MIPS_CS_HOSTID confstr_names->__setitem__(new str("MIPS_CS_HOSTID"), _MIPS_CS_HOSTID); #endif #ifdef _MIPS_CS_HW_NAME confstr_names->__setitem__(new str("MIPS_CS_HW_NAME"), _MIPS_CS_HW_NAME); #endif #ifdef _MIPS_CS_NUM_PROCESSORS confstr_names->__setitem__(new str("MIPS_CS_NUM_PROCESSORS"), _MIPS_CS_NUM_PROCESSORS); #endif #ifdef _MIPS_CS_OSREL_MAJ confstr_names->__setitem__(new str("MIPS_CS_OSREL_MAJ"), _MIPS_CS_OSREL_MAJ); #endif #ifdef _MIPS_CS_OSREL_MIN confstr_names->__setitem__(new str("MIPS_CS_OSREL_MIN"), _MIPS_CS_OSREL_MIN); #endif #ifdef _MIPS_CS_OSREL_PATCH confstr_names->__setitem__(new str("MIPS_CS_OSREL_PATCH"), _MIPS_CS_OSREL_PATCH); #endif #ifdef _MIPS_CS_OS_NAME confstr_names->__setitem__(new str("MIPS_CS_OS_NAME"), _MIPS_CS_OS_NAME); #endif #ifdef _MIPS_CS_OS_PROVIDER confstr_names->__setitem__(new str("MIPS_CS_OS_PROVIDER"), _MIPS_CS_OS_PROVIDER); #endif #ifdef _MIPS_CS_PROCESSORS confstr_names->__setitem__(new str("MIPS_CS_PROCESSORS"), _MIPS_CS_PROCESSORS); #endif #ifdef _MIPS_CS_SERIAL confstr_names->__setitem__(new str("MIPS_CS_SERIAL"), _MIPS_CS_SERIAL); #endif #ifdef _MIPS_CS_VENDOR confstr_names->__setitem__(new str("MIPS_CS_VENDOR"), _MIPS_CS_VENDOR); #endif sysconf_names = new dict(); #ifdef _SC_2_CHAR_TERM sysconf_names->__setitem__(new str("SC_2_CHAR_TERM"), _SC_2_CHAR_TERM); #endif #ifdef _SC_2_C_BIND sysconf_names->__setitem__(new str("SC_2_C_BIND"), _SC_2_C_BIND); #endif #ifdef _SC_2_C_DEV sysconf_names->__setitem__(new str("SC_2_C_DEV"), _SC_2_C_DEV); #endif #ifdef _SC_2_C_VERSION sysconf_names->__setitem__(new str("SC_2_C_VERSION"), _SC_2_C_VERSION); #endif #ifdef _SC_2_FORT_DEV sysconf_names->__setitem__(new str("SC_2_FORT_DEV"), _SC_2_FORT_DEV); #endif #ifdef _SC_2_FORT_RUN sysconf_names->__setitem__(new str("SC_2_FORT_RUN"), _SC_2_FORT_RUN); #endif #ifdef _SC_2_LOCALEDEF sysconf_names->__setitem__(new str("SC_2_LOCALEDEF"), _SC_2_LOCALEDEF); #endif #ifdef _SC_2_SW_DEV sysconf_names->__setitem__(new str("SC_2_SW_DEV"), _SC_2_SW_DEV); #endif #ifdef _SC_2_UPE sysconf_names->__setitem__(new str("SC_2_UPE"), _SC_2_UPE); #endif #ifdef _SC_2_VERSION sysconf_names->__setitem__(new str("SC_2_VERSION"), _SC_2_VERSION); #endif #ifdef _SC_ABI_ASYNCHRONOUS_IO sysconf_names->__setitem__(new str("SC_ABI_ASYNCHRONOUS_IO"), _SC_ABI_ASYNCHRONOUS_IO); #endif #ifdef _SC_ACL sysconf_names->__setitem__(new str("SC_ACL"), _SC_ACL); #endif #ifdef _SC_AIO_LISTIO_MAX sysconf_names->__setitem__(new str("SC_AIO_LISTIO_MAX"), _SC_AIO_LISTIO_MAX); #endif #ifdef _SC_AIO_MAX sysconf_names->__setitem__(new str("SC_AIO_MAX"), _SC_AIO_MAX); #endif #ifdef _SC_AIO_PRIO_DELTA_MAX sysconf_names->__setitem__(new str("SC_AIO_PRIO_DELTA_MAX"), _SC_AIO_PRIO_DELTA_MAX); #endif #ifdef _SC_ARG_MAX sysconf_names->__setitem__(new str("SC_ARG_MAX"), _SC_ARG_MAX); #endif #ifdef _SC_ASYNCHRONOUS_IO sysconf_names->__setitem__(new str("SC_ASYNCHRONOUS_IO"), _SC_ASYNCHRONOUS_IO); #endif #ifdef _SC_ATEXIT_MAX sysconf_names->__setitem__(new str("SC_ATEXIT_MAX"), _SC_ATEXIT_MAX); #endif #ifdef _SC_AUDIT sysconf_names->__setitem__(new str("SC_AUDIT"), _SC_AUDIT); #endif #ifdef _SC_AVPHYS_PAGES sysconf_names->__setitem__(new str("SC_AVPHYS_PAGES"), _SC_AVPHYS_PAGES); #endif #ifdef _SC_BC_BASE_MAX sysconf_names->__setitem__(new str("SC_BC_BASE_MAX"), _SC_BC_BASE_MAX); #endif #ifdef _SC_BC_DIM_MAX sysconf_names->__setitem__(new str("SC_BC_DIM_MAX"), _SC_BC_DIM_MAX); #endif #ifdef _SC_BC_SCALE_MAX sysconf_names->__setitem__(new str("SC_BC_SCALE_MAX"), _SC_BC_SCALE_MAX); #endif #ifdef _SC_BC_STRING_MAX sysconf_names->__setitem__(new str("SC_BC_STRING_MAX"), _SC_BC_STRING_MAX); #endif #ifdef _SC_CAP sysconf_names->__setitem__(new str("SC_CAP"), _SC_CAP); #endif #ifdef _SC_CHARCLASS_NAME_MAX sysconf_names->__setitem__(new str("SC_CHARCLASS_NAME_MAX"), _SC_CHARCLASS_NAME_MAX); #endif #ifdef _SC_CHAR_BIT sysconf_names->__setitem__(new str("SC_CHAR_BIT"), _SC_CHAR_BIT); #endif #ifdef _SC_CHAR_MAX sysconf_names->__setitem__(new str("SC_CHAR_MAX"), _SC_CHAR_MAX); #endif #ifdef _SC_CHAR_MIN sysconf_names->__setitem__(new str("SC_CHAR_MIN"), _SC_CHAR_MIN); #endif #ifdef _SC_CHILD_MAX sysconf_names->__setitem__(new str("SC_CHILD_MAX"), _SC_CHILD_MAX); #endif #ifdef _SC_CLK_TCK sysconf_names->__setitem__(new str("SC_CLK_TCK"), _SC_CLK_TCK); #endif #ifdef _SC_COHER_BLKSZ sysconf_names->__setitem__(new str("SC_COHER_BLKSZ"), _SC_COHER_BLKSZ); #endif #ifdef _SC_COLL_WEIGHTS_MAX sysconf_names->__setitem__(new str("SC_COLL_WEIGHTS_MAX"), _SC_COLL_WEIGHTS_MAX); #endif #ifdef _SC_DCACHE_ASSOC sysconf_names->__setitem__(new str("SC_DCACHE_ASSOC"), _SC_DCACHE_ASSOC); #endif #ifdef _SC_DCACHE_BLKSZ sysconf_names->__setitem__(new str("SC_DCACHE_BLKSZ"), _SC_DCACHE_BLKSZ); #endif #ifdef _SC_DCACHE_LINESZ sysconf_names->__setitem__(new str("SC_DCACHE_LINESZ"), _SC_DCACHE_LINESZ); #endif #ifdef _SC_DCACHE_SZ sysconf_names->__setitem__(new str("SC_DCACHE_SZ"), _SC_DCACHE_SZ); #endif #ifdef _SC_DCACHE_TBLKSZ sysconf_names->__setitem__(new str("SC_DCACHE_TBLKSZ"), _SC_DCACHE_TBLKSZ); #endif #ifdef _SC_DELAYTIMER_MAX sysconf_names->__setitem__(new str("SC_DELAYTIMER_MAX"), _SC_DELAYTIMER_MAX); #endif #ifdef _SC_EQUIV_CLASS_MAX sysconf_names->__setitem__(new str("SC_EQUIV_CLASS_MAX"), _SC_EQUIV_CLASS_MAX); #endif #ifdef _SC_EXPR_NEST_MAX sysconf_names->__setitem__(new str("SC_EXPR_NEST_MAX"), _SC_EXPR_NEST_MAX); #endif #ifdef _SC_FSYNC sysconf_names->__setitem__(new str("SC_FSYNC"), _SC_FSYNC); #endif #ifdef _SC_GETGR_R_SIZE_MAX sysconf_names->__setitem__(new str("SC_GETGR_R_SIZE_MAX"), _SC_GETGR_R_SIZE_MAX); #endif #ifdef _SC_GETPW_R_SIZE_MAX sysconf_names->__setitem__(new str("SC_GETPW_R_SIZE_MAX"), _SC_GETPW_R_SIZE_MAX); #endif #ifdef _SC_ICACHE_ASSOC sysconf_names->__setitem__(new str("SC_ICACHE_ASSOC"), _SC_ICACHE_ASSOC); #endif #ifdef _SC_ICACHE_BLKSZ sysconf_names->__setitem__(new str("SC_ICACHE_BLKSZ"), _SC_ICACHE_BLKSZ); #endif #ifdef _SC_ICACHE_LINESZ sysconf_names->__setitem__(new str("SC_ICACHE_LINESZ"), _SC_ICACHE_LINESZ); #endif #ifdef _SC_ICACHE_SZ sysconf_names->__setitem__(new str("SC_ICACHE_SZ"), _SC_ICACHE_SZ); #endif #ifdef _SC_INF sysconf_names->__setitem__(new str("SC_INF"), _SC_INF); #endif #ifdef _SC_INT_MAX sysconf_names->__setitem__(new str("SC_INT_MAX"), _SC_INT_MAX); #endif #ifdef _SC_INT_MIN sysconf_names->__setitem__(new str("SC_INT_MIN"), _SC_INT_MIN); #endif #ifdef _SC_IOV_MAX sysconf_names->__setitem__(new str("SC_IOV_MAX"), _SC_IOV_MAX); #endif #ifdef _SC_IP_SECOPTS sysconf_names->__setitem__(new str("SC_IP_SECOPTS"), _SC_IP_SECOPTS); #endif #ifdef _SC_JOB_CONTROL sysconf_names->__setitem__(new str("SC_JOB_CONTROL"), _SC_JOB_CONTROL); #endif #ifdef _SC_KERN_POINTERS sysconf_names->__setitem__(new str("SC_KERN_POINTERS"), _SC_KERN_POINTERS); #endif #ifdef _SC_KERN_SIM sysconf_names->__setitem__(new str("SC_KERN_SIM"), _SC_KERN_SIM); #endif #ifdef _SC_LINE_MAX sysconf_names->__setitem__(new str("SC_LINE_MAX"), _SC_LINE_MAX); #endif #ifdef _SC_LOGIN_NAME_MAX sysconf_names->__setitem__(new str("SC_LOGIN_NAME_MAX"), _SC_LOGIN_NAME_MAX); #endif #ifdef _SC_LOGNAME_MAX sysconf_names->__setitem__(new str("SC_LOGNAME_MAX"), _SC_LOGNAME_MAX); #endif #ifdef _SC_LONG_BIT sysconf_names->__setitem__(new str("SC_LONG_BIT"), _SC_LONG_BIT); #endif #ifdef _SC_MAC sysconf_names->__setitem__(new str("SC_MAC"), _SC_MAC); #endif #ifdef _SC_MAPPED_FILES sysconf_names->__setitem__(new str("SC_MAPPED_FILES"), _SC_MAPPED_FILES); #endif #ifdef _SC_MAXPID sysconf_names->__setitem__(new str("SC_MAXPID"), _SC_MAXPID); #endif #ifdef _SC_MB_LEN_MAX sysconf_names->__setitem__(new str("SC_MB_LEN_MAX"), _SC_MB_LEN_MAX); #endif #ifdef _SC_MEMLOCK sysconf_names->__setitem__(new str("SC_MEMLOCK"), _SC_MEMLOCK); #endif #ifdef _SC_MEMLOCK_RANGE sysconf_names->__setitem__(new str("SC_MEMLOCK_RANGE"), _SC_MEMLOCK_RANGE); #endif #ifdef _SC_MEMORY_PROTECTION sysconf_names->__setitem__(new str("SC_MEMORY_PROTECTION"), _SC_MEMORY_PROTECTION); #endif #ifdef _SC_MESSAGE_PASSING sysconf_names->__setitem__(new str("SC_MESSAGE_PASSING"), _SC_MESSAGE_PASSING); #endif #ifdef _SC_MMAP_FIXED_ALIGNMENT sysconf_names->__setitem__(new str("SC_MMAP_FIXED_ALIGNMENT"), _SC_MMAP_FIXED_ALIGNMENT); #endif #ifdef _SC_MQ_OPEN_MAX sysconf_names->__setitem__(new str("SC_MQ_OPEN_MAX"), _SC_MQ_OPEN_MAX); #endif #ifdef _SC_MQ_PRIO_MAX sysconf_names->__setitem__(new str("SC_MQ_PRIO_MAX"), _SC_MQ_PRIO_MAX); #endif #ifdef _SC_NACLS_MAX sysconf_names->__setitem__(new str("SC_NACLS_MAX"), _SC_NACLS_MAX); #endif #ifdef _SC_NGROUPS_MAX sysconf_names->__setitem__(new str("SC_NGROUPS_MAX"), _SC_NGROUPS_MAX); #endif #ifdef _SC_NL_ARGMAX sysconf_names->__setitem__(new str("SC_NL_ARGMAX"), _SC_NL_ARGMAX); #endif #ifdef _SC_NL_LANGMAX sysconf_names->__setitem__(new str("SC_NL_LANGMAX"), _SC_NL_LANGMAX); #endif #ifdef _SC_NL_MSGMAX sysconf_names->__setitem__(new str("SC_NL_MSGMAX"), _SC_NL_MSGMAX); #endif #ifdef _SC_NL_NMAX sysconf_names->__setitem__(new str("SC_NL_NMAX"), _SC_NL_NMAX); #endif #ifdef _SC_NL_SETMAX sysconf_names->__setitem__(new str("SC_NL_SETMAX"), _SC_NL_SETMAX); #endif #ifdef _SC_NL_TEXTMAX sysconf_names->__setitem__(new str("SC_NL_TEXTMAX"), _SC_NL_TEXTMAX); #endif #ifdef _SC_NPROCESSORS_CONF sysconf_names->__setitem__(new str("SC_NPROCESSORS_CONF"), _SC_NPROCESSORS_CONF); #endif #ifdef _SC_NPROCESSORS_ONLN sysconf_names->__setitem__(new str("SC_NPROCESSORS_ONLN"), _SC_NPROCESSORS_ONLN); #endif #ifdef _SC_NPROC_CONF sysconf_names->__setitem__(new str("SC_NPROC_CONF"), _SC_NPROC_CONF); #endif #ifdef _SC_NPROC_ONLN sysconf_names->__setitem__(new str("SC_NPROC_ONLN"), _SC_NPROC_ONLN); #endif #ifdef _SC_NZERO sysconf_names->__setitem__(new str("SC_NZERO"), _SC_NZERO); #endif #ifdef _SC_OPEN_MAX sysconf_names->__setitem__(new str("SC_OPEN_MAX"), _SC_OPEN_MAX); #endif #ifdef _SC_PAGESIZE sysconf_names->__setitem__(new str("SC_PAGESIZE"), _SC_PAGESIZE); #endif #ifdef _SC_PAGE_SIZE sysconf_names->__setitem__(new str("SC_PAGE_SIZE"), _SC_PAGE_SIZE); #endif #ifdef _SC_PASS_MAX sysconf_names->__setitem__(new str("SC_PASS_MAX"), _SC_PASS_MAX); #endif #ifdef _SC_PHYS_PAGES sysconf_names->__setitem__(new str("SC_PHYS_PAGES"), _SC_PHYS_PAGES); #endif #ifdef _SC_PII sysconf_names->__setitem__(new str("SC_PII"), _SC_PII); #endif #ifdef _SC_PII_INTERNET sysconf_names->__setitem__(new str("SC_PII_INTERNET"), _SC_PII_INTERNET); #endif #ifdef _SC_PII_INTERNET_DGRAM sysconf_names->__setitem__(new str("SC_PII_INTERNET_DGRAM"), _SC_PII_INTERNET_DGRAM); #endif #ifdef _SC_PII_INTERNET_STREAM sysconf_names->__setitem__(new str("SC_PII_INTERNET_STREAM"), _SC_PII_INTERNET_STREAM); #endif #ifdef _SC_PII_OSI sysconf_names->__setitem__(new str("SC_PII_OSI"), _SC_PII_OSI); #endif #ifdef _SC_PII_OSI_CLTS sysconf_names->__setitem__(new str("SC_PII_OSI_CLTS"), _SC_PII_OSI_CLTS); #endif #ifdef _SC_PII_OSI_COTS sysconf_names->__setitem__(new str("SC_PII_OSI_COTS"), _SC_PII_OSI_COTS); #endif #ifdef _SC_PII_OSI_M sysconf_names->__setitem__(new str("SC_PII_OSI_M"), _SC_PII_OSI_M); #endif #ifdef _SC_PII_SOCKET sysconf_names->__setitem__(new str("SC_PII_SOCKET"), _SC_PII_SOCKET); #endif #ifdef _SC_PII_XTI sysconf_names->__setitem__(new str("SC_PII_XTI"), _SC_PII_XTI); #endif #ifdef _SC_POLL sysconf_names->__setitem__(new str("SC_POLL"), _SC_POLL); #endif #ifdef _SC_PRIORITIZED_IO sysconf_names->__setitem__(new str("SC_PRIORITIZED_IO"), _SC_PRIORITIZED_IO); #endif #ifdef _SC_PRIORITY_SCHEDULING sysconf_names->__setitem__(new str("SC_PRIORITY_SCHEDULING"), _SC_PRIORITY_SCHEDULING); #endif #ifdef _SC_REALTIME_SIGNALS sysconf_names->__setitem__(new str("SC_REALTIME_SIGNALS"), _SC_REALTIME_SIGNALS); #endif #ifdef _SC_RE_DUP_MAX sysconf_names->__setitem__(new str("SC_RE_DUP_MAX"), _SC_RE_DUP_MAX); #endif #ifdef _SC_RTSIG_MAX sysconf_names->__setitem__(new str("SC_RTSIG_MAX"), _SC_RTSIG_MAX); #endif #ifdef _SC_SAVED_IDS sysconf_names->__setitem__(new str("SC_SAVED_IDS"), _SC_SAVED_IDS); #endif #ifdef _SC_SCHAR_MAX sysconf_names->__setitem__(new str("SC_SCHAR_MAX"), _SC_SCHAR_MAX); #endif #ifdef _SC_SCHAR_MIN sysconf_names->__setitem__(new str("SC_SCHAR_MIN"), _SC_SCHAR_MIN); #endif #ifdef _SC_SELECT sysconf_names->__setitem__(new str("SC_SELECT"), _SC_SELECT); #endif #ifdef _SC_SEMAPHORES sysconf_names->__setitem__(new str("SC_SEMAPHORES"), _SC_SEMAPHORES); #endif #ifdef _SC_SEM_NSEMS_MAX sysconf_names->__setitem__(new str("SC_SEM_NSEMS_MAX"), _SC_SEM_NSEMS_MAX); #endif #ifdef _SC_SEM_VALUE_MAX sysconf_names->__setitem__(new str("SC_SEM_VALUE_MAX"), _SC_SEM_VALUE_MAX); #endif #ifdef _SC_SHARED_MEMORY_OBJECTS sysconf_names->__setitem__(new str("SC_SHARED_MEMORY_OBJECTS"), _SC_SHARED_MEMORY_OBJECTS); #endif #ifdef _SC_SHRT_MAX sysconf_names->__setitem__(new str("SC_SHRT_MAX"), _SC_SHRT_MAX); #endif #ifdef _SC_SHRT_MIN sysconf_names->__setitem__(new str("SC_SHRT_MIN"), _SC_SHRT_MIN); #endif #ifdef _SC_SIGQUEUE_MAX sysconf_names->__setitem__(new str("SC_SIGQUEUE_MAX"), _SC_SIGQUEUE_MAX); #endif #ifdef _SC_SIGRT_MAX sysconf_names->__setitem__(new str("SC_SIGRT_MAX"), _SC_SIGRT_MAX); #endif #ifdef _SC_SIGRT_MIN sysconf_names->__setitem__(new str("SC_SIGRT_MIN"), _SC_SIGRT_MIN); #endif #ifdef _SC_SOFTPOWER sysconf_names->__setitem__(new str("SC_SOFTPOWER"), _SC_SOFTPOWER); #endif #ifdef _SC_SPLIT_CACHE sysconf_names->__setitem__(new str("SC_SPLIT_CACHE"), _SC_SPLIT_CACHE); #endif #ifdef _SC_SSIZE_MAX sysconf_names->__setitem__(new str("SC_SSIZE_MAX"), _SC_SSIZE_MAX); #endif #ifdef _SC_STACK_PROT sysconf_names->__setitem__(new str("SC_STACK_PROT"), _SC_STACK_PROT); #endif #ifdef _SC_STREAM_MAX sysconf_names->__setitem__(new str("SC_STREAM_MAX"), _SC_STREAM_MAX); #endif #ifdef _SC_SYNCHRONIZED_IO sysconf_names->__setitem__(new str("SC_SYNCHRONIZED_IO"), _SC_SYNCHRONIZED_IO); #endif #ifdef _SC_THREADS sysconf_names->__setitem__(new str("SC_THREADS"), _SC_THREADS); #endif #ifdef _SC_THREAD_ATTR_STACKADDR sysconf_names->__setitem__(new str("SC_THREAD_ATTR_STACKADDR"), _SC_THREAD_ATTR_STACKADDR); #endif #ifdef _SC_THREAD_ATTR_STACKSIZE sysconf_names->__setitem__(new str("SC_THREAD_ATTR_STACKSIZE"), _SC_THREAD_ATTR_STACKSIZE); #endif #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS sysconf_names->__setitem__(new str("SC_THREAD_DESTRUCTOR_ITERATIONS"), _SC_THREAD_DESTRUCTOR_ITERATIONS); #endif #ifdef _SC_THREAD_KEYS_MAX sysconf_names->__setitem__(new str("SC_THREAD_KEYS_MAX"), _SC_THREAD_KEYS_MAX); #endif #ifdef _SC_THREAD_PRIORITY_SCHEDULING sysconf_names->__setitem__(new str("SC_THREAD_PRIORITY_SCHEDULING"), _SC_THREAD_PRIORITY_SCHEDULING); #endif #ifdef _SC_THREAD_PRIO_INHERIT sysconf_names->__setitem__(new str("SC_THREAD_PRIO_INHERIT"), _SC_THREAD_PRIO_INHERIT); #endif #ifdef _SC_THREAD_PRIO_PROTECT sysconf_names->__setitem__(new str("SC_THREAD_PRIO_PROTECT"), _SC_THREAD_PRIO_PROTECT); #endif #ifdef _SC_THREAD_PROCESS_SHARED sysconf_names->__setitem__(new str("SC_THREAD_PROCESS_SHARED"), _SC_THREAD_PROCESS_SHARED); #endif #ifdef _SC_THREAD_SAFE_FUNCTIONS sysconf_names->__setitem__(new str("SC_THREAD_SAFE_FUNCTIONS"), _SC_THREAD_SAFE_FUNCTIONS); #endif #ifdef _SC_THREAD_STACK_MIN sysconf_names->__setitem__(new str("SC_THREAD_STACK_MIN"), _SC_THREAD_STACK_MIN); #endif #ifdef _SC_THREAD_THREADS_MAX sysconf_names->__setitem__(new str("SC_THREAD_THREADS_MAX"), _SC_THREAD_THREADS_MAX); #endif #ifdef _SC_TIMERS sysconf_names->__setitem__(new str("SC_TIMERS"), _SC_TIMERS); #endif #ifdef _SC_TIMER_MAX sysconf_names->__setitem__(new str("SC_TIMER_MAX"), _SC_TIMER_MAX); #endif #ifdef _SC_TTY_NAME_MAX sysconf_names->__setitem__(new str("SC_TTY_NAME_MAX"), _SC_TTY_NAME_MAX); #endif #ifdef _SC_TZNAME_MAX sysconf_names->__setitem__(new str("SC_TZNAME_MAX"), _SC_TZNAME_MAX); #endif #ifdef _SC_T_IOV_MAX sysconf_names->__setitem__(new str("SC_T_IOV_MAX"), _SC_T_IOV_MAX); #endif #ifdef _SC_UCHAR_MAX sysconf_names->__setitem__(new str("SC_UCHAR_MAX"), _SC_UCHAR_MAX); #endif #ifdef _SC_UINT_MAX sysconf_names->__setitem__(new str("SC_UINT_MAX"), _SC_UINT_MAX); #endif #ifdef _SC_UIO_MAXIOV sysconf_names->__setitem__(new str("SC_UIO_MAXIOV"), _SC_UIO_MAXIOV); #endif #ifdef _SC_ULONG_MAX sysconf_names->__setitem__(new str("SC_ULONG_MAX"), _SC_ULONG_MAX); #endif #ifdef _SC_USHRT_MAX sysconf_names->__setitem__(new str("SC_USHRT_MAX"), _SC_USHRT_MAX); #endif #ifdef _SC_VERSION sysconf_names->__setitem__(new str("SC_VERSION"), _SC_VERSION); #endif #ifdef _SC_WORD_BIT sysconf_names->__setitem__(new str("SC_WORD_BIT"), _SC_WORD_BIT); #endif #ifdef _SC_XBS5_ILP32_OFF32 sysconf_names->__setitem__(new str("SC_XBS5_ILP32_OFF32"), _SC_XBS5_ILP32_OFF32); #endif #ifdef _SC_XBS5_ILP32_OFFBIG sysconf_names->__setitem__(new str("SC_XBS5_ILP32_OFFBIG"), _SC_XBS5_ILP32_OFFBIG); #endif #ifdef _SC_XBS5_LP64_OFF64 sysconf_names->__setitem__(new str("SC_XBS5_LP64_OFF64"), _SC_XBS5_LP64_OFF64); #endif #ifdef _SC_XBS5_LPBIG_OFFBIG sysconf_names->__setitem__(new str("SC_XBS5_LPBIG_OFFBIG"), _SC_XBS5_LPBIG_OFFBIG); #endif #ifdef _SC_XOPEN_CRYPT sysconf_names->__setitem__(new str("SC_XOPEN_CRYPT"), _SC_XOPEN_CRYPT); #endif #ifdef _SC_XOPEN_ENH_I18N sysconf_names->__setitem__(new str("SC_XOPEN_ENH_I18N"), _SC_XOPEN_ENH_I18N); #endif #ifdef _SC_XOPEN_LEGACY sysconf_names->__setitem__(new str("SC_XOPEN_LEGACY"), _SC_XOPEN_LEGACY); #endif #ifdef _SC_XOPEN_REALTIME sysconf_names->__setitem__(new str("SC_XOPEN_REALTIME"), _SC_XOPEN_REALTIME); #endif #ifdef _SC_XOPEN_REALTIME_THREADS sysconf_names->__setitem__(new str("SC_XOPEN_REALTIME_THREADS"), _SC_XOPEN_REALTIME_THREADS); #endif #ifdef _SC_XOPEN_SHM sysconf_names->__setitem__(new str("SC_XOPEN_SHM"), _SC_XOPEN_SHM); #endif #ifdef _SC_XOPEN_UNIX sysconf_names->__setitem__(new str("SC_XOPEN_UNIX"), _SC_XOPEN_UNIX); #endif #ifdef _SC_XOPEN_VERSION sysconf_names->__setitem__(new str("SC_XOPEN_VERSION"), _SC_XOPEN_VERSION); #endif #ifdef _SC_XOPEN_XCU_VERSION sysconf_names->__setitem__(new str("SC_XOPEN_XCU_VERSION"), _SC_XOPEN_XCU_VERSION); #endif #ifdef _SC_XOPEN_XPG2 sysconf_names->__setitem__(new str("SC_XOPEN_XPG2"), _SC_XOPEN_XPG2); #endif #ifdef _SC_XOPEN_XPG3 sysconf_names->__setitem__(new str("SC_XOPEN_XPG3"), _SC_XOPEN_XPG3); #endif #ifdef _SC_XOPEN_XPG4 sysconf_names->__setitem__(new str("SC_XOPEN_XPG4"), _SC_XOPEN_XPG4); #endif } } // module namespace shedskin-0.9.4/shedskin/lib/os/path.hpp0000664000175000017500000000555712157270661017741 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __PATH_HPP #define __PATH_HPP #include "builtin.hpp" #include "os/__init__.hpp" #include "stat.hpp" using namespace __shedskin__; namespace __os__ { namespace __path__ { extern str *__name__, *altsep, *curdir, *defpath, *devnull, *extsep, *pardir, *pathsep, *sep; extern tuple2 *const_2; extern str *const_0, *const_1, *const_10, *const_11, *const_12, *const_13, *const_14, *const_15, *const_16, *const_17, *const_18, *const_19, *const_3, *const_4, *const_5, *const_6, *const_7, *const_8, *const_9; #ifdef WIN32 extern __ss_int supports_unicode_filenames; #endif str *normcase(str *s); __ss_bool isabs(str *s); str *joinl(list *l); str *join(__ss_int n, ...); tuple2 *split(str *p); tuple2 *splitext(str *p); tuple2 *splitdrive(str *p); str *basename(str *p); str *dirname(str *p); str *commonprefix(list *m); __ss_int getsize(str *filename); double getmtime(str *filename); double getatime(str *filename); double getctime(str *filename); __ss_bool islink(str *path); __ss_bool exists(str *path); __ss_bool lexists(str *path); __ss_bool isdir(str *path); __ss_bool isfile(str *path); str *normpath(str *path); str *abspath(str *path); str *realpath(str *filename); #ifndef WIN32 __ss_bool samefile(str *f1, str *f2); __ss_bool samestat(__os__::__cstat *s1, __os__::__cstat *s2); str *_resolve_link(str *path); #endif #ifndef WIN32 template void *walk(str *top, void *(*func)(A, str *, list *), A arg) { list *__21, *names; list::for_in_loop __123; str *name; __ss_int __23; __os__::__cstat *st; try { names = __os__::listdir(top); } catch (__os__::error *) { return NULL; } func(arg, top, names); FOR_IN(name,names,21,23,123) name = join(2, top, name); try { st = __os__::lstat(name); } catch (__os__::error *) { continue; } if (__stat__::__ss_S_ISDIR(st->st_mode)) { walk(name, func, arg); } END_FOR return NULL; } #else template void *walk(str *top, void *(*func)(A, str *, list *), A arg) { list *__33, *names; list::for_in_loop __123; str *name; tuple2 *exceptions; __ss_int __35; try { names = __os__::listdir(top); } catch (__os__::error *) { return NULL; } func(arg, top, names); exceptions = (new tuple2(2, const_0, const_3)); FOR_IN(name,names,33,35,123) if ((!exceptions->__contains__(name))) { name = join(2, top, name); if (isdir(name)) { walk(name, func, arg); } } END_FOR return NULL; } #endif void __init(); } // module namespace } // module namespace #endif shedskin-0.9.4/shedskin/lib/os/__init__.py0000664000175000017500000001452012157270661020373 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) import path name = '' linesep = '' environ = {'': ''} curdir = '' pardir = '' extsep = '' sep = '' pathsep = '' defpath = '' altsep = '' devnull = '' EX_CANTCREAT = 0 EX_CONFIG = 0 EX_DATAERR = 0 EX_IOERR = 0 EX_NOHOST = 0 EX_NOINPUT = 0 EX_NOPERM = 0 EX_NOUSER = 0 EX_OK = 0 EX_OSERR = 0 EX_OSFILE = 0 EX_PROTOCOL = 0 EX_SOFTWARE = 0 EX_TEMPFAIL = 0 EX_UNAVAILABLE = 0 EX_USAGE = 0 F_OK = 0 NGROUPS_MAX = 0 O_APPEND = 0 O_CREAT = 0 O_DIRECT = 0 O_DIRECTORY = 0 O_DSYNC = 0 O_EXCL = 0 O_LARGEFILE = 0 O_NDELAY = 0 O_NOCTTY = 0 O_NOFOLLOW = 0 O_NONBLOCK = 0 O_RDONLY = 0 O_RDWR = 0 O_RSYNC = 0 O_SYNC = 0 O_TRUNC = 0 O_WRONLY = 0 P_NOWAIT = 0 P_NOWAITO = 0 P_WAIT = 0 R_OK = 0 SEEK_CUR = 0 SEEK_END = 0 SEEK_SET = 0 TMP_MAX = 0 WCONTINUED = 0 WNOHANG = 0 WUNTRACED = 0 W_OK = 0 X_OK = 0 pathconf_names = {'': 1} sysconf_names = {'': 1} confstr_names = {'': 1} class error(OSError): pass class __cstat: def __init__(self): self.st_mode = 1 self.st_size = 1 self.st_ino = 1 self.st_dev = 1 self.st_rdev = 1 self.st_nlink = 1 self.st_mtime = 1 self.st_atime = 1 self.st_ctime = 1 self.st_uid = 1 self.st_gid = 1 self.st_blksize = 1 self.st_blocks = 1 def __len__(self): return 1 def __getitem__(self, i): return 1 def __slice__(self, x, l, u, s): return (1,) def __repr__(self): return '' class __vfsstat: def __init__(self): self.f_bsize = 1 self.f_frsize = 1 self.f_blocks = 1 self.f_bfree = 1 self.f_bavail = 1 self.f_files = 1 self.f_ffree = 1 self.f_favail = 1 self.f_flag = 1 self.f_namemax = 1 def __len__(self): return 1 def __getitem__(self, i): return 1 def __slice__(self, x, l, u, s): return (1,) def __repr__(self): return '' def statvfs(path): return __vfsstat() def fstatvfs(fd): return __vfsstat() class popen_pipe(file): pass def listdir(path): return [''] def getenv(name, alternative=''): return '' def getcwd(): return '' def getlogin(): return '' def chdir(d): pass def rename(a, b): pass def remove(path): pass def rmdir(a): pass def removedirs(name): pass def mkdir(a, mode=0777): pass def makedirs(name, mode=0777): pass def fork(): return 1 def forkpty(): return (1,) def openpty(): return (1,) def abort(): pass def chown(path, uid, gid): pass def system(c): return 1 def strerror(i): return '' def stat(path): return __cstat() def lstat(path): return __cstat() def fstat(file): return __cstat() def readlink(path): return '' def getuid(): return 1 def setuid(uid): pass def getgid(): return 1 def setgid(gid): pass def getegid(): return 1 def setegid(egid): pass def geteuid(): return 1 def seteuid(euid): pass def getgroups(): return [1] def setgroups(groups): pass def getpgid(pid): return 1 def setpgid(pid, pgrp): pass def getpgrp(): return 1 def setpgrp(): pass def getppid(): return 1 def getsid(pid): return 1 def setsid(): return 1 def getpid(): return 1 def setreuid(ruid, euid): pass def setregid(guid, egid): pass def tcgetpgrp(fd): return 1 def tcsetpgrp(fd, pg): pass def stat_float_times(n=False): return True def putenv(variable, value): pass def umask(newmask): return 0 def chmod(path, val): return 0 def unsetenv(var): pass def renames(old, new): pass def popen(cmd, mode='r', bufsize=-1): return popen_pipe() def popen2(cmd, mode='r', bufsize=-1): return ( file('/bin/sh'), file('/bin/sh') ) def popen3(cmd, mode='r', bufsize=-1): return ( file('/bin/sh'), file('/bin/sh'), file('/bin/sh') ) def popen4(cmd, mode='r', bufsize=-1): return ( file('/bin/sh'), file('/bin/sh') ) def close(fd): pass def execl(*path): pass def execlp(*path): pass def execle(*path): pass def execlpe(*path): pass def execv(path, args): pass def execvp(path, args): pass def execve(path, args, env): pass def execvpe(path, args, env): pass def spawnl(mode, *path): return 1 def spawnlp(mode, *path): return 1 def spawnle(mode, *path): return 1 def spawnlpe(mode, *path): return 1 def spawnv(mode, path, args): return 1 def spawnvp(mode, path, args): return 1 def spawnve(mode, path, args, env): return 1 def spawnvpe(mode, path, args, env): return 1 def open(name, flags): return 1 def read(fd, n): return '' def write(fd, s): return 1 def fdopen(fd, mode='r', bufsize=-1): return file('/bin/sh') def pipe(): return (0,0) def dup(f1): return 1 def dup2(f1,f2): pass def fchdir(f1): pass def fdatasync(f1): pass def chroot(dir): pass def ctermid(): return '' def isatty(fd): return True def ttyname(fd): return '' def uname(): return ('',) def lchown(p, u, g): pass def link(a, b): pass def symlink(a, b): pass def nice(n): return 1 def wait(): return (1,) def waitpid(pid, options): return (1,) def kill(pid, sig): pass def killpg(pgid, sig): pass def pathconf(path, name): return 1 def fpathconf(fd, name): return 1 def confstr(name): return '' def sysconf(name): return 1 def ftruncate(fd, n): pass def getloadavg(): return (1.0,) def mkfifo(path, mode=438): pass def unlink(path): pass def lseek(fd, pos, how): pass def fsync(fd): pass def urandom(n): return '' def utime(path, times): pass def access(path, mode): return True def times(): return (1.0,) def tmpnam(): return '' def tempnam(dir, prefix=None): return '' def tmpfile(): return file() def makedev(major, minor): return 1 def major(dev): return 1 def minor(dev): return 1 def mknod(filename, mode=438, device=0): pass def WCOREDUMP(status): return 1 def WEXITSTATUS(status): return 1 def WIFCONTINUED(status): return 1 def WIFEXITED(status): return 1 def WIFSIGNALED(status): return 1 def WIFSTOPPED(status): return 1 def WSTOPSIG(status): return 1 def WTERMSIG(status): return 1 def _exit(code): pass shedskin-0.9.4/shedskin/lib/os/path.cpp0000664000175000017500000004627512157270661017736 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "os/path.hpp" /* converted using Shed Skin from the CPython implementation */ /** Common operations on Posix pathnames. Instead of importing this module directly, import os and refer to this module as os.path. The "os.path" name is an alias for this module on Posix systems; on other systems (e.g. Mac, Windows), os.path provides the same operations in a manner specific to that platform, and is an alias to another module (e.g. macpath, ntpath). Some of this can actually be useful on non-Posix systems too, e.g. for manipulation of the pathname component of URLs. */ namespace __os__ { namespace __path__ { tuple2 *const_2; str *const_0, *const_1, *const_10, *const_11, *const_12, *const_13, *const_14, *const_15, *const_16, *const_17, *const_18, *const_19, *const_3, *const_4, *const_5, *const_6, *const_7, *const_8, *const_9; str *__name__, *altsep, *curdir, *defpath, *devnull, *extsep, *pardir, *pathsep, *sep; #ifdef WIN32 __ss_int supports_unicode_filenames; #endif str *join(__ss_int n, ...) { /* varargs hack */ list *p = new list(); va_list ap; va_start(ap, n); for(__ss_int i=0; iappend(t); } va_end(ap); return joinl(p); } #ifndef WIN32 void __init() { const_0 = new str(""); const_1 = new str("."); const_2 = (new tuple2(2, const_0, const_1)); const_3 = new str(".."); const_4 = new str("/"); const_5 = new str(":"); const_6 = new str(":/bin:/usr/bin"); const_7 = new str("/dev/null"); const_14 = new str("//"); const_15 = new str("///"); __name__ = new str("__main__"); curdir = const_1; pardir = const_3; extsep = const_1; sep = const_4; pathsep = const_5; defpath = const_6; altsep = const_0; devnull = const_7; } str *normcase(str *s) { /** Normalize case of pathname. Has no effect under Posix */ return s; } __ss_bool isabs(str *s) { /** Test whether a path is absolute */ return __mbool(s->startswith(const_4)); } str *joinl(list *l) { /** Join two or more pathname components, inserting '/' as needed */ list *__1, *__2, *p; list::for_in_loop __123; str *__0, *b, *path; __ss_int __4, __5; __0 = l->__getfast__(0); __1 = l->__slice__(1, 1, 0, 0); path = __0; p = __1; FOR_IN(b,p,2,4,123) if (b->startswith(const_4)) { path = b; } else if (__OR(__eq(path, const_0), path->endswith(const_4), 5)) { path = path->__iadd__(b); } else { path = __add_strs(3, path, const_4, b); } END_FOR return path; } tuple2 *split(str *p) { /** Split a pathname. Returns tuple "(head, tail)" where "tail" is everything after the final slash. Either part may be empty. */ str *__7, *__8, *head, *tail; __ss_int i; i = (p->rfind(const_4)+1); __7 = p->__slice__(2, 0, i, 0); __8 = p->__slice__(1, i, 0, 0); head = __7; tail = __8; if ((___bool(head) && __ne(head, (const_4)->__mul__(len(head))))) { head = head->rstrip(const_4); } return (new tuple2(2, head, tail)); } tuple2 *splitext(str *p) { /** Split the extension from a pathname. Extension is everything from the last dot to the end. Returns "(root, ext)", either part may be empty. */ __ss_int i; i = p->rfind(const_1); if ((i<=p->rfind(const_4))) { return (new tuple2(2, p, const_0)); } else { return (new tuple2(2, p->__slice__(2, 0, i, 0), p->__slice__(1, i, 0, 0))); } return 0; } tuple2 *splitdrive(str *p) { /** Split a pathname into drive and path. On Posix, drive is always empty. */ return (new tuple2(2, const_0, p)); } str *basename(str *p) { /** Returns the final component of a pathname */ return (split(p))->__getsecond__(); } str *dirname(str *p) { /** Returns the directory component of a pathname */ return (split(p))->__getfirst__(); } str *commonprefix(list *m) { /** Given a list of pathnames, returns the longest common leading component */ str *s1, *s2; __ss_int __11, __12, i, n; if ((!___bool(m))) { return const_0; } s1 = ___min(1, 0, m); s2 = ___max(1, 0, m); n = ___min(2, 0, len(s1), len(s2)); FAST_FOR(i,0,n,1,11,12) if (__ne(s1->__getitem__(i), s2->__getitem__(i))) { return s1->__slice__(2, 0, i, 0); } END_FOR return s1->__slice__(2, 0, n, 0); } __ss_int getsize(str *filename) { /** Return the size of a file, reported by os.stat(). */ return (__os__::stat(filename))->st_size; } double getmtime(str *filename) { /** Return the last modification time of a file, reported by os.stat(). */ return (__os__::stat(filename))->__ss_st_mtime; } double getatime(str *filename) { /** Return the last access time of a file, reported by os.stat(). */ return (__os__::stat(filename))->__ss_st_atime; } double getctime(str *filename) { /** Return the metadata change time of a file, reported by os.stat(). */ return (__os__::stat(filename))->__ss_st_ctime; } __ss_bool islink(str *path) { /** Test whether a path is a symbolic link */ __os__::__cstat *st; try { st = __os__::lstat(path); } catch (__os__::error *) { return False; } return __mbool(__stat__::__ss_S_ISLNK(st->st_mode)); } __ss_bool exists(str *path) { /** Test whether a path exists. Returns False for broken symbolic links */ __os__::__cstat *st; try { st = __os__::stat(path); } catch (__os__::error *) { return False; } return True; } __ss_bool lexists(str *path) { /** Test whether a path exists. Returns True for broken symbolic links */ __os__::__cstat *st; try { st = __os__::lstat(path); } catch (__os__::error *) { return False; } return True; } __ss_bool isdir(str *path) { /** Test whether a path is a directory */ __os__::__cstat *st; try { st = __os__::stat(path); } catch (__os__::error *) { return False; } return __mbool(__stat__::__ss_S_ISDIR(st->st_mode)); } __ss_bool isfile(str *path) { /** Test whether a path is a regular file */ __os__::__cstat *st; try { st = __os__::stat(path); } catch (__os__::error *) { return False; } return __mbool(__stat__::__ss_S_ISREG(st->st_mode)); } __ss_bool samefile(str *f1, str *f2) { /** Test whether two pathnames reference the same actual file */ __os__::__cstat *s1, *s2; s1 = __os__::stat(f1); s2 = __os__::stat(f2); return __mbool(samestat(s1, s2)); } __ss_bool samestat(__os__::__cstat *s1, __os__::__cstat *s2) { /** Test whether two stat buffers reference the same file */ __ss_int __18; return __mbool(__AND((s1->st_ino==s2->st_ino), (s1->st_dev==s2->st_dev), 18)); } str *normpath(str *path) { /** Normalize path, eliminating double slashes, etc. */ list *__28, *comps, *new_comps; list::for_in_loop __123; str *__38, *comp; __ss_int __25, __26, __30, __32, initial_slashes; if (__eq(path, const_0)) { return const_1; } initial_slashes = path->startswith(const_4); if (__AND(initial_slashes, __AND(path->startswith(const_14), (!path->startswith(const_15)), 26), 25)) { initial_slashes = 2; } comps = path->split(const_4); new_comps = (new list()); FOR_IN(comp,comps,28,30,123) if ((const_2)->__contains__(comp)) { continue; } if ((__ne(comp, const_3) || __AND((!initial_slashes), (!___bool(new_comps)), 32) || ___bool((___bool(new_comps) && __eq(new_comps->__getfast__(-1), const_3))))) { new_comps->append(comp); } else if (___bool(new_comps)) { new_comps->pop(); } END_FOR comps = new_comps; path = (const_4)->join(comps); if (initial_slashes) { path = ((const_4)->__mul__(initial_slashes))->__add__(path); } return __OR(path, const_1, 38); } str *abspath(str *path) { /** Return an absolute path. */ if ((!isabs(path))) { path = join(2, __os__::getcwd(), path); } return normpath(path); } str *realpath(str *filename) { /** Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path. */ list *bits; str *component, *newpath, *resolved; __ss_int __40, __41, i; if (isabs(filename)) { bits = ((new list(1, const_4)))->__add__((filename->split(const_4))->__slice__(1, 1, 0, 0)); } else { bits = filename->split(const_4); } FAST_FOR(i,2,(len(bits)+1),1,40,41) component = joinl(bits->__slice__(3, 0, i, 0)); if (islink(component)) { resolved = _resolve_link(component); if ((resolved==0)) { return abspath(joinl(((new list(1, component)))->__add__(bits->__slice__(1, i, 0, 0)))); } else { newpath = joinl(((new list(1, resolved)))->__add__(bits->__slice__(1, i, 0, 0))); return realpath(newpath); } } END_FOR return abspath(filename); } str *_resolve_link(str *path) { /** Internal helper function. Takes a path and follows symlinks until we either arrive at something that isn't a symlink, or encounter a path we've seen before (meaning that there's a loop). */ list *paths_seen; str *dir, *resolved; paths_seen = (new list()); while(islink(path)) { if (paths_seen->__contains__(path)) { return 0; } paths_seen->append(path); resolved = __os__::readlink(path); if ((!isabs(resolved))) { dir = dirname(path); path = normpath(join(2, dir, resolved)); } else { path = normpath(resolved); } } return path; } #else void __init() { const_0 = new str("."); const_1 = new str(""); const_2 = (new tuple2(2, const_0, const_1)); const_3 = new str(".."); const_4 = new str("\\"); const_5 = new str(";"); const_6 = new str("/"); const_7 = new str(".;C:\\bin"); const_8 = new str("nul"); const_18 = new str("/\\"); const_19 = new str(":"); __name__ = new str("__main__"); curdir = const_0; pardir = const_3; extsep = const_0; sep = const_4; pathsep = const_5; altsep = const_6; defpath = const_7; devnull = const_8; supports_unicode_filenames = 0; } str *normcase(str *s) { /** Normalize case of pathname. Makes all characters lowercase and all slashes into backslashes. */ return (s->replace(const_6, const_4))->lower(); } __ss_bool isabs(str *s) { /** Test whether a path is absolute */ __ss_int __0, __1; s = (splitdrive(s))->__getsecond__(); return __mbool(__AND(__ne(s, const_1), (const_18)->__contains__(s->__slice__(2, 0, 1, 0)), 0)); } str *joinl(list *l) { /** Join two or more pathname components, inserting "\" as needed */ list *__3, *__4, *p; list::for_in_loop __123; __iter *__5; str *__2, *b, *path; __ss_int __10, __11, __12, __13, __14, __6, __7, __8, __9, b_wins; __2 = l->__getfast__(0); __3 = l->__slice__(1, 1, 0, 0); path = __2; p = __3; FOR_IN(b,p,4,6,123) b_wins = 0; if (__eq(path, const_1)) { b_wins = 1; } else if (isabs(b)) { if (__OR(__ne(path->__slice__(3, 1, 2, 0), const_19), __eq(b->__slice__(3, 1, 2, 0), const_19), 7)) { b_wins = 1; } else if (__OR((len(path)>3), __AND((len(path)==3), (!(const_18)->__contains__(path->__getitem__(-1))), 10), 9)) { b_wins = 1; } } if (b_wins) { path = b; } else { ASSERT((len(path)>0), 0); if ((const_18)->__contains__(path->__getitem__(-1))) { if ((___bool(b) && (const_18)->__contains__(b->__getitem__(0)))) { path = path->__iadd__(b->__slice__(1, 1, 0, 0)); } else { path = path->__iadd__(b); } } else if (__eq(path->__getitem__(-1), const_19)) { path = path->__iadd__(b); } else if (___bool(b)) { if ((const_18)->__contains__(b->__getitem__(0))) { path = path->__iadd__(b); } else { path = __add_strs(3, path, const_4, b); } } else { path = path->__iadd__(const_4); } } END_FOR return path; } tuple2 *splitdrive(str *p) { /** Split a pathname into drive and path specifiers. Returns a 2-tuple "(drive,path)"; either part may be empty */ if (__eq(p->__slice__(3, 1, 2, 0), const_19)) { return (new tuple2(2, p->__slice__(3, 0, 2, 0), p->__slice__(1, 2, 0, 0))); } return (new tuple2(2, const_1, p)); } tuple2 *split(str *p) { /** Split a pathname. Return tuple (head, tail) where tail is everything after the final slash. Either part may be empty. */ tuple2 *__15; str *__18, *__19, *__22, *__23, *d, *head, *head2, *tail; __ss_int __16, __17, __20, __21, i; __15 = splitdrive(p); d = __15->__getfirst__(); p = __15->__getsecond__(); i = len(p); while(__AND(i, (!(const_18)->__contains__(p->__getitem__((i-1)))), 16)) { i = (i-1); } __18 = p->__slice__(2, 0, i, 0); __19 = p->__slice__(1, i, 0, 0); head = __18; tail = __19; head2 = head; while((___bool(head2) && (const_18)->__contains__(head2->__getitem__(-1)))) { head2 = head2->__slice__(2, 0, -1, 0); } head = __OR(head2, head, 22); return (new tuple2(2, d->__add__(head), tail)); } tuple2 *splitext(str *p) { /** Split the extension from a pathname. Extension is everything from the last dot to the end. Return (root, ext), either part may be empty. */ __ss_int i; i = p->rfind(const_0); if ((i<=___max(2, 0, p->rfind(const_6), p->rfind(const_4)))) { return (new tuple2(2, p, const_1)); } else { return (new tuple2(2, p->__slice__(2, 0, i, 0), p->__slice__(1, i, 0, 0))); } return 0; } str *basename(str *p) { /** Returns the final component of a pathname */ return (split(p))->__getsecond__(); } str *dirname(str *p) { /** Returns the directory component of a pathname */ return (split(p))->__getfirst__(); } str *commonprefix(list *m) { /** Given a list of pathnames, returns the longest common leading component */ list *__24; list::for_in_loop __123; __ss_int __26, __27, __28, i; str *item, *prefix; if ((!___bool(m))) { return const_1; } prefix = m->__getfast__(0); FOR_IN(item,m,24,26,123) FAST_FOR(i,0,len(prefix),1,27,28) if (__ne(prefix->__slice__(2, 0, (i+1), 0), item->__slice__(2, 0, (i+1), 0))) { prefix = prefix->__slice__(2, 0, i, 0); if ((i==0)) { return const_1; } break; } END_FOR END_FOR return prefix; } __ss_int getsize(str *filename) { /** Return the size of a file, reported by os.stat() */ return (__os__::stat(filename))->st_size; } double getmtime(str *filename) { /** Return the last modification time of a file, reported by os.stat() */ return (__os__::stat(filename))->__ss_st_mtime; } double getatime(str *filename) { /** Return the last access time of a file, reported by os.stat() */ return (__os__::stat(filename))->__ss_st_atime; } double getctime(str *filename) { /** Return the creation time of a file, reported by os.stat(). */ return (__os__::stat(filename))->__ss_st_ctime; } __ss_bool islink(str *path) { /** Test for symbolic link. On WindowsNT/95 always returns false */ return False; } __ss_bool exists(str *path) { /** Test whether a path exists */ __os__::__cstat *st; try { st = __os__::stat(path); } catch (__os__::error *) { return False; } return True; } __ss_bool lexists(str *path) { return exists(path); } __ss_bool isdir(str *path) { /** Test whether a path is a directory */ __os__::__cstat *st; try { st = __os__::stat(path); } catch (__os__::error *) { return False; } return __mbool(__stat__::__ss_S_ISDIR(st->st_mode)); } __ss_bool isfile(str *path) { /** Test whether a path is a regular file */ __os__::__cstat *st; try { st = __os__::stat(path); } catch (__os__::error *) { return False; } return __mbool(__stat__::__ss_S_ISREG(st->st_mode)); } str *normpath(str *path) { /** Normalize path, eliminating double slashes, etc. */ tuple2 *__36; list *comps; str *prefix; __ss_int __37, __38, __39, __40, __41, __42, i; path = path->replace(const_6, const_4); __36 = splitdrive(path); prefix = __36->__getfirst__(); path = __36->__getsecond__(); if (__eq(prefix, const_1)) { while(__eq(path->__slice__(2, 0, 1, 0), const_4)) { prefix = prefix->__add__(const_4); path = path->__slice__(1, 1, 0, 0); } } else { if (path->startswith(const_4)) { prefix = prefix->__add__(const_4); path = path->lstrip(const_4); } } comps = path->split(const_4); i = 0; while((i__contains__(comps->__getfast__(i))) { comps->__delitem__(i); } else if (__eq(comps->__getfast__(i), const_3)) { if (__AND((i>0), __ne(comps->__getfast__((i-1)), const_3), 37)) { comps->__delete__(3, (i-1), (i+1), 0); i = (i-1); } else if (__AND((i==0), prefix->endswith(const_4), 39)) { comps->__delitem__(i); } else { i = (i+1); } } else { i = (i+1); } } if (__AND((!___bool(prefix)), (!___bool(comps)), 41)) { comps->append(const_0); } return prefix->__add__((const_4)->join(comps)); } str *abspath(str *path) { /** Return an absolute path. */ if ((!isabs(path))) { path = join(2, __os__::getcwd(), path); } return normpath(path); } str *realpath(str *path) { return abspath(path); } #endif } // module namespace } // module namespace shedskin-0.9.4/shedskin/lib/os/path.py0000664000175000017500000000176512157270661017577 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) import stat curdir = '' pardir = '' extsep = '' sep = '' pathsep = '' defpath = '' altsep = '' devnull = '' def isdir(path): return True def exists(path): return True def lexists(path): return True def islink(path): return True def isfile(path): return True def samefile(a, b): return True def samestat(a, b): return True def split(p): return ('','') def splitext(p): return ('','') def join(*a): return '' def normcase(s): return s def isabs(s): return True def splitdrive(s): return ('', '') def basename(s): return s def dirname(s): return s def commonprefix(s): return '' def abspath(s): return '' def realpath(s): return '' def normpath(s): return '' def getsize(s): return 1 def getatime(s): return 1.0 def getmtime(s): return 1.0 def getctime(s): return 1.0 def walk(top, func, arg): func(arg, '', ['']) shedskin-0.9.4/shedskin/lib/fnmatch.py0000664000175000017500000000044112157270661017630 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) import re, os, os.path def fnmatch(name, pat): return True def filter(names, pat): return [''] def fnmatchcase(name, pat): return True def translate(pat): return '' def init(): pass shedskin-0.9.4/shedskin/lib/fnmatch.cpp0000664000175000017500000001152412157270661017766 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "fnmatch.hpp" /** Filename matching with shell patterns. fnmatch(FILENAME, PATTERN) matches according to the local convention. fnmatchcase(FILENAME, PATTERN) always takes case in account. The functions operate by translating the pattern into a regular expression. They cache the compiled regular expressions for speed. The function translate(PATTERN) returns a regular expression corresponding to PATTERN. (It does not compile it.) */ namespace __fnmatch__ { str *const_10, *const_11, *const_12, *const_13, *const_14, *const_15, *const_16, *const_17, *const_18, *const_5, *const_6, *const_7, *const_8, *const_9; dict *_cache; str *__name__; void __init() { const_5 = new str(""); const_6 = new str("*"); const_7 = new str(".*"); const_8 = new str("?"); const_9 = new str("."); const_10 = new str("["); const_11 = new str("!"); const_12 = new str("]"); const_13 = new str("\\["); const_14 = new str("\\"); const_15 = new str("\\\\"); const_16 = new str("^"); const_17 = new str("%s[%s]"); const_18 = new str("$"); __name__ = new str("__fnmatch__"); _cache = (new dict()); } __ss_bool fnmatch(str *name, str *pat) { /** Test whether FILENAME matches PATTERN. Patterns are Unix shell style: * matches everything ? matches any single character [seq] matches any character in seq [!seq] matches any char not in seq An initial period in FILENAME is not special. Both FILENAME and PATTERN are first case-normalized if the operating system requires it. If you don't want this, use fnmatchcase(FILENAME, PATTERN). */ name = __os__::__path__::normcase(name); pat = __os__::__path__::normcase(pat); return fnmatchcase(name, pat); } list *filter(list *names, str *pat) { /** Return the subset of the list NAMES that match PAT */ list *__0, *result; str *name, *res; __re__::re_object *cpat; __ss_int __2; list::for_in_loop __123; result = (new list()); pat = __os__::__path__::normcase(pat); if ((!_cache->__contains__(pat))) { res = translate(pat); _cache->__setitem__(pat, __re__::compile(res)); } cpat = _cache->__getitem__(pat); FOR_IN(name,names,0,2,123) #ifndef WIN32 if (___bool(cpat->match(name))) { #else if (___bool(cpat->match(__os__::__path__::normcase(name)))) { #endif result->append(name); } END_FOR return result; } __ss_bool fnmatchcase(str *name, str *pat) { /** Test whether FILENAME matches PATTERN, including case. This is a version of fnmatch() which doesn't case-normalize its arguments. */ str *res; if ((!_cache->__contains__(pat))) { res = translate(pat); _cache->__setitem__(pat, __re__::compile(res)); } return __mbool((_cache->__getitem__(pat))->match(name)!=0); } str *translate(str *pat) { /** Translate a shell PATTERN to a regular expression. There is no way to quote meta-characters. */ str *c, *res, *stuff; __ss_int __11, __6, __7, __9, i, j, n; __6 = len(pat); i = 0; n = __6; res = const_5; while((i__getitem__(i); i = (i+1); if (__eq(c, const_6)) { res = res->__add__(const_7); } else if (__eq(c, const_8)) { res = res->__add__(const_9); } else if (__eq(c, const_10)) { j = i; if (__AND((j__getitem__(j), const_11), 7)) { j = (j+1); } if (__AND((j__getitem__(j), const_12), 9)) { j = (j+1); } while(__AND((j__getitem__(j), const_12), 11)) { j = (j+1); } if ((j>=n)) { res = res->__add__(const_13); } else { stuff = (pat->__slice__(3, i, j, 0))->replace(const_14, const_15); i = (j+1); if (__eq(stuff->__getitem__(0), const_11)) { stuff = (const_16)->__add__(stuff->__slice__(1, 1, 0, 0)); } else if (__eq(stuff->__getitem__(0), const_16)) { stuff = (const_14)->__add__(stuff); } res = __modct(const_17, 2, res, stuff); } } else { res = res->__add__(__re__::escape(c)); } } return res->__add__(const_18); } } // module namespace shedskin-0.9.4/shedskin/lib/collections.hpp0000664000175000017500000001631612157270661020675 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __COLLECTIONS_HPP #define __COLLECTIONS_HPP #include "builtin.hpp" #include using namespace __shedskin__; namespace __collections__ { template class deque; template class __dequeiter; extern class_ *cl_deque; template class deque : public pyiter { public: std::deque > units; typename std::deque >::iterator iter; /* XXX modulo rotate, specialized reversed, copy, deepcopy */ deque(pyiter *iterable=0) { this->__class__ = cl_deque; if(iterable) extend(iterable); } void *append(A a) { units.push_back(a); return NULL; } void *appendleft(A a) { units.push_front(a); return NULL; } A pop() { if(units.empty()) throw new IndexError(new str("pop from an empty deque")); A x = units.back(); units.pop_back(); return x; } A popleft() { if(units.empty()) throw new IndexError(new str("pop from an empty deque")); A x = units.front(); units.pop_front(); return x; } A __getitem__(__ss_int i) { i = __wrap(this, i); return units[i]; } void *__setitem__(__ss_int i, A value) { i = __wrap(this, i); units[i] = value; return NULL; } void *__delitem__(__ss_int i) { i = __wrap(this, i); units.erase(units.begin()+i); return NULL; } __ss_bool __contains__(A value) { iter = units.begin(); while(iter != units.end()) { if(__eq(*iter, value)) return True; iter++; } return False; } __ss_int __len__() { return units.size(); } __iter *__iter__() { return new __dequeiter(this); } str * __repr__() { str *r = new str("deque(["); for(__ss_int i = 0; i__len__();i++) { r->unit += repr(units[i])->unit; if (i__len__()-1) r->unit += ", "; } r->unit += "])"; return r; } template void *extend(U *iter) { typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,iter,1,2,3) append(e); END_FOR return NULL; } template void *extendleft(U *iter) { typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,iter,1,2,3) appendleft(e); END_FOR return NULL; } void *remove(A value) { iter = units.begin(); while(iter != units.end()) { if(__eq(*iter, value)) { units.erase(iter); return NULL; } iter++; } throw new ValueError(new str("hops")); return NULL; } void *rotate(__ss_int n) { if(!units.empty()) { n = n % __len__(); if(n<0) for(__ss_int i=0; i>n; i--) append(popleft()); else for(__ss_int i=0; i *__copy__() { deque *c = new deque(); c->units = this->units; return c; } deque *__deepcopy__(dict *memo) { deque *c = new deque(); memo->__setitem__(this, c); for(__ss_int i=0; i__len__(); i++) c->units.push_back(__deepcopy(this->units[i], memo)); return c; } }; template class __dequeiter : public __iter { public: deque *p; __ss_int i, size; __dequeiter(deque *p) { this->p = p; size = p->units.size(); i = 0; } T next() { if(i == size) throw new StopIteration(); return p->units[i++]; } }; template class __dequereviter : public __iter { public: deque *p; __ss_int i; __dequereviter(deque *p) { this->p = p; i = p->units.size()-1; } T next() { if(i >= 0) return p->units[i--]; throw new StopIteration(); } }; template __iter *reversed(deque *d) { return new __dequereviter(d); } template class defaultdict : public dict { V (*func)(); public: defaultdict(V (*func)()=NULL) { this->func = func; } defaultdict(V (*func)(), dict *d) : dict(d) { this->func = func; } defaultdict(V (*func)(), pyiter *> *i) { /* XXX */ this->func = func; tuple2 *k; typename pyiter *>::for_in_loop __3; int __2; pyiter *> *__1; FOR_IN(k,i,1,2,3) this->__setitem__(k->__getfirst__(), k->__getsecond__()); END_FOR } V __getitem__(K key) { register long hash = hasher(key); register dictentry *entry; entry = this->lookup(key, hash); if (entry->use != active) return __missing__(key); return entry->value; } V __missing__(K k) { if(func) { V v = func(); this->__setitem__(k, v); return v; } throw new KeyError(repr(k)); } void *__addtoitem__(K key, V value) { /* XXX */ register long hash = hasher(key); register dictentry *entry; entry = this->lookup(key, hash); if (entry->use != active) { if(func) this->__setitem__(key, __add(func(), value)); else throw new KeyError(repr(key)); } else entry->value = __add(entry->value, value); return NULL; } str *__repr__() { return __add_strs(3, new str("defaultdict("), dict::__repr__(), new str(")")); } #ifdef __SS_BIND defaultdict(PyObject *p) { /* XXX merge with dict */ if(!PyDict_Check(p)) throw new TypeError(new str("error in conversion to Shed Skin (dictionary expected)")); this->__class__ = cl_dict; PyObject *key, *value; PyObject *iter = PyObject_GetIter(p); while(key = PyIter_Next(iter)) { value = PyDict_GetItem(p, key); this->__setitem__(__to_ss(key), __to_ss(value)); Py_DECREF(key); } Py_DECREF(iter); } #endif }; void __init(); } // module namespace namespace __defaultdict__ { using __collections__::defaultdict; template defaultdict *fromkeys(pyiter *f, B b) { defaultdict *d = new defaultdict(); A e; typename pyiter::for_in_loop __3; int __2; pyiter *__1; FOR_IN(e,f,1,2,3) d->__setitem__(e, b); END_FOR return d; } template defaultdict *fromkeys(pyiter *f) { return fromkeys(f, 0); } } #endif shedskin-0.9.4/shedskin/lib/socket.py0000664000175000017500000000430312157270661017501 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) # model for module socket for shed skin # from python 2.5.1 documentation SHUT_RD=0 SHUT_WR=1 SHUT_RDWR=2 SOL_IP=0 SOL_SOCKET=1 SO_REUSEADDR=2 AI_PASSIVE=1 AF_UNIX=1 AF_INET=2 AF_INET6=10 IP_TOS=1 SOCK_STREAM=1 SOCK_DGRAM=2 SOMAXCONN=128 INADDR_ANY=0 INADDR_BROADCAST=0xffffffff INADDR_NONE=0xffffffff INADDR_LOOPBACK=0x7f000001 class error(Exception): pass class herror(Exception): pass class gaierror(Exception): pass class timeout(Exception): pass class socket(object): def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0): pass def accept(self): return (socket(), ('', 1) ) def fileno(self): return 0 def makefile(self, flags=None): return file('', flags) def listen(self, backlog): return self def shutdown(self, how): return self def close(self): return self # setblocking(0) == settimeout(0.0) # setblocking(1) == settimeout(None) def setblocking(self, flag): return self def settimeout(self, value): return self def gettimeout(self): return 0.0 def setsockopt(self, level, optname, value): return self def getsockopt(self, level, optname, value=0): return '' def bind(self, address): return self def connect(self, address): return self def recv(self, bufsize, flags=0): return '' def send(self, string, flags=0): return 0 def sendall(self, string, flags=0): pass def getsockname(self): return ('', 0) def getpeername(self): return ('', 0) def recvfrom(self, bufsize, flags=0): return ('', ('', 0)) def sendto(self, bufsize, flags=0, address=0): return 0 def getfqdn(host): return '' def gethostname(): return '' def gethostbyname(hostname): return '' def ntohs(x): return 0 def htons(x): return 0 def ntohl(x): return 0 def htonl(x): return 0 def inet_aton(x): return '' def inet_ntoa(x): return '' def has_ipv6(): return True def getdefaulttimeout(): return 0.0 def setdefaulttimeout(x): pass shedskin-0.9.4/shedskin/lib/select.hpp0000664000175000017500000000443612157270661017636 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __SELECT_HPP #define __SELECT_HPP #include "builtin.hpp" using namespace __shedskin__; namespace __select__ { extern str *__name__; template tuple2 *, list<__ss_int> *> *select(A *rFDs, B *wFDs, C *xFDs, double timeout) { __ss_int __2, __6, __10; A *__0; typename A::for_in_unit FDa; typename A::for_in_loop __3; B *__4; typename B::for_in_unit FDb; typename B::for_in_loop __7; C *__8; typename C::for_in_unit FDc; typename C::for_in_loop __11; list<__ss_int> *rrFDs, *rwFDs, *rxFDs; __ss_int FD; fd_set lrFDs; fd_set lwFDs; fd_set lxFDs; int maxFD = 0; struct timeval ltimeout; FD_ZERO(&lrFDs); FD_ZERO(&lwFDs); FD_ZERO(&lxFDs); FOR_IN(FDa,rFDs,0,2,3) FD = (intptr_t)FDa; if(FD > -1) { FD_SET(FD, &lrFDs); if(FD > maxFD) maxFD = FD; } END_FOR FOR_IN(FDb,wFDs,4,6,7) FD = (intptr_t)FDb; if(FD > -1) { FD_SET(FD, &lwFDs); if(FD > maxFD) maxFD = FD; } END_FOR FOR_IN(FDc,xFDs,8,10,11) FD = (intptr_t)FDc; if(FD > -1) { FD_SET(FD, &lxFDs); if(FD > maxFD) maxFD = FD; } END_FOR memset(<imeout, 0, sizeof(ltimeout)); ltimeout.tv_sec = timeout; ltimeout.tv_usec = (timeout - floor(timeout))*1E6; if(::select(maxFD + 1, &lrFDs, &lwFDs, &lxFDs, (timeout < 0) ? NULL : <imeout) == -1) { throw new IOError(); } rrFDs = (new list<__ss_int>()); FOR_IN(FDa,rFDs,0,2,3) FD = (intptr_t)FDa; if(FD > -1 && FD_ISSET(FD, &lrFDs)) rrFDs->append(FD); END_FOR rwFDs = (new list<__ss_int>()); FOR_IN(FDb,wFDs,4,6,7) FD = (intptr_t)FDb; if(FD > -1 && FD_ISSET(FD, &lwFDs)) rwFDs->append(FD); END_FOR rxFDs = (new list<__ss_int>()); FOR_IN(FDc,xFDs,8,10,11) FD = (intptr_t)FDc; if(FD > -1 && FD_ISSET(FD, &lxFDs)) rxFDs->append(FD); END_FOR return (new tuple2 *, list<__ss_int> *>(3,rrFDs,rwFDs,rxFDs)); } void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/glob.cpp0000664000175000017500000001124712157270661017273 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "glob.hpp" /** Filename globbing utility. */ namespace __glob__ { str *const_0, *const_2, *const_3; str *__name__; __re__::re_object *magic_check; void __init() { const_0 = new str("[*?[]"); const_2 = new str("."); const_3 = new str(""); __name__ = new str("__glob__"); magic_check = __re__::compile(const_0); } list *glob(str *pathname) { /** Return a list of paths matching a pathname pattern. The pattern may contain simple shell-style wildcards a la fnmatch. */ return new list(iglob(pathname)); } class __gen_iglob : public __iter { public: pyiter *dirs; str *name; pyiter *__10; str *basename; __ss_int __15; list *__13; pyiter *__4; list *__7; __ss_int __6; list *__1; tuple2 *__0; __ss_int __3; __iter *__2; str *pathname; str *dirname; __ss_int __9; __iter *__8; __ss_int __12; __iter *__14; pyiter::for_in_loop __103; int __102; pyiter *__101; int __last_yield; list::for_in_loop __123; __gen_iglob(str *pathname) { this->pathname = pathname; __last_yield = -1; } str * next() { switch(__last_yield) { case 0: goto __after_yield_0; case 1: goto __after_yield_1; case 2: goto __after_yield_2; case 3: goto __after_yield_3; default: break; } if ((!has_magic(pathname))) { if (__os__::__path__::lexists(pathname)) { __last_yield = 0; return pathname; __after_yield_0:; } throw new StopIteration(); } __0 = __os__::__path__::split(pathname); dirname = __0->__getfirst__(); basename = __0->__getsecond__(); if ((!___bool(dirname))) { FOR_IN(name,glob1(__os__::curdir, basename),1,3,123) __last_yield = 1; return name; __after_yield_1:; END_FOR throw new StopIteration(); } if (has_magic(dirname)) { dirs = iglob(dirname); } else { dirs = (new list(1, dirname)); } if (has_magic(basename)) { FOR_IN(dirname,dirs,101,102,103) FOR_IN(name,glob1(dirname, basename),7,9,123) __last_yield = 2; return __os__::__path__::join(2, dirname, name); __after_yield_2:; END_FOR END_FOR } else { FOR_IN(dirname,dirs,101,102,103) FOR_IN(name,glob0(dirname, basename),13,15,123) __last_yield = 3; return __os__::__path__::join(2, dirname, name); __after_yield_3:; END_FOR END_FOR } throw new StopIteration(); } }; __iter *iglob(str *pathname) { /** Return a list of paths matching a pathname pattern. The pattern may contain simple shell-style wildcards a la fnmatch. */ return new __gen_iglob(pathname); } list *glob1(str *dirname, str *pattern) { list *__17, *n2, *names; str *nm; __ss_int __19; list::for_in_loop __123; if ((!___bool(dirname))) { dirname = __os__::curdir; } try { names = __os__::listdir(dirname); } catch (__os__::error *) { return ((list *)((new list()))); } if (__ne(pattern->__getitem__(0), const_2)) { n2 = (new list()); FOR_IN(nm,names,17,19,123) if (__ne(nm->__getitem__(0), const_2)) { n2->append(nm); names = n2; } END_FOR } return __fnmatch__::filter(names, pattern); } list *glob0(str *dirname, str *basename) { if (__eq(basename, const_3)) { if (__os__::__path__::isdir(dirname)) { return (new list(1, basename)); } } else { if (__os__::__path__::lexists(__os__::__path__::join(2, dirname, basename))) { return (new list(1, basename)); } } return ((list *)((new list()))); } __ss_bool has_magic(str *s) { return __mbool(magic_check->search(s)!=0); } } // module namespace shedskin-0.9.4/shedskin/lib/bisect.py0000664000175000017500000000067212157270661017467 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) def insort_right(a, x, lo=0, hi=0): insort(a, x) def insort_left(a, x, lo=0, hi=0): insort(a, x) def insort(a, x, lo=0, hi=0): a.append(x) x.__cmp__(x) def bisect_right(a, x, lo=0, hi=0): bisect(a, x) return 1 def bisect_left(a, x, lo=0, hi=0): bisect(a, x) return 1 def bisect(a, x, lo=0, hi=0): x.__cmp__(x) return 1 shedskin-0.9.4/shedskin/lib/time.hpp0000664000175000017500000000340712157270661017312 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef TIME_HPP #define TIME_HPP #include "builtin.hpp" #include #ifdef WIN32 #include #include #include #else #include #endif using namespace __shedskin__; namespace __time__ { #ifdef WIN32 struct __ss_timezone { int tz_minuteswest; int tz_dsttime; }; __ss_int gettimeofday (struct timeval *tv, struct __ss_timezone *tz); #endif extern __ss_int timezone; extern tuple2 *tzname; double clock(); double time(); void *sleep(double s); extern str *const_0, *const_1; class struct_time; extern str *__name__; extern class_ *cl_struct_time; class struct_time : public pyobj { public: __ss_int tm_sec; __ss_int tm_hour; __ss_int tm_mday; __ss_int tm_isdst; __ss_int tm_year; __ss_int tm_mon; __ss_int tm_yday; __ss_int tm_wday; __ss_int tm_min; struct_time() { __class__ = cl_struct_time; } __ss_int __getitem__(__ss_int n); struct_time(tuple2<__ss_int, __ss_int> *_tuple); str *__repr__(); }; double mktime(struct_time *tuple); double mktime(tuple2<__ss_int, __ss_int> *tuple); struct_time *localtime(); struct_time *localtime(const double timep); struct_time *gmtime(); struct_time *gmtime(const double seconds); str *asctime(); str *asctime(struct_time *tuple); str *ctime(); str *ctime(const double seconds); str *strftime(str *format, struct_time* tuple); str *strftime(str *format); str *strftime(str *format, tuple2<__ss_int, __ss_int> *tuple); struct_time *strptime(str *string, str *format); #ifdef WIN32 char *strptime(const char *, const char *, struct tm *); #endif void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/fnmatch.hpp0000664000175000017500000000135212157270661017771 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __FNMATCH_HPP #define __FNMATCH_HPP #include "builtin.hpp" #include "os/path.hpp" #include "os/__init__.hpp" #include "re.hpp" using namespace __shedskin__; namespace __fnmatch__ { extern str *const_10, *const_11, *const_12, *const_13, *const_14, *const_15, *const_16, *const_17, *const_18, *const_5, *const_6, *const_7, *const_8, *const_9; extern dict *_cache; extern str *__name__; __ss_bool fnmatch(str *name, str *pat); list *filter(list *names, str *pat); __ss_bool fnmatchcase(str *name, str *pat); str *translate(str *pat); void __init(void); } // module namespace #endif shedskin-0.9.4/shedskin/lib/datetime.hpp0000664000175000017500000001507112157270661020150 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef DATETIME_HPP #define DATETIME_HPP #include "builtin.hpp" #include "time.hpp" #include #if defined( _MSC_VER ) #include #else #include #endif #ifdef WIN32 #include #endif #include using namespace __shedskin__; namespace __datetime__ { extern __ss_int MINYEAR, MAXYEAR; void __init(); class date; class tzinfo; class datetime; class timedelta; class time; //todo: //timedelta::timedelta() rounding problems //check (unsigned) integer/long ranges (arguments as well) //class date extern class_ *cl_date; class date : public pyobj { public: __ss_int year; __ss_int month; __ss_int day; date(__ss_int year, __ss_int month, __ss_int day); date(date* d):year(d->year),month(d->month),day(d->day){__class__=cl_date;}; static date *today(); static date *fromtimestamp(__ss_int timestamp); static date *fromordinal(__ss_int o); //copied from cpython date *__add__(timedelta *other); date *__sub__(timedelta *other); timedelta *__sub__(date *other); date *replace(__ss_int year=0, __ss_int month=0, __ss_int day=0); //ok (how to handle keyword variables?) __time__::struct_time *timetuple(); //ok (depends on function from cpython) __ss_int toordinal(); //copied from cpython __ss_int weekday(); //copied from cpython __ss_int isoweekday(); //copied from cpython tuple2<__ss_int, __ss_int> *isocalendar(); str *isoformat(); str *__str__(); str *ctime(); str *strftime(str *format); __ss_int __cmp__(date *other); __ss_bool __eq__(date *other); __ss_bool __ne__(date *other); __ss_bool __gt__(date *other); __ss_bool __lt__(date *other); __ss_bool __ge__(date *other); __ss_bool __le__(date *other); }; //class tzinfo extern class_ *cl_tzinfo; class tzinfo : public pyobj { public: tzinfo(){__class__=cl_tzinfo;}; virtual timedelta *utcoffset(datetime *) {throw new NotImplementedError(new str("a tzinfo subclass must implement utcoffset()"));}; virtual timedelta *dst(datetime *) {throw new NotImplementedError(new str("a tzinfo subclass must implement dst()"));}; virtual str *tzname(datetime *) {throw new NotImplementedError(new str("a tzinfo subclass must implement tzname()"));}; virtual datetime *fromutc(datetime *dt); str *minutes_to_str(datetime *dt); void __init__() {}; }; //class datetime extern class_ *cl_datetime; class datetime : public date { public: __ss_int hour, minute, second, microsecond; tzinfo *_tzinfo; datetime(datetime *d) : date(d),hour(d->hour),minute(d->minute),second(d->second),microsecond(d->microsecond),_tzinfo(d->_tzinfo) {__class__=cl_datetime;}; datetime(__ss_int year, __ss_int month, __ss_int day, __ss_int hour=0, __ss_int minute=0, __ss_int second=0, __ss_int microsecond=0, tzinfo *tzinfo=NULL); static datetime *today(); static datetime *now(tzinfo *tzinfo=NULL); static datetime *utcnow(); static datetime *from_timestamp(double timestamp, tzinfo *tzinfo, bool timefn); static datetime *fromtimestamp(double timestamp, tzinfo *tzinfo=NULL); static datetime *utcfromtimestamp(double timestamp); static datetime *fromordinal(__ss_int o); static datetime *combine(date *d, time *t); static datetime *strptime(str *date_string, str *format); datetime *__add__(timedelta *other); datetime *__sub__(timedelta *other); timedelta *__sub__(datetime *other); date *_date(); //why is it exactly these two have a _? time *_time(); time *timetz(); datetime *replace(__ss_int __args, __ss_int year=-1, __ss_int month=-1, __ss_int day=-1, __ss_int hour=-1, __ss_int minute=-1, __ss_int second=-1, __ss_int microsecond=-1,tzinfo *tzinfo=NULL); datetime *astimezone(tzinfo *tzinfo); timedelta *utcoffset(); timedelta *dst(); str *tzname(); __time__::struct_time *timetuple(); __time__::struct_time *utctimetuple(); str *isoformat(str *sep = new str("T")); str *__str__(); str *ctime(); str *strftime(str *format); __ss_int __cmp__(datetime *other); __ss_bool __eq__(datetime *other); __ss_bool __ne__(datetime *other); __ss_bool __gt__(datetime *other); __ss_bool __lt__(datetime *other); __ss_bool __ge__(datetime *other); __ss_bool __le__(datetime *other); }; //class time extern class_ *cl_time; class time : public pyobj { public: __ss_int hour, minute, second, microsecond; tzinfo *_tzinfo; time(time *t):hour(t->hour), minute(t->minute), second(t->second), microsecond(t->microsecond), _tzinfo(t->_tzinfo) {__class__=cl_time;}; //copyconstructor time(__ss_int hour=0, __ss_int minute=0, __ss_int second=0, __ss_int microsecond=0, tzinfo *tzinfo=NULL); time *replace(__ss_int __args, __ss_int hour=-1, __ss_int minute=-1, __ss_int second=-1, __ss_int microsecond=-1, tzinfo *tzinfo=NULL); str *isoformat(); str *__str__(); str *strftime(str* format); timedelta *utcoffset(); timedelta *dst(); str *tzname(); __ss_int __cmp__(time *other); __ss_bool __eq__(time *other); __ss_bool __ne__(time *other); __ss_bool __gt__(time *other); __ss_bool __lt__(time *other); __ss_bool __ge__(time *other); __ss_bool __le__(time *other); }; //class timedelta extern class_ *cl_timedelta; class timedelta : public pyobj { public: __ss_int days; __ss_int seconds; __ss_int microseconds; timedelta(double days=0., double seconds=0., double microseconds=0., double milliseconds=0., double minutes=0., double hours=0., double weeks=0.); timedelta(timedelta *c):days(c->days),seconds(c->seconds),microseconds(c->microseconds){__class__=cl_timedelta;} str *__str__(); timedelta *__add__(timedelta *other); timedelta *__sub__(timedelta *other); timedelta *__mul__(__ss_int n); timedelta *__div__(__ss_int n); timedelta *__neg__(); timedelta *__floordiv__(__ss_int n); //what's the difference between this and __div__? timedelta *__abs__(); __ss_int __cmp__(timedelta *other); __ss_bool __eq__(timedelta *other); __ss_bool __ne__(timedelta *other); __ss_bool __gt__(timedelta *other); __ss_bool __lt__(timedelta *other); __ss_bool __ge__(timedelta *other); __ss_bool __le__(timedelta *other); }; } // module namespace #endif shedskin-0.9.4/shedskin/lib/time.py0000664000175000017500000000162612157270661017154 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) timezone = 0 tzname = ("str", "str") def clock(): return 1.0 def sleep(s): pass def time(): return 1.0 class struct_time: def __init__(self, tuple): self.tm_year = 0 self.tm_mon = 0 self.tm_mday = 0 self.tm_hour = 0 self.tm_min = 0 self.tm_sec = 0 self.tm_wday = 0 self.tm_yday = 0 self.isdst = 0 def __getitem__(self, n): return 1 def __repr__(self): return "str" def mktime(tuple): return 1.0 def localtime(timep=None): return struct_time((1,2,3)) def gmtime(seconds=None): return struct_time((1,2,3)) def asctime(tuple=None): return "str" def ctime(seconds=None): return "str" def strftime(format, tuple=None): return "str" def strptime(string, format): return struct_time((1,2,3)) shedskin-0.9.4/shedskin/lib/builtin.hpp0000664000175000017500000010211412157270661020015 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef BUILTIN_HPP #define BUILTIN_HPP #ifdef __SS_BIND #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef WIN32 #include #include #include #endif #if defined(_MSC_VER) #include "builtin/msvc.hpp" #endif namespace __shedskin__ { /* integer type */ #ifdef __SS_LONG typedef long long __ss_int; #else typedef int __ss_int; #endif /* forward class declarations */ class __ss_bool; class complex; class pyobj; class class_; class str; class file; class int_; class bool_; class float_; class complex_; template class pyiter; template class pyseq; template class list; template class tuple2; template class set; template class dict; template class __iter; template class __seqiter; template class __setiter; template class __dictiterkeys; template class __dictitervalues; template class __dictiteritems; class __fileiter; class __xrange; class __rangeiter; class BaseException; class Exception; class StandardError; class AssertionError; class KeyError; class ValueError; class IndexError; class NotImplementedError; class IOError; class OSError; class SyntaxError; class StopIteration; class TypeError; class RuntimeError; class OverflowError; /* STL types */ #define __GC_VECTOR(T) std::vector< T, gc_allocator< T > > #define __GC_DEQUE(T) std::deque< T, gc_allocator< T > > #define __GC_STRING std::basic_string,gc_allocator > extern __ss_bool True; extern __ss_bool False; /* class declarations */ #ifdef __SS_GC_CLEANUP class pyobj : public gc_cleanup { #else class pyobj : public gc { #endif public: class_ *__class__; virtual str *__repr__(); virtual str *__str__(); virtual long __hash__(); virtual __ss_int __cmp__(pyobj *p); virtual __ss_bool __eq__(pyobj *p); virtual __ss_bool __ne__(pyobj *p); virtual __ss_bool __gt__(pyobj *p); virtual __ss_bool __lt__(pyobj *p); virtual __ss_bool __ge__(pyobj *p); virtual __ss_bool __le__(pyobj *p); virtual pyobj *__copy__(); virtual pyobj *__deepcopy__(dict *); virtual __ss_int __len__(); virtual __ss_int __int__(); virtual __ss_bool __nonzero__(); virtual __ss_int __index__(); static const bool is_pyseq = false; }; template class pyiter : public pyobj { public: virtual __iter *__iter__() = 0; virtual __ss_bool __contains__(T t); typedef T for_in_unit; typedef __iter * for_in_loop; inline __iter *for_in_init(); inline bool for_in_has_next(__iter *iter); inline T for_in_next(__iter *iter); }; template class pyseq : public pyiter { public: virtual __ss_int __len__() = 0; virtual T __getitem__(__ss_int i) = 0; virtual __ss_int __cmp__(pyobj *p); virtual __iter *__iter__(); typedef T for_in_unit; typedef size_t for_in_loop; inline size_t for_in_init(); inline bool for_in_has_next(size_t i); inline T for_in_next(size_t &i); static const bool is_pyseq = true; }; template class pycall1 : public pyobj { public: virtual R __call__(A a) = 0; }; template class pycall2 : public pyobj { public: virtual R __call__(A a, B b) = 0; }; template class list : public pyseq { public: __GC_VECTOR(T) units; list(); list(int count, ...); template list(U *iter); list(list *p); list(tuple2 *p); list(str *s); void clear(); void *__setitem__(__ss_int i, T e); void *__delitem__(__ss_int i); int empty(); list *__slice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s); void *__setslice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s, pyiter *b); void *__setslice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s, list *b); void *__delete__(__ss_int i); void *__delete__(__ss_int x, __ss_int l, __ss_int u, __ss_int s); void *__delslice__(__ss_int a, __ss_int b); __ss_bool __contains__(T a); list *__add__(list *b); list *__mul__(__ss_int b); template void *extend(U *iter); void *extend(list *p); void *extend(tuple2 *p); void *extend(str *s); template list *__iadd__(U *iter); list *__imul__(__ss_int n); __ss_int index(T a); __ss_int index(T a, __ss_int s); __ss_int index(T a, __ss_int s, __ss_int e); __ss_int count(T a); str *__repr__(); __ss_bool __eq__(pyobj *l); void resize(__ss_int i); /* XXX remove */ inline T __getfast__(__ss_int i); inline T __getitem__(__ss_int i); inline __ss_int __len__(); T pop(); T pop(int m); void *remove(T e); void *insert(int m, T e); void *append(T a); void *reverse(); template void *sort(__ss_int (*cmp)(T, T), U (*key)(T), __ss_int reverse); template void *sort(__ss_int cmp, U (*key)(T), __ss_int reverse); void *sort(__ss_int (*cmp)(T, T), __ss_int key, __ss_int reverse); void *sort(__ss_int cmp, __ss_int key, __ss_int reverse); list *__copy__(); list *__deepcopy__(dict *memo); /* iteration */ inline bool for_in_has_next(size_t i); inline T for_in_next(size_t &i); #ifdef __SS_BIND list(PyObject *); PyObject *__to_py__(); #endif }; template class tuple2 : public pyobj { public: A first; B second; tuple2(); tuple2(int n, A a, B b); void __init2__(A a, B b); A __getfirst__(); B __getsecond__(); str *__repr__(); __ss_int __len__(); __ss_bool __eq__(pyobj *p); __ss_int __cmp__(pyobj *p); long __hash__(); tuple2 *__copy__(); tuple2 *__deepcopy__(dict *memo); #ifdef __SS_BIND tuple2(PyObject *p); PyObject *__to_py__(); #endif }; template class tuple2 : public pyseq { public: __GC_VECTOR(T) units; tuple2(); tuple2(int count, ...); template tuple2(U *iter); tuple2(list *p); tuple2(tuple2 *p); tuple2(str *s); void __init2__(T a, T b); T __getfirst__(); T __getsecond__(); inline T __getfast__(__ss_int i); inline T __getitem__(__ss_int i); inline __ss_int __len__(); str *__repr__(); tuple2 *__add__(tuple2 *b); tuple2 *__mul__(__ss_int b); tuple2 *__iadd__(tuple2 *b); tuple2 *__imul__(__ss_int n); __ss_bool __contains__(T a); __ss_bool __eq__(pyobj *p); tuple2 *__slice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s); long __hash__(); tuple2 *__deepcopy__(dict *memo); tuple2 *__copy__(); /* iteration */ inline bool for_in_has_next(size_t i); inline T for_in_next(size_t &i); #ifdef __SS_BIND tuple2(PyObject *p); PyObject *__to_py__(); #endif }; class str : public pyseq { public: __GC_STRING unit; long hash; bool charcache; str(); str(const char *s); str(__GC_STRING s); str(const char *s, int size); /* '\0' delimiter in C */ __ss_bool __contains__(str *s); str *strip(str *chars=0); str *lstrip(str *chars=0); str *rstrip(str *chars=0); list *split(str *sep=0, int maxsplit=-1); __ss_bool __eq__(pyobj *s); str *__add__(str *b); template str *join(U *); str *__str__(); str *__repr__(); str *__mul__(__ss_int n); inline str *__getitem__(__ss_int n); inline str *__getfast__(__ss_int i); inline __ss_int __len__(); str *__slice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s); list *rsplit(str *sep = 0, int maxsplit = -1); tuple2 *rpartition(str *sep); tuple2 *partition(str *sep); list *splitlines(int keepends = 0); int __fixstart(int a, int b); int __checkneg(int i); int find(str *s, int a=0); int find(str *s, int a, int b); int rfind(str *s, int a=0); int rfind(str *s, int a, int b); int index(str *s, int a=0); int index(str *s, int a, int b); int rindex(str *s, int a=0); int rindex(str *s, int a, int b); __ss_int count(str *s, __ss_int start=0); __ss_int count(str *s, __ss_int start, __ss_int end); str *upper(); str *lower(); str *title(); str *capitalize(); str *replace(str *a, str *b, int c=-1); str *translate(str *table, str *delchars=0); str *swapcase(); str *center(int w, str *fill=0); __ss_bool __ctype_function(int (*cfunc)(int)); __ss_bool istitle(); __ss_bool isspace(); __ss_bool isalpha(); __ss_bool isdigit(); __ss_bool islower(); __ss_bool isupper(); __ss_bool isalnum(); __ss_bool startswith(str *s, __ss_int start=0); __ss_bool startswith(str *s, __ss_int start, __ss_int end); __ss_bool endswith(str *s, __ss_int start=0); __ss_bool endswith(str *s, __ss_int start, __ss_int end); str *zfill(int width); str *expandtabs(int width=8); str *ljust(int width, str *fchar=0); str *rjust(int width, str *fchar=0); __ss_int __cmp__(pyobj *p); long __hash__(); __ss_int __int__(); /* XXX compilation warning for int(pyseq *) */ str *__iadd__(str *b); str *__imul__(__ss_int n); /* iteration */ inline bool for_in_has_next(size_t i); inline str *for_in_next(size_t &i); #ifdef __SS_BIND str(PyObject *p); PyObject *__to_py__(); #endif }; void __throw_index_out_of_range(); void __throw_range_step_zero(); void __throw_set_changed(); void __throw_dict_changed(); void __throw_slice_step_zero(); void __throw_stop_iteration(); template class dictentry; const int MINSIZE = 8; template struct dict_looper { __ss_int pos; int si_used; dictentry *entry; }; template class dict : public pyiter { public: int fill; int used; int mask; dictentry *table; dictentry smalltable[MINSIZE]; dict(); dict(int count, ...); template dict(U *other); dict(dict *p); dict& operator=(const dict& other); void *__setitem__(K k, V v); V __getitem__(K k); void *__delitem__(K k); int do_discard(K key); list *keys(); list *values(); list *> *items(); __ss_int __len__(); str *__repr__(); __ss_bool has_key(K k); __ss_bool __contains__(K key); __ss_bool __contains__(dictentry* entry); void *clear(); dict *copy(); V get(K k); V get(K k, V v); V pop(K k); tuple2 *popitem(); template void *update(U *other); void *update(dict *e); __ss_bool __gt__(dict *s); __ss_bool __lt__(dict *s); __ss_bool __ge__(dict *s); __ss_bool __le__(dict *s); __ss_bool __eq__(pyobj *p); __ss_int __cmp__(pyobj *p); V setdefault(K k, V v=0); __dictiterkeys *__iter__() { return new __dictiterkeys(this);} __dictiterkeys *iterkeys() { return new __dictiterkeys(this);} __dictitervalues *itervalues() { return new __dictitervalues(this);} __dictiteritems *iteritems() { return new __dictiteritems(this);} dict *__deepcopy__(dict *memo); dict *__copy__(); void *__addtoitem__(K k, V v); /* iteration */ typedef K for_in_unit; typedef dict_looper for_in_loop; inline dict_looper for_in_init() { dict_looper l; l.pos = 0; l.si_used = used; return l; } inline bool for_in_has_next(dict_looper &l) { if (l.si_used != used) { l.si_used = -1; __throw_dict_changed(); } int ret = next(&l.pos, &l.entry); if (!ret) return false; return true; } inline K for_in_next(dict_looper &l) { return l.entry->key; } #ifdef __SS_BIND dict(PyObject *); PyObject *__to_py__(); #endif // used internally dictentry* lookup(K key, long hash) const; void insert_key(K key, V value, long hash); void insert_clean(K key, V value, long hash); int next(__ss_int *pos_ptr, dictentry **entry_ptr); void resize(int minused); }; template struct setentry; template struct set_looper { int pos; int si_used; setentry *entry; }; template class set : public pyiter { public: int frozen; int fill; int used; int mask; setentry *table; setentry smalltable[MINSIZE]; long hash; template set(U *other, int frozen); template set(U *other); set(int frozen=0); set& operator=(const set& other); void *add(T key); void *add(setentry* entry); void *discard(T key); int do_discard(T key); void *remove(T key); T pop(); str* __repr__(); __ss_bool __contains__(T key); __ss_bool __contains__(setentry* entry); __ss_int __len__(); void *clear(); set *copy(); template void *update(int, U *other); void *update(int, set *s); template void *update(int, U *other, V *other2); template void *update(int, U *other, V *other2, W *other3); template set *intersection(int, U *other); set *intersection(int, set *s); template set *intersection(int, U *iter, V *iter2); template set *intersection(int, U *iter, V *iter2, W *iter3); template void *intersection_update(int, U *other); void *intersection_update(int, set *s); template void *intersection_update(int, U *other, V *other2); template void *intersection_update(int, U *other, V *other2, W *other3); template set *difference(int, U *other); set *difference(int, set *s); template set *difference(int, U *other, V *other2); template set *difference(int, U *other, V *other2, W *other3); template void *difference_update(int, U *other); void *difference_update(int, set *s); template void *difference_update(int, U *other, V *other2); template void *difference_update(int, U *other, V *other2, W *other3); set *symmetric_difference(set *s); void *symmetric_difference_update(set *s); template set *__ss_union(int, U *other); set *__ss_union(int, set *s); template set *__ss_union(int, U *other, V *other2); template set *__ss_union(int, U *other, V *other2, W *other3); set *__and__(set *s); set *__or__(set *s); set *__xor__(set *s); set *__sub__(set *s); set *__iand__(set *s); set *__ior__(set *s); set *__ixor__(set *s); set *__isub__(set *s); __ss_bool issubset(pyiter *s); __ss_bool issubset(set *s); __ss_bool issuperset(set *s); __ss_bool issuperset(pyiter *s); __ss_bool isdisjoint(set *s); __ss_bool isdisjoint(pyiter *s); __ss_bool __gt__(set *s); __ss_bool __lt__(set *s); __ss_bool __ge__(set *s); __ss_bool __le__(set *s); __ss_bool __eq__(pyobj *p); __ss_int __cmp__(pyobj *p); __setiter *__iter__() { return new __setiter(this); } set *__copy__(); set *__deepcopy__(dict *memo); /* iteration */ typedef T for_in_unit; typedef set_looper for_in_loop; inline set_looper for_in_init() { set_looper l; l.pos = 0; l.si_used = used; return l; } inline bool for_in_has_next(set_looper &l) { if (l.si_used != used) { l.si_used = -1; __throw_set_changed(); } int ret = next(&l.pos, &l.entry); if (!ret) return false; return true; } inline T for_in_next(set_looper &l) { return l.entry->key; } #ifdef __SS_BIND set(PyObject *); PyObject *__to_py__(); #endif long __hash__(); // used internally setentry* lookup(T key, long hash) const; void insert_key(T key, long hash); void insert_clean(T key, long hash); int next(int *pos_ptr, setentry **entry_ptr); void resize(int minused); }; class __ss_bool { public: int value; inline __ss_int operator+(__ss_bool b); inline __ss_bool operator==(__ss_bool b); inline __ss_bool operator&(__ss_bool b); inline __ss_bool operator|(__ss_bool b); inline __ss_bool operator^(__ss_bool b); inline bool operator!(); inline operator bool(); inline __ss_bool& operator=(int a); }; class complex { public: double real, imag; inline complex operator+(complex b); inline complex operator+(double b); inline complex operator-(complex b); inline complex operator-(double b); inline complex operator*(complex b); inline complex operator*(double b); inline complex operator/(complex b); inline complex operator/(double b); inline complex operator%(complex b); inline complex operator%(double b); inline complex operator+(); inline complex operator-(); inline __ss_bool operator==(complex b); inline __ss_bool operator==(double b); inline __ss_bool operator!=(complex b); inline __ss_bool operator!=(double b); inline complex& operator=(double a); inline complex conjugate(); complex parsevalue(str *s); inline long __hash__(); str *__repr__(); }; complex mcomplex(double real=0.0, double imag=0.0); template complex mcomplex(T t); complex mcomplex(str *s); inline complex operator+(double a, complex b) { return mcomplex(a)+b; } inline complex operator-(double a, complex b) { return mcomplex(a)-b; } inline complex operator*(double a, complex b) { return mcomplex(a)*b; } inline complex operator/(double a, complex b) { return mcomplex(a)/b; } inline complex operator%(double a, complex b) { return mcomplex(a)%b; } inline __ss_bool operator==(double a, complex b) { return mcomplex(a)==b; } inline __ss_bool operator!=(double a, complex b) { return mcomplex(a)!=b; } class class_: public pyobj { public: str *__name__; class_(const char *name); str *__repr__(); __ss_bool __eq__(pyobj *c); }; class int_ : public pyobj { public: __ss_int unit; int_(__ss_int i); str *__repr__(); __ss_bool __nonzero__(); }; class float_ : public pyobj { public: double unit; float_(double f); str *__repr__(); __ss_bool __nonzero__(); }; class bool_ : public pyobj { public: __ss_bool unit; bool_(__ss_bool i); str *__repr__(); __ss_bool __nonzero__(); __ss_int __index__(); }; class complex_ : public pyobj { public: complex unit; complex_(complex i); str *__repr__(); __ss_bool __nonzero__(); }; class object : public pyobj { public: object(); }; template class __iter : public pyiter { public: T __result; bool __stop_iteration; __iter *__iter__(); virtual T next(); /* __get_next can be overloaded to avoid (slow) exception handling */ virtual T __get_next(); str *__repr__(); }; template class __setiter : public __iter { public: set *p; int pos; int si_used; int len; setentry* entry; __setiter(set *p); T next(); }; class __xrange : public pyiter<__ss_int> { public: __ss_int a, b, s; __xrange(__ss_int a, __ss_int b, __ss_int s); __iter<__ss_int> *__iter__(); __ss_int __len__(); str *__repr__(); }; template class __seqiter : public __iter { public: size_t counter, size; pyseq *p; __seqiter(); __seqiter(pyseq *p); T next(); }; template class __dictiterkeys : public __iter { public: dict *p; __ss_int pos; int si_used; int len; dictentry* entry; __dictiterkeys(dict *p); K next(); }; template class __dictitervalues : public __iter { public: dict *p; __ss_int pos; int si_used; int len; dictentry* entry; __dictitervalues(dict *p); V next(); }; template class __dictiteritems : public __iter *> { public: dict *p; __ss_int pos; int si_used; int len; dictentry* entry; __dictiteritems(dict *p); tuple2 *next(); }; static inline __ss_bool __mbool(bool c) { __ss_bool b; b.value=(int)c; return b; } /* builtin function declarations */ template __iter *___iter(pyiter *p) { return p->__iter__(); } file *open(str *name, str *flags = 0); str *raw_input(str *msg = 0); void print(int n, file *f, str *end, str *sep, ...); void print2(file *f, int comma, int n, ...); list<__ss_int> *range(__ss_int b); list<__ss_int> *range(__ss_int a, __ss_int b, __ss_int s=1); __xrange *xrange(__ss_int b); __xrange *xrange(__ss_int a, __ss_int b, __ss_int s=1); static inline double __portableround(double x) { if(x<0) return ceil(x-0.5); return floor(x+0.5); } inline double ___round(double a) { return __portableround(a); } inline double ___round(double a, int n) { return __portableround(pow((double)10,n)*a)/pow((double)10,n); } template inline T __abs(T t) { return t->__abs__(); } #ifdef __SS_LONG template<> inline __ss_int __abs(__ss_int a) { return a<0?-a:a; } #endif template<> inline int __abs(int a) { return a<0?-a:a; } template<> inline double __abs(double a) { return a<0?-a:a; } inline int __abs(__ss_bool b) { return __abs(b.value); } template str *hex(T t) { return t->__hex__(); } #ifdef __SS_LONG template<> str *hex(__ss_int a); #endif template<> str *hex(int a); template<> str *hex(__ss_bool b); template str *oct(T t) { return t->__oct__(); } #ifdef __SS_LONG template<> str *oct(__ss_int a); #endif template<> str *oct(int a); template<> str *oct(__ss_bool b); template str *bin(T t) { return bin(t->__index__()); } #ifdef __SS_LONG template<> str *bin(__ss_int a); #endif template<> str *bin(int a); template<> str *bin(__ss_bool b); str *__mod4(str *fmt, list *vals); str *__modct(str *fmt, int n, ...); str *__modcd(str *fmt, list *l, ...); template str *__modtuple(str *fmt, tuple2 *t); template str *__modtuple(str *fmt, tuple2 *t); /* internal use */ #ifdef __sun #define INFINITY __builtin_inff() #endif #if defined( _MSC_VER ) #define INFINITY std::numeric_limits::infinity() #endif #define __SS_MIN(a,b) ((a) < (b) ? (a) : (b)) #define __SS_MIN3(a,b,c) (__SS_MIN((a), __SS_MIN((b), (c)))) #define __SS_MAX(a,b) ((a) > (b) ? (a) : (b)) #define __SS_MAX3(a,b,c) (__SS_MAX((a), __SS_MAX((b), (c)))) void __init(); void __start(void (*initfunc)()); void __ss_exit(int code=0); /* slicing */ static void inline slicenr(__ss_int x, __ss_int &l, __ss_int &u, __ss_int &s, __ss_int len); #include "builtin/hash.hpp" #include "builtin/compare.hpp" template inline int __is_none(T *t) { return !t; } template inline int __is_none(T) { return 0; } /* externs */ extern class_ *cl_str_, *cl_int_, *cl_bool, *cl_float_, *cl_complex, *cl_list, *cl_tuple, *cl_dict, *cl_set, *cl_object, *cl_xrange, *cl_rangeiter; extern __GC_VECTOR(str *) __char_cache; extern list *__join_cache; extern file *__ss_stdin, *__ss_stdout, *__ss_stderr; /* set */ const int PERTURB_SHIFT = 5; const int DISCARD_NOTFOUND = 0; const int DISCARD_FOUND = 1; const int unused = 0; const int dummy = 1; const int active = 2; template struct setentry { long hash; // avoid rehashings... T key; int use; }; template struct dictentry { long hash; K key; V value; int use; }; /* int */ inline __ss_int __int() { return 0; } __ss_int __int(str *s, __ss_int base); template inline __ss_int __int(T t) { return t->__int__(); } #ifdef __SS_LONG template<> inline __ss_int __int(__ss_int i) { return i; } #endif template<> inline __ss_int __int(int i) { return i; } template<> inline __ss_int __int(str *s) { return __int(s, 10); } template<> inline __ss_int __int(__ss_bool b) { return b.value; } template<> inline __ss_int __int(double d) { return (__ss_int)d; } /* float */ inline double __float() { return 0; } template inline double __float(T t) { return t->__float__(); } #ifdef __SS_LONG template<> inline double __float(__ss_int p) { return p; } #endif template<> inline double __float(int p) { return p; } template<> inline double __float(__ss_bool b) { return __float(b.value); } template<> inline double __float(double d) { return d; } template<> double __float(str *s); /* str */ template str *__str(T t) { if (!t) return new str("None"); return t->__str__(); } template<> str *__str(double t); #ifdef __SS_LONG str *__str(__ss_int t, __ss_int base=10); #endif str *__str(int t, int base=10); str *__str(__ss_bool b); str *__str(void *); str *__str(); str *__add_strs(int n, str *a, str *b, str *c); str *__add_strs(int n, str *a, str *b, str *c, str *d); str *__add_strs(int n, str *a, str *b, str *c, str *d, str *e); str *__add_strs(int n, ...); /* repr */ template str *repr(T t) { if (!t) return new str("None"); return t->__repr__(); } template<> str *repr(double t); #ifdef __SS_LONG template<> str *repr(__ss_int t); #endif template<> str *repr(int t); template<> str *repr(__ss_bool b); template<> str *repr(void *t); #ifndef __SS_NOASSERT #define ASSERT(x, y) if(!(x)) throw new AssertionError(y); #else #define ASSERT(x, y) #endif #include "builtin/iter.hpp" /* len */ template inline __ss_int len(T x) { return x->__len__(); } template inline __ss_int len(list *x) { return x->units.size(); } /* XXX more general solution? */ #include "builtin/bool.hpp" #include "builtin/exception.hpp" #include "builtin/extmod.hpp" /* file objects */ struct __file_options { char lastchar; int space; bool universal_mode; bool cr; __file_options() : lastchar('\n'), space(0), universal_mode(false), cr(false) {} }; class file : public pyiter { public: str *name; str *mode; FILE *f; __ss_int closed; __file_options options; __GC_VECTOR(char) __read_cache; file(FILE *g=0) : f(g) {} file(str *name, str *mode=0); virtual void * close(); virtual void * flush(); virtual int __ss_fileno(); virtual __ss_bool isatty(); virtual str * next(); virtual str * read(int n=-1); virtual str * readline(int n=-1); list * readlines(__ss_int size_hint=-1); virtual void * seek(__ss_int i, __ss_int w=0); virtual __ss_int tell(); virtual void * truncate(int size); virtual void * write(str *s); template void *writelines(U *iter); __iter *xreadlines(); virtual void __enter__(); virtual void __exit__(); virtual __iter *__iter__(); virtual str *__repr__(); virtual bool __eof(); virtual bool __error(); inline void __check_closed() { if(closed) throw new ValueError(new str("I/O operation on closed file")); } }; class __fileiter : public __iter { public: file *p; __fileiter(file *p); str *next(); }; /* with statement */ template class __With { public: __With(T expr) : _expr(expr) { _expr->__enter__(); } ~__With() { _expr->__exit__(); } operator T() const { return _expr; } private: T _expr; }; #define WITH(e, n) { \ __With __with##n(e); // TODO unique id #define WITH_VAR(e, v, n) { \ __With __with##n(e); \ typeof(e) v = __with##n; #define END_WITH } template static inline int __wrap(T a, int i) { #ifndef __SS_NOWRAP if(i<0) return len(a)+i; #endif #ifndef __SS_NOBOUNDS if(i<0 || i>= len(a)) __throw_index_out_of_range(); #endif return i; } /* copy */ template T __copy(T t) { if(!t) return (T)NULL; return (T)(t->__copy__()); } #ifdef __SS_LONG template<> inline __ss_int __copy(__ss_int i) { return i; } #endif template<> inline int __copy(int i) { return i; } template<> inline __ss_bool __copy(__ss_bool b) { return b; } template<> inline double __copy(double d) { return d; } template<> inline void *__copy(void *p) { return p; } template T __deepcopy(T t, dict *memo=0) { if(!t) return (T)NULL; if(!memo) memo = new dict(); T u = (T)(memo->get(t, 0)); if(u) return u; return (T)(t->__deepcopy__(memo)); } #ifdef __SS_LONG template<> inline __ss_int __deepcopy(__ss_int i, dict *) { return i; } #endif template<> inline int __deepcopy(int i, dict *) { return i; } template<> inline __ss_bool __deepcopy(__ss_bool b, dict *) { return b; } template<> inline double __deepcopy(double d, dict *) { return d; } template<> inline void *__deepcopy(void *p, dict *) { return p; } /* and, or, not */ #define __OR(a, b, t) ((___bool(__ ## t = a))?(__ ## t):(b)) #define __AND(a, b, t) ((!___bool(__ ## t = a))?(__ ## t):(b)) #define __NOT(x) (__mbool(!(x))) /* 'zero' value for type */ template T __zero() { return 0; } template<> inline __ss_bool __zero<__ss_bool>() { return False; } template<> inline complex __zero() { return mcomplex(0,0); } #include "builtin/list.hpp" #include "builtin/tuple.hpp" #include "builtin/str.hpp" #include "builtin/math.hpp" #include "builtin/dict.hpp" #include "builtin/set.hpp" #include "builtin/file.hpp" #include "builtin/format.hpp" #include "builtin/function.hpp" #include "builtin/complex.hpp" /* pyiter methods */ template inline __iter *pyiter::for_in_init() { return this->__iter__(); } template inline bool pyiter::for_in_has_next(__iter *iter) { iter->__result = iter->__get_next(); return not iter->__stop_iteration; } template inline T pyiter::for_in_next(__iter *iter) { return iter->__result; } template inline __ss_bool pyiter::__contains__(T t) { T e; typename pyiter::for_in_loop __3; int __2; pyiter *__1; FOR_IN(e,this,1,2,3) if(__eq(e,t)) return __mbool(true); END_FOR return __mbool(false); } /* pyseq methods */ template __ss_int pyseq::__cmp__(pyobj *p) { if (!p) return 1; pyseq *b = (pyseq *)p; int i, cmp; int mnm = ___min(2, 0, this->__len__(), b->__len__()); for(i = 0; i < mnm; i++) { cmp = __cmp(this->__getitem__(i), b->__getitem__(i)); if(cmp) return cmp; } return __cmp(this->__len__(), b->__len__()); } template __iter *pyseq::__iter__() { return new __seqiter(this); } template inline size_t pyseq::for_in_init() { return 0; } template inline bool pyseq::for_in_has_next(size_t i) { return (__ss_int)i < __len__(); /* XXX opt end cond */ } template inline T pyseq::for_in_next(size_t &i) { return __getitem__(i++); } /* __iter methods */ template __iter *__iter::__iter__() { __stop_iteration = false; return this; } template T __iter::next() { /* __get_next can be overloaded instead to avoid (slow) exception handling */ __result = this->__get_next(); if(__stop_iteration) throw new StopIteration(); return __result; } template T __iter::__get_next() { try { __result = this->next(); } catch (StopIteration *) { __stop_iteration = true; } return __result; } /* iterators */ template str *__iter::__repr__() { return new str("iterator instance"); } template __seqiter::__seqiter() {} template __seqiter::__seqiter(pyseq *p) { this->p = p; size = p->__len__(); counter = 0; } template T __seqiter::next() { if(counter==size) __throw_stop_iteration(); return p->__getitem__(counter++); } /* slicing */ static void inline slicenr(__ss_int x, __ss_int &l, __ss_int &u, __ss_int &s, __ss_int len) { if(x&4) { if (s == 0) __throw_slice_step_zero(); } else s = 1; if (l>=len) l = len; else if (l<0) { l = len+l; if(l<0) l = 0; } if (u>=len) u = len; else if (u<0) { u = len+u; if(u<0) u = 0; } if(s<0) { if (!(x&1)) l = len-1; if (!(x&2)) u = -1; } else { if (!(x&1)) l = 0; if (!(x&2)) u = len; } } } // namespace __shedskin__ #endif shedskin-0.9.4/shedskin/lib/colorsys.cpp0000664000175000017500000001106712157270661020225 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "builtin.hpp" #include "colorsys.hpp" /** Conversion functions between RGB and other color systems. This modules provides two functions for each color system ABC: rgb_to_abc(r, g, b) --> a, b, c abc_to_rgb(a, b, c) --> r, g, b All inputs and outputs are triples of floats in the range [0.0...1.0] (with the exception of I and Q, which covers a slightly larger range). Inputs outside the valid range may cause exceptions or invalid outputs. Supported color systems: RGB: Red, Green, Blue components YIQ: Luminance, Chrominance (used by composite video signals) HLS: Hue, Luminance, Saturation HSV: Hue, Saturation, Value */ namespace __colorsys__ { double ONE_SIXTH, ONE_THIRD, TWO_THIRD; tuple2 *rgb_to_yiq(double r, double g, double b) { double i, q, y; y = (((0.3*r)+(0.59*g))+(0.11*b)); i = (((0.6*r)-(0.28*g))-(0.32*b)); q = (((0.21*r)-(0.52*g))+(0.31*b)); return (new tuple2(3,y,i,q)); } tuple2 *yiq_to_rgb(double y, double i, double q) { double b, g, r; r = ((y+(0.948262*i))+(0.624013*q)); g = ((y-(0.276066*i))-(0.63981*q)); b = ((y-(1.10545*i))+(1.72986*q)); if ((r<0.0)) { r = 0.0; } if ((g<0.0)) { g = 0.0; } if ((b<0.0)) { b = 0.0; } if ((r>1.0)) { r = 1.0; } if ((g>1.0)) { g = 1.0; } if ((b>1.0)) { b = 1.0; } return (new tuple2(3,r,g,b)); } tuple2 *rgb_to_hls(double r, double g, double b) { double bc, gc, h, l, maxc, minc, rc, s; maxc = ___max(3, ((double)(0)), r, g, b); minc = ___min(3, ((double)(0)), r, g, b); l = ((minc+maxc)/2.0); if ((minc==maxc)) { return (new tuple2(3,0.0,l,0.0)); } if ((l<=0.5)) { s = ((maxc-minc)/(maxc+minc)); } else { s = ((maxc-minc)/((2.0-maxc)-minc)); } rc = ((maxc-r)/(maxc-minc)); gc = ((maxc-g)/(maxc-minc)); bc = ((maxc-b)/(maxc-minc)); if ((r==maxc)) { h = (bc-gc); } else if ((g==maxc)) { h = ((2.0+rc)-bc); } else { h = ((4.0+gc)-rc); } h = __mods((h/6.0), 1.0); return (new tuple2(3,h,l,s)); } tuple2 *hls_to_rgb(double h, double l, double s) { double m1, m2; if ((s==0.0)) { return (new tuple2(3,l,l,l)); } if ((l<=0.5)) { m2 = (l*(1.0+s)); } else { m2 = ((l+s)-(l*s)); } m1 = ((2.0*l)-m2); return (new tuple2(3,_v(m1, m2, (h+ONE_THIRD)),_v(m1, m2, h),_v(m1, m2, (h-ONE_THIRD)))); } double _v(double m1, double m2, double hue) { hue = __mods(hue, 1.0); if ((hue *rgb_to_hsv(double r, double g, double b) { double bc, gc, h, maxc, minc, rc, s, v; maxc = ___max(3, ((double)(0)), r, g, b); minc = ___min(3, ((double)(0)), r, g, b); v = maxc; if ((minc==maxc)) { return (new tuple2(3,0.0,0.0,v)); } s = ((maxc-minc)/maxc); rc = ((maxc-r)/(maxc-minc)); gc = ((maxc-g)/(maxc-minc)); bc = ((maxc-b)/(maxc-minc)); if ((r==maxc)) { h = (bc-gc); } else if ((g==maxc)) { h = ((2.0+rc)-bc); } else { h = ((4.0+gc)-rc); } h = __mods((h/6.0), 1.0); return (new tuple2(3,h,s,v)); } tuple2 *hsv_to_rgb(double h, double s, double v) { double f, p, q, t; __ss_int i; if ((s==0.0)) { return (new tuple2(3,v,v,v)); } i = __int((h*6.0)); f = ((h*6.0)-i); p = (v*(1.0-s)); q = (v*(1.0-(s*f))); t = (v*(1.0-(s*(1.0-f)))); i = __mods(i, (__ss_int)6); if ((i==0)) { return (new tuple2(3,v,t,p)); } if ((i==1)) { return (new tuple2(3,q,v,p)); } if ((i==2)) { return (new tuple2(3,p,v,t)); } if ((i==3)) { return (new tuple2(3,p,q,v)); } if ((i==4)) { return (new tuple2(3,t,p,v)); } if ((i==5)) { return (new tuple2(3,v,p,q)); } return 0; } void __init() { ONE_THIRD = (1.0/3.0); ONE_SIXTH = (1.0/6.0); TWO_THIRD = (2.0/3.0); } } // module namespace shedskin-0.9.4/shedskin/lib/copy.cpp0000664000175000017500000000022712157270661017316 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "copy.hpp" namespace __copy__ { void __init() { } } shedskin-0.9.4/shedskin/lib/copy.py0000664000175000017500000000031112157270661017156 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) def copy(a): a.__copy__() # XXX hardcode in ss.py? return a def deepcopy(a): a.__deepcopy__() return ashedskin-0.9.4/shedskin/lib/re.py0000664000175000017500000000403612157270661016622 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) I = IGNORECASE = 2 L = LOCALE = 4 M = MULTILINE = 8 S = DOTALL = 16 U = UNICODE = 32 X = VERBOSE = 64 class error(Exception): pass class match_object: def __init__(self): self.pos = 0 self.endpos = 0 self.lastindex = 0 self.lastgroup = '' self.re = re_object() self.string = '' def expand(self, tpl): return '' def group(self, *args): return ('',) def __group0(self, arg): return '' def __group1(self, arg): return '' def start(self, i = 0): return 1 def end(self, i = 0): return 1 def span(self): return (1,) def groups(self, defval = 0): return ('',) def groupdict(self, defval = 0): return {'' : ''} def __repr__(self): return '' class re_object: def __init__(self): self.flags = 0 self.groupindex = {'' : ''} self.pattern = '' def match(self, s, pos=0, endpos=-1): return match_object() def search(self, s, pos=0, endpos=-1): return match_object() def split(self, s, maxn=0): return [''] def sub(self, tpl, s, maxn=0): tpl(match_object()) return '' def subn(self, tpl, s, maxn=0): tpl(match_object()) return ('', 0) def finditer(self, s, pos=0, endpos=-1): return __iter(match_object()) def findall(self, s, flags=0): return [''] def __repr__(self): return '' def compile(pat, flgs=0): return re_object() def match(pat, s, flags=0): return match_object() def search(pat, s, flags=0): return match_object() def split(pat, s, maxn=0): return [''] def sub(pat, tpl, s, maxn=0): tpl(match_object()) return '' def subn(pat, tpl, s, maxn=0): tpl(match_object()) return ('', 0) def finditer(pat, s, pos=0, endpos=-1): return __iter(match_object()) def findall(pat, s, flags=0): return [''] def escape(s): return '' shedskin-0.9.4/shedskin/lib/csv.py0000664000175000017500000000415012157270661017004 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) START_RECORD = START_FIELD = ESCAPED_CHAR = IN_FIELD = IN_QUOTED_FIELD = ESCAPE_IN_QUOTED_FIELD = QUOTE_IN_QUOTED_FIELD = EAT_CRNL = 0 QUOTE_MINIMAL = QUOTE_ALL = QUOTE_NONNUMERIC = QUOTE_NONE = 0 class Error(Exception): pass def list_dialects(): return ['excel', 'excel-tab'] def field_size_limit(new_limit=-1): return new_limit class reader: def __init__(self, input_iter, dialect=None, delimiter=None, quotechar=None, doublequote=-1, skipinitialspace=-1, lineterminator=None, quoting=-1, escapechar=None, strict=-1): pass def __iter__(self): return __iter(['']) def next(self): return [''] class writer: def __init__(self, output_file, dialect=None, delimiter=None, quotechar=None, doublequote=-1, skipinitialspace=-1, lineterminator=None, quoting=-1, escapechar=None, strict=-1): pass def writerow(self, seq): pass def writerows(self, seqs): pass class DictReader: def __init__(self, f, fieldnames=None, restkey=None, restval=None, dialect=None, delimiter=None, quotechar=None, doublequote=-1, skipinitialspace=-1, lineterminator=None, quoting=-1, escapechar=None, strict=-1): self._fieldnames = [''] self.restkey = '' self.restval = '' self.reader = reader(f) self.dialect = '' self.line_num = 0 def __iter__(self): return __iter({'': ''}) def getfieldnames(self): return self._fieldnames def setfieldnames(self, value): pass fieldnames = property(getfieldnames, setfieldnames) def next(self): return {'': ''} class DictWriter: def __init__(self, f, fieldnames, restval="", extrasaction="raise", dialect="excel", delimiter=None, quotechar=None, doublequote=-1, skipinitialspace=-1, lineterminator=None, quoting=-1, escapechar=None, strict=-1): self.fieldnames = [''] self.restval = '' self.extrasaction = '' self.writer = writer(f) def writerow(self, rowdict): pass def writerows(self, rowdicts): pass shedskin-0.9.4/shedskin/lib/math.py0000664000175000017500000000140412157270661017141 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) e = 2.7182818284590451 pi = 3.1415926535897931 def sqrt(x): return 1.0 def sin(x): return 1.0 def fabs(x): return 1.0 def factorial(x): return 1 def floor(x): return 1.0 def ceil(x): return 1.0 def exp(x): return 1.0 def log10(x): return 1.0 def acos(x): return 1.0 def asin(x): return 1.0 def atan(x): return 1.0 def cos(x): return 1.0 def tan(x): return 1.0 def degrees(x): return 1.0 def radians(x): return 1.0 def cosh(x): return 1.0 def sinh(x): return 1.0 def tanh(x): return 1.0 def fmod(x, y): return 1.0 def ldexp(x, y): return 1.0 def pow(x, y): return 1.0 def atan2(x, y): return 1.0 def hypot(x, y): return 1.0 def log(x, base=1): return 1.0 def modf(x): return (1.0, 1.0) shedskin-0.9.4/shedskin/lib/ConfigParser.hpp0000664000175000017500000001775612157270661020752 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __CONFIGPARSER_HPP #define __CONFIGPARSER_HPP #include "builtin.hpp" #include "re.hpp" using namespace __shedskin__; namespace __ConfigParser__ { extern tuple2 *const_2; extern str *const_0, *const_1, *const_10, *const_11, *const_12, *const_13, *const_14, *const_15, *const_16, *const_17, *const_18, *const_19, *const_20, *const_21, *const_22, *const_23, *const_24, *const_25, *const_26, *const_27, *const_28, *const_29, *const_3, *const_30, *const_31, *const_32, *const_33, *const_34, *const_35, *const_36, *const_37, *const_38, *const_39, *const_4, *const_40, *const_41, *const_42, *const_43, *const_44, *const_45, *const_46, *const_47, *const_48, *const_49, *const_5, *const_50, *const_51, *const_52, *const_53, *const_6, *const_7, *const_8, *const_9; class Error; class NoSectionError; class DuplicateSectionError; class NoOptionError; class InterpolationError; class InterpolationMissingOptionError; class InterpolationSyntaxError; class InterpolationDepthError; class ParsingError; class MissingSectionHeaderError; class RawConfigParser; class ConfigParser; extern list *__all__; extern str *DEFAULTSECT, *__name__; extern __ss_int MAX_INTERPOLATION_DEPTH; extern class_ *cl_Error; class Error : public Exception { /** Base class for ConfigParser exceptions. */ public: str *message; Error() {} Error(str *msg) { this->__class__ = cl_Error; __init__(msg); } void *__init__(str *msg); }; extern class_ *cl_NoSectionError; class NoSectionError : public Error { /** Raised when no section matches a requested option. */ public: str *section; NoSectionError() {} NoSectionError(str *section) { this->__class__ = cl_NoSectionError; __init__(section); } void *__init__(str *section); }; extern class_ *cl_DuplicateSectionError; class DuplicateSectionError : public Error { /** Raised when a section is multiply-created. */ public: str *section; DuplicateSectionError() {} DuplicateSectionError(str *section) { this->__class__ = cl_DuplicateSectionError; __init__(section); } void *__init__(str *section); }; extern class_ *cl_NoOptionError; class NoOptionError : public Error { /** A requested option was not found. */ public: str *option; str *section; NoOptionError() {} NoOptionError(str *option, str *section) { this->__class__ = cl_NoOptionError; __init__(option, section); } void *__init__(str *option, str *section); }; extern class_ *cl_InterpolationError; class InterpolationError : public Error { /** Base class for interpolation-related exceptions. */ public: str *section; str *option; InterpolationError() {} InterpolationError(str *option, str *section, str *msg) { this->__class__ = cl_InterpolationError; __init__(option, section, msg); } void *__init__(str *option, str *section, str *msg); }; extern class_ *cl_InterpolationMissingOptionError; class InterpolationMissingOptionError : public InterpolationError { /** A string substitution required a setting which was not available. */ public: str *reference; InterpolationMissingOptionError() {} InterpolationMissingOptionError(str *option, str *section, str *rawval, str *reference) { this->__class__ = cl_InterpolationMissingOptionError; __init__(option, section, rawval, reference); } void *__init__(str *option, str *section, str *rawval, str *reference); }; extern class_ *cl_InterpolationSyntaxError; class InterpolationSyntaxError : public InterpolationError { /** Raised when the source text into which substitutions are made does not conform to the required syntax. */ public: InterpolationSyntaxError() { this->__class__ = cl_InterpolationSyntaxError; } }; extern class_ *cl_InterpolationDepthError; class InterpolationDepthError : public InterpolationError { /** Raised when substitutions are nested too deeply. */ public: InterpolationDepthError() {} InterpolationDepthError(str *option, str *section, str *rawval) { this->__class__ = cl_InterpolationDepthError; __init__(option, section, rawval); } void *__init__(str *option, str *section, str *rawval); }; extern class_ *cl_ParsingError; class ParsingError : public Error { /** Raised when a configuration file does not follow legal syntax. */ public: list *> *errors; str *filename; ParsingError() {} ParsingError(str *filename) { this->__class__ = cl_ParsingError; __init__(filename); } void *__init__(str *filename); void *append(__ss_int lineno, str *line); }; extern class_ *cl_MissingSectionHeaderError; class MissingSectionHeaderError : public ParsingError { /** Raised when a key-value pair is found before any section header. */ public: __ss_int lineno; str *line; MissingSectionHeaderError() {} MissingSectionHeaderError(str *filename, __ss_int lineno, str *line) { this->__class__ = cl_MissingSectionHeaderError; __init__(filename, lineno, line); } void *__init__(str *filename, __ss_int lineno, str *line); }; extern class_ *cl_RawConfigParser; class RawConfigParser : public pyobj { public: static dict *_boolean_states; static __re__::re_object *SECTCRE; static __re__::re_object *OPTCRE; dict *_defaults; dict *> *_sections; RawConfigParser() {} RawConfigParser(dict *defaults) { this->__class__ = cl_RawConfigParser; __init__(defaults); } virtual str *get(str *section, str *option, __ss_int raw, dict *vars); str *optionxform(str *optionstr); double getfloat(str *section, str *option); void *_set(str *section, str *option, str *value); __ss_bool has_section(str *section); __ss_bool remove_option(str *section, str *option); __ss_bool remove_section(str *section); void *__init__(dict *defaults); __ss_bool has_option(str *section, str *option); void *write(file *fp); void *add_section(str *section); list *sections(); list *read(str *filename); list *read(list *filenames); __ss_bool getboolean(str *section, str *option); list *> *items(str *section); void *_read(file *fp, str *fpname); __ss_int getint(str *section, str *option); dict *defaults(); list *options(str *section); }; extern class_ *cl_ConfigParser; class ConfigParser : public RawConfigParser { public: static __re__::re_object *_KEYCRE; ConfigParser() {} ConfigParser(dict *defaults) { this->__class__ = cl_ConfigParser; __init__(defaults); } str *_interpolate(str *section, str *option, str *rawval, dict *vars); str *get(str *section, str *option, __ss_int raw, dict *vars); list *> *items(str *section, __ss_int raw, dict *vars); }; str *_interpolation_replace(__re__::match_object *match); extern str * default_11; extern __ss_int default_10; extern __ss_int default_13; extern __ss_int default_23; extern __ss_int default_2; extern __ss_int default_25; extern __ss_int default_28; extern __ss_int default_1; extern __ss_int default_5; extern __ss_int default_3; extern __ss_int default_19; extern __ss_int default_29; extern __ss_int default_12; extern str * default_22; extern __ss_int default_31; extern __ss_int default_30; extern str * default_9; extern __ss_int default_8; extern str * default_24; extern str * default_14; extern str * default_16; extern str * default_26; extern __ss_int default_17; extern str * default_18; extern __ss_int default_6; extern __ss_int default_15; extern __ss_int default_27; extern str * default_20; extern __ss_int default_7; extern __ss_int default_4; extern str * default_0; extern __ss_int default_21; void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/string.hpp0000664000175000017500000000371612157270661017665 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __STRING_HPP #define __STRING_HPP #include "builtin.hpp" using namespace __shedskin__; namespace __string__ { extern str *ascii_letters, *ascii_uppercase, *ascii_lowercase, *lowercase, *uppercase, *whitespace, *punctuation, *printable, *hexdigits, *octdigits, *digits, *letters; str *join(pyiter *a, str *b=0); str *join(pyseq *a, str *b=0); str *joinfields(pyiter *a, str *b=0); str *joinfields(pyseq *a, str *b=0); __ss_int count(str *a, str *b, __ss_int start=0); __ss_int count(str *a, str *b, __ss_int start, __ss_int end); __ss_int find(str *s, str *t, __ss_int a=0); __ss_int find(str *s, str *t, __ss_int a, __ss_int b); __ss_int rfind(str *s, str *t, __ss_int a=0); __ss_int rfind(str *s, str *t, __ss_int a, __ss_int b); __ss_int index(str *s, str *t, __ss_int a=0); __ss_int index(str *s, str *t, __ss_int a, __ss_int b); __ss_int rindex(str *s, str *t, __ss_int a=0); __ss_int rindex(str *s, str *t, __ss_int a, __ss_int b); str *expandtabs(str *s, __ss_int width=8); list *split(str *s, str *sep=0, __ss_int c=-1); list *splitfields(str *s, str *sep=0, __ss_int c=-1); str *replace(str *s, str *a, str *b, __ss_int c=-1); str *translate(str *s, str *table, str *delchars=0); str *zfill(str *s, __ss_int width); str *upper(str *s); str *lower(str *s); list *rsplit(str *s, str *sep = 0, __ss_int maxsep = -1); str *strip(str *s, str *chars=0); str *lstrip(str *s, str *chars=0); str *rstrip(str *s, str *chars=0); str *ljust(str *s, __ss_int width, str *fchar=0); str *rjust(str *s, __ss_int width, str *fchar=0); str *maketrans(str *frm, str *to); str *capitalize(str *s); str *capwords(str *s, str *sep=0); str *swapcase(str *s); str *center(str *s, __ss_int w, str *fill=0); __ss_int atoi(str *s, __ss_int base=10); __ss_int atol(str *s, __ss_int base=10); double atof(str *s); void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/binascii.hpp0000664000175000017500000000270412157270661020134 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __BINASCII_HPP #define __BINASCII_HPP #include "builtin.hpp" #include "binascii.hpp" #ifdef USE_ZLIB_CRC32 #include "zlib.h" #endif using namespace __shedskin__; namespace __binascii__ { class Error; class Incomplete; extern str *__name__; extern class_ *cl_Error; class Error : public Exception { public: Error() {} Error(str *msg) { this->__class__ = cl_Error; __init__(msg); } }; extern class_ *cl_Incomplete; class Incomplete : public Exception { public: Incomplete() {} Incomplete(str *msg) { this->__class__ = cl_Incomplete; __init__(msg); } }; extern void * default_4; extern __ss_bool default_1; extern __ss_bool default_0; extern __ss_bool default_3; extern __ss_bool default_2; extern void * default_5; str *a2b_uu(str *string); str *b2a_uu(str *data); str *a2b_base64(str *string); str *b2a_base64(str *data); str *a2b_qp(str *string, __ss_bool header); str *b2a_qp(str *data, __ss_bool quotetabs, __ss_bool istext, __ss_bool header); tuple2 *a2b_hqx(str *string); str *b2a_hqx(str *data); str *rledecode_hqx(str *data); str *rlecode_hqx(str *data); __ss_int crc_hqx(str *data, __ss_int crc); __ss_int crc32(str *data, __ss_int crc=0); str *b2a_hex(str *data); str *a2b_hex(str *data); str *hexlify(str *data); str *unhexlify(str *data); void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/datetime.cpp0000664000175000017500000011271512157270661020146 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "datetime.hpp" #include "time.hpp" #include "string.hpp" #include namespace __datetime__ { str *date_format,*hour_format1,*hour_format2,*ctime_format; str *one_day_string,*minus_one_day_string,*multiple_days_string,*point_string,*space_string,*none_string,*empty_string,*z_string,*Z_string; __ss_int MINYEAR, MAXYEAR; list *DayNames, *MonthNames; class_ *cl_date, *cl_tzinfo, *cl_timedelta, *cl_time, *cl_datetime; void __init() { cl_date = new class_("date"); cl_tzinfo = new class_("tzinfo"); cl_datetime = new class_("datetime"); cl_time = new class_("time"); cl_timedelta = new class_("timedelta"); date_format = new str("%04d-%02d-%02d"); hour_format1 = new str("%d:%02d:%02d"); hour_format2 = new str("%02d:%02d:%02d"); ctime_format = new str("%s %s %2d %02d:%02d:%02d %04d"); one_day_string = new str("1 day, %d:%02d:%02d"); minus_one_day_string = new str("-1 day, %d:%02d:%02d"); multiple_days_string = new str("%d days, %d:%02d:%02d"); point_string = new str("%s.%06d"); space_string = new str(" "); none_string = new str("None"); empty_string = new str(""); z_string = new str("%z"); Z_string = new str("%Z"); MINYEAR = 1; MAXYEAR = 9999; DayNames = __string__::split(new str("Mon Tue Wed Thu Fri Sat Sun")); MonthNames = __string__::split(new str("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")); } /* helper functions */ static __ss_int divmod(__ss_int x, __ss_int y, __ss_int *r); static __ss_int is_leap(__ss_int year); static __ss_int days_in_month(__ss_int year, __ss_int month); static __ss_int days_before_month(__ss_int year, __ss_int month); static __ss_int days_before_year(__ss_int year); static void ord_to_ymd(__ss_int ordinal, __ss_int *year, __ss_int *month, __ss_int *day); static __ss_int ymd_to_ord(__ss_int year, __ss_int month, __ss_int day); static __ss_int iso_week1_monday(__ss_int year); //class date date::date(__ss_int year, __ss_int month, __ss_int day){ __class__=cl_date; if(yearMAXYEAR) throw new ValueError(new str("year is out of range")); if(month<=0 || month>12) throw new ValueError(new str("month must be in 1..12")); if(day<=0 || day>days_in_month(year,month)) throw new ValueError(new str("day is out of range for month")); this->year=year; this->month=month; this->day=day; } date *date::today() { //today's date using localtime time_t rawtime; struct tm * t; std::time( &rawtime ); t = localtime( &rawtime ); return new date(t->tm_year+1900,t->tm_mon+1,t->tm_mday); } date* date::fromtimestamp(__ss_int timestamp) { //date from timestamp using localtime struct tm *tm; time_t t = (time_t)timestamp; tm = localtime(&t); if (tm) return new date(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); else throw new ValueError(new str("timestamp out of range for platform localtime() function")); } date* date::fromordinal(__ss_int o) { //OverflowError is raised if date2.year would be smaller than MINYEAR or larger than MAXYEAR. if(o<1) //1 = date.min.toordinal() throw new OverflowError(new str("ordinal must be >= 1")); if(o>3652059) //3652059 = date.max.toordinal() throw new OverflowError(new str("year is out of range")); date *r = new date(1,1,1); ord_to_ymd(o,&(r->year),&(r->month),&(r->day)); return r; } date *date::__add__(timedelta *other) { return fromordinal(toordinal()+(other->days)); } date *date::__sub__(timedelta *other) { return fromordinal(toordinal()-(other->days)); } timedelta *date::__sub__(date *other) { return new timedelta(toordinal()-other->toordinal(), 0, 0, 0 ,0, 0, 0); } __ss_int date::__cmp__(date *other) { if(year==other->year && month==other->month && day==other->day) return 0; if (year*366+month*31+day > other->year*366+other->month*31+other->day) return 1; return -1; } __ss_bool date::__eq__(date *other) { return __mbool(__cmp__(other) == 0); } __ss_bool date::__ne__(date *other) { return __mbool(__cmp__(other) != 0); } __ss_bool date::__gt__(date *other) { return __mbool(__cmp__(other) == 1); } __ss_bool date::__lt__(date *other) { return __mbool(__cmp__(other) == -1); } __ss_bool date::__ge__(date *other) { return __mbool(__cmp__(other) != -1); } __ss_bool date::__le__(date *other) { return __mbool(__cmp__(other) != 1); } date *date::replace(__ss_int year, __ss_int month, __ss_int day) { date* t = new date(this); if(year!=0) { if(yearMAXYEAR) throw new ValueError(new str("year is out of range")); t->year=year;} if(month!=0) { if(month<=0 || month>12) throw new ValueError(new str("month must be in 1..12")); t->month=month;} if(day!=0) { if(day<=0 || day>days_in_month(t->year,t->month)) throw new ValueError(new str("day is out of range for month")); t->day=day;} return t; } __time__::struct_time *date::timetuple() { return new __time__::struct_time(new tuple2<__ss_int, __ss_int>(9, (__ss_int)year, (__ss_int)month, (__ss_int)day, (__ss_int)0, (__ss_int)0, (__ss_int)0, (__ss_int)(weekday()), (__ss_int)(days_before_month(year,month)+day), (__ss_int)(-1))); } __ss_int date::toordinal() { return ymd_to_ord(year,month,day); } __ss_int date::weekday() { return (ymd_to_ord(year, month, day) + 6) % 7; } __ss_int date::isoweekday() { return (ymd_to_ord(year, month, day) + 6) % 7+1; } tuple2<__ss_int, __ss_int> *date::isocalendar() { //modified from cpython __ss_int week1_monday = iso_week1_monday(year); __ss_int today = ymd_to_ord(year, month, day); __ss_int tmpyear = year; __ss_int tmpweek; __ss_int tmpday; tmpweek = divmod(today - week1_monday, 7, &tmpday); if (tmpweek < 0) { --tmpyear; week1_monday = iso_week1_monday(tmpyear); tmpweek = divmod(today - week1_monday, 7, &tmpday); } else if (tmpweek >= 52 && today >= iso_week1_monday(tmpyear + 1)) { ++tmpyear; tmpweek = 0; } return new tuple2<__ss_int, __ss_int>(3, (__ss_int)tmpyear, (__ss_int)(tmpweek+1), (__ss_int)(tmpday+1)); } str *date::isoformat() { return __str__(); } str *date::__str__() { return __modct(date_format, 3, ___box(this->year), ___box(this->month), ___box(this->day)); } str *date::ctime() { __ss_int wday = weekday(); return __modct(ctime_format, 7, DayNames->__getitem__(wday), MonthNames->__getitem__(month-1), ___box(day), ___box(0), ___box(0), ___box(0), ___box(year)); } str *date::strftime(str *format) { return __time__::strftime(format,timetuple()); } //class tzinfo datetime *tzinfo::fromutc(datetime *dt) { if(dt->_tzinfo!=this) throw new ValueError(new str("fromutc: dt.tzinfo is not self")); timedelta *dtoff = utcoffset(dt); if(dtoff==NULL) throw new ValueError(new str("fromutc: non-None utcoffset() result required")); timedelta *dtdst = dst(dt); if(dtdst==NULL) throw new ValueError(new str("fromutc: non-None dst() result required")); timedelta *delta = dtoff->__sub__(dtdst); dt = dt->__add__(delta); dtdst = dst(dt); if(dtdst==NULL) throw new ValueError(new str("fromutc: non-None dst() result required")); dt = dt->__add__(dtdst); delete delta; delete dtoff; delete dtdst; return dt; /* dtdst = dt.dst() # raise ValueError if dtoff is None or dtdst is None delta = dtoff - dtdst # this is self's standard offset if delta: dt += delta # convert to standard local time dtdst = dt.dst() # raise ValueError if dtdst is None if dtdst: return dt + dtdst else: return dt*/ } str *tzinfo::minutes_to_str(datetime *dt) { /* timedelta *offset; str *f; offset = utcoffset(dt); if(offset==NULL) return new str(""); if(offset->days<0) { offset = offset->__neg__(); f = new str("-%02d:%02d"); } else f = new str("+%02d:%02d"); return __mod(f,offset->seconds/3600,(offset->seconds/60)%60);*/ timedelta *offset; str f; offset = utcoffset(dt); if(offset==NULL) return empty_string; if(offset->days<0) { offset = offset->__neg__(); f = str("-%02d:%02d"); } else f = str("+%02d:%02d"); return __modct(&f,2,___box(offset->seconds/3600),___box((offset->seconds/60)%60)); } //class datetime datetime::datetime(__ss_int year, __ss_int month, __ss_int day, __ss_int hour, __ss_int minute, __ss_int second, __ss_int microsecond, tzinfo *tzinfo) : date(year,month,day) { __class__=cl_datetime; if(hour>=24 || hour<0) throw new ValueError(new str("hour must be in 0..23")); if(minute>=60 || minute<0) throw new ValueError(new str("minute must be in 0..59")); if(second>=60 || second<0) throw new ValueError(new str("second must be in 0..59")); if(microsecond>=1000000 || microsecond<0) throw new ValueError(new str("microsecond must be in 0..999999")); this->hour = hour; this->minute = minute; this->second = second; this->microsecond = microsecond; this->_tzinfo = tzinfo; } datetime *datetime::today() { time_t rawtime; struct tm * t; std::time( &rawtime ); t = localtime( &rawtime ); struct timeval tv; #ifdef WIN32 __time__::gettimeofday(&tv, NULL); #else gettimeofday(&tv, NULL); #endif return new datetime(t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec,tv.tv_usec); } datetime *datetime::now(tzinfo *tzinfo) { if(!tzinfo) return today(); datetime *r = utcnow(); r->_tzinfo = tzinfo; if(r->_tzinfo) try { return r->__add__(r->_tzinfo->utcoffset(r)); } catch (Exception *e) {return r;} else return r; } datetime *datetime::utcnow() { time_t rawtime; struct tm * t; std::time( &rawtime ); t = gmtime( &rawtime ); struct timeval tv; #ifdef WIN32 __time__::gettimeofday(&tv, NULL); #else gettimeofday(&tv, NULL); #endif return new datetime(t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec,tv.tv_usec); } datetime *datetime::from_timestamp(double timestamp, tzinfo *tzinfo, bool timefn) { //modified from cpython time_t timet; double fraction; __ss_int us; timet = (time_t)timestamp; fraction = timestamp - (double)timet; if (fraction * 1e6 >= 0.0) us = (__ss_int)floor(fraction * 1e6 + 0.5); else us = (__ss_int)ceil(fraction * 1e6 - 0.5); if (us < 0) { /* Truncation towards zero is not what we wanted for negative numbers (Python's mod semantics) */ timet -= 1; us += 1000000; } /* If timestamp is less than one microsecond smaller than a * full second, round up. Otherwise, ValueErrors are raised * for some floats. */ if (us == 1000000) { timet += 1; us = 0; } struct tm *tm; if(timefn) tm = gmtime(&timet); else tm = localtime(&timet); if (tm) { /* The platform localtime/gmtime may insert leap seconds, * indicated by tm->tm_sec > 59. We don't care about them, * except to the extent that passing them on to the datetime * constructor would raise ValueError for a reason that * made no sense to the user. */ if (tm->tm_sec > 59) tm->tm_sec = 59; return new datetime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, us, tzinfo); } else throw new ValueError(new str("timestamp out of range for platform localtime()/gmtime() function")); return (datetime *)NULL; } datetime *datetime::utcfromtimestamp(double timestamp) { return from_timestamp(timestamp,NULL,true); //true=gmtime } datetime *datetime::fromtimestamp(double timestamp, tzinfo *tzinfo) { datetime *tmp = from_timestamp(timestamp, tzinfo, (bool)tzinfo); // tzinfo == Py_None ? localtime : gmtime, if(tzinfo!=NULL) return tzinfo->fromutc(tmp); else return tmp; } datetime *datetime::fromordinal(__ss_int o) { if(o<1) //1 = date.min.toordinal() throw new OverflowError(new str("ordinal must be >= 1")); if(o>3652059) //3652059 = date.max.toordinal() throw new OverflowError(new str("year is out of range")); datetime *r = new datetime(1,1,1); ord_to_ymd(o,&(r->year),&(r->month),&(r->day)); return r; } datetime *datetime::combine(date *d, time *t) { return new datetime(d->year,d->month,d->day,t->hour,t->minute,t->second,t->microsecond,t->_tzinfo); } datetime *datetime::strptime(str *date_string, str *format) { #ifdef WIN32 struct tm t = {0, 0, 0, 1, 0, 0, 0, 1, -1}; char *e = __time__::strptime(date_string->unit.c_str(), format->unit.c_str(), &t); #else struct tm t = {0, 0, 0, 1, 0, 0, 0, 1, -1, 0, 0}; char *e = ::strptime(date_string->unit.c_str(), format->unit.c_str(), &t); #endif if(!e) throw new ValueError(new str("time data did not match format: data="+date_string->unit+" fmt="+format->unit)); if((*e)!='\0') throw new ValueError((new str("ValueError: unconverted data remains: "))->__add__(new str(e))); return new datetime(t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); } datetime *datetime::__add__(timedelta *other) { __ss_int usec = this->microsecond + other->microseconds; __ss_int sec = this->second + other->seconds; datetime *r = datetime::fromordinal(this->toordinal()+other->days + (((usec/1000000 + sec)/60 + this->minute)/60 + this->hour)/24); r->microsecond = usec%1000000; r->second = (sec + usec/1000000)%(60); r->minute = (this->minute + (sec + usec/1000000)/60)%60; r->hour = (this->hour + (this->minute + (sec + usec/1000000)/60)/60)%24; r->_tzinfo = _tzinfo; return r; } datetime *datetime::__sub__(timedelta *other) { __ss_int usec = this->microsecond - other->microseconds; __ss_int sec = this->second - other->seconds; __ss_int days = this->toordinal()-other->days + (((usec/1000000 + sec)/60 + this->minute)/60 + this->hour)/24; datetime *r = datetime::fromordinal(days); r->microsecond = usec%1000000; r->second = (sec + usec/1000000)%(60); r->minute = (this->minute + (sec + usec/1000000)/60)%60; r->hour = (this->hour + (this->minute + (sec + usec/1000000)/60)/60)%24; r->_tzinfo = _tzinfo; //make positive if(r->microsecond<0) { r->second--; r->microsecond+=1000000; } if(r->second<0) { r->minute--; r->second+=60; } if(r->minute<0) { r->hour--; r->minute+=60; } if(r->hour<0) { r->hour+=24; date *tmp = date::fromordinal(days-1); r->year=tmp->year; r->month=tmp->month; r->day=tmp->day; delete tmp; } return r; } timedelta *datetime::__sub__(datetime *other) { timedelta *td = new timedelta(this->toordinal()-other->toordinal(),this->second-other->second,this->microsecond-other->microsecond,0,this->minute-other->minute,this->hour-other->hour); if(_tzinfo==NULL && other->_tzinfo==NULL) return td; if(_tzinfo!=NULL && other->_tzinfo!=NULL) { timedelta *offset1 = _tzinfo->utcoffset(this); timedelta *offset2 = other->_tzinfo->utcoffset(other); if(offset1!=NULL && offset2!=NULL) { timedelta *tmp = td->__sub__(offset1); delete td; delete offset1; td = tmp->__add__(offset2); delete tmp; delete offset2; return td; } if(offset1==NULL && offset2==NULL) { return td; } } throw new TypeError(new str("can't subtract offset-naive and offset-aware datetimes")); } void datetime_compare_check(datetime *&f, datetime *&s) { if((f->_tzinfo && !s->_tzinfo) || (s->_tzinfo && !f->_tzinfo)) throw new TypeError(new str("can't compare offset-naive and offset-aware datetimes")); if(f->_tzinfo) { f = f->__sub__(f->_tzinfo->utcoffset(f)); s = s->__sub__(s->_tzinfo->utcoffset(s)); } } __ss_int datetime::__cmp__(datetime *other) { datetime *f = this; datetime_compare_check(f, other); if(f->year==other->year && f->month==other->month && f->day==other->day && f->hour==other->hour && f->minute==other->minute && f->second==other->second && f->microsecond==other->microsecond) return 0; if(f->year*366+f->month*31+f->day > other->year*366+other->month*31+other->day) return 1; if(f->year*366+f->month*31+f->day == other->year*366+other->month*31+other->day && (f->hour*3600+f->minute*60+f->second)*1000000+f->microsecond > (other->hour*3600+other->minute*60+other->second)*1000000+other->microsecond) return 1; return -1; } __ss_bool datetime::__eq__(datetime *other) { return __mbool(__cmp__(other) == 0); } __ss_bool datetime::__ne__(datetime *other) { return __mbool(__cmp__(other) != 0); } __ss_bool datetime::__gt__(datetime *other) { return __mbool(__cmp__(other) == 1); } __ss_bool datetime::__lt__(datetime *other) { return __mbool(__cmp__(other) == -1); } __ss_bool datetime::__ge__(datetime *other) { return __mbool(__cmp__(other) != -1); } __ss_bool datetime::__le__(datetime *other) { return __mbool(__cmp__(other) != 1); } date *datetime::_date() { return new date(year,month,day); } time *datetime::_time() { return new time(hour,minute,second,microsecond); } time *datetime::timetz() { return new time(hour,minute,second,microsecond,_tzinfo); } datetime *datetime::replace(__ss_int __args, __ss_int year, __ss_int month, __ss_int day, __ss_int hour, __ss_int minute, __ss_int second, __ss_int microsecond, tzinfo *tzinfo) { datetime *t = new datetime(this); if((__args & 1)==1) { if(yearMAXYEAR) throw new ValueError(new str("year is out of range")); t->year=year;} if((__args & 2)==2) { if(month<=0 || month>12) throw new ValueError(new str("month must be in 1..12")); t->month=month;} if((__args & 4)==4) { if(day<=0 || day>days_in_month(t->year,t->month)) throw new ValueError(new str("day is out of range for month")); t->day=day;} if((__args & 8)==8) { if(hour<0 || hour>=24) throw new ValueError(new str("hour must be in 0..23")); t->hour=hour;} if((__args & 16)==16) { if(minute<0 || minute>=60) throw new ValueError(new str("minute must be in 0..59")); t->minute=minute;} if((__args & 32)==32) { if(second<0 || second>=60) throw new ValueError(new str("second must be in 0..59")); t->second=second;} if((__args & 64)==64) { if(microsecond<0 || microsecond>=1000000) throw new ValueError(new str("microsecond must be in 0..999999")); t->microsecond=microsecond;} if((__args & 128)==128) t->_tzinfo = tzinfo; return t; } datetime *datetime::astimezone(tzinfo *tzinfo) { if(this->_tzinfo == NULL) throw new ValueError(new str("astimezone() cannot be applied to a naive datetime")); if(this->_tzinfo == tzinfo) return this; datetime *utc = this->__sub__(this->utcoffset())->replace(128,-1,-1,-1,-1,-1,-1,-1,tzinfo); datetime *r = tzinfo->fromutc(utc); delete utc; return r; /*def astimezone(self, tz): if self.tzinfo is tz: return self # Convert self to UTC, and attach the new time zone object. utc = (self - self.utcoffset()).replace(tzinfo=tz) # Convert from UTC to tz's local time. return tz.fromutc(utc) */ } timedelta *datetime::utcoffset() { if(_tzinfo==NULL) return (timedelta *)NULL; else return _tzinfo->utcoffset(this); } timedelta *datetime::dst() { if(_tzinfo==NULL) return (timedelta *)NULL; else return _tzinfo->dst(this); } str *datetime::tzname() { if(_tzinfo==NULL) return none_string; else return _tzinfo->tzname(this); } __time__::struct_time *datetime::timetuple() { char dst=-1; if(_tzinfo) { timedelta *tmp = _tzinfo->dst(this); if(tmp->days==0 && tmp->seconds==0 && tmp->microseconds==0) dst=0; else dst=1; delete tmp; } return new __time__::struct_time(new tuple2<__ss_int, __ss_int>(9, (__ss_int)year, (__ss_int)month, (__ss_int)day, (__ss_int)hour, (__ss_int)minute, (__ss_int)second, (__ss_int)(weekday()), (__ss_int)(days_before_month(year,month)+day), (__ss_int)dst)); } __time__::struct_time *datetime::utctimetuple() { datetime *tmp = this; timedelta *offset; if(_tzinfo!=NULL && NULL!=(offset=_tzinfo->utcoffset(this))) { tmp = this->__sub__(offset); delete offset; } return new __time__::struct_time(new tuple2<__ss_int, __ss_int>(9, (__ss_int)(tmp->year), (__ss_int)(tmp->month), (__ss_int)(tmp->day), (__ss_int)(tmp->hour), (__ss_int)(tmp->minute), (__ss_int)(tmp->second), (__ss_int)(tmp->weekday()), (__ss_int)(days_before_month(tmp->year,tmp->month)+tmp->day), (__ss_int)0)); } str *datetime::isoformat(str *sep) { if(sep->__len__()!=1) { throw new TypeError(new str("isoformat() argument 1 must be char, not str")); } str *r; r=__add_strs(3,date::__str__(),sep,__modct(hour_format2, 3, ___box(hour), ___box(minute), ___box(second))); if(microsecond!=0) r=__modct(new str("%s.%06d"),2, r, ___box(microsecond)); if(this->_tzinfo!=NULL) return r->__add__(this->_tzinfo->minutes_to_str(this)); return r; } str *datetime::__str__() { return isoformat(space_string); } str *datetime::ctime() { __ss_int wday = weekday(); return __modct(ctime_format, 7, DayNames->__getitem__(wday), MonthNames->__getitem__(month-1), ___box(day), ___box(hour), ___box(minute), ___box(second), ___box(year)); } str *datetime::strftime(str *format) { str *tmp; if(_tzinfo) { tmp = format->replace(z_string,_tzinfo->minutes_to_str(this)->__str__()); format = tmp->replace(Z_string,_tzinfo->tzname(this)); } else { tmp = format->replace(z_string,empty_string); format = tmp->replace(Z_string,empty_string); } delete tmp; tmp = __time__::strftime(format,timetuple()); delete format; return tmp; } //class time time::time(__ss_int hour, __ss_int minute, __ss_int second, __ss_int microsecond, tzinfo *tzinfo) { __class__=cl_time; if(hour>=24 || hour<0) throw new ValueError(new str("hour must be in 0..23")); if(minute>=60 || minute<0) throw new ValueError(new str("minute must be in 0..59")); if(second>=60 || second<0) throw new ValueError(new str("second must be in 0..59")); if(microsecond>=1000000 || microsecond<0) throw new ValueError(new str("microsecond must be in 0..999999")); this->hour = hour; this->minute = minute; this->second = second; this->microsecond = microsecond; this->_tzinfo = tzinfo; } time *time::replace(__ss_int __args, __ss_int hour, __ss_int minute, __ss_int second, __ss_int microsecond, tzinfo *tzinfo) { time *t = new time(this); if((__args & 1)==1) { if(hour<0 || hour>=24) throw new ValueError(new str("hour must be in 0..23")); t->hour=hour;} if((__args & 2)==2) { if(minute<0 || minute>=60) throw new ValueError(new str("minute must be in 0..59")); t->minute=minute;} if((__args & 4)==4) { if(second<0 || second>=60) throw new ValueError(new str("second must be in 0..59")); t->second=second;} if((__args & 8)==8) { if(microsecond<0 || microsecond>=1000000) throw new ValueError(new str("microsecond must be in 0..999999")); t->microsecond=microsecond;} if((__args & 16)==16) t->_tzinfo = tzinfo; return t; } str *time::isoformat() { return __str__(); } str *time::__str__() { str * s; if(microsecond==0) s = __modct(hour_format2, 3, ___box(hour), ___box(minute), ___box(second)); else s = __modct(point_string,2,__modct(hour_format2, 3,___box(hour), ___box(minute), ___box(second)),___box(microsecond)); if(_tzinfo!=NULL) return s->__add__(_tzinfo->minutes_to_str(NULL)); return s; } str *time::strftime(str* format) { str *tmp; if(_tzinfo) { tmp = format->replace(z_string,_tzinfo->minutes_to_str(NULL)->__str__()); format = tmp->replace(Z_string,_tzinfo->tzname(NULL)); } else { tmp = format->replace(z_string,empty_string); format = tmp->replace(Z_string,empty_string); } delete tmp; tmp = __time__::strftime(format, new __time__::struct_time( new tuple2<__ss_int, __ss_int>(9, (__ss_int)1900, (__ss_int)1, (__ss_int)1,//according to cpython implementation, but 0,0, according to description I found on the internet (__ss_int)hour, (__ss_int)minute, (__ss_int)second, (__ss_int)0, (__ss_int)0, (__ss_int)(-1)))); delete format; return tmp; } timedelta *time::utcoffset() { if(_tzinfo==NULL) return (timedelta *)NULL; else return _tzinfo->utcoffset(NULL); } timedelta *time::dst() { if(_tzinfo==NULL) return (timedelta *)NULL; else return _tzinfo->dst(NULL); } str *time::tzname() { if(_tzinfo==NULL) return none_string; else return _tzinfo->tzname(NULL); } void time_compare_check(time *&f, time *&s) { if((f->_tzinfo && !s->_tzinfo) || (s->_tzinfo && !f->_tzinfo)) throw new TypeError(new str("can't compare offset-naive and offset-aware datetimes")); if(f->_tzinfo) { timedelta *fdelta = f->_tzinfo->utcoffset(NULL), *sdelta = s->_tzinfo->utcoffset(NULL); time *ft = new time(), *st = new time(); ft->hour = f->hour-fdelta->days*24; ft->minute = f->minute; ft->second = f->second - fdelta->seconds; ft->microsecond = f->microsecond - fdelta->microseconds; st->hour = s->hour-sdelta->days*24; st->minute = s->minute; st->second = s->second - sdelta->seconds; st->microsecond = s->microsecond - sdelta->microseconds; f = ft; s = st; } } __ss_int time::__cmp__(time *other) { time *f = this; time_compare_check(f, other); if((f->hour*3600+f->minute*60+f->second)*1000000+f->microsecond == (other->hour*3600+other->minute*60+other->second)*1000000+other->microsecond) return 0; if((f->hour*3600+f->minute*60+f->second)*1000000+f->microsecond > (other->hour*3600+other->minute*60+other->second)*1000000+other->microsecond) return 1; return -1; } __ss_bool time::__eq__(time *other) { return __mbool(__cmp__(other) == 0); } __ss_bool time::__ne__(time *other) { return __mbool(__cmp__(other) != 0); } __ss_bool time::__gt__(time *other) { return __mbool(__cmp__(other) == 1); } __ss_bool time::__lt__(time *other) { return __mbool(__cmp__(other) == -1); } __ss_bool time::__ge__(time *other) { return __mbool(__cmp__(other) != -1); } __ss_bool time::__le__(time *other) { return __mbool(__cmp__(other) != 1); } //class timedelta timedelta::timedelta(double days, double seconds, double microseconds, double milliseconds, double minutes, double hours, double weeks) { __class__=cl_timedelta; //still some rounding errors //all little bits of hours and seconds added up double usec1 = milliseconds*1000 + microseconds + (((weeks*7 + days)*24*3600 + hours*3600 + minutes*60 + seconds) -(__ss_int)(hours*3600 + minutes*60 + seconds + (weeks*7 + days)*24*3600))*1000000; this->days = (__ss_int)(weeks*7 + days); this->seconds = (__ss_int)(hours*3600 + minutes*60 + seconds + (weeks*7 + days - (__ss_int)(weeks*7 + days))*24*3600); //rounding to nearest microsec if(usec1>=0.0) this->microseconds = (__ss_int)(floor(usec1+0.5)); else this->microseconds = (__ss_int)(ceil(usec1-0.5)); //move 1000000us to 1s this->seconds += this->microseconds/1000000; this->microseconds %= 1000000; //move 24*3600s to 1 day this->days += this->seconds/(24*3600); this->seconds %= 24*3600; //make positive (% doesn't do that in C++) if(this->microseconds<0) { this->microseconds+=1000000; this->seconds--; } if(this->seconds<0) { this->seconds+=24*3600; this->days--; } if(this->days>999999999 || this->days<(-999999999)) { throw new OverflowError(); } } str *timedelta::__str__() { str *s; if(days==0) s=__modct(hour_format1, 3, ___box(seconds/3600), ___box((seconds%3600)/60), ___box(seconds%60)); else if(days==1) s=__modct(one_day_string,3,___box(seconds/3600), ___box((seconds%3600)/60), ___box(seconds%60)); else if(days==-1) s=__modct(minus_one_day_string,3,___box(seconds/3600), ___box((seconds%3600)/60), ___box(seconds%60)); else s=__modct(multiple_days_string,4,___box(days),___box(seconds/3600), ___box((seconds%3600)/60), ___box(seconds%60)); if(microseconds==0) return s; else return __modct(point_string,2,s,___box(microseconds)); } timedelta *timedelta::__add__(timedelta *other) { return new timedelta(days+other->days, seconds+other->seconds, microseconds+other->microseconds,0,0,0,0); } timedelta *timedelta::__sub__(timedelta *other) { return new timedelta(days-other->days, seconds-other->seconds, microseconds-other->microseconds,0,0,0,0); } timedelta *timedelta::__mul__(__ss_int n) { return new timedelta(days*n, seconds*n, microseconds*n,0,0,0,0); } timedelta *timedelta::__div__(__ss_int n) { if(n==0) { throw new ZeroDivisionError(new str("integer division or modulo by zero")); } double d,s,us; d = double(days)/n; s = double(seconds)/n; us = double(microseconds)/n+(((long double)(days)/n-double(days)/n)*24*3600+(long double)(seconds)/n-s)*1000000; return new timedelta(0,d*24*3600+s,us,0,0,0,0); } timedelta *timedelta::__neg__() { return new timedelta(-days, -seconds, -microseconds,0,0,0,0); } timedelta *timedelta::__floordiv__(__ss_int n) { if(n==0) { throw new ZeroDivisionError(new str("integer division or modulo by zero")); } return new timedelta(double(days)/n,double(seconds)/n,double(microseconds)/n,0,0,0,0); } timedelta *timedelta::__abs__() { if(days>=0) return new timedelta(this); else return __neg__(); } __ss_int timedelta::__cmp__(timedelta *other) { if ((days == other->days) && (seconds == other->seconds) && (microseconds == other->microseconds)) return 0; if (((days * 24 * 3600) + seconds) > ((other->days * 24 * 3600) + other->seconds)) return 1; if ((((days * 24 * 3600) + seconds) == ((other->days * 24 * 3600) + other->seconds)) && (microseconds > other->microseconds)) return 1; return -1; } __ss_bool timedelta::__eq__(timedelta *other) { return __mbool(__cmp__(other) == 0); } __ss_bool timedelta::__ne__(timedelta *other) { return __mbool(__cmp__(other) != 0); } __ss_bool timedelta::__gt__(timedelta *other) { return __mbool(__cmp__(other) == 1); } __ss_bool timedelta::__lt__(timedelta *other) { return __mbool(__cmp__(other) == -1); } __ss_bool timedelta::__ge__(timedelta *other) { return __mbool(__cmp__(other) != -1); } __ss_bool timedelta::__le__(timedelta *other) { return __mbool(__cmp__(other) != 1); } /*functions taken and modified from cpython, to be copied to datetime.cpp later*/ /* Compute Python divmod(x, y), returning the quotient and storing the * remainder into *r. The quotient is the floor of x/y, and that's * the real point of this. C will probably truncate instead (C99 * requires truncation; C89 left it implementation-defined). * Simplification: we *require* that y > 0 here. That's appropriate * for all the uses made of it. This simplifies the code and makes * the overflow case impossible (divmod(LONG_MIN, -1) is the only * overflow case). */ static __ss_int divmod(__ss_int x, __ss_int y, __ss_int *r) { __ss_int quo; assert(y > 0); quo = x / y; *r = x - quo * y; if (*r < 0) { --quo; *r += y; } assert(0 <= *r && *r < y); return quo; } /* --------------------------------------------------------------------------- * General calendrical helper functions */ /* For each month ordinal in 1..12, the number of days in that month, * and the number of days before that month in the same year. These * are correct for non-leap years only. */ static __ss_int _days_in_month[] = { 0, /* unused; this vector uses 1-based indexing */ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; static __ss_int _days_before_month[] = { 0, /* unused; this vector uses 1-based indexing */ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; /* year -> 1 if leap year, else 0. */ static __ss_int is_leap(__ss_int year) { /* Cast year to unsigned. The result is the same either way, but * C can generate faster code for unsigned mod than for signed * mod (especially for % 4 -- a good compiler should just grab * the last 2 bits when the LHS is unsigned). */ const unsigned int ayear = (unsigned int)year; return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0); } /* year, month -> number of days in that month in that year */ static __ss_int days_in_month(__ss_int year, __ss_int month) { assert(month >= 1); assert(month <= 12); if (month == 2 && is_leap(year)) return 29; else return _days_in_month[month]; } /* year, month -> number of days in year preceeding first day of month */ static __ss_int days_before_month(__ss_int year, __ss_int month) { __ss_int days; assert(month >= 1); assert(month <= 12); days = _days_before_month[month]; if (month > 2 && is_leap(year)) ++days; return days; } /* year -> number of days before January 1st of year. Remember that we * start with year 1, so days_before_year(1) == 0. */ static __ss_int days_before_year(__ss_int year) { __ss_int y = year - 1; /* This is incorrect if year <= 0; we really want the floor * here. But so long as MINYEAR is 1, the smallest year this * can see is 0 (this can happen in some normalization endcases), * so we'll just special-case that. */ assert (year >= 0); if (y >= 0) return y*365 + y/4 - y/100 + y/400; else { assert(y == -1); return -366; } } /* Number of days in 4, 100, and 400 year cycles. That these have * the correct values is asserted in the module init function. */ #define DI4Y 1461 /* days_before_year(5); days in 4 years */ #define DI100Y 36524 /* days_before_year(101); days in 100 years */ #define DI400Y 146097 /* days_before_year(401); days in 400 years */ /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */ static void ord_to_ymd(__ss_int ordinal, __ss_int *year, __ss_int *month, __ss_int *day) { __ss_int n, n1, n4, n100, n400, leapyear, preceding; /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of * leap years repeats exactly every 400 years. The basic strategy is * to find the closest 400-year boundary at or before ordinal, then * work with the offset from that boundary to ordinal. Life is much * clearer if we subtract 1 from ordinal first -- then the values * of ordinal at 400-year boundaries are exactly those divisible * by DI400Y: * * D M Y n n-1 * -- --- ---- ---------- ---------------- * 31 Dec -400 -DI400Y -DI400Y -1 * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary * ... * 30 Dec 000 -1 -2 * 31 Dec 000 0 -1 * 1 Jan 001 1 0 400-year boundary * 2 Jan 001 2 1 * 3 Jan 001 3 2 * ... * 31 Dec 400 DI400Y DI400Y -1 * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary */ assert(ordinal >= 1); --ordinal; n400 = ordinal / DI400Y; n = ordinal % DI400Y; *year = n400 * 400 + 1; /* Now n is the (non-negative) offset, in days, from January 1 of * year, to the desired date. Now compute how many 100-year cycles * precede n. * Note that it's possible for n100 to equal 4! In that case 4 full * 100-year cycles precede the desired day, which implies the * desired day is December 31 at the end of a 400-year cycle. */ n100 = n / DI100Y; n = n % DI100Y; /* Now compute how many 4-year cycles precede it. */ n4 = n / DI4Y; n = n % DI4Y; /* And now how many single years. Again n1 can be 4, and again * meaning that the desired day is December 31 at the end of the * 4-year cycle. */ n1 = n / 365; n = n % 365; *year += n100 * 100 + n4 * 4 + n1; if (n1 == 4 || n100 == 4) { assert(n == 0); *year -= 1; *month = 12; *day = 31; return; } /* Now the year is correct, and n is the offset from January 1. We * find the month via an estimate that's either exact or one too * large. */ leapyear = n1 == 3 && (n4 != 24 || n100 == 3); assert(leapyear == is_leap(*year)); *month = (n + 50) >> 5; preceding = (_days_before_month[*month] + (*month > 2 && leapyear)); if (preceding > n) { /* estimate is too large */ *month -= 1; preceding -= days_in_month(*year, *month); } n -= preceding; assert(0 <= n); assert(n < days_in_month(*year, *month)); *day = n + 1; } /* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */ static __ss_int ymd_to_ord(__ss_int year, __ss_int month, __ss_int day) { return days_before_year(year) + days_before_month(year, month) + day; } static __ss_int iso_week1_monday(__ss_int year) { __ss_int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */ /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */ __ss_int first_weekday = (first_day + 6) % 7; /* ordinal of closest Monday at or before 1/1 */ __ss_int week1_monday = first_day - first_weekday; if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */ week1_monday += 7; return week1_monday; } } shedskin-0.9.4/shedskin/lib/mmap.hpp0000664000175000017500000001115212157270661017302 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __MMAP_HPP #define __MMAP_HPP #ifdef WIN32 #include #endif /* WIN32 */ #include "builtin.hpp" using namespace __shedskin__; /** * mmap module for Shed Skin. */ namespace __mmap__ { extern __ss_int ALLOCATIONGRANULARITY; extern const __ss_int PAGESIZE, /* 4096 bytes usually. */ /* Access */ ACCESS_READ, /* Read-only memory. */ ACCESS_WRITE, /* Write-through memory. */ ACCESS_COPY, /* Copy-on-write memory. */ /* Prot */ PROT_READ, /* Page can be read. */ PROT_WRITE, /* Page can be written. */ PROT_EXEC, /* Page can be executed. */ /* Flags */ MAP_SHARED, /* Share changes. */ MAP_PRIVATE, /* Changes are private. */ MAP_ANONYMOUS, /* Don't use a file. */ MAP_ANON; /* Syn. MAP_ANONYMOUS. */ extern str *__name__; extern class_ *cl_mmap; #ifndef WIN32 /* UNIX */ extern __ss_int default_0, default_1; #else extern str *default_2; #endif /* WIN32 */ class __mmapiter; /** * mmap class. * ref: http://docs.python.org/library/mmap.html */ class mmap : public pyseq { public: typedef char* iterator; static const __ss_int all = -1; /** * Constructors. */ #ifndef WIN32 mmap(int __ss_fileno, __ss_int length, __ss_int flags = MAP_SHARED, __ss_int prot = PROT_READ | PROT_WRITE, __ss_int access = 0, __ss_int offset = 0) : closed(false), fd(-1) { this->__class__ = cl_mmap; __init__(__ss_fileno, length, flags, prot, access, offset); } void *__init__(int __ss_fileno, __ss_int length, __ss_int flags, __ss_int prot, __ss_int access, __ss_int offset); #else /* WIN32 */ mmap(int __ss_fileno, __ss_int length, str *tagname = 0, __ss_int access = 0, __ss_int offset = 0) : closed(false), file_handle(INVALID_HANDLE_VALUE) { this->__class__ = cl_mmap; __init__(__ss_fileno, length, tagname, access, offset); } void *__init__(int __ss_fileno, __ss_int length, str *tagname, __ss_int access, __ss_int offset); #endif /* WIN32 */ // mmap void * close(); __ss_int flush(__ss_int offset=0, __ss_int size=-1); __ss_int find(str *s, __ss_int start=-1, __ss_int end=-1); void * move(__ss_int destination, __ss_int source, __ss_int count); str * read(__ss_int size=all); str * read_byte(); str * readline(__ss_int size=all, const char eol='\n'); void * resize(__ss_int newsize); __ss_int rfind(str *string, __ss_int start=-1, __ss_int end=-1); void * seek(__ss_int offset, __ss_int whence=0); __ss_int size(); __ss_int tell(); void * write(str *string); void * write_byte(str *string); // pyraw __ss_int __len__(); char * data() { return m_begin; } // pyiter __ss_bool __contains__(str *s); __iter *__iter__(); // pyseq str *__getitem__(__ss_int index); void *__setitem__(__ss_int index, str *value); str *__slice__(__ss_int kind, __ss_int lower, __ss_int upper, __ss_int step); void *__setslice__(__ss_int kind, __ss_int lower, __ss_int upper, __ss_int step, str *sequence); // impl inline size_t __size() const { return (m_end - m_begin); } inline bool __eof() const { return (m_position >= m_end); } inline bool for_in_has_next(size_t i) const { return i < __size(); } inline str *for_in_next(size_t &i) const { return __char_cache[(unsigned char)(m_begin[i++])]; } private: iterator m_begin; iterator m_end; iterator m_position; bool closed; #ifndef WIN32 int fd; __ss_int flags; __ss_int prot; #else /* WIN32 */ HANDLE map_handle; HANDLE file_handle; char * tagname; size_t offset; #endif /* WIN32 */ __ss_int access; void *__raise_if_closed(); void *__raise_if_closed_or_not_readable(); void *__raise_if_closed_or_not_writable(); void *__seek_failed(); inline size_t __subscript(__ss_int index, bool include_end=false) const; inline __ss_int __clamp(__ss_int index) const; inline size_t __tell() const { return (m_position - m_begin); } iterator __next_line(const char eol); __ss_int __find(const __GC_STRING& needle, __ss_int start, __ss_int end, bool reverse=false); }; /** * mmap byte iterator. */ class __mmapiter : public __iter { public: mmap *map; __mmapiter(mmap *map) : map(map) {} str *next(); }; void __init(); } // __mmap__ namespace #endif // __MMAP_HPP shedskin-0.9.4/shedskin/lib/copy.hpp0000664000175000017500000000055312157270661017325 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef COPY_HPP #define COPY_HPP #include "builtin.hpp" using namespace __shedskin__; namespace __copy__ { template T deepcopy(T t) { return __deepcopy(t); } template T copy(T t) { return __copy(t); } void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/heapq.cpp0000664000175000017500000000024212157270661017437 0ustar srepmubsrepmub/* Copyright (c) 2009 Jérémie Roquet ; License Expat (See LICENSE) */ #include "heapq.hpp" namespace __heapq__ { void __init() { } } shedskin-0.9.4/shedskin/lib/getopt.cpp0000664000175000017500000002664112157270661017656 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "getopt.hpp" /** Parser for command line options. This module helps scripts to parse the command line arguments in sys.argv. It supports the same conventions as the Unix getopt() function (including the special meanings of arguments of the form `-' and `--'). Long options similar to those supported by GNU software may be used as well via an optional third argument. This module provides two functions and an exception: getopt() -- Parse command line options gnu_getopt() -- Like getopt(), but allow option and non-option arguments to be intermixed. GetoptError -- exception (class) raised with 'opt' attribute, which is the option involved with the exception. */ namespace __getopt__ { str *const_0, *const_1, *const_10, *const_11, *const_12, *const_2, *const_3, *const_4, *const_5, *const_6, *const_7, *const_8, *const_9; str *__name__; __ss_int __18; static inline list *list_comp_0(str *opt, pyiter *longopts) { list *result = new list(); str *o; pyiter::for_in_loop __3; int __2; pyiter *__1; FOR_IN(o,longopts,1,2,3) if (o->startswith(opt)) result->append(o); END_FOR return result; } /** class GetoptError */ class_ *cl_GetoptError; GetoptError::GetoptError(str *message, str *opt) { this->__class__ = cl_GetoptError; this->message = message; this->msg = message; if(opt) this->opt = opt; else this->opt = const_0; } void __init() { const_0 = new str(""); const_1 = new str("-"); const_2 = new str("--"); const_3 = new str("+"); const_4 = new str("POSIXLY_CORRECT"); const_5 = new str("="); const_6 = new str("option --%s requires argument"); const_7 = new str("option --%s must not have an argument"); const_8 = new str("option --%s not recognized"); const_9 = new str("option --%s not a unique prefix"); const_10 = new str("option -%s requires argument"); const_11 = new str(":"); const_12 = new str("option -%s not recognized"); __name__ = new str("getopt"); cl_GetoptError = new class_("GetoptError"); } tuple2 *> *, list *> *getopt(list *args, str *shortopts) { return getopt(args, shortopts, new list()); } tuple2 *> *, list *> *getopt(list *args, str *shortopts, str *longopts) { return getopt(args, shortopts, new list(1, longopts)); } tuple2 *> *, list *> *getopt(list *args, str *shortopts, pyiter *longopts) { /** getopt(args, options[, long_options]) -> opts, args Parses command line options and parameter list. args is the argument list to be parsed, without the leading reference to the running program. Typically, this means "sys.argv[1:]". shortopts is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (i.e., the same format that Unix getopt() uses). If specified, longopts is a list of strings with the names of the long options which should be supported. The leading '--' characters should not be included in the option name. Options which require an argument should be followed by an equal sign ('='). The return value consists of two elements: the first is a list of (option, value) pairs; the second is the list of program arguments left after the option list was stripped (this is a trailing slice of the first argument). Each option-and-value pair returned has the option as its first element, prefixed with a hyphen (e.g., '-x'), and the option argument as its second element, or an empty string if the option has no argument. The options occur in the list in the same order in which they were found, thus allowing multiple occurrences. Long and short options may be mixed. */ list *> *opts; tuple2 *> *, list *> *__3, *__4; opts = (new list *>()); longopts = new list(longopts); while((___bool(args) && (args->__getfast__(0))->startswith(const_1) && __ne(args->__getfast__(0), const_1))) { if (__eq(args->__getfast__(0), const_2)) { args = args->__slice__(1, 1, 0, 0); break; } if ((args->__getfast__(0))->startswith(const_2)) { __3 = do_longs(opts, (args->__getfast__(0))->__slice__(1, 2, 0, 0), longopts, args->__slice__(1, 1, 0, 0)); opts = __3->__getfirst__(); args = __3->__getsecond__(); } else { __4 = do_shorts(opts, (args->__getfast__(0))->__slice__(1, 1, 0, 0), shortopts, args->__slice__(1, 1, 0, 0)); opts = __4->__getfirst__(); args = __4->__getsecond__(); } } return (new tuple2 *> *, list *>(2, opts, args)); } tuple2 *> *, list *> *gnu_getopt(list *args, str *shortopts) { return gnu_getopt(args, shortopts, new list()); } tuple2 *> *, list *> *gnu_getopt(list *args, str *shortopts, str *longopts) { return gnu_getopt(args, shortopts, new list(1, longopts)); } tuple2 *> *, list *> *gnu_getopt(list *args, str *shortopts, pyiter *longopts) { /** getopt(args, options[, long_options]) -> opts, args This function works like getopt(), except that GNU style scanning mode is used by default. This means that option and non-option arguments may be intermixed. The getopt() function stops processing options as soon as a non-option argument is encountered. If the first character of the option string is `+', or if the environment variable POSIXLY_CORRECT is set, then option processing stops as soon as a non-option argument is encountered. */ list *prog_args; list *> *opts; __ss_int all_options_first; tuple2 *> *, list *> *__5, *__6; opts = (new list *>()); prog_args = (new list()); longopts = new list(longopts); if (shortopts->startswith(const_3)) { shortopts = shortopts->__slice__(1, 1, 0, 0); all_options_first = 1; } else if (___bool((__os__::__ss_environ)->get(const_4))) { all_options_first = 1; } else { all_options_first = 0; } while(___bool(args)) { if (__eq(args->__getfast__(0), const_2)) { prog_args = prog_args->__iadd__(args->__slice__(1, 1, 0, 0)); break; } if (__eq((args->__getfast__(0))->__slice__(2, 0, 2, 0), const_2)) { __5 = do_longs(opts, (args->__getfast__(0))->__slice__(1, 2, 0, 0), longopts, args->__slice__(1, 1, 0, 0)); opts = __5->__getfirst__(); args = __5->__getsecond__(); } else if (__eq((args->__getfast__(0))->__slice__(2, 0, 1, 0), const_1)) { __6 = do_shorts(opts, (args->__getfast__(0))->__slice__(1, 1, 0, 0), shortopts, args->__slice__(1, 1, 0, 0)); opts = __6->__getfirst__(); args = __6->__getsecond__(); } else { if (all_options_first) { prog_args = prog_args->__iadd__(args); break; } else { prog_args->append(args->__getfast__(0)); args = args->__slice__(1, 1, 0, 0); } } } return (new tuple2 *> *, list *>(2, opts, prog_args)); } tuple2 *> *, list *> *do_longs(list *> *opts, str *opt, pyiter *longopts, list *args) { list *__13; str *__12, *__14, *__8, *__9, *optarg; __ss_int __10, i; __ss_bool has_arg; tuple2<__ss_bool, str *> *__11; try { __10 = 0; i = opt->index(const_5); __10 = 1; } catch (ValueError *) { optarg = 0; } if(__10) { // else __8 = opt->__slice__(2, 0, i, 0); __9 = opt->__slice__(1, (i+1), 0, 0); opt = __8; optarg = __9; } __11 = long_has_args(opt, longopts); has_arg = __11->__getfirst__(); opt = __11->__getsecond__(); if (has_arg) { if ((optarg==0)) { if ((!___bool(args))) { throw ((new GetoptError(__modct(const_6, 1, opt),opt))); } __12 = args->__getfast__(0); __13 = args->__slice__(1, 1, 0, 0); optarg = __12; args = __13; } } else if (___bool(optarg)) { throw ((new GetoptError(__modct(const_7, 1, opt),opt))); } opts->append((new tuple2(2, (const_2)->__add__(opt), __OR(optarg, const_0, 14)))); return (new tuple2 *> *, list *>(2, opts, args)); } tuple2<__ss_bool, str *> *long_has_args(str *opt, pyiter *longopts) { list *possibilities; str *unique_match; __ss_bool has_arg; possibilities = list_comp_0(opt, longopts); if ((!___bool(possibilities))) { throw ((new GetoptError(__modct(const_8, 1, opt),opt))); } if (possibilities->__contains__(opt)) { return (new tuple2<__ss_bool, str *>(2, False, opt)); } else if (possibilities->__contains__(opt->__add__(const_5))) { return (new tuple2<__ss_bool, str *>(2, True, opt)); } if ((len(possibilities)>1)) { throw ((new GetoptError(__modct(const_9, 1, opt),opt))); } ASSERT((len(possibilities)==1), 0); unique_match = possibilities->__getfast__(0); has_arg = __mbool(unique_match->endswith(const_5)); if (has_arg) { unique_match = unique_match->__slice__(2, 0, -1, 0); } return (new tuple2<__ss_bool, str *>(2, has_arg, unique_match)); } tuple2 *> *, list *> *do_shorts(list *> *opts, str *optstring, str *shortopts, list *args) { list *__22; str *__19, *__20, *__21, *__23, *opt, *optarg; while(__ne(optstring, const_0)) { __19 = optstring->__getitem__(0); __20 = optstring->__slice__(1, 1, 0, 0); opt = __19; optstring = __20; if (short_has_arg(opt, shortopts)) { if (__eq(optstring, const_0)) { if ((!___bool(args))) { throw ((new GetoptError(__modct(const_10, 1, opt),opt))); } __21 = args->__getfast__(0); __22 = args->__slice__(1, 1, 0, 0); optstring = __21; args = __22; } __23 = optstring; optarg = __23; optstring = const_0; } else { optarg = const_0; } opts->append((new tuple2(2, (const_1)->__add__(opt), optarg))); } return (new tuple2 *> *, list *>(2, opts, args)); } __ss_bool short_has_arg(str *opt, str *shortopts) { str *__26; __ss_int __24, __25, i; FAST_FOR(i,0,len(shortopts),1,24,25) if ((__eq(opt, (__26=shortopts->__getitem__(i)))&&__ne(__26, const_11))) { return __mbool(shortopts->startswith(const_11, (i+1))); } END_FOR throw ((new GetoptError(__modct(const_12, 1, opt),opt))); } } // module namespace shedskin-0.9.4/shedskin/lib/colorsys.hpp0000664000175000017500000000132412157270661020225 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __COLORSYS_HPP #define __COLORSYS_HPP using namespace __shedskin__; namespace __colorsys__ { extern double ONE_SIXTH, ONE_THIRD, TWO_THIRD; tuple2 *rgb_to_yiq(double r, double g, double b); tuple2 *yiq_to_rgb(double y, double i, double q); tuple2 *rgb_to_hls(double r, double g, double b); tuple2 *hls_to_rgb(double h, double l, double s); double _v(double m1, double m2, double hue); tuple2 *rgb_to_hsv(double r, double g, double b); tuple2 *hsv_to_rgb(double h, double s, double v); void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/ConfigParser.cpp0000664000175000017500000007055612157270661020742 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "ConfigParser.hpp" /** Configuration file parser. A setup file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the style of RFC 822. The option values can contain format strings which refer to other values in the same section, or values in a special [DEFAULT] section. For example: something: %(dir)s/whatever would resolve the "%(dir)s" to the value of dir. All reference expansions are done late, on demand. Intrinsic defaults can be specified by passing them into the ConfigParser constructor as a dictionary. class: ConfigParser -- responsible for parsing a list of configuration files, and managing the parsed database. methods: __init__(defaults=None) create the parser and specify a dictionary of intrinsic defaults. The keys must be strings, the values must be appropriate for %()s string interpolation. Note that `__name__' is always an intrinsic default; its value is the section's name. sections() return all the configuration section names, sans DEFAULT has_section(section) return whether the given section exists has_option(section, option) return whether the given option exists in the given section options(section) return list of configuration options for the named section read(filenames) read and parse the list of named configuration files, given by name. A single filename is also allowed. Non-existing files are ignored. Return list of successfully read files. readfp(fp, filename=None) read and parse one configuration file, given as a file object. The filename defaults to fp.name; it is only used in error messages (if fp has no `name' attribute, the string `' is used). get(section, option, raw=False, vars=None) return a string value for the named option. All % interpolations are expanded in the return values, based on the defaults passed into the constructor and the DEFAULT section. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose contents override any pre-existing defaults. getint(section, options) like get(), but convert value to an integer getfloat(section, options) like get(), but convert value to a float getboolean(section, options) like get(), but convert value to a boolean (currently case insensitively defined as 0, false, no, off for False, and 1, true, yes, on for True). Returns False or True. items(section, raw=False, vars=None) return a list of tuples with (name, value) for each option in the section. remove_section(section) remove the given file section and all its options remove_option(section, option) remove the given option from the given section set(section, option, value) set the given option write(fp) write the configuration state in .ini format */ namespace __ConfigParser__ { tuple2 *const_2; str *const_0, *const_1, *const_10, *const_11, *const_12, *const_13, *const_14, *const_15, *const_16, *const_17, *const_18, *const_19, *const_20, *const_21, *const_22, *const_23, *const_24, *const_25, *const_26, *const_27, *const_28, *const_29, *const_3, *const_30, *const_31, *const_32, *const_33, *const_34, *const_35, *const_36, *const_37, *const_38, *const_39, *const_4, *const_40, *const_41, *const_42, *const_43, *const_44, *const_45, *const_46, *const_47, *const_48, *const_49, *const_5, *const_50, *const_51, *const_52, *const_53, *const_6, *const_7, *const_8, *const_9; list *__all__; str *DEFAULTSECT, *__name__; __ss_int MAX_INTERPOLATION_DEPTH; str * default_11; __ss_int default_10; __ss_int default_13; __ss_int default_23; __ss_int default_2; __ss_int default_25; __ss_int default_28; __ss_int default_1; __ss_int default_5; __ss_int default_3; __ss_int default_19; __ss_int default_29; __ss_int default_12; str * default_22; __ss_int default_31; __ss_int default_30; str * default_9; __ss_int default_8; str * default_24; str * default_14; str * default_16; str * default_26; __ss_int default_17; str * default_18; __ss_int default_6; __ss_int default_15; __ss_int default_27; str * default_20; __ss_int default_7; __ss_int default_4; str * default_0; __ss_int default_21; static inline list *> *list_comp_0(list *options, dict *d) { list *__56; str *option; __ss_int __58; list *> *result = new list *>(); list::for_in_loop __123; result->resize(len(options)); FOR_IN(option,options,56,58,123) result->units[__58] = (new tuple2(2, option, d->__getitem__(option))); END_FOR return result; } static inline list *> *list_comp_1(dict *d, ConfigParser *self, list *options, str *section) { list *__59; str *option; __ss_int __61; list *> *result = new list *>(); list::for_in_loop __123; result->resize(len(options)); FOR_IN(option,options,59,61,123) result->units[__61] = (new tuple2(2, option, self->_interpolate(section, option, d->__getitem__(option), d))); END_FOR return result; } /** class Error */ class_ *cl_Error; void *Error::__init__(str *msg) { this->message = msg; Exception::__init__(msg); return NULL; } /** class NoSectionError */ class_ *cl_NoSectionError; void *NoSectionError::__init__(str *section) { Error::__init__(__modct(const_3, 1, section)); this->section = section; return NULL; } /** class DuplicateSectionError */ class_ *cl_DuplicateSectionError; void *DuplicateSectionError::__init__(str *section) { Error::__init__(__modct(const_4, 1, section)); this->section = section; return NULL; } /** class NoOptionError */ class_ *cl_NoOptionError; void *NoOptionError::__init__(str *option, str *section) { Error::__init__(__modct(const_5, 2, option, section)); this->option = option; this->section = section; return NULL; } /** class InterpolationError */ class_ *cl_InterpolationError; void *InterpolationError::__init__(str *option, str *section, str *msg) { Error::__init__(msg); this->option = option; this->section = section; return NULL; } /** class InterpolationMissingOptionError */ class_ *cl_InterpolationMissingOptionError; void *InterpolationMissingOptionError::__init__(str *option, str *section, str *rawval, str *reference) { str *msg; msg = __modct(const_6, 4, section, option, reference, rawval); InterpolationError::__init__(option, section, msg); this->reference = reference; return NULL; } /** class InterpolationSyntaxError */ class_ *cl_InterpolationSyntaxError; /** class InterpolationDepthError */ class_ *cl_InterpolationDepthError; void *InterpolationDepthError::__init__(str *option, str *section, str *rawval) { str *msg; msg = __modct(const_7, 3, section, option, rawval); InterpolationError::__init__(option, section, msg); return NULL; } /** class ParsingError */ class_ *cl_ParsingError; void *ParsingError::__init__(str *filename) { Error::__init__(__modct(const_8, 1, filename)); this->filename = filename; this->errors = (new list *>()); return NULL; } void *ParsingError::append(__ss_int lineno, str *line) { (this->errors)->append((new tuple2<__ss_int, str *>(2, lineno, line))); this->message = (this->message)->__iadd__(__modct(const_9, 2, ___box(lineno), line)); return NULL; } /** class MissingSectionHeaderError */ class_ *cl_MissingSectionHeaderError; void *MissingSectionHeaderError::__init__(str *filename, __ss_int lineno, str *line) { Error::__init__(__modct(const_10, 3, filename, ___box(lineno), line)); this->filename = filename; this->lineno = lineno; this->line = line; return NULL; } /** class RawConfigParser */ class_ *cl_RawConfigParser; str *RawConfigParser::optionxform(str *optionstr) { return optionstr->lower(); } double RawConfigParser::getfloat(str *section, str *option) { return __float(this->get(section, option, default_5, NULL)); } void *RawConfigParser::_set(str *section, str *option, str *value) { /** Set an option. */ __ss_int __16; dict *sectdict; if (__OR((!___bool(section)), __eq(section, DEFAULTSECT), 16)) { sectdict = this->_defaults; } else { try { sectdict = (this->_sections)->__getitem__(section); } catch (KeyError *) { throw ((new NoSectionError(section))); } } sectdict->__setitem__(this->optionxform(option), value); return NULL; } __ss_bool RawConfigParser::has_section(str *section) { /** Indicate whether the named section is present in the configuration. The DEFAULT section is not acknowledged. */ return __mbool((this->_sections)->__contains__(section)); } __ss_bool RawConfigParser::remove_option(str *section, str *option) { /** Remove an option. */ __ss_int __30; __ss_bool existed; dict *sectdict; if (__OR((!___bool(section)), __eq(section, DEFAULTSECT), 30)) { sectdict = this->_defaults; } else { try { sectdict = (this->_sections)->__getitem__(section); } catch (KeyError *) { throw ((new NoSectionError(section))); } } option = this->optionxform(option); existed = sectdict->__contains__(option); if (existed) { sectdict->__delitem__(option); } return existed; } __ss_bool RawConfigParser::remove_section(str *section) { /** Remove a file section. */ __ss_bool existed; existed = (this->_sections)->__contains__(section); if (existed) { (this->_sections)->__delitem__(section); } return existed; } void *RawConfigParser::__init__(dict *defaults) { __ss_int __3; tuple2 *__0; str *key, *value; list *> *__1; list *>::for_in_loop __123; this->_sections = (new dict *>()); this->_defaults = (new dict()); if (___bool(defaults)) { FOR_IN(__0,defaults->items(),1,3,123) __0 = __0; key = __0->__getfirst__(); value = __0->__getsecond__(); this->_defaults->__setitem__(this->optionxform(key), value); END_FOR } return NULL; } __ss_bool RawConfigParser::has_option(str *section, str *option) { /** Check for the existence of a given option in a given section. */ __ss_int __12; __ss_bool __14; if (__OR((!___bool(section)), __eq(section, DEFAULTSECT), 12)) { option = this->optionxform(option); return (this->_defaults)->__contains__(option); } else if ((!(this->_sections)->__contains__(section))) { return False; } else { option = this->optionxform(option); return __OR(((this->_sections)->__getitem__(section))->__contains__(option), (this->_defaults)->__contains__(option), 14); } return False; } void *RawConfigParser::write(file *fp) { /** Write an .ini-format representation of the configuration state. */ __ss_int __22, __29; str *key, *section, *value; tuple2 *__19, *__26; list *> *__20, *__27; list *>::for_in_loop __123; if (___bool(this->_defaults)) { fp->write(__modct(const_11, 1, DEFAULTSECT)); FOR_IN(__19,(this->_defaults)->items(),20,22,123) __19 = __19; key = __19->__getfirst__(); value = __19->__getsecond__(); fp->write(__modct(const_12, 2, key, (__str(value))->replace(const_13, const_14))); END_FOR fp->write(const_13); } dict *>::for_in_loop __3; int __2; dict *> *__1; FOR_IN(section,this->_sections,1,2,3) fp->write(__modct(const_11, 1, section)); FOR_IN(__26,((this->_sections)->__getitem__(section))->items(),27,29,123) __26 = __26; key = __26->__getfirst__(); value = __26->__getsecond__(); if (__ne(key, const_15)) { fp->write(__modct(const_12, 2, key, (__str(value))->replace(const_13, const_14))); } END_FOR fp->write(const_13); END_FOR return NULL; } void *RawConfigParser::add_section(str *section) { /** Create a new section in the configuration. Raise DuplicateSectionError if a section by the specified name already exists. */ if ((this->_sections)->__contains__(section)) { throw ((new DuplicateSectionError(section))); } this->_sections->__setitem__(section, (new dict())); return NULL; } list *RawConfigParser::sections() { /** Return a list of section names, excluding [DEFAULT] */ return (this->_sections)->keys(); } str *RawConfigParser::get(str *section, str *option, __ss_int, dict *) { str *opt; opt = this->optionxform(option); if ((!(this->_sections)->__contains__(section))) { if (__ne(section, DEFAULTSECT)) { throw ((new NoSectionError(section))); } if ((this->_defaults)->__contains__(opt)) { return (this->_defaults)->__getitem__(opt); } else { throw ((new NoOptionError(option,section))); } } else if (((this->_sections)->__getitem__(section))->__contains__(opt)) { return ((this->_sections)->__getitem__(section))->__getitem__(opt); } else if ((this->_defaults)->__contains__(opt)) { return (this->_defaults)->__getitem__(opt); } else { throw ((new NoOptionError(option,section))); } return (str *)NULL; } list *RawConfigParser::read(str *filename) { return read(new list(1, filename)); } list *RawConfigParser::read(list *filenames) { /** Read and parse a filename or a list of filenames. Files that cannot be opened are silently ignored; this is designed so that you can specify a list of potential configuration file locations (e.g. current directory, user's home directory, systemwide directory), and all existing configuration files in the list will be read. A single filename may also be given. Return list of successfully read files. */ list *__7, *read_ok; str *filename; __ss_int __9; file *fp; list::for_in_loop __123; read_ok = (new list()); FOR_IN(filename,filenames,7,9,123) try { fp = open(filename); } catch (IOError *) { continue; } this->_read(fp, filename); fp->close(); read_ok->append(filename); END_FOR return read_ok; } __ss_bool RawConfigParser::getboolean(str *section, str *option) { str *v; v = this->get(section, option, default_5, NULL); if ((!(RawConfigParser::_boolean_states)->__contains__(v->lower()))) { throw ((new ValueError(__modct(const_16, 1, v)))); } return __mbool((RawConfigParser::_boolean_states)->__getitem__(v->lower())); } list *> *RawConfigParser::items(str *section) { dict *d, *d2; try { d2 = (this->_sections)->__getitem__(section); } catch (KeyError *) { if (__ne(section, DEFAULTSECT)) { throw ((new NoSectionError(section))); } d2 = (new dict()); } d = (this->_defaults)->copy(); d->update(d2); if (d->__contains__(const_15)) { d->__delitem__(const_15); } return d->items(); } void *RawConfigParser::_read(file *fp, str *fpname) { /** Parse a sectioned setup file. The sections in setup file contains a title line at the top, indicated by a name in square brackets (`[]'), plus key/value options lines, indicated by `name: value' format lines. Continuations are represented by an embedded newline then leading whitespace. Blank lines, lines beginning with a '#', and just about everything else are ignored. */ __re__::match_object *mo; __ss_int __33, __35, __41, __43, lineno, pos; ParsingError *e; str *line, *optname, *optval, *sectname, *value, *vi; dict *cursect; cursect = 0; optname = 0; lineno = 0; e = 0; while (1) { line = fp->readline(); if ((!___bool(line))) { break; } lineno = (lineno+1); if (__OR(__eq(line->strip(), const_17), (const_18)->__contains__(line->__getitem__(0)), 33)) { continue; } if (__AND(__eq(((line->split(NULL, 1))->__getfast__(0))->lower(), const_19), (const_20)->__contains__(line->__getitem__(0)), 35)) { continue; } if (((line->__getitem__(0))->isspace() && (cursect!=0) && ___bool(optname))) { value = line->strip(); if (___bool(value)) { cursect->__setitem__(optname, __modct(const_21, 2, cursect->__getitem__(optname), value)); } } else { mo = (RawConfigParser::SECTCRE)->match(line); if (___bool(mo)) { sectname = mo->group(1, const_22); if ((this->_sections)->__contains__(sectname)) { cursect = (this->_sections)->__getitem__(sectname); } else if (__eq(sectname, DEFAULTSECT)) { cursect = this->_defaults; } else { cursect = (new dict(1, new tuple2(2,const_15,sectname))); this->_sections->__setitem__(sectname, cursect); } optname = 0; } else if ((cursect==0)) { throw ((new MissingSectionHeaderError(fpname,lineno,line))); } else { mo = (RawConfigParser::OPTCRE)->match(line); if (___bool(mo)) { optname = mo->group(1, const_23); vi = mo->group(1, const_24); optval = mo->group(1, const_25); if (__AND((const_2)->__contains__(vi), optval->__contains__(const_26), 41)) { pos = optval->find(const_26); if (__AND((pos!=(-1)), (optval->__getitem__((pos-1)))->isspace(), 43)) { optval = optval->__slice__(2, 0, pos, 0); } } optval = optval->strip(); if (__eq(optval, const_27)) { optval = const_17; } optname = this->optionxform(optname->rstrip()); cursect->__setitem__(optname, optval); } else { if ((!___bool(e))) { e = (new ParsingError(fpname)); } e->append(lineno, repr(line)); } } } } if (___bool(e)) { throw (e); } return NULL; } __ss_int RawConfigParser::getint(str *section, str *option) { return __int(this->get(section, option, default_5, NULL)); } dict *RawConfigParser::defaults() { return this->_defaults; } list *RawConfigParser::options(str *section) { /** Return a list of option names for the given section name. */ dict *opts; try { opts = ((this->_sections)->__getitem__(section))->copy(); } catch (KeyError *) { throw ((new NoSectionError(section))); } opts->update(this->_defaults); if (opts->__contains__(const_15)) { opts->__delitem__(const_15); } return opts->keys(); } dict *RawConfigParser::_boolean_states; __re__::re_object *RawConfigParser::SECTCRE; __re__::re_object *RawConfigParser::OPTCRE; /** class ConfigParser */ class_ *cl_ConfigParser; str *ConfigParser::_interpolate(str *section, str *option, str *rawval, dict *vars) { str *value; __ss_int depth; value = rawval; depth = MAX_INTERPOLATION_DEPTH; while (depth) { depth = (depth-1); if (value->__contains__(const_28)) { value = (ConfigParser::_KEYCRE)->sub(_interpolation_replace, value); try { value = __moddict(value, vars); } catch (KeyError *e) { throw ((new InterpolationMissingOptionError(option,section,rawval,const_17))); } } else { break; } } if (value->__contains__(const_28)) { throw ((new InterpolationDepthError(option,section,rawval))); } return value; } str *ConfigParser::get(str *section, str *option, __ss_int raw, dict *vars) { /** Get an option value for a given section. All % interpolations are expanded in the return values, based on the defaults passed into the constructor, unless the optional argument `raw' is true. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose contents overrides any pre-existing defaults. The section DEFAULT is special. */ __ss_int __49; tuple2 *__46; str *key, *value; list *> *__47; dict *d; list *>::for_in_loop __123; d = (this->_defaults)->copy(); try { d->update((this->_sections)->__getitem__(section)); } catch (KeyError *) { if (__ne(section, DEFAULTSECT)) { throw ((new NoSectionError(section))); } } if (___bool(vars)) { FOR_IN(__46,vars->items(),47,49,123) __46 = __46; key = __46->__getfirst__(); value = __46->__getsecond__(); d->__setitem__(this->optionxform(key), value); END_FOR } option = this->optionxform(option); try { value = d->__getitem__(option); } catch (KeyError *) { throw ((new NoOptionError(option,section))); } if (raw) { return value; } else { return this->_interpolate(section, option, value, d); } return (str *)NULL; } list *> *ConfigParser::items(str *section, __ss_int raw, dict *vars) { /** Return a list of tuples with (name, value) for each option in the section. All % interpolations are expanded in the return values, based on the defaults passed into the constructor, unless the optional argument `raw' is true. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose contents overrides any pre-existing defaults. The section DEFAULT is special. */ list *options; __ss_int __55; tuple2 *__52; str *key, *value; list *> *__53; dict *d; list *>::for_in_loop __123; d = (this->_defaults)->copy(); try { d->update((this->_sections)->__getitem__(section)); } catch (KeyError *) { if (__ne(section, DEFAULTSECT)) { throw ((new NoSectionError(section))); } } if (___bool(vars)) { FOR_IN(__52,vars->items(),53,55,123) __52 = __52; key = __52->__getfirst__(); value = __52->__getsecond__(); d->__setitem__(this->optionxform(key), value); END_FOR } options = d->keys(); if (options->__contains__(const_15)) { options->remove(const_15); } if (raw) { return list_comp_0(options, d); } else { return list_comp_1(d, this, options, section); } return (list *> *)NULL; } str *_interpolation_replace(__re__::match_object *match) { str *s; s = match->group(1, 1); if ((s==0)) { return match->group(1); } else { return __modct(new str("%%(%s)s"), 1, s->lower()); } return (str *)NULL; } __re__::re_object *ConfigParser::_KEYCRE; void __init() { const_0 = new str("="); const_1 = new str(":"); const_2 = (new tuple2(2, const_0, const_1)); const_3 = new str("No section: %r"); const_4 = new str("Section %r already exists"); const_5 = new str("No option %r in section: %r"); const_6 = new str("Bad value substitution:\n\tsection: [%s]\n\toption : %s\n\tkey : %s\n\trawval : %s\n"); const_7 = new str("Value interpolation too deeply recursive:\n\tsection: [%s]\n\toption : %s\n\trawval : %s\n"); const_8 = new str("File contains parsing errors: %s"); const_9 = new str("\n\t[line %2d]: %s"); const_10 = new str("File contains no section headers.\nfile: %s, line: %d\n%r"); const_11 = new str("[%s]\n"); const_12 = new str("%s = %s\n"); const_13 = new str("\n"); const_14 = new str("\n\t"); const_15 = new str("__name__"); const_16 = new str("Not a boolean: %s"); const_17 = new str(""); const_18 = new str("#;"); const_19 = new str("rem"); const_20 = new str("rR"); const_21 = new str("%s\n%s"); const_22 = new str("header"); const_23 = new str("option"); const_24 = new str("vi"); const_25 = new str("value"); const_26 = new str(";"); const_27 = new str("\"\""); const_28 = new str("%("); const_29 = new str("1"); const_30 = new str("yes"); const_31 = new str("true"); const_32 = new str("on"); const_33 = new str("0"); const_34 = new str("no"); const_35 = new str("false"); const_36 = new str("off"); const_37 = new str("\\[(?P
[^]]+)\\]"); const_38 = new str("(?P *a, A x, __ss_int lo, __ss_int hi) { __ss_int mid; __pos_check(lo, hi); while((lounits[mid])==-1) { hi = mid; } else { lo = (mid+1); } } a->insert(lo, x); return NULL; } template void *insort(list *a, A x, __ss_int lo, __ss_int hi) { insort_right(a, x, lo, hi); return NULL; } template __ss_int bisect_right(list *a, A x, __ss_int lo, __ss_int hi) { __ss_int mid; __pos_check(lo, hi); while((lounits[mid])==-1) { hi = mid; } else { lo = (mid+1); } } return lo; } template __ss_int bisect(list *a, A x, __ss_int lo, __ss_int hi) { return bisect_right(a, x, lo, hi); } template void *insort_left(list *a, A x, __ss_int lo, __ss_int hi) { __ss_int mid; __pos_check(lo, hi); while((lounits[mid], x)==-1) { lo = (mid+1); } else { hi = mid; } } a->insert(lo, x); return NULL; } template __ss_int bisect_left(list *a, A x, __ss_int lo, __ss_int hi) { __ss_int mid; __pos_check(lo, hi); while((lounits[mid], x)==-1) { lo = (mid+1); } else { hi = mid; } } return lo; } template __ss_int bisect_left(list *a, A x, __ss_int lo=0) { return bisect_left(a, x, lo, len(a)); } template __ss_int bisect_right(list *a, A x, __ss_int lo=0) { return bisect_right(a, x, lo, len(a)); } template __ss_int bisect(list *a, A x, __ss_int lo=0) { return bisect_right(a, x, lo, len(a)); } template void *insort_left(list *a, A x, __ss_int lo=0) { return insort_left(a, x, lo, len(a)); } template void *insort_right(list *a, A x, __ss_int lo=0) { return insort_right(a, x, lo, len(a)); } template void *insort(list *a, A x, __ss_int lo=0) { return insort_right(a, x, lo, len(a)); } } // module namespace #endif shedskin-0.9.4/shedskin/lib/array.hpp0000664000175000017500000003142112157270661017467 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __ARRAY_HPP #define __ARRAY_HPP #include "builtin.hpp" using namespace __shedskin__; namespace __array__ { extern str *const_0; extern str *__name__; extern void *buffy; unsigned int get_itemsize(char typechar); extern class_ *cl_array; template class array : public pyseq { public: __GC_VECTOR(char) units; /* XXX no pointers, so avoid GC */ str *typecode; char typechar; unsigned int itemsize; array(str *typecode) { this->__class__ = cl_array; this->typecode = typecode; this->typechar = typecode->unit[0]; this->itemsize = get_itemsize(typechar); } template array(str *typecode, U *iter) { /* XXX iter with type None */ this->__class__ = cl_array; __init__(typecode, iter); } template void *__init__(str *typecode, U *iter); template void *extend(U *iter); template void *fromlist(U *iter); void *fromstring(str *s); list *tolist(); str *tostring(); T __getitem__(__ss_int i); T __getfast__(__ss_int i); void *__setitem__(__ss_int i, T t); void *__delitem__(__ss_int i); void *append(T t); void *insert(__ss_int i, T t); __ss_bool __eq__(pyobj *p); array *__mul__(__ss_int i); array *__imul__(__ss_int i); array *__add__(array *a); array *__iadd__(array *a); __ss_int count(T t); __ss_int index(T t); void *remove(T t); T pop(__ss_int i=-1); __ss_int __len__(); str *__repr__(); void *reverse(); void *byteswap(); void *tofile(file *f); void *fromfile(file *f, __ss_int n); void fillbuf(T t); array *__copy__(); array *__deepcopy__(dict *memo); array *__slice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s); void *__setslice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s, array *b); void *__delete__(__ss_int i); void *__delete__(__ss_int x, __ss_int l, __ss_int u, __ss_int s); void *__delslice__(__ss_int a, __ss_int b); }; template template void *array::__init__(str *typecode, U *iter) { this->typecode = typecode; this->typechar = typecode->unit[0]; this->itemsize = get_itemsize(typechar); this->extend(iter); return NULL; } template template void *array::extend(U *iter) { if(iter->__class__ == cl_array) { array *arr = (array *)iter; size_t s1 = this->units.size(); size_t s2 = arr->units.size(); this->units.resize(s1+s2); memcpy(&(this->units[s1]), &(arr->units[0]), s2); } else { typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,iter,1,2,3) this->append(e); END_FOR } return NULL; } template<> template<> void *array::extend(list<__ss_int> *l); template<> template<> void *array::extend(str *s); template template void *array::fromlist(U *iter) { extend(iter); return NULL; } template str *array::tostring() { str *s = new str(); size_t s1 = this->units.size(); s->unit.resize(s1); memcpy(&(s->unit[0]), &(this->units[0]), s1); return s; } template void *array::fromstring(str *s) { size_t len = s->unit.size(); if(len == 1) this->units.push_back(s->unit[0]); else { size_t s1 = this->units.size(); this->units.resize(s1+len); memcpy(&(this->units[0]), &(s->unit[0]), len); } return NULL; } template list *array::tolist() { list *l = new list(); size_t len = this->__len__(); l->resize(len); for(size_t i=0; iunits[i] = __getitem__(i); return l; } template __ss_int array::__len__() { return units.size() / itemsize; } template __ss_bool array::__eq__(pyobj *p) { if(p->__class__ != cl_array) return False; array *b = (array *)p; size_t len = this->__len__(); if(b->__len__() != len) return False; if(this->typechar == b->typechar) return __mbool(memcmp(&(this->units[0]), &(b->units[0]), this->units.size()) == 0); for(size_t i=0; i__getitem__(i), b->__getitem__(i))) return False; return True; } template array *array::__mul__(__ss_int n) { array *a = new array(typecode); size_t len = this->units.size(); a->units.resize(len*n); for(size_t i=0; iunits[i*len]), &(this->units[0]), len); return a; } template array *array::__imul__(__ss_int n) { size_t len = this->units.size(); this->units.resize(len*n); for(size_t i=1; iunits[i*len]), &(this->units[0]), len); return this; } template array *array::__add__(array *b) { if(this->typecode != b->typecode) throw new TypeError(new str("bad argument type for built-in operation")); array *a = new array(typecode); size_t s1 = this->units.size(); size_t s2 = b->units.size(); a->units.resize(s1+s2); memcpy(&(a->units[0]), &(this->units[0]), s1); memcpy(&(a->units[s1]), &(b->units[0]), s2); return a; } template array *array::__iadd__(array *b) { if(this->typecode != b->typecode) throw new TypeError(new str("bad argument type for built-in operation")); size_t s1 = this->units.size(); size_t s2 = b->units.size(); this->units.resize(s1+s2); memcpy(&(this->units[s1]), &(b->units[0]), s2); return this; } template __ss_int array::count(T t) { __ss_int result = 0; size_t len = this->__len__(); for(size_t i=0; i__getitem__(i))) result += 1; return result; } template<> __ss_int array::count(str *t); template __ss_int array::index(T t) { size_t len = this->__len__(); for(size_t i=0; i__getitem__(i))) return i; throw new ValueError(new str("array.index(x): x not in list")); } template<> __ss_int array::index(str *t); template void *array::remove(T t) { this->pop(this->index(t)); return NULL; } template T array::pop(__ss_int i) { size_t len = this->__len__(); if(len==0) throw new IndexError(new str("pop from empty list")); if(i<0) i = len+i; if(i<0 or i>=len) throw new IndexError(new str("pop index out of range")); T t = this->__getitem__(i); this->units.erase(this->units.begin()+(i*itemsize), this->units.begin()+((i+1)*itemsize)); return t; } template void array::fillbuf(T t) { switch(typechar) { case 'b': *((signed char *)buffy) = t; break; case 'B': *((unsigned char *)buffy) = t; break; case 'h': *((signed short *)buffy) = t; break; case 'H': *((unsigned short *)buffy) = t; break; case 'i': *((signed int *)buffy) = t; break; case 'I': *((unsigned int *)buffy) = t; break; case 'l': *((signed long *)buffy) = t; break; case 'L': *((unsigned long *)buffy) = t; break; case 'f': *((float *)buffy) = t; break; case 'd': *((double *)buffy) = t; break; } } template T array::__getitem__(__ss_int i) { return __getfast__(i); } template<> inline __ss_int array<__ss_int>::__getfast__(__ss_int i) { i = __wrap(this, i); switch(typechar) { case 'b': return *((signed char *)(&units[i*itemsize])); case 'B': return *((unsigned char *)(&units[i*itemsize])); case 'h': return *((signed short *)(&units[i*itemsize])); case 'H': return *((unsigned short *)(&units[i*itemsize])); case 'i': return *((signed int *)(&units[i*itemsize])); case 'I': return *((unsigned int *)(&units[i*itemsize])); case 'l': return *((signed long *)(&units[i*itemsize])); case 'L': return *((unsigned long *)(&units[i*itemsize])); } return 0; } template<> inline str *array::__getfast__(__ss_int i) { i = __wrap(this, i); return __char_cache[(unsigned char)units[i]]; } template<> inline double array::__getfast__(__ss_int i) { i = __wrap(this, i); if(typechar == 'f') return *((float *)(&units[i*itemsize])); else return *((double *)(&units[i*itemsize])); } template void *array::append(T t) { fillbuf(t); for(unsigned int i=0; i void *array::append(str *t); template void *array::__setitem__(__ss_int i, T t) { i = __wrap(this, i); fillbuf(t); for(unsigned int j=0; junits[i*itemsize+j] = ((char *)buffy)[j]; return NULL; } template<> void *array::__setitem__(__ss_int i, str *t); template void *array::insert(__ss_int i, T t) { i = __wrap(this, i); this->units.insert(this->units.begin()+(i*itemsize), itemsize, '\0'); this->__setitem__(i, t); return NULL; } template void *array::__delitem__(__ss_int i) { i = __wrap(this, i); this->units.erase(units.begin()+(i*itemsize), units.begin()+((i+1)*itemsize)); return NULL; } template str *array::__repr__() { return __add_strs(5, new str("array('"), typecode, new str("', "), repr(tolist()), new str(")")); } template<> str *array::__repr__(); template void *array::reverse() { /* use fillbuf, __setitem__ or standard C function? */ size_t len = this->__len__(); if(len > 1) { char *first = &units[0]; char *second = &units[(len-1)*itemsize]; for(size_t i=0; i void *array::byteswap() { /* standard C function? */ size_t len = this->__len__(); for(size_t i=0; i void *array::tofile(file *f) { f->write(this->tostring()); return NULL; } template void *array::fromfile(file *f, __ss_int n) { str *s = f->read(n*itemsize); size_t len = s->__len__(); size_t bytes = (len/itemsize)*itemsize; for(size_t i=0; iunit[i]); if (len < n*itemsize) throw new EOFError(new str("not enough items in file")); } template array *array::__copy__() { array *a = new array(this->typecode); a->units = this->units; return a; } template array *array::__deepcopy__(dict *memo) { return this->__copy__(); } template array *array::__slice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s) { array *c = new array(typecode); slicenr(x, l, u, s, this->__len__()); if(s == 1) { c->units.resize((u-l)*itemsize); memcpy(&(c->units[0]), &(this->units[l*itemsize]), (u-l)*itemsize); } else if(s > 0) for(int i=l; iunits.push_back(units[i*itemsize+j]); else for(int i=l; i>u; i += s) for(int j=0; junits.push_back(units[i*itemsize+j]); return c; } /* XXX optimize XXX */ template void *array::__setslice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s, array *b) { list *l2 = this->tolist(); l2->__setslice__(x, l, u, s, b->tolist()); this->units.clear(); this->fromlist(l2); return NULL; } template void *array::__delete__(__ss_int i) { i = __wrap(this, i); this->units.erase(units.begin()+(i*itemsize), units.begin()+((i+1)*itemsize)); return NULL; } /* XXX optimize XXX */ template void *array::__delete__(__ss_int x, __ss_int l, __ss_int u, __ss_int s) { list *l2 = this->tolist(); l2->__delete__(x, l, u, s); this->units.clear(); this->fromlist(l2); return NULL; } template void *array::__delslice__(__ss_int a, __ss_int b) { if(a>this->__len__()) return NULL; if(b>this->__len__()) b = this->__len__(); units.erase(units.begin()+(a*itemsize),units.begin()+(b*itemsize)); return NULL; } extern void * default_0; void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/math.cpp0000664000175000017500000000036512157270661017300 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "math.hpp" namespace __math__ { double e, pi; void __init() { e = 2.71828182846; pi = 3.14159265359; } } // module namespace shedskin-0.9.4/shedskin/lib/signal.cpp0000664000175000017500000000354512157270661017627 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "signal.hpp" #include namespace __signal__ { str *__name__; __ss_int __ss_SIGABRT, __ss_SIGALRM, __ss_SIGBUS, __ss_SIGCHLD, __ss_SIGCLD, __ss_SIGCONT, __ss_SIGFPE, __ss_SIGHUP, __ss_SIGILL, __ss_SIGINT, __ss_SIGIO, __ss_SIGIOT, __ss_SIGKILL, __ss_SIGPIPE, __ss_SIGPOLL, __ss_SIGPROF, __ss_SIGPWR, __ss_SIGQUIT, __ss_SIGRTMAX, __ss_SIGRTMIN, __ss_SIGSEGV, __ss_SIGSTOP, __ss_SIGSYS, __ss_SIGTERM, __ss_SIGTRAP, __ss_SIGTSTP, __ss_SIGTTIN, __ss_SIGTTOU, __ss_SIGURG, __ss_SIGUSR1, __ss_SIGUSR2, __ss_SIGVTALRM, __ss_SIGWINCH, __ss_SIGXCPU, __ss_SIGXFSZ, __ss_SIGBREAK; //, __ss_SIG_DFL, __ss_SIG_IGN; void __init() { __name__ = new str("signal"); __ss_SIGABRT = SIGABRT; __ss_SIGFPE = SIGFPE; __ss_SIGILL = SIGILL; __ss_SIGINT = SIGINT; __ss_SIGSEGV = SIGSEGV; __ss_SIGTERM = SIGTERM; #ifdef WIN32 __ss_SIGBREAK = SIGBREAK; #else #ifndef __APPLE__ __ss_SIGCLD = SIGCLD; __ss_SIGPOLL = SIGPOLL; __ss_SIGPWR = SIGPWR; __ss_SIGRTMAX = SIGRTMAX; __ss_SIGRTMIN = SIGRTMIN; #endif __ss_SIGALRM = SIGALRM; __ss_SIGBUS = SIGBUS; __ss_SIGCHLD = SIGCHLD; __ss_SIGCONT = SIGCONT; __ss_SIGHUP = SIGHUP; __ss_SIGIO = SIGIO; __ss_SIGIOT = SIGIOT; __ss_SIGKILL = SIGKILL; __ss_SIGPIPE = SIGPIPE; __ss_SIGPROF = SIGPROF; __ss_SIGQUIT = SIGQUIT; __ss_SIGSTOP = SIGSTOP; __ss_SIGSYS = SIGSYS; __ss_SIGTRAP = SIGTRAP; __ss_SIGTSTP = SIGTSTP; __ss_SIGTTIN = SIGTTIN; __ss_SIGTTOU = SIGTTOU; __ss_SIGURG = SIGURG; __ss_SIGUSR1 = SIGUSR1; __ss_SIGUSR2 = SIGUSR2; __ss_SIGVTALRM = SIGVTALRM; __ss_SIGWINCH = SIGWINCH; __ss_SIGXCPU = SIGXCPU; __ss_SIGXFSZ = SIGXFSZ; // __ss_SIG_DFL = SIG_DFL; // __ss_SIG_IGN = SIG_IGN; #endif } } // module namespace shedskin-0.9.4/shedskin/lib/csv.cpp0000664000175000017500000004741112157270661017145 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "csv.hpp" namespace __csv__ { tuple2 *const_3; str *const_1, *const_10, *const_11, *const_12, *const_13, *const_14, *const_15, *const_16, *const_17, *const_18, *const_19, *const_2, *const_20, *const_21, *const_22, *const_23, *const_24, *const_25, *const_26, *const_27, *const_4, *const_5, *const_6, *const_7, *const_8, *const_9; list *const_0; str *const_88; str *__name__; list<__ss_int> *__0, *__1; __ss_int EAT_CRNL, ESCAPED_CHAR, ESCAPE_IN_QUOTED_FIELD, IN_FIELD, IN_QUOTED_FIELD, QUOTE_ALL, QUOTE_IN_QUOTED_FIELD, QUOTE_MINIMAL, QUOTE_NONE, QUOTE_NONNUMERIC, START_FIELD, START_RECORD, _field_limit; OSError *__exception; void * default_9; void * default_14; void * default_16; void * default_21; void * default_23; str * default_18; void * default_25; void * default_0; void * default_2; void * default_6; void * default_3; void * default_8; void * default_10; void * default_11; void * default_13; void * default_15; void * default_12; void * default_17; void * default_24; str * default_19; str * default_20; void * default_22; void * default_7; void * default_1; void * default_5; void * default_4; __csviter::__csviter(reader *r) { this->r = r; } list *__csviter::next() { return r->next(); } __csviter *reader::__iter__() { return new __csviter(this); } __driter::__driter(DictReader *r) { this->r = r; } dict *__driter::next() { return r->next(); } __driter *DictReader::__iter__() { return new __driter(this); } static inline list *list_comp_0(DictWriter *self, dict *rowdict) { list *__ss_result = new list(); str *k; dict::for_in_loop __3; int __2; dict *__1; FOR_IN(k,rowdict,1,2,3) if ((!(self->fieldnames)->__contains__(k))) __ss_result->append(k); END_FOR return __ss_result; } static inline list *list_comp_1(DictWriter *self, dict *rowdict) { list *__37; list::for_in_loop __123; __ss_int __39; str *key; list *__ss_result = new list(); __37 = self->fieldnames; __ss_result->resize(len(__37)); FOR_IN(key,__37,37,39,123) __ss_result->units[__39] = rowdict->get(key, self->restval); END_FOR return __ss_result; } /** class Error */ class_ *cl_Error; /** class Excel */ class_ *cl_Excel; void *Excel::__init__() { this->delimiter = const_4; this->quotechar = const_5; this->doublequote = 1; this->skipinitialspace = 0; this->lineterminator = const_6; this->quoting = QUOTE_MINIMAL; this->escapechar = ((str *)(NULL)); this->strict = 0; return NULL; } /** class reader */ class_ *cl_reader; void *reader::parse_process_char(str *c) { Excel *dialect; __ss_int __11, __13, __7, __9; dialect = this->dialect; if ((this->state==START_RECORD)) { if (__eq(c, const_7)) { return NULL; } else if ((const_8)->__contains__(c)) { this->state = EAT_CRNL; return NULL; } this->state = START_FIELD; } if ((this->state==START_FIELD)) { if ((const_9)->__contains__(c)) { this->parse_save_field(); if (__eq(c, const_7)) { this->state = START_RECORD; } else { this->state = EAT_CRNL; } } else if (__AND(__eq(c, dialect->quotechar), (dialect->quoting!=QUOTE_NONE), 7)) { this->state = IN_QUOTED_FIELD; } else if (__eq(c, dialect->escapechar)) { this->state = ESCAPED_CHAR; } else if (__AND(__eq(c, const_10), dialect->skipinitialspace, 9)) { } else if (__eq(c, dialect->delimiter)) { this->parse_save_field(); } else { if ((dialect->quoting==QUOTE_NONNUMERIC)) { this->numeric_field = 1; } this->parse_add_char(c); this->state = IN_FIELD; } } else if ((this->state==ESCAPED_CHAR)) { if (__eq(c, const_7)) { c = const_11; } this->parse_add_char(c); this->state = IN_FIELD; } else if ((this->state==IN_FIELD)) { if ((const_12)->__contains__(c)) { this->parse_save_field(); if (__eq(c, const_7)) { this->state = START_RECORD; } else { this->state = EAT_CRNL; } } else if (__eq(c, dialect->escapechar)) { this->state = ESCAPED_CHAR; } else if (__eq(c, dialect->delimiter)) { this->parse_save_field(); this->state = START_FIELD; } else { this->parse_add_char(c); } } else if ((this->state==IN_QUOTED_FIELD)) { if (__eq(c, const_7)) { } else if (__eq(c, dialect->escapechar)) { this->state = ESCAPE_IN_QUOTED_FIELD; } else if (__AND(__eq(c, dialect->quotechar), (dialect->quoting!=QUOTE_NONE), 11)) { if (dialect->doublequote) { this->state = QUOTE_IN_QUOTED_FIELD; } else { this->state = IN_FIELD; } } else { this->parse_add_char(c); } } else if ((this->state==ESCAPE_IN_QUOTED_FIELD)) { if (__eq(c, const_7)) { c = const_11; } this->parse_add_char(c); this->state = IN_QUOTED_FIELD; } else if ((this->state==QUOTE_IN_QUOTED_FIELD)) { if (__AND((dialect->quoting!=QUOTE_NONE), __eq(c, dialect->quotechar), 13)) { this->parse_add_char(c); this->state = IN_QUOTED_FIELD; } else if (__eq(c, dialect->delimiter)) { this->parse_save_field(); this->state = START_FIELD; } else if ((const_12)->__contains__(c)) { this->parse_save_field(); if (__eq(c, const_7)) { this->state = START_RECORD; } else { this->state = EAT_CRNL; } } else if ((!dialect->strict)) { this->parse_add_char(c); this->state = IN_FIELD; } else { throw ((new Error(__modct(const_13, 2, dialect->delimiter, dialect->quotechar)))); } } else if ((this->state==EAT_CRNL)) { if ((const_8)->__contains__(c)) { } else if (__eq(c, const_7)) { this->state = START_RECORD; } else { throw ((new Error(const_14))); } } return 0; } void *reader::parse_reset() { this->fields = (new list()); this->field = (new list()); this->state = START_RECORD; this->numeric_field = 0; return NULL; } list *reader::next() { list *fields; str *line; this->parse_reset(); while (1) { line = (this->input_iter)->next(); this->line_num = (this->line_num+1); str *c; str::for_in_loop __3; int __2; str *__1; FOR_IN(c,line,1,2,3) if (__eq(c, const_7)) { throw ((new Error(const_15))); } this->parse_process_char(c); END_FOR this->parse_process_char(const_7); if ((this->state==START_RECORD)) { break; } } fields = this->fields; this->fields = (new list()); return fields; } void *reader::__init__(file *input_iter, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict) { if ((quoting==QUOTE_NONNUMERIC)) { throw ((new ValueError(const_88))); } this->input_iter = input_iter; this->line_num = 0; this->dialect = _get_dialect(dialect, delimiter, quotechar, doublequote, skipinitialspace, lineterminator, quoting, escapechar, strict); return NULL; } void *reader::parse_save_field() { str *field; field = (const_16)->join(this->field); this->field = (new list()); if (this->numeric_field) { this->numeric_field = 0; } (this->fields)->append(field); return NULL; } void *reader::parse_add_char(str *c) { if ((len(this->field)>=_field_limit)) { throw ((new Error(__modct(const_17, 1, ___box(_field_limit))))); } (this->field)->append(c); return NULL; } /** class writer */ class_ *cl_writer; __ss_int writer::join_append_data(str *field, __ss_int quote_empty, __ss_int quoted) { str *lineterm; Excel *dialect; __ss_int __18, __19, __20, __22, want_escape; dialect = this->dialect; lineterm = dialect->lineterminator; if ((this->num_fields>0)) { (this->rec)->append(dialect->delimiter); } if (quoted) { (this->rec)->append(dialect->quotechar); } str *c; str::for_in_loop __3; int __2; str *__1; FOR_IN(c,field,1,2,3) want_escape = 0; if (__eq(c, const_7)) { break; } if (__OR(__eq(c, dialect->delimiter), __OR(__eq(c, dialect->escapechar), __OR(__eq(c, dialect->quotechar), lineterm->__contains__(c), 20), 19), 18)) { if ((dialect->quoting==QUOTE_NONE)) { want_escape = 1; } else { if (__eq(c, dialect->quotechar)) { if (dialect->doublequote) { (this->rec)->append(dialect->quotechar); } else { want_escape = 1; } } if ((!want_escape)) { quoted = 1; } } if (want_escape) { if ((!___bool(dialect->escapechar))) { throw ((new Error(const_18))); } (this->rec)->append(dialect->escapechar); } } (this->rec)->append(c); END_FOR if (__AND((!___bool(field)), quote_empty, 22)) { if ((dialect->quoting==QUOTE_NONE)) { throw ((new Error(const_19))); } else { quoted = 1; } } if (quoted) { (this->rec)->append(dialect->quotechar); } return quoted; } void *writer::writerow(list *seq) { list *__24; list::for_in_loop __123; Excel *dialect; __ss_int __26, quoted; str *field; dialect = this->dialect; this->join_reset(); FOR_IN(field,seq,24,26,123) quoted = 0; if ((dialect->quoting==QUOTE_NONNUMERIC)) { quoted = 1; } else if ((dialect->quoting==QUOTE_ALL)) { quoted = 1; } if ((field==NULL)) { quoted = this->join_append(const_16, quoted, (len(seq)==1)); } else { quoted = this->join_append(__str(field), quoted, (len(seq)==1)); } END_FOR (this->rec)->append((this->dialect)->lineterminator); (this->output_file)->write((const_16)->join(this->rec)); return NULL; } void *writer::join_reset() { this->rec = (new list()); this->num_fields = 0; return NULL; } void *writer::writerows(list *> *seqs) { list *seq; list *> *__27; list *>::for_in_loop __123; __ss_int __29; FOR_IN(seq,seqs,27,29,123) this->writerow(seq); END_FOR return NULL; } __ss_int writer::join_append(str *field, __ss_int quoted, __ss_int quote_empty) { quoted = this->join_append_data(field, quote_empty, quoted); this->num_fields = (this->num_fields+1); return quoted; } void *writer::__init__(file *output_file, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict) { if ((quoting==QUOTE_NONNUMERIC)) { throw ((new ValueError(const_88))); } this->output_file = output_file; this->dialect = _get_dialect(dialect, delimiter, quotechar, doublequote, skipinitialspace, lineterminator, quoting, escapechar, strict); return NULL; } /** class DictReader */ class_ *cl_DictReader; void *DictReader::setfieldnames(list *value) { this->_fieldnames = value; return NULL; } dict *DictReader::next() { str *key; list *__31, *row; list::for_in_loop __123; __ss_int __33, lf, lr; dict *d; if ((this->line_num==0)) { this->getfieldnames(); } row = (this->_reader)->next(); this->line_num = (this->_reader)->line_num; while (row->empty()) { row = (this->_reader)->next(); } d = new dict(__zip(2, this->getfieldnames(), row)); lf = len(this->getfieldnames()); lr = len(row); if ((lflr)) { FOR_IN(key,(this->getfieldnames())->__slice__(1, lr, 0, 0),31,33,123) d->__setitem__(key, this->restval); END_FOR } return d; } list *DictReader::getfieldnames() { if ((this->_fieldnames==NULL)) { try { this->_fieldnames = (this->_reader)->next(); } catch (StopIteration *) { } } this->line_num = (this->_reader)->line_num; return this->_fieldnames; } void *DictReader::__init__(file *f, list *fieldnames, str *restkey, str *restval, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict) { this->_fieldnames = fieldnames; this->restkey = restkey; this->restval = restval; this->_reader = (new reader(f, dialect, delimiter, quotechar, doublequote, skipinitialspace, lineterminator, quoting, escapechar, strict)); this->dialect = dialect; this->line_num = 0; return NULL; } /** class DictWriter */ class_ *cl_DictWriter; list *DictWriter::_dict_to_list(dict *rowdict) { list *wrong_fields; if (__eq(this->extrasaction, const_1)) { wrong_fields = list_comp_0(this, rowdict); if (___bool(wrong_fields)) { throw ((new ValueError((const_21)->__add__((const_22)->join(wrong_fields))))); } } return list_comp_1(this, rowdict); } void *DictWriter::writerow(dict *rowdict) { return (this->_writer)->writerow(this->_dict_to_list(rowdict)); } void *DictWriter::writerows(list *> *rowdicts) { list *> *rows; list *> *__40; list *>::for_in_loop __123; __ss_int __42; dict *rowdict; rows = (new list *>()); FOR_IN(rowdict,rowdicts,40,42,123) rows->append(this->_dict_to_list(rowdict)); END_FOR return (this->_writer)->writerows(rows); } void *DictWriter::__init__(file *f, list *fieldnames, str *restval, str *extrasaction, str *dialect, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict) { this->fieldnames = fieldnames; this->restval = restval; if ((!(const_3)->__contains__(extrasaction->lower()))) { throw ((new ValueError(__modct(const_23, 1, extrasaction)))); } this->extrasaction = extrasaction; this->_writer = (new writer(f, dialect, delimiter, quotechar, doublequote, skipinitialspace, lineterminator, quoting, escapechar, strict)); return NULL; } void __init() { const_0 = (new list()); const_1 = new str("raise"); const_2 = new str("ignore"); const_3 = (new tuple2(2, const_1, const_2)); const_4 = new str(","); const_5 = new str("\""); const_6 = new str("\r\n"); const_7 = new str("\000", 1); const_8 = new str("\n\r"); const_9 = new str("\n\r\000", 3); const_10 = new str(" "); const_11 = new str("\n"); const_12 = new str("\000\n\r", 3); const_13 = new str("'%c' expected after '%c'"); const_14 = new str("new-line character seen in unquoted field - do you need to open the file in universal-newline mode?"); const_15 = new str("line contains NULL byte"); const_16 = new str(""); const_17 = new str("field larger than field limit (%d)"); const_18 = new str("need to escape, but no escapechar set"); const_19 = new str("single empty field record must be quoted"); const_20 = new str("shedskin: DictReader 'restkey' is not supported"); const_21 = new str("dict contains fields not in fieldnames: "); const_22 = new str(", "); const_23 = new str("extrasaction (%s) must be 'raise' or 'ignore'"); const_24 = new str("excel"); const_25 = new str("excel-tab"); const_26 = new str("\t"); const_27 = new str("unknown dialect"); const_88 = new str("shedskin: QUOTE_NONNUMERIC is not supported"); __name__ = new str("csv"); cl_writer = new class_("writer"); cl_DictReader = new class_("DictReader"); cl_Excel = new class_("Excel"); cl_reader = new class_("reader"); cl_Error = new class_("Error"); cl_DictWriter = new class_("DictWriter"); __0 = range(8); START_RECORD = __0->__getfast__(0); START_FIELD = __0->__getfast__(1); ESCAPED_CHAR = __0->__getfast__(2); IN_FIELD = __0->__getfast__(3); IN_QUOTED_FIELD = __0->__getfast__(4); ESCAPE_IN_QUOTED_FIELD = __0->__getfast__(5); QUOTE_IN_QUOTED_FIELD = __0->__getfast__(6); EAT_CRNL = __0->__getfast__(7); __1 = range(4); QUOTE_MINIMAL = __1->__getfast__(0); QUOTE_ALL = __1->__getfast__(1); QUOTE_NONNUMERIC = __1->__getfast__(2); QUOTE_NONE = __1->__getfast__(3); _field_limit = (128*1024); default_0 = NULL; default_1 = NULL; default_2 = NULL; default_3 = NULL; default_4 = NULL; default_5 = NULL; default_6 = NULL; default_7 = NULL; default_8 = NULL; default_9 = NULL; default_10 = NULL; default_11 = NULL; default_12 = NULL; default_13 = NULL; default_14 = NULL; default_15 = NULL; default_16 = NULL; default_17 = NULL; default_18 = const_16; default_19 = const_1; default_20 = const_24; default_21 = NULL; default_22 = NULL; default_23 = NULL; default_24 = NULL; } list *list_dialects() { return (new list(2, const_24, const_25)); } Excel *_get_dialect(str *name, str *delimiter, str *quotechar, __ss_int doublequote, __ss_int skipinitialspace, str *lineterminator, __ss_int quoting, str *escapechar, __ss_int strict) { Excel *dialect; __ss_int __2; if (__OR((name==NULL), __eq(name, const_24), 2)) { dialect = (new Excel(1)); } else if (__eq(name, const_25)) { dialect = (new Excel(1)); dialect->delimiter = const_26; } else { throw ((new Error(const_27))); } if ((delimiter!=NULL)) { dialect->delimiter = delimiter; } if ((quotechar!=NULL)) { dialect->quotechar = quotechar; } if ((doublequote!=(-1))) { dialect->doublequote = doublequote; } if ((skipinitialspace!=(-1))) { dialect->skipinitialspace = skipinitialspace; } if ((lineterminator!=NULL)) { dialect->lineterminator = lineterminator; } if ((quoting!=(-1))) { dialect->quoting = quoting; } if ((escapechar!=NULL)) { dialect->escapechar = escapechar; } if ((strict!=(-1))) { dialect->strict = strict; } return dialect; } __ss_int field_size_limit(__ss_int new_limit) { __ss_int old_limit; old_limit = _field_limit; if ((new_limit!=(-1))) { _field_limit = new_limit; } return old_limit; } } // module namespace shedskin-0.9.4/shedskin/lib/itertools.hpp0000664000175000017500000012612012157270661020376 0ustar srepmubsrepmub/* Copyright (c) 2009 Jérémie Roquet ; License Expat (See LICENSE) */ #ifndef ITERTOOLS_HPP #define ITERTOOLS_HPP #include "builtin.hpp" #include #define __SS_ALLOC_TUPLES 25 using namespace __shedskin__; namespace __itertools__ { /* Local helpers */ template static bool _identity(T value) { return value; } /* Infinite Iterators */ // count template class countiter : public __iter { public: T counter; T step; countiter(); countiter(T start, T step); T next(); }; template inline countiter::countiter() {} template inline countiter::countiter(T start, T step) { this->counter = start - step; this->step = step; } template inline T countiter::next() { return this->counter += this->step; } template inline countiter *count(T start, T step) { return new countiter(start, step); } inline countiter *count(int start = 0) { return new countiter(start, 1); } inline countiter *count(double start, double step = 1.) { return new countiter(start, step); } // cycle template class cycleiter : public __iter { public: bool exhausted; int position; __iter *iter; __GC_VECTOR(T) cache; cycleiter(); cycleiter(pyiter *iterable); T next(); }; template inline cycleiter::cycleiter() {} template inline cycleiter::cycleiter(pyiter *iterable) { this->exhausted = false; this->position = 0; this->iter = iterable->__iter__(); } template T cycleiter::next() { if (!this->exhausted) { try { this->cache.push_back(this->iter->next()); return this->cache.back(); } catch (StopIteration *) { if (this->cache.empty()) throw new StopIteration(); this->exhausted = true; } } assert(this->cache.size()); const T& value = this->cache[position]; this->position = (this->position + 1) % this->cache.size(); return value; } template inline cycleiter *cycle(pyiter *iterable) { return new cycleiter(iterable); } // repeat template class repeatiter : public __iter { public: T object; int times; repeatiter(); repeatiter(T object, int times); T next(); }; template inline repeatiter::repeatiter() {} template inline repeatiter::repeatiter(T object, int times) { this->object = object; this->times = times ? times : -1; } template T repeatiter::next() { if (!times) throw new StopIteration(); if (times > 0) --times; return object; } template inline repeatiter *repeat(T object, int times = 0) { return new repeatiter(object, times); } /* Iterators terminating on the shortest input sequence */ // chain template class chainiter : public __iter { public: unsigned int iterable; __GC_VECTOR(__iter *) iters; chainiter(); chainiter(pyiter *iterable); void push_iter(pyiter *iterable); T next(); }; template inline chainiter::chainiter() {} template inline chainiter::chainiter(pyiter *iterable) { this->iterable = 0; this->push_iter(iterable); } template void chainiter::push_iter(pyiter *iterable) { this->iters.push_back(iterable->__iter__()); } template T chainiter::next() { for (; ; ) { try { return this->iters[iterable]->next(); } catch (StopIteration *) { if (this->iterable == this->iters.size() - 1) throw new StopIteration(); ++this->iterable; } } } template inline chainiter *chain(int iterable_count, pyiter *iterable, ...) { chainiter *iter = new chainiter(iterable); va_list ap; va_start(ap, iterable); while (--iterable_count) { iter->push_iter(va_arg(ap, pyiter *)); } va_end(ap); return iter; } // compress template class compressiter : public __iter { public: __iter *data_iter; __iter *selectors_iter; compressiter(); compressiter(pyiter *data, pyiter *selectors); T next(); }; template inline compressiter::compressiter() {} template inline compressiter::compressiter(pyiter *data, pyiter *selectors) { this->data_iter = data->__iter__(); this->selectors_iter = selectors->__iter__(); } template T compressiter::next() { while (!this->selectors_iter->next()) { this->data_iter->next(); } return this->data_iter->next(); } template inline compressiter *compress(pyiter *data, pyiter *selectors) { return new compressiter(data, selectors); } // dropwhile template class dropwhileiter : public __iter { public: bool drop; B (*predicate)(T); __iter *iter; dropwhileiter(); dropwhileiter(B (*predicate)(T), pyiter *iterable); T next(); }; template inline dropwhileiter::dropwhileiter() {} template inline dropwhileiter::dropwhileiter(B (*predicate)(T), pyiter *iterable) { this->drop = true; this->predicate = predicate; this->iter = iterable->__iter__(); } template T dropwhileiter::next() { if (drop) { for (; ; ) { const T& value = this->iter->next(); if (!this->predicate(value)) { this->drop = false; return value; } } } return this->iter->next(); } template inline dropwhileiter *dropwhile(B (*predicate)(T), pyiter *iterable) { return new dropwhileiter(predicate, iterable); } // groupby template class groupiter; template class groupbyiter : public __iter *> *> { public: bool first; bool skip; T current_value; K current_key; K (*key)(T); __iter *iter; groupbyiter(); groupbyiter(pyiter *iterable, K (*key)(T)); tuple2 *> *next(); friend class groupiter; }; template class groupiter : public __iter { public: bool first; groupbyiter* iter; groupiter(); groupiter(groupbyiter* iter); T next(); }; template inline groupiter::groupiter() {} template inline groupiter::groupiter(groupbyiter* iter) { this->first = true; this->iter = iter; } template T groupiter::next() { if (this->first) { this->first = false; return this->iter->current_value; } this->iter->current_value = this->iter->iter->next();; const K& new_key = this->iter->key(this->iter->current_value); if (new_key != this->iter->current_key) { this->iter->current_key = new_key; this->iter->skip = false; throw new StopIteration(); } return this->iter->current_value; } template inline groupbyiter::groupbyiter() {} template inline groupbyiter::groupbyiter(pyiter *iterable, K (*key)(T)) { this->first = true; this->skip = false; this->key = key; this->iter = iterable->__iter__(); } template tuple2 *> *groupbyiter::next() { if (!this->skip) { if (this->first) { this->current_value = this->iter->next();; this->current_key = this->key(this->current_value); this->first = false; } this->skip = true; return new tuple2 *>(2, this->current_key, new groupiter(this)); } for (; ; ) { this->current_value = this->iter->next(); const K& new_key = this->key(this->current_value); if (new_key != this->current_key) { this->current_key = new_key; return new tuple2 *>(2, this->current_key, new groupiter(this)); } } } template inline groupbyiter *groupby(pyiter *iterable, K (*key)(T)) { return new groupbyiter(iterable, key); } // ifilter template class ifilteriter : public __iter { public: B (*predicate)(T); __iter *iter; ifilteriter(); ifilteriter(B (*predicate)(T), pyiter *iterable); T next(); }; template inline ifilteriter::ifilteriter() {} template inline ifilteriter::ifilteriter(B (*predicate)(T), pyiter *iterable) { this->predicate = predicate; this->iter = iterable->__iter__(); } template T ifilteriter::next() { for (; ; ) { const T& value = this->iter->next(); if (this->predicate(value)) { return value; } } assert(false && "unreachable"); } template inline ifilteriter *ifilter(B (*predicate)(T), pyiter *iterable) { return new ifilteriter(predicate, iterable); } template inline ifilteriter *ifilter(void * /* null */, pyiter *iterable) { return new ifilteriter(_identity, iterable); } // ifilterfalse template class ifilterfalseiter : public __iter { public: B (*predicate)(T); __iter *iter; ifilterfalseiter(); ifilterfalseiter(B (*predicate)(T), pyiter *iterable); T next(); }; template inline ifilterfalseiter::ifilterfalseiter() {} template inline ifilterfalseiter::ifilterfalseiter(B (*predicate)(T), pyiter *iterable) { this->predicate = predicate; this->iter = iterable->__iter__(); } template T ifilterfalseiter::next() { for (; ; ) { const T& value = this->iter->next(); if (!this->predicate(value)) { return value; } } assert(false && "unreachable"); } template inline ifilterfalseiter *ifilterfalse(B (*predicate)(T), pyiter *iterable) { return new ifilterfalseiter(predicate, iterable); } template inline ifilterfalseiter *ifilterfalse(void * /* null */, pyiter *iterable) { return new ifilterfalseiter(_identity, iterable); } // islice template class isliceiter : public __iter { public: int current_position; int next_position; int stop; int step; __iter *iter; isliceiter(); isliceiter(pyiter *iterable, int start, int stop, int step); T next(); }; template inline isliceiter::isliceiter() {} template inline isliceiter::isliceiter(pyiter *iterable, int start, int stop, int step) { this->current_position = 0; this->next_position = start; this->stop = stop; this->step = step; this->iter = iterable->__iter__(); } template T isliceiter::next() { if (this->next_position >= this->stop && this->stop != -1) throw new StopIteration(); for (; this->current_position < this->next_position; ++this->current_position) { this->iter->next(); } ++this->current_position; this->next_position += this->step; return this->iter->next(); } inline int _start(int start) { return start; } inline int _start(void*) { return 0; } inline int _stop(int stop) { return stop; } inline int _stop(void*) { return -1; } inline int _step(int step) { if (step > 0) { return step; } else { return 1; } } inline int _step(void*) { return 1; } template inline bool _onearg(T /* stop */) { return false; } template<> inline bool _onearg(__ss_int stop) { return stop == -1; } template inline isliceiter *islice(pyiter *iterable, U start, V stop, W step) { if (_onearg(stop)) { return new isliceiter(iterable, 0, _stop(start), _step(step)); } else { return new isliceiter(iterable, _start(start), _stop(stop), _step(step)); } } // imap #define I(N, TP, FP, RP, AP, DP, CP, VP) \ template class imapiter##N : public __iter { \ public: \ bool exhausted; \ R (*function)(FP); \ RP \ \ imapiter##N(); \ imapiter##N(R (*function)(FP), DP); \ \ R next(); \ }; \ \ template inline imapiter##N::imapiter##N() { \ this->exhausted = true; \ } \ template inline imapiter##N::imapiter##N(R (*function)(FP), DP) { \ this->exhausted = false; \ this->function = function; \ AP \ } \ \ template R imapiter##N::next() { \ if (this->exhausted) { \ throw new StopIteration(); \ } \ \ try { \ return this->function(CP); \ } catch (StopIteration *) { \ this->exhausted = true; \ throw; \ } \ } \ \ template inline imapiter##N *imap(int /* iterable_count */, R (*function)(FP), DP) { \ return new imapiter##N(function, VP); \ } #define S , #define L(P) class T##P #define F(P) T##P #define E(P) __iter *iter##P; #define A(P) this->iter##P = iterable##P->__iter__(); #define D(P) pyiter *iterable##P #define C(P) this->iter##P->next() #define V(P) iterable##P I(1, L(1), F(1), E(1), A(1), D(1), C(1), V(1)) I(2, L(1) S L(2), F(1) S F(2), E(1) E(2), A(1) A(2), D(1) S D(2), C(1) S C(2), V(1) S V(2)) I(3, L(1) S L(2) S L(3), F(1) S F(2) S F(3), E(1) E(2) E(3), A(1) A(2) A(3), D(1) S D(2) S D(3), C(1) S C(2) S C(3), V(1) S V(2) S V(3)) I(4, L(1) S L(2) S L(3) S L(4), F(1) S F(2) S F(3) S F(4), E(1) E(2) E(3) E(4), A(1) A(2) A(3) A(4), D(1) S D(2) S D(3) S D(4), C(1) S C(2) S C(3) S C(4), V(1) S V(2) S V(3) S V(4)) I(5, L(1) S L(2) S L(3) S L(4) S L(5), F(1) S F(2) S F(3) S F(4) S F(5), E(1) E(2) E(3) E(4) E(5), A(1) A(2) A(3) A(4) A(5), D(1) S D(2) S D(3) S D(4) S D(5), C(1) S C(2) S C(3) S C(4) S C(5), V(1) S V(2) S V(3) S V(4) S V(5)) #undef S #undef L #undef F #undef E #undef A #undef D #undef C #undef V #undef I // starmap // TODO // tee template class teecache { public: typedef std::pair item; int begin; int end; int clients; __GC_DEQUE(item) cache; teecache(int clients); void add(const T& value); T get(int position); }; template inline teecache::teecache(int clients) { this->begin = 0; this->end = 0; this->clients = clients; } template void teecache::add(const T& value) { ++this->end; this->cache.push_back(std::make_pair(value, clients)); } template T teecache::get(int position) { assert(!this->cache.empty()); while (!this->cache.front().second) { ++this->begin; this->cache.pop_front(); } --this->cache[position - this->begin].second; return this->cache[position - this->begin].first; } template class teeiter : public __iter { public: int position; __iter *iter; teecache *cache; teeiter(); teeiter(pyiter *iterable, teecache *cache); T next(); }; template inline teeiter::teeiter() {} template inline teeiter::teeiter(pyiter *iterable, teecache *cache) { this->position = 0; this->iter = iterable->__iter__(); this->cache = cache; } template T teeiter::next() { if (this->position == this->cache->end) { this->cache->add(this->iter->next()); } return this->cache->get(position++); } template inline tuple2<__iter *, __iter *> *tee(pyiter *iterable, int n = 2) { teecache *cache = new teecache(n); if (n == 2) { return new tuple2<__iter *, __iter *>(n, new teeiter(iterable, cache), new teeiter(iterable, cache)); } tuple2<__iter *, __iter *>* tuple = new tuple2<__iter *, __iter *>(1, new teeiter(iterable, cache)); for (int i = 1; i < n; ++i) { tuple->units.push_back(new teeiter(iterable, cache)); } return tuple; } // takewhile template class takewhileiter : public __iter { public: bool take; B (*predicate)(T); __iter *iter; takewhileiter(); takewhileiter(B (*predicate)(T), pyiter *iterable); T next(); }; template inline takewhileiter::takewhileiter() {} template inline takewhileiter::takewhileiter(B (*predicate)(T), pyiter *iterable) { this->take = true; this->predicate = predicate; this->iter = iterable->__iter__(); } template T takewhileiter::next() { if (take) { const T& value = this->iter->next(); if (this->predicate(value)) { return value; } this->take = false; } throw new StopIteration(); } template inline takewhileiter *takewhile(B (*predicate)(T), pyiter *iterable) { return new takewhileiter(predicate, iterable); } // izip template class izipiter; template inline izipiter *izip(int iterable_count, pyiter *iterable, ...); template class izipiter : public __iter *> { public: bool exhausted; __iter *first; __iter *second; izipiter(); izipiter(pyiter *iterable1, pyiter *iterable2); tuple2 *next(); }; template inline izipiter::izipiter() { this->exhausted = true; } template inline izipiter::izipiter(pyiter *iterable1, pyiter *iterable2) { this->exhausted = false; this->first = iterable1->__iter__(); this->second = iterable2->__iter__(); } template tuple2 *izipiter::next() { if (this->exhausted) { throw new StopIteration(); } tuple2 *tuple = new tuple2; try { tuple->first = this->first->next(); tuple->second = this->second->next(); } catch (StopIteration *) { this->exhausted = true; throw; } return tuple; } template class izipiter : public __iter *> { public: bool exhausted; __GC_VECTOR(__iter *) iters; izipiter(); izipiter(pyiter *iterable); izipiter(pyiter *iterable1, pyiter *iterable2); void push_iter(pyiter *iterable); tuple2 *next(); friend izipiter *izip(int iterable_count, pyiter *iterable, ...); }; template inline izipiter::izipiter() { this->exhausted = true; } template inline izipiter::izipiter(pyiter *iterable) { this->exhausted = false; this->push_iter(iterable); } template inline izipiter::izipiter(pyiter *iterable1, pyiter *iterable2) { this->exhausted = false; this->push_iter(iterable1); this->push_iter(iterable2); } template void izipiter::push_iter(pyiter *iterable) { this->iters.push_back(iterable->__iter__()); } template tuple2 *izipiter::next() { if (this->exhausted) { throw new StopIteration(); } tuple2 *tuple = new tuple2; for (unsigned int i = 0; i < this->iters.size(); ++i) { try { tuple->units.push_back(this->iters[i]->next()); } catch (StopIteration *) { this->exhausted = true; throw; } } return tuple; } inline izipiter *izip(int /* iterable_count */) { return new izipiter(); } template inline izipiter *izip(int /* iterable_count */, pyiter *iterable1, pyiter *iterable2) { return new izipiter(iterable1, iterable2); } template inline izipiter *izip(int iterable_count, pyiter *iterable, ...) { izipiter *iter = new izipiter(iterable); va_list ap; va_start(ap, iterable); while (--iterable_count) { iter->push_iter(va_arg(ap, pyiter *)); } va_end(ap); return iter; } // izip_longest template class izip_longestiter; template inline izip_longestiter *izip_longest(int iterable_count, F fillvalue, pyiter *iterable, ...); template class izip_longestiter : public __iter *> { public: bool exhausted; bool first_exhausted; bool second_exhausted; __iter *first; __iter *second; T fillvalue; izip_longestiter(); izip_longestiter(T fillvalue, pyiter *iterable1, pyiter* iterable2); tuple2 *next(); }; template inline izip_longestiter::izip_longestiter() { this->exhausted = true; } template inline izip_longestiter::izip_longestiter(T fillvalue, pyiter *iterable1, pyiter *iterable2) { this->exhausted = false; this->first_exhausted = false; this->second_exhausted = false; this->first = iterable1->__iter__(); this->second = iterable2->__iter__(); this->fillvalue = fillvalue; } template tuple2 *izip_longestiter::next() { if (this->exhausted) { throw new StopIteration(); } tuple2 *tuple = new tuple2; if (this->first_exhausted) { tuple->first = this->fillvalue; } else { try { tuple->first = this->first->next(); } catch (StopIteration *) { if (this->second_exhausted) { this->exhausted = true; throw; } this->first_exhausted = true; tuple->first = this->fillvalue; } if (this->second_exhausted) { tuple->second = (U)this->fillvalue; return tuple; } } try { tuple->second = this->second->next(); } catch (StopIteration *) { if (this->first_exhausted) { this->exhausted = true; throw; } this->second_exhausted = true; tuple->second = (U)this->fillvalue; } return tuple; } template class izip_longestiter : public __iter *> { public: unsigned int exhausted; std::vector exhausted_iter; // never use std::vector because this is *slow* __GC_VECTOR(__iter *) iters; T fillvalue; izip_longestiter(); izip_longestiter(T fillvalue, pyiter *iterable); void push_iter(pyiter *iterable); tuple2 *next(); friend izip_longestiter *izip_longest(int iterable_count, T fillvalue, pyiter *iterable, ...); }; template inline izip_longestiter::izip_longestiter() { this->exhausted = 0; } template inline izip_longestiter::izip_longestiter(T fillvalue, pyiter *iterable) { this->exhausted = 0; this->push_iter(iterable); this->fillvalue = fillvalue; } template void izip_longestiter::push_iter(pyiter *iterable) { this->iters.push_back(iterable->__iter__()); this->exhausted_iter.push_back(0); } template tuple2 *izip_longestiter::next() { if (this->exhausted == this->iters.size()) { throw new StopIteration(); } tuple2 *tuple = new tuple2; for (unsigned int i = 0; i < this->iters.size(); ++i) { if (!this->exhausted_iter[i]) { try { tuple->units.push_back(this->iters[i]->next()); continue; } catch (StopIteration *) { ++this->exhausted; if (this->exhausted == this->iters.size()) { throw; } this->exhausted_iter[i] = 1; } } tuple->units.push_back(this->fillvalue); } return tuple; } template inline izip_longestiter *izip_longest(int /* iterable_count */, T /* fillvalue */) { return new izip_longestiter(); } template inline izip_longestiter *izip_longest(int iterable_count, F fillvalue, pyiter *iterable1, pyiter *iterable2) { return new izip_longestiter((T)fillvalue, iterable1, iterable2); } template inline izip_longestiter *izip_longest(int iterable_count, F fillvalue, pyiter *iterable, ...) { izip_longestiter *iter = new izip_longestiter((T)fillvalue, iterable); va_list ap; va_start(ap, iterable); while (--iterable_count) { iter->push_iter(va_arg(ap, pyiter *)); } va_end(ap); return iter; } /* Combinatoric generators */ // product template class productiter; template inline productiter *product(int iterable_count, int repeat, pyiter *iterable, ...); template class productiter : public __iter *> { public: bool exhausted; std::vector values1; std::vector values2; unsigned int indice1; unsigned int indice2; tuple2 *__tuple_cache; int __tuple_count; productiter(); productiter(pyiter *iterable1, pyiter *iterable2); tuple2 *next(); }; template inline productiter::productiter() { this->__tuple_cache = new tuple2[__SS_ALLOC_TUPLES]; this->__tuple_count = 0; } template inline productiter::productiter(pyiter *iterable1, pyiter *iterable2) { this->exhausted = false; this->indice1 = 0; this->indice2 = 0; // TODO this is not optimal at all for pyseq // (could be improved with static polymorphism and partial specialization on templates templates) #define CACHE_VALUES(TYPE, ID) \ __iter *iter##ID = iterable##ID->__iter__(); \ for (; ; ) { \ try { \ this->values##ID.push_back(iter##ID->next()); \ } catch (StopIteration *) { \ break; \ } \ } \ if (this->values##ID.empty()) { \ this->exhausted = true; \ return; \ } CACHE_VALUES(T, 1) CACHE_VALUES(U, 2) #undef CACHE_VALUES this->__tuple_cache = new tuple2[__SS_ALLOC_TUPLES]; this->__tuple_count = 0; } template tuple2 *productiter::next() { if (this->exhausted) { throw new StopIteration(); } tuple2 *tuple = &(this->__tuple_cache[this->__tuple_count++]); if(this->__tuple_count == __SS_ALLOC_TUPLES) { /* XXX make this more generic? */ this->__tuple_count = 0; this->__tuple_cache = new tuple2[__SS_ALLOC_TUPLES]; } tuple->first = this->values1[this->indice1]; tuple->second = this->values2[this->indice2]; ++this->indice2; if (this->indice2 == this->values2.size()) { this->indice2 = 0; ++this->indice1; if (this->indice1 == this->values1.size()) { this->exhausted = true; } } return tuple; } template class productiter : public __iter *> { public: bool exhausted; unsigned int highest_exhausted; std::vector > values; std::vector iter; std::vector indices; tuple2 *__tuple_cache; int __tuple_count; productiter(); void push_iter(pyiter *iterable); void repeat(int times); tuple2 *next(); friend productiter *product(int iterable_count, int repeat, pyiter *iterable, ...); }; template inline productiter::productiter() { this->exhausted = false; this->highest_exhausted = 0; this->__tuple_cache = new tuple2[__SS_ALLOC_TUPLES]; this->__tuple_count = 0; } template void productiter::push_iter(pyiter *iterable) { this->values.push_back(std::vector()); this->indices.push_back(0); // TODO this is not optimal at all for pyseq // (could be improved with static polymorphism and partial specialization on templates templates) __iter *iter = iterable->__iter__(); for (; ; ) { try { this->values.back().push_back(iter->next()); } catch (StopIteration *) { break; } } if (!this->values.back().size()) { this->exhausted = true; } else if (this->values.back().size() == 1 && this->highest_exhausted == this->values.size() - 1) { ++this->highest_exhausted; } } template inline void productiter::repeat(int times) { if (this->highest_exhausted == this->values.size()) { this->highest_exhausted *= times; } for (int time = 0; time < times; ++time) { for (int iter = 0; iter < this->values.size(); ++iter) { this->iter.push_back(iter); this->indices.push_back(0); } } } template tuple2 *productiter::next() { if (this->exhausted) { throw new StopIteration(); } tuple2 *tuple = &(this->__tuple_cache[this->__tuple_count++]); if(this->__tuple_count == __SS_ALLOC_TUPLES) { /* XXX make this more generic? */ this->__tuple_count = 0; this->__tuple_cache = new tuple2[__SS_ALLOC_TUPLES]; } if (this->iter.size()) { size_t iter_size = this->iter.size(); tuple->units.resize(iter_size); for (size_t i = 0; i < iter_size; ++i) { int j = this->iter[i]; tuple->units[i] = this->values[j][this->indices[i]]; } for (int i = this->iter.size() - 1; i > -1; --i) { int j = this->iter[i]; ++this->indices[i]; if (i <= (int)this->highest_exhausted) { if (this->indices[i] >= this->values[j].size() - 1) { ++this->highest_exhausted; if (this->highest_exhausted > this->iter.size()) { this->exhausted = true; } break; } } if (this->indices[i] == this->values[j].size()) { this->indices[i] = 0; } else { break; } } } else { this->exhausted = true; } return tuple; } inline productiter *product(int /* iterable_count */, int /* repeat */) { return new productiter(); } template inline productiter *product(int /* iterable_count */, int repeat, pyiter *iterable1, pyiter *iterable2) { productiter *iter = new productiter(); iter->push_iter(iterable1); iter->push_iter(iterable2); iter->repeat(repeat); return iter; } template inline productiter *product(int /* iterable_count */, int /* repeat */, pyiter *iterable1, pyiter *iterable2) { return new productiter(iterable1, iterable2); } template inline productiter *product(int iterable_count, int repeat, pyiter *iterable, ...) { productiter *iter = new productiter(); int iter_count = iterable_count; iter->push_iter(iterable); va_list ap; va_start(ap, iterable); while (--iter_count) { iter->push_iter(va_arg(ap, pyiter *)); } va_end(ap); iter->repeat(repeat); return iter; } // permutations template class permutationsiter : public __iter *> { public: int r; int len; int current; unsigned int* indices; unsigned int* cycles; std::vector cache; permutationsiter(); permutationsiter(pyiter *iterable, __ss_int r); ~permutationsiter(); tuple2 *next(); private: // We might want to implement this, but we certainly don't want the default ones permutationsiter(const permutationsiter& other); permutationsiter& operator=(const permutationsiter& other); }; template inline permutationsiter::permutationsiter() { this->indices = 0; this->cycles = 0; } template inline permutationsiter::permutationsiter(pyiter *iterable, __ss_int r) { this->r = r; this->len = 0; // TODO this is not optimal at all for pyseq // (could be improved with static polymorphism and partial specialization on templates templates) __iter *iter = iterable->__iter__(); for (; ; ) { try { this->cache.push_back(iter->next()); } catch (StopIteration *) { break; } } this->len = this->cache.size(); if (r > this->len) { this->current = -1; this->indices = 0; this->cycles = 0; } else { this->current = this->r; this->indices = new unsigned int[this->len]; this->cycles = new unsigned int[this->r]; for (int i = 0; i < this->len; ++i) { this->indices[i] = i; } for (int i = 0; i < this->r; ++i) { this->cycles[i] = this->len - i; } } } template inline permutationsiter::~permutationsiter() { delete[] this->indices; delete[] this->cycles; } template tuple2 *permutationsiter::next() { if (this->current == this->r) { tuple2 *tuple = new tuple2; for (int i = 0; i < this->r; ++i) { tuple->units.push_back(this->cache[i]); } --this->current; return tuple; } for (; ; ) { if (this->current == -1) { throw new StopIteration(); } int cycle = --this->cycles[this->current]; if (cycle) { assert(this->current < this->len); std::swap(this->indices[this->current], this->indices[cycle ? this->len - cycle : 0]); tuple2 *tuple = new tuple2; for (int i = 0; i < this->r; ++i) { tuple->units.push_back(this->cache[this->indices[i]]); } this->current = this->r - 1; return tuple; } else { int last = this->indices[this->current]; for (int i = this->current; i < this->len - 1; ++i) { this->indices[i] = this->indices[i + 1]; } this->indices[this->len - 1] = last; this->cycles[this->current] = this->len - this->current; --this->current; } } } template inline permutationsiter *permutations(pyiter *iterable, void* /* r */) { return new permutationsiter(iterable, iterable->__len__()); } template inline permutationsiter *permutations(pyiter *iterable, __ss_int r) { return new permutationsiter(iterable, r); } // combinations template class combinationsiter : public __iter *> { public: int r; int len; int current; int* indices; __GC_VECTOR(T) cache; combinationsiter(); combinationsiter(pyiter *iterable, int r); ~combinationsiter(); tuple2 *next(); private: // We might want to implement this, but we certainly don't want the default ones combinationsiter(const combinationsiter& other); combinationsiter& operator=(const combinationsiter& other); }; template inline combinationsiter::combinationsiter() { this->indices = 0; } template inline combinationsiter::combinationsiter(pyiter *iterable, int r) { this->r = r; this->len = 0; // TODO this is not optimal at all for pyseq // (could be improved with static polymorphism and partial specialization on templates templates) __iter *iter = iterable->__iter__(); for (; ; ) { try { this->cache.push_back(iter->next()); } catch (StopIteration *) { break; } } this->len = this->cache.size(); if (r > this->len) { this->current = -1; this->indices = 0; } else { this->current = r; this->indices = new int[r]; for (int i = 0; i < this->r; ++i) { this->indices[i] = i; } } } template inline combinationsiter::~combinationsiter() { delete[] this->indices; } template tuple2 *combinationsiter::next() { if (this->current == this->r) { tuple2 *tuple = new tuple2; for (int i = 0; i < this->r; ++i) { tuple->units.push_back(this->cache[i]); } --this->current; return tuple; } for (; ; ) { if (this->current == -1) { throw new StopIteration(); } while (this->indices[this->current] == this->current + this->len - this->r) { --this->current; if (this->current == -1) { throw new StopIteration(); } } ++this->indices[this->current]; for (int i = this->current + 1; i < this->r; ++i) { this->indices[i] = this->indices[i - 1] + 1; } tuple2 *tuple = new tuple2; for (int i = 0; i < this->r; ++i) { tuple->units.push_back(this->cache[this->indices[i]]); } this->current = this->r - 1; return tuple; } } template inline combinationsiter *combinations(pyiter *iterable, int r) { return new combinationsiter(iterable, r); } // combinations_with_replacement template class combinations_with_replacementiter : public __iter *> { public: int r; int len; int current; int* indices; __GC_VECTOR(T) cache; combinations_with_replacementiter(); combinations_with_replacementiter(pyiter *iterable, int r); ~combinations_with_replacementiter(); tuple2 *next(); private: // We might want to implement this, but we certainly don't want the default ones combinations_with_replacementiter(const combinations_with_replacementiter& other); combinations_with_replacementiter& operator=(const combinations_with_replacementiter& other); }; template inline combinations_with_replacementiter::combinations_with_replacementiter() { this->indices = 0; } template inline combinations_with_replacementiter::combinations_with_replacementiter(pyiter *iterable, int r) { this->r = r; this->len = 0; // TODO this is not optimal at all for pyseq // (could be improved with static polymorphism and partial specialization on templates templates) __iter *iter = iterable->__iter__(); for (; ; ) { try { this->cache.push_back(iter->next()); } catch (StopIteration *) { break; } } this->len = this->cache.size(); if (!this->len && r) { this->current = -1; this->indices = 0; } else { this->current = r; this->indices = new int[r]; for (int i = 0; i < this->r; ++i) { this->indices[i] = 0; } } } template inline combinations_with_replacementiter::~combinations_with_replacementiter() { delete[] this->indices; } template tuple2 *combinations_with_replacementiter::next() { if (this->current == this->r) { tuple2 *tuple = new tuple2; for (int i = 0; i < this->r; ++i) { tuple->units.push_back(this->cache[0]); } --this->current; return tuple; } for (; ; ) { if (this->current == -1) { throw new StopIteration(); } while (this->indices[this->current] == this->len - 1) { --this->current; if (this->current == -1) { throw new StopIteration(); } } ++this->indices[this->current]; for (int i = this->current + 1; i < this->r; ++i) { this->indices[i] = this->indices[this->current]; } tuple2 *tuple = new tuple2; for (int i = 0; i < this->r; ++i) { tuple->units.push_back(this->cache[this->indices[i]]); } this->current = this->r - 1; return tuple; } } template inline combinations_with_replacementiter *combinations_with_replacement(pyiter *iterable, int r) { return new combinations_with_replacementiter(iterable, r); } void __init(); } // module namespace #endif shedskin-0.9.4/shedskin/lib/builtin.py0000664000175000017500000004476512157270661017677 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) class class_: def __repr__(self): return self.__name__ class int_: def __add__(self, b): return b.__with_int__() def __sub__(self, b): return b.__with_int__() def __mul__(self, b): return b.__with_int__() def __div__(self, b): return b.__with_int__() def __floordiv__(self, b): return b.__with_int__() def __mod__(self, b): return b.__with_int__() def __divmod__(self, b): return (b.__with_int__(),) def __and__(self, b): return 1 def __or__(self, b): return 1 def __xor__(self, b): return 1 def __rshift__(self, b): return 1 def __lshift__(self, b): return 1 def __invert__(self): return 1 def __neg__(self): return 1 def __pos__(self): return 1 def __hash__(self): return 1 def __abs__(self): return 1 def __pow__(self, b): return b def __copy__(self): return self def __deepcopy__(self): return self def __with_int__(self): return 1 def __with_float__(self): return 1.0 def __with_bool__(self): return 1 def __repr__(self): return '' class bool_: def __add__(self, b): return b.__with_int__() def __sub__(self, b): return b.__with_int__() def __mul__(self, b): return b.__with_int__() def __div__(self, b): return b.__with_int__() def __floordiv__(self, b): return b.__with_int__() def __mod__(self, b): return b.__with_int__() def __divmod__(self, b): return (b.__with_int__(),) def __and__(self, b): return b.__with_bool__() def __or__(self, b): return b.__with_bool__() def __xor__(self, b): return b.__with_bool__() def __rshift__(self, b): return 1 def __lshift__(self, b): return 1 def __invert__(self): return 1 def __neg__(self): return 1 def __pos__(self): return 1 def __hash__(self): return 1 def __abs__(self): return 1 def __pow__(self, b): return b def __copy__(self): return self def __deepcopy__(self): return self def __with_int__(self): return 1 def __with_float__(self): return 1.0 def __with_bool__(self): return self def __repr__(self): return '' class float_: def __add__(self, b): return b.__with_float__() def __sub__(self, b): return b.__with_float__() def __mul__(self, b): return b.__with_float__() def __div__(self, b): return b.__with_float__() def __floordiv__(self, b): return b.__with_float__() def __mod__(self, b): return b.__with_float__() def __divmod__(self, b): return (b.__with_float__(),) def __pow__(self, b): return b.__with_float__() def is_integer(self): return True def __neg__(self): return 1.0 def __pos__(self): return 1.0 def __abs__(self): return 1.0 def __hash__(self): return 1 def __copy__(self): return self def __deepcopy__(self): return self def __with_int__(self): return 1.0 def __with_float__(self): return 1.0 def __repr__(self): return '' class none: def __hash__(self): return 1 class pyiter: def __init__(self, i=None): pass def __inititer__(self, i): self.unit = iter(i).next() def __iter__(self): return __iter(self.unit) def __copy__(self): # XXX to base class return self def __deepcopy__(self): return self def __with_int__(self): return self def __contains__(self, x): x == self.unit return True class pyseq(pyiter): pass class list(pyseq): def append(self, u): self.unit = u def index(self, u, s=0, e=0): u == self.unit return 1 def count(self, u): u == self.unit return 1 def remove(self, u): u == self.unit def __getitem__(self, i): return self.unit def __setitem__(self, i, u): self.unit = u def __delitem__(self, i): pass def __len__(self): return 1 def __add__(self, b): return self return b def __mul__(self, b): return self def __iadd__(self, b): self.unit = b.unit return self def __imul__(self, n): return self def __slice__(self, x, lower, upper, step): return self def __delslice__(self, a, b): pass def __setslice__(self, x, lower, upper, step, r): self.unit = r.unit def __delete__(self, x, a=1, b=1, s=1): pass def __repr__(self): self.unit.__repr__() return '' def __str__(self): return self.__repr__() def extend(self, other): self.unit = other.unit def insert(self, i, u): self.unit = u def pop(self, m=0): return self.unit def reverse(self): pass def sort(self, cmp=0, key=0, reverse=0): elem = self.unit cmp(elem, elem) cmp2(elem, elem) key(elem) class tuple(pyseq): def __len__(self): return 1 def __repr__(self): self.unit.__repr__() return '' def __str__(self): return self.__repr__() def __add__(self, b): a = self.unit a = b.unit return (a,) def __mul__(self, b): return self def __getitem__(self, i): a = self.unit return a def __slice__(self, x, l, u, s): return self def __hash__(self): return 1 class tuple2(pyseq): def __len__(self): return 1 def __repr__(self): self.first.__repr__() self.second.__repr__() return '' def __str__(self): return self.__repr__() def __add__(self, b): a = self.unit a = b.unit return (a,) def __mul__(self, b): return (self.unit,) def __getitem__(self, i): return self.unit def __getfirst__(self, i): return self.first def __getsecond__(self, i): return self.second def __hash__(self): return 1 class str_(pyseq): def strip(self, chars=''): return '' def lstrip(self, chars=''): return '' def rstrip(self, chars=''): return '' def istitle(self): return True def splitlines(self, c=0): return [''] def partition(self, sep): return ('',) def rpartition(self, sep): return ('',) def rsplit(self, sep='', c=-1): return [''] def split(self, sep='',c=-1): return [''] def join(self, l): return self def __getitem__(self, i): return '' def __mul__(self, n): return '' def __repr__(self): return '' def __mod__(self, a=None): a = a.unit a.__str__() a.__repr__() return '' def __add__(self,be): return '' def __len__(self): return 1 def upper(self): return '' def lower(self): return '' def title(self): return '' def capitalize(self): return '' def find(self, sub, s=0, e=0): return 1 def rfind(self, sub, s=0, e=0): return 1 def index(self, sub, s=0, e=0): return 1 def rindex(self, sub, s=0, e=0): return 1 def isdigit(self): return True def islower(self): return True def isupper(self): return True def isalpha(self): return True def isspace(self): return True def isalnum(self): return True def zfill(self, width): return '' def ljust(self, width, chars=''): return '' def rjust(self, width, chars=''): return '' def expandtabs(self, width=8): return '' def count(self, e, start=0, end=0): return 1 def startswith(self, e, start=0, end=0): return True def endswith(self, e, start=0, end=0): return True def replace(self, a, b, c=0): return '' def translate(self, table, delchars=''): return '' def swapcase(self): return '' def center(self, w, fill=''): return '' def __slice__(self, x, l, u, s): return self def __hash__(self): return 1 class dict(pyiter): def __initdict__(self, other): self.__setunit__(other.unit, other.value) def __inititer__(self, other): item = iter(other).next() self.__setunit__(item[0], item[1]) def __repr__(self): self.unit.__repr__() self.value.__repr__() return '' def __str__(self): return self.__repr__() def __key__(self, k): k.__hash__() k.__eq__(k) def __setunit__(self, k, v): self.__key__(k) self.unit = k self.value = v def __setitem__(self, u, v): self.__setunit__(u, v) def __getitem__(self, k): self.__key__(k) return self.value def __delitem__(self, k): self.__key__(k) def setdefault(self, u, v=None): self.__setunit__(u, v) return v def keys(self): return [self.unit] def values(self): return [self.value] def items(self): return [(self.unit, self.value)] def has_key(self, u): self.__key__(u) return True def __len__(self): return 1 def clear(self): pass def copy(self): return {self.unit: self.value} def get(self, u, v=None): self.__key__(u) return self.value return v def pop(self, u): self.__key__(u) return self.value def popitem(self): return (self.unit, self.value) def update(self, d): self.__setunit__(d.unit, d.value) def updateiter(self, other): item = iter(other).next() self.__setunit__(item[0], item[1]) def __delete__(self, k): self.__key__(k) def fromkeys(l, b=None): return {l.unit: b} fromkeys = staticmethod(fromkeys) # XXX classmethod def iterkeys(self): return __iter(self.unit) def itervalues(self): return __iter(self.value) def iteritems(self): return __iter((self.unit, self.value)) class pyset(pyiter): def __inititer__(self, i): self.__setunit__(iter(i).next()) def __setunit__(self, unit): self.unit = unit unit.__hash__() unit.__eq__(unit) def issubset(self, b): return True def issuperset(self, b): return True def isdisjoint(self, b): return True def intersection(self, *b): return self def difference(self, *b): return self def symmetric_difference(self, b): return self return b def __sub__(self, b): return self def __and__(self, b): return self def __or__(self, b): return self def __xor__(self, b): return self def __ior__(self, b): self.__setunit__(iter(b).next()) return self def __iand__(self, b): self.__setunit__(iter(b).next()) return self def __ixor__(self, b): self.__setunit__(iter(b).next()) return self def __isub__(self, b): self.__setunit__(iter(b).next()) return self def union(self, *b): return self return set(b) def copy(self): return self def __hash__(self): return 1 def __len__(self): return 1 def __repr__(self): self.unit.__repr__() return '' class frozenset(pyset): pass class set(pyset): def add(self, u): self.__setunit__(u) def discard(self, u): pass def remove(self, u): pass def pop(self): return self.unit def clear(self): pass def update(self, *b): self.__setunit__(iter(b).next()) def difference_update(self, *b): self.__setunit__(iter(b).next()) def symmetric_difference_update(self, b): self.__setunit__(iter(b).next()) def intersection_update(self, *b): self.__setunit__(iter(b).next()) class complex: def __init__(self, real=None, imag=None): real.__float__() self.real = self.imag = 1.0 def __add__(self, c): return self def __sub__(self, c): return self def __mul__(self, c): return self def __div__(self, c): return self def __floordiv__(self, b): return self def __mod__(self, b): return self def __divmod__(self, b): return (self,) def __pos__(self): return self def __neg__(self): return self def __abs__(self): return 1.0 def conjugate(self): return self def __pow__(self, b): return self def __hash__(self): return 1 def __with_int__(self): return self def __with_float__(self): return self def __repr__(self): return '' complex(1.0, 1.0) class object: pass class BaseException: def __init__(self, msg=None): self.msg = msg # XXX needed? self.message = msg class Exception(BaseException): pass class StandardError(Exception): pass class AssertionError(StandardError): pass class EOFError(StandardError): pass class FloatingPointError(StandardError): pass class IndexError(StandardError): pass class IOError(StandardError): pass class KeyError(StandardError): pass class MemoryError(StandardError): pass class NameError(StandardError): pass class NotImplementedError(StandardError): pass class OSError(StandardError): pass class OverflowError(StandardError): pass class RuntimeError(StandardError): pass class SyntaxError(StandardError): pass class SystemError(StandardError): pass class TypeError(StandardError): pass class ValueError(StandardError): pass class ZeroDivisionError(StandardError): pass class StopIteration(Exception): pass class GeneratorExit(BaseException): pass class KeyboardInterrupt(BaseException): pass class SystemExit(BaseException): pass __exception = OSError('') # XXX remove __exception = IOError('') __exception.errno = 0 __exception.filename = '' __exception.strerror = '' __exception2 = SystemExit('') __exception2.code = 1 def str(x=None): x.__str__() x.__repr__() return '' def int(x=None, base=1): x.__int__() return 1 def float(x=None): x.__float__() return 1.0 def hex(x): x.__hex__() return '' def oct(x): x.__oct__() return '' def bin(x): x.__index__() return '' def isinstance(a, b): return True def range(a, b=1, s=1): return [1] def raw_input(msg=''): return '' class file(pyiter): def __init__(self, name, flags=None): self.unit = '' self.closed = 0 self.name = '' self.mode = '' def read(self, size=0): return '' def readline(self, n=-1): return '' def readlines(self, sizehint=-1): return [''] def xreadlines(self): return iter(self) def write(self, s): pass def writelines(self, it): pass def seek(self, i, w=0): pass def tell(self): return 1 def flush(self): pass def close(self): pass def fileno(self): return 1 def __repr__(self): return '' def isatty(self): return False def truncate(self, size=-1): pass def next(self): return '' def open(name, flags=None): return file(name, flags) def ord(c): return 1 def chr(i): return 'c' def round(x, n=0): return 1.0 def divmod(a, b): return a.__divmod__(b) def bool(x): x.__nonzero__() x.__len__() return True def repr(x): return x.__repr__() def hash(x): return x.__hash__() def len(w): return w.__len__() def pow(a, b, c=1): return a.__pow__(b) def abs(x): return x.__abs__() def sorted(it, cmp=0, key=0, reverse=0): elem = iter(it).next() cmp(elem, elem) cmp2(elem, elem) key(elem) return [elem] def reversed(l): return iter(l) def enumerate(x, start=0): return __iter((1, iter(x).next())) class __xrange: def __init__(self): self.unit = 1 def __iter__(self): return __iter(1) def __len__(self): return 1 def xrange(a, b=1, s=1): return __xrange() def zip(*args): return [(iter(args).next(),)] def __zip2(arg1, arg2): return [(iter(arg1).next(), iter(arg2).next())] def max(__kw_key=0, *arg): # XXX 0 cmp(arg, arg) __kw_key(arg) return arg def __max1(arg, __kw_key=0): elem = iter(arg).next() cmp(elem, elem) __kw_key(elem) return elem def min(__kw_key=0, *arg): # XXX 0 cmp(arg, arg) __kw_key(arg) return arg def __min1(arg, __kw_key=0): elem = iter(arg).next() cmp(elem, elem) __kw_key(elem) return elem def sum(l, b): return sum(l) return b def __sum1(l): elem = iter(l).next() elem.__add__(elem) return elem def cmp(a, b): a.__cmp__(b) a.__eq__(b) a.__lt__(b) a.__gt__(b) return 1 def cmp2(a, b): cmp(a, b) def __lt(a, b): a.__lt__(b) b.__gt__(a) def __gt(a, b): a.__gt__(b) b.__lt__(a) def __le(a, b): a.__le__(b) b.__ge__(a) def __ge(a, b): a.__ge__(b) b.__le__(a) def any(a): return True def all(a): return True class __iter(pyiter): def __init__(self, a): self.unit = a def next(self): return self.unit def __iter__(self): return self def __len__(self): # xrange and such return 1 def __getitem__(self, i): # modeling shortcut return self.unit def iter(a): return a.__iter__() def exit(code=0): pass def quit(code=0): pass def map(func, *iter1): return [func(*iter(iter1).next())] def __map3(func, iter1, iter2): return [func(iter(iter1).next(), iter(iter2).next())] def __map4(func, iter1, iter2, iter3): # XXX return [func(iter(iter1).next(), iter(iter2).next(), iter(iter3).next())] def filter(func, iter1): elem = iter(iter1).next() func(elem) return [elem] def __filter(func, iter1): func(iter(iter1).next()) return iter1 def reduce(func, iter1, init=None): elem = iter(iter1).next() # elem = init elem = func(elem, elem) return elem def next(iter1, fillvalue=None): return iter1.next() return fillvalue def id(x): return 1 def __print(__kw_sep=0, __kw_end=0, __kw_file=0, *value): value.__str__() shedskin-0.9.4/shedskin/lib/socket.hpp0000664000175000017500000000747712157270661017657 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* * Implementation of the Python 2.5.1 socket module for Shed Skin * by: Michael Elkins * February 25, 2008 * * Current Issues: * - unix domain sockets are not implemented * - can't call socket.settimeout(None) */ #ifndef __SOCKET_HPP #define __SOCKET_HPP #include "builtin.hpp" #ifdef WIN32 #include #define WIN32_LEAN_AND_MEAN #include #define socklen_t int typedef SOCKET socket_type; #else #include #include typedef int socket_type; extern int __ss_AI_PASSIVE; #endif /* WIN32 */ using namespace __shedskin__; namespace __socket__ { class error; class herror; class gaierror; class timeout; class socket; extern __ss_int default_0; extern __ss_int default_1; extern str *__name__; extern class_ *cl_error; class error : public Exception { public: error(str *msg=0) : Exception(msg) { __class__ = cl_error; } }; extern class_ *cl_herror; class herror : public Exception { public: herror(str *msg=0) : Exception(msg) { __class__ = cl_herror; } }; extern class_ *cl_gaierror; class gaierror : public Exception { public: gaierror(str *msg=0) : Exception(msg) { __class__ = cl_gaierror; } }; extern class_ *cl_timeout; class timeout : public Exception { public: timeout(str *msg=0) : Exception(msg) { __class__ = cl_timeout; } }; extern class_ *cl_socket; class socket : public object { double _timeout; bool _blocking; socket_type _fd; void read_wait(); void write_wait(); int send(const char *s, size_t len, int flags=0); socket *bind(const sockaddr *, socklen_t); socket *connect(const sockaddr *, socklen_t); socket *accept(sockaddr *, socklen_t *); ssize_t recvfrom(char *, size_t, int, sockaddr *, socklen_t *); public: __ss_int family; __ss_int proto; __ss_int type; typedef tuple2 *inet_address; socket(__ss_int family=2, __ss_int type=1, __ss_int proto=0); ~socket(); __ss_int __ss_fileno(); str *getsockopt(__ss_int level, __ss_int optname, __ss_int value); socket *bind(inet_address address); socket *bind(pyseq *address); file *makefile(str *flags=NULL); socket *setsockopt(__ss_int level, __ss_int optname, __ss_int value); socket *connect(inet_address address); socket *connect(pyseq *address); socket *setblocking(__ss_int flag); socket *shutdown(__ss_int how); __ss_int send(str *string, __ss_int flags=0); __ss_int sendall(str *string, __ss_int flags=0); __ss_int sendto(str *string, __ss_int flags, inet_address addr); __ss_int sendto(str *string, inet_address addr); socket *close(); socket *settimeout(double value); double gettimeout() { return _timeout; } str *recv(__ss_int bufsize, __ss_int flags=0); tuple2 *recvfrom(__ss_int bufsize, __ss_int flags=0); socket *listen(__ss_int backlog); inet_address getpeername(); inet_address getsockname(); //INET tuple2 *accept(); }; extern str * __name__; void __init(); str *gethostbyname(str *hostname); str *inet_aton(str *x); str *inet_ntoa(str *x); __ss_int __ss_htonl(__ss_int); __ss_int __ss_htons(__ss_int); __ss_int __ss_ntohl(__ss_int); __ss_int __ss_ntohs(__ss_int); __ss_bool has_ipv6(); double getdefaulttimeout(); void *setdefaulttimeout(double x); str *gethostname(); extern __ss_int __ss_SOCK_STREAM, __ss_AF_INET, __ss_AF_INET6, __ss_AF_UNIX, __ss_SOCK_DGRAM, __ss_SOL_IP, __ss_SOL_SOCKET, __ss_IP_TOS, __ss_IP_TTL; extern __ss_int __ss_SHUT_RD, __ss_SHUT_WR, __ss_SHUT_RDWR, __ss_SOMAXCONN, __ss_SO_REUSEADDR; extern __ss_int __ss_INADDR_ANY, __ss_INADDR_LOOPBACK, __ss_INADDR_NULL, __ss_INADDR_BROADCAST; } // module namespace #endif shedskin-0.9.4/shedskin/lib/builtin/0000775000175000017500000000000012157270661017305 5ustar srepmubsrepmubshedskin-0.9.4/shedskin/lib/builtin/format.cpp0000664000175000017500000002451512157270661021310 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* mod helpers */ #if defined(WIN32) || defined(__sun) # if defined (_MSC_VER) # define va_copy(dest, src) ((void)((dest) = (src))) # endif int vasprintf(char **ret, const char *format, va_list ap) { va_list ap2; int len= 100; /* First guess at the size */ if ((*ret= (char *)malloc(len)) == NULL) return -1; while (1) { int nchar; va_copy(ap2, ap); nchar= vsnprintf(*ret, len, format, ap2); if (nchar > -1 && nchar < len) return nchar; if (nchar > len) len= nchar+1; else len*= 2; if ((*ret= (char *)realloc(*ret, len)) == NULL) { free(*ret); return -1; } } } int asprintf(char **ret, const char *format, ...) { va_list ap; int nc; va_start (ap, format); nc = vasprintf(ret, format, ap); va_end(ap); return nc; } #endif int __fmtpos(str *fmt) { int i = fmt->unit.find('%'); if(i == -1) return -1; return fmt->unit.find_first_not_of(__fmtchars, i+1); } int __fmtpos2(str *fmt) { unsigned int i = 0; while((i = fmt->unit.find('%', i)) != -1) { if(i != fmt->unit.size()-1) { char nextchar = fmt->unit[i+1]; if(nextchar == '%') i++; else if(nextchar == '(') return i; } i++; } return -1; } template str *do_asprintf(const char *fmt, T t, pyobj *a1, pyobj *a2) { char *d; int x; str *r; if(a2) x = asprintf(&d, fmt, ((int)(((int_ *)a1)->unit)), ((int)(((int_ *)a2)->unit)), t); else if(a1) x = asprintf(&d, fmt, ((int)(((int_ *)a1)->unit)), t); else x = asprintf(&d, fmt, t); r = new str(d); free(d); return r; } /* XXX deal with null-chars.. ugh */ str *do_asprintf_str(const char *fmt, str *s, pyobj *a1, pyobj *a2) { char *d; int x; str *r; int nullchars = (s->unit.find('\0') != -1); /* XXX %6.s */ ssize_t len = s->unit.size(); str *old_s = s; if(nullchars) { s = new str(s->unit); std::replace(s->unit.begin(), s->unit.end(), '\0', ' '); } if(a2) x = asprintf(&d, fmt, ((int)(((int_ *)a1)->unit)), ((int)(((int_ *)a2)->unit)), s->unit.c_str()); else if(a1) x = asprintf(&d, fmt, ((int)(((int_ *)a1)->unit)), s->unit.c_str()); else x = asprintf(&d, fmt, s->unit.c_str()); if(nullchars) { for(int i=0; iunit[i] == '\0') d[i] = '\0'; } r = new str(d, x); free(d); return r; } void __modfill(str **fmt, pyobj *t, str **s, pyobj *a1, pyobj *a2) { char c; int i = (*fmt)->unit.find('%'); int j = __fmtpos(*fmt); *s = new str((*s)->unit + (*fmt)->unit.substr(0, i)); str *add; c = (*fmt)->unit[j]; if(c == 's' or c == 'r') { if(c == 's') add = __str(t); else add = repr(t); (*fmt)->unit[j] = 's'; add = do_asprintf_str((*fmt)->unit.substr(i, j+1-i).c_str(), add, a1, a2); } else if(c == 'c') add = __str(t); else if(c == '%') add = new str("%"); else if(t->__class__ == cl_int_) { #ifdef __SS_LONG add = do_asprintf(((*fmt)->unit.substr(i, j-i)+__GC_STRING("ll")+(*fmt)->unit[j]).c_str(), ((int_ *)t)->unit, a1, a2); #else add = do_asprintf((*fmt)->unit.substr(i, j+1-i).c_str(), ((int_ *)t)->unit, a1, a2); #endif } else { /* cl_float_ */ if(c == 'H') { (*fmt)->unit.replace(j, 1, ".12g"); j += 3; } add = do_asprintf((*fmt)->unit.substr(i, j+1-i).c_str(), ((float_ *)t)->unit, a1, a2); if(c == 'H' && ((float_ *)t)->unit-((int)(((float_ *)t)->unit)) == 0) add->unit += ".0"; } *s = (*s)->__add__(add); *fmt = new str((*fmt)->unit.substr(j+1, (*fmt)->unit.size()-j-1)); } pyobj *modgetitem(list *vals, int i) { if(i==len(vals)) throw new TypeError(new str("not enough arguments for format string")); return vals->__getitem__(i); } str *__mod4(str *fmts, list *vals) { int i, j; str *r = new str(); str *fmt = new str(fmts->unit); i = 0; while((j = __fmtpos(fmt)) != -1) { pyobj *p, *a1, *a2; int perc_pos = fmt->unit.find('%'); int asterisks = std::count(fmt->unit.begin()+perc_pos+1, fmt->unit.begin()+j, '*'); a1 = a2 = NULL; if(asterisks==1) { a1 = modgetitem(vals, i++); } else if(asterisks==2) { a1 = modgetitem(vals, i++); a2 = modgetitem(vals, i++); } char c = fmt->unit[j]; if(c != '%') p = modgetitem(vals, i++); switch(c) { case 'c': __modfill(&fmt, mod_to_c2(p), &r, a1, a2); break; case 's': case 'r': __modfill(&fmt, p, &r, a1, a2); break; case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': __modfill(&fmt, mod_to_int(p), &r, a1, a2); break; case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': case 'H': __modfill(&fmt, mod_to_float(p), &r, a1, a2); break; case '%': __modfill(&fmt, NULL, &r, a1, a2); break; default: throw new ValueError(new str("unsupported format character")); } } if(i!=len(vals)) throw new TypeError(new str("not all arguments converted during string formatting")); r->unit += fmt->unit; return r; } str *__mod5(list *vals, str *sep) { __mod5_cache->units.resize(0); for(int i=0;i__getitem__(i); if(p == NULL) __mod5_cache->append(__fmt_s); else if(p->__class__ == cl_float_) __mod5_cache->append(__fmt_H); else if(p->__class__== cl_int_) __mod5_cache->append(__fmt_d); else __mod5_cache->append(__fmt_s); } str *s = __mod4(sep->join(__mod5_cache), vals); return s; } str *__modcd(str *fmt, list *names, ...) { int i; list *vals = new list(); va_list args; va_start(args, names); for(i=0; iappend(va_arg(args, pyobj *)); va_end(args); str *naam; int pos, pos2; dict *d = new dict(__zip(2, names, vals)); str *const_6 = new str(")"); list *values = new list(); while((pos = __fmtpos2(fmt)) != -1) { pos2 = fmt->find(const_6, pos); naam = fmt->__slice__(3, (pos+2), pos2, 0); values->append(d->__getitem__(naam)); fmt = (fmt->__slice__(2, 0, (pos+1), 0))->__add__(fmt->__slice__(1, (pos2+1), 0, 0)); } return __mod4(fmt, values); } /* mod */ str *mod_to_c2(pyobj *t) { if(t == NULL) throw new TypeError(new str("an integer is required")); if(t->__class__ == cl_str_) { if(len((str *)t) == 1) return (str *)t; else throw new TypeError(new str("%c requires int or char")); } int value; if(t->__class__ == cl_int_) value = ((int_ *)t)->unit; else if(t->__class__ == cl_float_) value = ((int)(((float_ *)t)->unit)); else value = t->__int__(); if(value < 0) throw new OverflowError(new str("unsigned byte integer is less than minimum")); else if(value > 255) throw new OverflowError(new str("unsigned byte integer is greater than minimum")); return chr(value); } int_ *mod_to_int(pyobj *t) { if(t == NULL) throw new TypeError(new str("int argument required")); if(t->__class__ == cl_int_) return (int_ *)t; else if(t->__class__ == cl_float_) return new int_(((int)(((float_ *)t)->unit))); else return new int_(t->__int__()); } float_ *mod_to_float(pyobj *t) { if(t == NULL) throw new TypeError(new str("float argument required")); if(t->__class__ == cl_float_) return (float_ *)t; else if(t->__class__ == cl_int_) return new float_(((int_ *)t)->unit); throw new TypeError(new str("float argument required")); } str *__modct(str *fmt, int n, ...) { list *vals = new list(); va_list args; va_start(args, n); for(int i=0; iappend(va_arg(args, pyobj *)); va_end(args); str *s = __mod4(fmt, vals); return s; } /* print .., */ void print(int n, file *f, str *end, str *sep, ...) { __print_cache->units.resize(0); va_list args; va_start(args, sep); for(int i=0; iappend(va_arg(args, pyobj *)); va_end(args); str *s = __mod5(__print_cache, sep?sep:sp); if(!end) end = nl; if(f) { f->write(s); f->write(end); } else printf("%s%s", s->unit.c_str(), end->unit.c_str()); } void print2(file *f, int comma, int n, ...) { __print_cache->units.resize(0); va_list args; va_start(args, n); for(int i=0; iappend(va_arg(args, pyobj *)); va_end(args); if (!f) f = __ss_stdout; __file_options *p_opt = &f->options; str *s = __mod5(__print_cache, sp); if(len(s)) { if(p_opt->space && (!isspace(p_opt->lastchar) || p_opt->lastchar==' ') && s->unit[0] != '\n') f->write(sp); /* space */ f->write(s); p_opt->lastchar = s->unit[len(s)-1]; } else if (comma) p_opt->lastchar = ' '; if(!comma) { f->write(nl); /* newline */ p_opt->lastchar = '\n'; } p_opt->space = comma; } #ifdef __SS_LONG int_ *___box(__ss_int i) { return new int_(i); } #endif int_ *___box(int i) { return new int_(i); } int_ *___box(unsigned int i) { return new int_(i); } int_ *___box(long i) { return new int_(i); } int_ *___box(unsigned long i) { return new int_(i); } int_ *___box(unsigned long long i) { return new int_(i); } bool_ *___box(__ss_bool b) { return new bool_(b); } float_ *___box(double d) { return new float_(d); } complex_ *___box(complex c) { return new complex_(c); } shedskin-0.9.4/shedskin/lib/builtin/function.hpp0000664000175000017500000004346512157270661021657 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* sum */ template struct __sumtype1 { typedef A type; }; template<> struct __sumtype1<__ss_bool> { typedef int type; }; template struct __sumtype2 { typedef A type; }; template<> struct __sumtype2<__ss_bool, __ss_bool> { typedef __ss_int type; }; template<> struct __sumtype2<__ss_bool, __ss_int> { typedef __ss_int type; }; template<> struct __sumtype2<__ss_bool, double> { typedef double type; }; template<> struct __sumtype2<__ss_int, double> { typedef double type; }; template typename __sumtype1::type __sum(U *iter) { typename __sumtype1::type result; result = __zero::type>(); typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; bool first = true; FOR_IN(e,iter,1,2,3) if(first) { result = (typename __sumtype1::type)e; first = false; } else result = __add(result, (typename __sumtype1::type)e); END_FOR return result; } template typename __sumtype2::type __sum(U *iter, B b) { typename __sumtype1::type result1 = __sum(iter); return __add((typename __sumtype2::type)b, (typename __sumtype2::type)result1); } /* max */ template typename A::for_in_unit ___max(int, B (*key)(typename A::for_in_unit), A *iter) { typename A::for_in_unit max; B maxkey, maxkey2; max = __zero(); maxkey = __zero(); maxkey2 = __zero();; int first = 1; typename A::for_in_unit e; typename A::for_in_loop __3; int __2; A *__1; FOR_IN(e,iter,1,2,3) if(key) { maxkey2 = key(e); if(first || __cmp(maxkey2, maxkey) == 1) { max = e; maxkey = maxkey2; } } else if(first || __cmp(e, max) == 1) max = e; if(first) first = 0; END_FOR if(first) throw new ValueError(new str("max() arg is an empty sequence")); return max; } /* XXX copy-pasto */ template typename A::for_in_unit ___max(int, pycall1 *key, A *iter) { typename A::for_in_unit max; B maxkey, maxkey2; int first = 1; typename A::for_in_unit e; typename A::for_in_loop __3; int __2; A *__1; FOR_IN(e,iter,1,2,3) if(key) { maxkey2 = key->__call__(e); if(first || __cmp(maxkey2, maxkey) == 1) { max = e; maxkey = maxkey2; } } else if(first || __cmp(e, max) == 1) max = e; if(first) first = 0; END_FOR if(first) throw new ValueError(new str("max() arg is an empty sequence")); return max; } template typename A::for_in_unit ___max(int nn, int, A *iter) { return ___max(nn, (int (*)(typename A::for_in_unit))0, iter); } template inline T ___max(int, B (*key)(T), T a, T b) { return (__cmp(key(a), key(b))==1)?a:b; } template inline T ___max(int, int, T a, T b) { return (__cmp(a, b)==1)?a:b; } template T ___max(int n, B (*key)(T), T a, T b, T c, ...) { T m = ___max(2, key, ___max(2, key, a, b), c); B maxkey = key(m); va_list ap; va_start(ap, c); for(int i=0; i T ___max(int n, int key, T a, T b, T c, ...) { T m = ___max(2, key, ___max(2, key, a, b), c); va_list ap; va_start(ap, c); for(int i=0; i typename A::for_in_unit ___min(int, B (*key)(typename A::for_in_unit), A *iter) { typename A::for_in_unit min; B minkey, minkey2; min = __zero(); minkey = __zero(); minkey2 = __zero(); int first = 1; typename A::for_in_unit e; typename A::for_in_loop __3; int __2; A *__1; FOR_IN(e,iter,1,2,3) if(key) { minkey2 = key(e); if(first || __cmp(minkey2, minkey) == -1) { min = e; minkey = minkey2; } } else if(first || __cmp(e, min) == -1) min = e; if(first) first = 0; END_FOR if(first) throw new ValueError(new str("min() arg is an empty sequence")); return min; } template typename A::for_in_unit ___min(int nn, int, A *iter) { return ___min(nn, (int (*)(typename A::for_in_unit))0, iter); } template inline T ___min(int, B (*key)(T), T a, T b) { return (__cmp(key(a), key(b))==-1)?a:b; } template inline T ___min(int, int, T a, T b) { return (__cmp(a, b)==-1)?a:b; } template T ___min(int n, B (*key)(T), T a, T b, T c, ...) { T m = ___min(2, key, ___min(2, key, a, b), c); B minkey = key(m); va_list ap; va_start(ap, c); for(int i=0; i T ___min(int n, int key, T a, T b, T c, ...) { /* XXX */ T m = ___min(2, key, ___min(2, key, a, b), c); va_list ap; va_start(ap, c); for(int i=0; i list *sorted(U *iter, V cmp, W key, __ss_int reverse) { typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; list *l = new list(); FOR_IN(e,iter,1,2,3) l->units.push_back(e); END_FOR l->sort(cmp, key, reverse); return l; } template list *sorted(list *x, V cmp, W key, __ss_int reverse) { list *l = new list(); l->units = x->units; l->sort(cmp, key, reverse); return l; } template list *sorted(tuple2 *x, V cmp, W key, __ss_int reverse) { list *l = new list(); l->units = x->units; l->sort(cmp, key, reverse); return l; } template list *sorted(str *x, V cmp, W key, __ss_int reverse) { list *l = new list(x); l->sort(cmp, key, reverse); return l; } /* reversed */ template class __ss_reverse : public __iter { public: pyseq *p; __ss_int i; __ss_reverse(pyseq *p) { this->p = p; i = len(p); } A __get_next() { if(i>0) return p->__getitem__(--i); /* XXX avoid wrap, str spec? */ this->__stop_iteration = true; } }; template __ss_reverse *reversed(pyiter *x) { return new __ss_reverse(new list(x)); } template __ss_reverse *reversed(pyseq *x) { return new __ss_reverse(x); } __iter<__ss_int> *reversed(__xrange *x); /* enumerate */ template class __enumiter : public __iter *> { public: __iter *p; __ss_int i; __enumiter(pyiter *p, __ss_int start=0) { this->p = ___iter(p); i = start; } tuple2<__ss_int, A> *next() { return new tuple2<__ss_int, A>(2, i++, p->next()); } }; template __iter *> *enumerate(pyiter *x) { return new __enumiter(x); } template __iter *> *enumerate(pyiter *x, __ss_int start) { return new __enumiter(x, start); } /* zip */ list *> *__zip(int nn); template list *> *__zip(int nn, A *iter) { list *> *result = (new list *>()); typename A::for_in_unit e; typename A::for_in_loop __3; int __2; A *__1; FOR_IN(e,iter,1,2,3) result->append((new tuple2(1, e))); END_FOR return result; } template list *> *__zip(int, A *itera, B *iterb) { list *> *result = (new list *>()); tuple2 *tuples; int count = -1; if(A::is_pyseq && B::is_pyseq) { count = __SS_MIN(len(itera), len(iterb)); tuples = new tuple2[count]; result->units.resize(count); } typename A::for_in_unit e; typename A::for_in_loop __3 = itera->for_in_init(); typename B::for_in_unit f; typename B::for_in_loop __6 = iterb->for_in_init(); int i = 0; while(itera->for_in_has_next(__3) and iterb->for_in_has_next(__6)) { e = itera->for_in_next(__3); f = iterb->for_in_next(__6); if(count == -1) result->append((new tuple2(2, e, f))); else { tuples[i].__init2__(e, f); result->units[i] = &tuples[i]; i++; } } return result; } template list *> *__zip(int, A *itera, B *iterb, C *iterc) { list *> *result = (new list *>()); tuple2 *tuples; int count = -1; if(A::is_pyseq && B::is_pyseq && C::is_pyseq) { count = __SS_MIN3(len(itera), len(iterb), len(iterc)); tuples = new tuple2[count]; result->units.resize(count); } typename A::for_in_unit e; typename A::for_in_loop __3 = itera->for_in_init(); typename B::for_in_unit f; typename B::for_in_loop __6 = iterb->for_in_init(); typename C::for_in_unit g; typename C::for_in_loop __7 = iterc->for_in_init(); int i = 0; while(itera->for_in_has_next(__3) and iterb->for_in_has_next(__6) and iterc->for_in_has_next(__7)) { e = itera->for_in_next(__3); f = iterb->for_in_next(__6); g = iterc->for_in_next(__7); if(count == -1) result->append((new tuple2(3, e, f, g))); else { tuples[i].units.push_back(e); tuples[i].units.push_back(f); tuples[i].units.push_back(g); result->units[i] = &tuples[i]; i++; } } return result; } /* next */ template A next(__iter *iter1, A fillvalue) { try { return iter1->next(); } catch(StopIteration *) { return fillvalue; } } template A next(__iter *iter1, void *) { return next(iter1, __zero()); } template A next(__iter *iter1) { return iter1->next(); } /* map */ template list *map(int, B (*func)(typename A::for_in_unit), A *iter) { if(!func) throw new ValueError(new str("'map' function argument cannot be None")); list *result = new list(); int count = -1; if(A::is_pyseq) { count = len(iter); result->units.resize(count); } typename A::for_in_unit e; typename A::for_in_loop __3 = iter->for_in_init(); int i = 0; while(iter->for_in_has_next(__3)) { e = iter->for_in_next(__3); if(count == -1) result->append((*func)(e)); else result->units[i++] = (*func)(e); } return result; } template list *map(int n, A (*func)(B, C), pyiter *b, pyiter *c) { if(!func) throw new ValueError(new str("'map' function argument cannot be None")); list *result = new list(); __iter *itb = b->__iter__(); __iter *itc = c->__iter__(); B nextb; C nextc; int total; while(1) { total = 0; try { nextb = next(itb); total += 1; } catch (StopIteration *) { nextb = __zero(); } try { nextc = next(itc); total += 1; } catch (StopIteration *) { nextc = __zero(); } if(total == 0) break; result->append((*func)(nextb, nextc)); } return result; } template list *map(int, A (*func)(B, C, D), pyiter *b1, pyiter *b2, pyiter *b3) { if(!func) throw new ValueError(new str("'map' function argument cannot be None")); list *result = new list(); __iter *itb1 = b1->__iter__(); __iter *itb2 = b2->__iter__(); __iter *itb3 = b3->__iter__(); B nextb1; C nextb2; D nextb3; int total; while(1) { total = 0; try { nextb1 = next(itb1); total += 1; } catch (StopIteration *) { nextb1 = __zero(); } try { nextb2 = next(itb2); total += 1; } catch (StopIteration *) { nextb2 = __zero(); } try { nextb3 = next(itb3); total += 1; } catch (StopIteration *) { nextb3 = __zero(); } if(total == 0) break; result->append((*func)(nextb1, nextb2, nextb3)); } return result; } /* reduce */ template A reduce(A (*func)(A, A), B *iter, C initial) { A result = initial; typename B::for_in_loop __7 = iter->for_in_init(); while(iter->for_in_has_next(__7)) result = (*func)(result, iter->for_in_next(__7)); return result; } template A reduce(A (*func)(A, A), B *iter) { A result; typename B::for_in_loop __7 = iter->for_in_init(); int first = 1; while(iter->for_in_has_next(__7)) { if(first) { result = iter->for_in_next(__7); first = 0; } else result = (*func)(result, iter->for_in_next(__7)); } if(first) throw new TypeError(new str("reduce() of empty sequence with no initial value")); return result; } /* filter */ template list *filter(B (*func)(typename A::for_in_unit), A *iter) { list *result = new list(); typename A::for_in_unit e; typename A::for_in_loop __3 = iter->for_in_init(); while(iter->for_in_has_next(__3)) { e = iter->for_in_next(__3); if(func) { if(___bool((*func)(e))) result->append(e); } else if(___bool(e)) result->append(e); } return result; } template tuple2 *filter(B (*func)(A), tuple2 *a) { tuple2 *result = new tuple2(); int size = len(a); A e; for(int i=0; iunits[i]; if(func) { if(___bool((*func)(e))) result->units.push_back(e); } else if(___bool(e)) result->units.push_back(e); } return result; } template str *filter(B (*func)(str *), str *a) { str *result = new str(); int size = len(a); char e; str *c; for(int i=0; iunit[i]; if(func) { c = __char_cache[((unsigned char)e)]; if(___bool((*func)(c))) result->unit.push_back(e); } else result->unit.push_back(e); } return result; } template list *filter(void *func, pyiter *a) { return filter(((int(*)(A))(func)), a); } inline str *filter(void *, str *a) { return filter(((int(*)(str *))(0)), a); } template tuple2 *filter(void *func, tuple2 *a) { return filter(((int(*)(A))(func)), a); } /* any */ template __ss_bool any(A *iter) { typename A::for_in_unit e; typename A::for_in_loop __3; int __2; A *__1; FOR_IN(e,iter,1,2,3) if(___bool(e)) return True; END_FOR return False; } /* all */ template __ss_bool all(A *iter) { typename A::for_in_unit e; typename A::for_in_loop __3; int __2; A *__1; FOR_IN(e,iter,1,2,3) if(!___bool(e)) return False; END_FOR return True; } /* ord, chr, hex, oct, bin */ static void __throw_ord_exc(size_t s) { /* improve inlining */ throw new TypeError(__modct(new str("ord() expected a character, but string of length %d found"), 1, ___box(s))); } inline __ss_int ord(str *s) { size_t len = s->unit.size(); if(len != 1) __throw_ord_exc(len); return (unsigned char)(s->unit[0]); } static void __throw_chr_out_of_range() { /* improve inlining */ throw new ValueError(new str("chr() arg not in range(256)")); } inline str *chr(int i) { if(i < 0 || i > 255) __throw_chr_out_of_range(); return __char_cache[i]; } inline str *chr(__ss_bool b) { return chr(b.value); } template inline str *chr(T t) { return chr(t->__int__()); } #ifdef __SS_LONG inline str *chr(__ss_int i) { return chr((int)i); } template<> inline str *hex(__ss_int i) { return hex((int)i); } template<> inline str *oct(__ss_int i) { return oct((int)i); } template<> inline str *bin(__ss_int i) { return bin((int)i); } #endif /* id */ template __ss_int id(T t) { return (intptr_t)t; } template <> __ss_int id(__ss_int); template <> __ss_int id(double); template <> __ss_int id(__ss_bool); /* type */ template class_ *__type(T t) { return t->__class__; } template<> class_ *__type(int i); template<> class_ *__type(double d); shedskin-0.9.4/shedskin/lib/builtin/exception.hpp0000664000175000017500000002232612157270661022021 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* exceptions */ #ifndef WIN32 // stacktrace.h (c) 2008, Timo Bingmann from http://idlebox.net/ // published under the WTFPL v2.0 /** Print a demangled stack backtrace of the caller function to FILE* out. */ static void print_traceback(FILE *out) { fprintf(out, "\nTraceback (most recent call last):\n"); // storage array for stack trace address data void* addrlist[64]; // retrieve current stack addresses int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*)); if (addrlen == 0) { fprintf(out, " \n"); return; } // resolve addresses into strings containing "filename(function+address)", // this array must be free()-ed char** symbollist = backtrace_symbols(addrlist, addrlen); // allocate string which will be filled with the demangled function name size_t funcnamesize = 256; char* funcname = (char*)malloc(funcnamesize); // iterate over the returned symbol lines. skip the first, it is the // address of this function. for (int i = addrlen-1; i > 0; i--) { char *begin_name = 0, *begin_offset = 0, *end_offset = 0; // find parentheses and +address offset surrounding the mangled name: // ./module(function+0x15c) [0x8048a6d] for (char *p = symbollist[i]; *p; ++p) { if (*p == '(') begin_name = p; else if (*p == '+') begin_offset = p; else if (*p == ')' && begin_offset) { end_offset = p; break; } } if (begin_name && begin_offset && end_offset && begin_name < begin_offset) { *begin_name++ = '\0'; *begin_offset++ = '\0'; *end_offset = '\0'; // mangled name is now in [begin_name, begin_offset) and caller // offset in [begin_offset, end_offset). now apply // __cxa_demangle(): int status; char* ret = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status); if (status == 0) { funcname = ret; // use possibly realloc()-ed string if(strncmp(funcname, "__shedskin__::", 14) != 0) //fprintf(out, " %s : %s+%s\n", symbollist[i], funcname, begin_offset); fprintf(out, " %s : %s\n", symbollist[i], funcname); } else { // demangling failed. Output function name as a C function with // no arguments. //fprintf(out, " %s : %s()+%s\n", // symbollist[i], begin_name, begin_offset); } } else { // couldn't parse the line? print the whole line. //fprintf(out, " %s\n", symbollist[i]); } } free(funcname); free(symbollist); } #endif extern class_ *cl_stopiteration, *cl_assertionerror, *cl_eoferror, *cl_floatingpointerror, *cl_keyerror, *cl_indexerror, *cl_typeerror, *cl_ioerror, *cl_valueerror, *cl_zerodivisionerror, *cl_keyboardinterrupt, *cl_memoryerror, *cl_nameerror, *cl_notimplementederror, *cl_oserror, *cl_overflowerror, *cl_runtimeerror, *cl_syntaxerror, *cl_systemerror, *cl_systemexit; class BaseException : public pyobj { public: str *message; BaseException(str *message=0) { __init__(message); } void __init__(str *message) { if(message) this->message = message; else this->message = new str(""); } void __init__(void *) { /* XXX test 148 */ this->message = new str(""); } str *__repr__() { return __add_strs(4, this->__class__->__name__, new str("('"), message, new str("',)")); } str *__str__() { return message; } }; class Exception: public BaseException { public: Exception(str *message=0) : BaseException(message) {} #ifdef __SS_BIND virtual PyObject *__to_py__() { return PyExc_Exception; } #endif }; class StopIteration : public Exception { public: StopIteration(str *message=0) : Exception(message) { this->__class__ = cl_stopiteration; } }; class StandardError : public Exception { public: StandardError(str *message=0) : Exception(message) {} }; class AssertionError : public StandardError { public: AssertionError(str *message=0) : StandardError(message) { this->__class__ = cl_assertionerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_AssertionError; } #endif }; class EOFError : public StandardError { public: EOFError(str *message=0) : StandardError(message) { this->__class__ = cl_eoferror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_EOFError; } #endif }; class FloatingPointError : public StandardError { public: FloatingPointError(str *message=0) : StandardError(message) { this->__class__ = cl_floatingpointerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_FloatingPointError; } #endif }; class KeyError : public StandardError { public: KeyError(str *message=0) : StandardError(message) { this->__class__ = cl_keyerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_KeyError; } #endif }; class IndexError : public StandardError { public: IndexError(str *message=0) : StandardError(message) { this->__class__ = cl_indexerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_IndexError; } #endif }; class TypeError : public StandardError { public: TypeError(str *message=0) : StandardError(message) { this->__class__ = cl_typeerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_TypeError; } #endif }; class IOError : public StandardError { public: int __ss_errno; str *filename; str *message; str *strerror; IOError(str *message=0); str *__str__(); str *__repr__(); #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_IOError; } #endif }; class KeyboardInterrupt : public BaseException { public: KeyboardInterrupt(str *message=0) : BaseException(message) { this->__class__ = cl_keyboardinterrupt; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_KeyboardInterrupt; } #endif }; class MemoryError : public StandardError { public: MemoryError(str *message=0) : StandardError(message) { this->__class__ = cl_memoryerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_MemoryError; } #endif }; class NameError : public StandardError { public: NameError(str *message=0) : StandardError(message) { this->__class__ = cl_nameerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_NameError; } #endif }; class NotImplementedError : public StandardError { public: NotImplementedError(str *message=0) : StandardError(message) { this->__class__ = cl_notimplementederror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_NotImplementedError; } #endif }; class OSError : public StandardError { public: int __ss_errno; str *filename; str *message; str *strerror; OSError(str *message=0); str *__str__(); str *__repr__(); #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_OSError; } #endif }; class OverflowError : public StandardError { public: OverflowError(str *message=0) : StandardError(message) { this->__class__ = cl_overflowerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_OverflowError; } #endif }; class RuntimeError : public StandardError { public: RuntimeError(str *message=0) : StandardError(message) { this->__class__ = cl_runtimeerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_RuntimeError; } #endif }; class SyntaxError : public StandardError { public: SyntaxError(str *message=0) : StandardError(message) { this->__class__ = cl_syntaxerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_SyntaxError; } #endif }; class SystemError : public StandardError { public: SystemError(str *message=0) : StandardError(message) { this->__class__ = cl_systemerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_SystemError; } #endif }; class SystemExit : public BaseException { public: int code; int show_message; SystemExit(__ss_int code) { this->__class__ = cl_systemexit; this->code = code; this->message = __str(this->code); this->show_message = 0; } SystemExit() { this->__class__ = cl_systemexit; this->code = 0; this->message = __str(this->code); this->show_message = 0; } SystemExit(str *message) : BaseException(message) { this->__class__ = cl_systemexit; this->code = 1; this->show_message = 1; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_SystemExit; } #endif }; class ValueError : public StandardError { public: ValueError(str *message=0) : StandardError(message) { this->__class__ = cl_valueerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_ValueError; } #endif }; class ZeroDivisionError : public StandardError { public: ZeroDivisionError(str *message=0) : StandardError(message) { this->__class__ = cl_zerodivisionerror; } #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_ZeroDivisionError; } #endif }; shedskin-0.9.4/shedskin/lib/builtin/complex.cpp0000664000175000017500000000503312157270661021461 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* complex methods */ complex mcomplex(str *s) { complex c; __re__::match_object *m; __re__::re_object *p; p = __re__::compile(new str("(?P[+-]?([\\d\\.]+e[+-]?\\d+|[\\d\\.]*)j?)(?P[+-]?([\\d\\.]+e[+-]?\\d+|[\\d\\.]*)j?)?$")); m = p->match(s->strip()); if (___bool(m)) { c = (c.parsevalue(m->group(1, new str("one")))) + (c.parsevalue(m->group(1, new str("two")))); } else { throw ((new ValueError(new str("complex() arg is a malformed string")))); } return c; } complex complex::parsevalue(str *s) { complex mult; if ((!___bool(s))) { return mcomplex(0.0, 0.0); } mult = mcomplex(1.0, 0.0); if (__eq(s->__getitem__((-1)), new str("j"))) { s = s->__slice__(2, 0, (-1), 0); mult = mcomplex(0.0, 1.0); } if (((new list(2, new str("+"), new str("-"))))->__contains__(s)) { s = s->__iadd__(new str("1")); } return mult * __float(s); } str *complex::__repr__() { str *left, *middle, *right; if(real==0) return __modct(new str("%gj"), 1, ___box(imag)); left = __modct(new str("(%g"), 1, ___box(real)); if(imag<0) middle = new str(""); else middle = new str("+"); right = __modct(new str("%gj)"), 1, ___box(imag)); return __add_strs(3, left, middle, right); } /* power */ template<> complex __power(complex a, complex b) { complex r; double vabs, len, at, phase; if(b.real == 0 and b.imag == 0) { r.real = 1; r.imag = 0; } else if(a.real == 0 and a.imag == 0) { r.real = 0; r.imag = 0; } else { vabs = __abs(a); len = std::pow(vabs,b.real); at = std::atan2(a.imag, a.real); phase = at*b.real; if (b.imag != 0.0) { len /= std::exp(at*b.imag); phase += b.imag*std::log(vabs); } r.real = len*std::cos(phase); r.imag = len*std::sin(phase); } return r; } /* glue */ #ifdef __SS_BIND template<> complex __to_ss(PyObject *p) { return mcomplex(PyComplex_RealAsDouble(p), PyComplex_ImagAsDouble(p)); } template<> PyObject *__to_py(complex c) { return PyComplex_FromDoubles(c.real, c.imag); } #endif /* boxed methods */ complex_::complex_(complex c) { unit = c; __class__ = cl_complex; } str *complex_::__repr__() { return unit.__repr__(); } __ss_bool complex_::__nonzero__() { return __mbool(unit.real == 0 and unit.imag == 0); } shedskin-0.9.4/shedskin/lib/builtin/file.hpp0000664000175000017500000000051712157270661020740 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* file */ template void *file::writelines(U *iter) { __check_closed(); typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,iter,1,2,3) write(e); END_FOR return NULL; } shedskin-0.9.4/shedskin/lib/builtin/set.hpp0000664000175000017500000004767112157270661020630 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* set implementation, partially derived from CPython, copyright Python Software Foundation (http://www.python.org/download/releases/2.6.2/license/) */ template set::set(int frozen) : frozen(frozen) { this->__class__ = cl_set; this->hash = -1; EMPTY_TO_MINSIZE(this); } #ifdef __SS_BIND #if (PY_MAJOR_VERSION == 2) #if (PY_MINOR_VERSION > 4) template set::set(PyObject *p) { this->__class__ = cl_set; this->hash = -1; EMPTY_TO_MINSIZE(this); if(PyFrozenSet_CheckExact(p)) frozen = 1; else if(PyAnySet_CheckExact(p)) frozen = 0; else throw new TypeError(new str("error in conversion to Shed Skin (set expected)")); PyObject *iter = PyObject_GetIter(p), *item; while(item = PyIter_Next(iter)) { add(__to_ss(item)); Py_DECREF(item); } Py_DECREF(iter); } template PyObject *set::__to_py__() { list *l = new list(this); /* XXX optimize */ PyObject *s; PyObject *p = __to_py(l); if(frozen) s = PyFrozenSet_New(p); else s = PySet_New(p); Py_DECREF(p); return s; } #endif #endif #endif template template set::set(U *other, int frozen) { this->__class__ = cl_set; this->frozen = frozen; this->hash = -1; EMPTY_TO_MINSIZE(this); update(1, other); } template template set::set(U *other) { this->__class__ = cl_set; this->frozen = 0; this->hash = -1; EMPTY_TO_MINSIZE(this); update(1, other); } template set& set::operator=(const set& other) { // copy test /*int i; for (i=0; i<8; i++) { smalltable[i].use = unused; } table = smalltable; mask = MINSIZE - 1; used = 0; fill = 0; update(other);*/ memcpy(this, &other, sizeof(set)); int table_size = sizeof(setentry) * (other.mask+1); table = (setentry*)myallocate(table_size); memcpy(table, other.table, table_size); return *this; } template __ss_bool set::__eq__(pyobj *p) { /* XXX check hash */ set *b = (set *)p; if( b->__len__() != this->__len__()) return False; int pos = 0; setentry *entry; while (next(&pos, &entry)) { if(!b->__contains__(entry)) return False; } return True; } template void *set::remove(T key) { if (!do_discard(key)) throw new KeyError(repr(key)); return NULL; } template __ss_bool set::__ge__(set *s) { return issuperset(s); } template __ss_bool set::__le__(set *s) { return issubset(s); } template __ss_bool set::__lt__(set *s) { return issubset(s); } template __ss_bool set::__gt__(set *s) { return issuperset(s); } template __ss_int set::__cmp__(pyobj *p) { /* XXX sometimes TypeError, sometimes not? */ set *s = (set *)p; if(issubset(s)) return -1; else if(issuperset(s)) return 1; return 0; } template long set::__hash__() { if(!this->frozen) throw new TypeError(new str("unhashable type: 'set'")); long h, hash = 1927868237L; if (this->hash != -1) return this->hash; hash *= __len__() + 1; int pos = 0; setentry *entry; while (next(&pos, &entry)) { h = entry->hash; hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u; } hash = hash * 69069L + 907133923L; if (hash == -1) hash = 590923713L; this->hash = hash; return hash; } template setentry* set::lookup(T key, long hash) const { int i = hash & mask; setentry* entry = &table[i]; if (!(entry->use) || __eq(entry->key, key)) return entry; setentry * freeslot; if (entry->use == dummy) freeslot = entry; else freeslot = NULL; unsigned int perturb; for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { i = (i << 2) + i + perturb + 1; entry = &table[i & mask]; if (!(entry->use)) { if (freeslot != NULL) entry = freeslot; break; } if (__eq(entry->key, key)) break; else if (entry->use == dummy && freeslot == NULL) freeslot = entry; } return entry; } template void set::insert_key(T key, long hash) { setentry* entry; entry = lookup(key, hash); if (!(entry->use)) { fill++; entry->key = key; entry->hash = hash; entry->use = active; used++; } else if (entry->use == dummy) { entry->key = key; entry->hash = hash; entry->use = active; used++; } } template void *set::add(T key) { long hash = hasher(key); int n_used = used; insert_key(key, hash); if ((used > n_used && fill*3 >= (mask+1)*2)) resize(used>50000 ? used*2 : used*4); return NULL; } template void *set::add(setentry* entry) { int n_used = used; insert_key(entry->key, entry->hash); if ((used > n_used && fill*3 >= (mask+1)*2)) resize(used>50000 ? used*2 : used*4); return NULL; } template int freeze(set *key) { int orig_frozen = key->frozen; key->frozen = 1; return orig_frozen; } template void unfreeze(set *key, int orig_frozen) { key->frozen = orig_frozen; } template int freeze(U key) { return 0; } template void unfreeze(U, int orig_frozen) { } template void *set::discard(T key) { do_discard(key); return NULL; } template int set::do_discard(T key) { int orig_frozen = freeze(key); register long hash = hasher(key); register setentry *entry; entry = lookup(key, hash); unfreeze(key, orig_frozen); if (entry->use != active) return DISCARD_NOTFOUND; // nothing to discard entry->use = dummy; used--; return DISCARD_FOUND; } template T set::pop() { register int i = 0; register setentry *entry; if (used == 0) throw new KeyError(new str("pop from an empty set")); entry = &table[0]; if (entry->use != active) { i = entry->hash; if (i > mask || i < 1) i = 1; /* skip slot 0 */ while ((entry = &table[i])->use != active) { i++; if (i > mask) i = 1; } } entry->use = dummy; used--; table[0].hash = i + 1; /* next place to start */ return entry->key; } /* * Iterate over a set table. Use like so: * * Py_ssize_t pos; * setentry *entry; * pos = 0; # important! pos should not otherwise be changed by you * while (set_next(yourset, &pos, &entry)) { * Refer to borrowed reference in entry->key. * } */ template int set::next(int *pos_ptr, setentry **entry_ptr) { int i; i = *pos_ptr; while (i <= mask && (table[i].use != active)) i++; *pos_ptr = i+1; if (i > mask) return 0; *entry_ptr = &table[i]; return 1; } /* Internal routine used by set_table_resize() to insert an item which is known to be absent from the set. This routine also assumes that the set contains no deleted entries. Besides the performance benefit, using insert() in resize() is dangerous (SF bug #1456209). */ template void set::insert_clean(T key, long hash) { int i; unsigned int perturb; register setentry *entry; i = hash & mask; entry = &table[i]; for (perturb = hash; entry->use; perturb >>= PERTURB_SHIFT) { i = (i << 2) + i + perturb + 1; entry = &table[i & mask]; } fill++; entry->key = key; entry->hash = hash; entry->use = active; used++; } /* Restructure the table by allocating a new table and reinserting all keys again. When entries have been deleted, the new table may actually be smaller than the old one. */ template void set::resize(int minused) { int newsize; setentry *oldtable, *newtable, *entry; int i; setentry small_copy[MINSIZE]; /* Find the smallest table size > minused. */ for (newsize = MINSIZE; newsize <= minused && newsize > 0; newsize <<= 1) ; if (newsize <= 0) { //XXX raise memory error } /* Get space for a new table. */ oldtable = table; if (newsize == MINSIZE) { /* A large table is shrinking, or we can't get any smaller. */ newtable = smalltable; if (newtable == oldtable) { if (fill == used) { /* No dummies, so no point doing anything. */ return; } /* We're not going to resize it, but rebuild the table anyway to purge old dummy entries. Subtle: This is *necessary* if fill==size, as set_lookkey needs at least one virgin slot to terminate failing searches. If fill < size, it's merely desirable, as dummies slow searches. */ memcpy(small_copy, oldtable, sizeof(small_copy)); oldtable = small_copy; } } else { newtable = (setentry*) myallocate(sizeof(setentry) * newsize); } /* Make the set empty, using the new table. */ table = newtable; mask = newsize - 1; memset(newtable, 0, sizeof(setentry) * newsize); i = used; used = 0; fill = 0; /* Copy the data over; dummy entries aren't copied over */ for (entry = oldtable; i > 0; entry++) { if (entry->use == active) { /* ACTIVE */ --i; insert_clean(entry->key, entry->hash); } } } template str *set::__repr__() { str *r; if(this->frozen) r = new str("frozenset(["); else r = new str("set(["); int rest = used-1; int pos = 0; setentry* entry; while (next(&pos, &entry)) { T e = entry->key; r->unit += repr(e)->unit; if(rest) r->unit += ", "; --rest; } r->unit += "])"; return r; } template __ss_int set::__len__() { return used; } template __ss_bool set::__contains__(T key) { long hash = hasher(key); setentry *entry; entry = lookup(key, hash); return __mbool(entry->use==active); } template __ss_bool set::__contains__(setentry* entry) { entry = lookup(entry->key, entry->hash); return __mbool(entry->use == active); } template void *set::clear() { setentry *entry, *table; int table_is_malloced; size_t fill; setentry small_copy[MINSIZE]; table = this->table; table_is_malloced = table != smalltable; /* This is delicate. During the process of clearing the set, * decrefs can cause the set to mutate. To avoid fatal confusion * (voice of experience), we have to make the set empty before * clearing the slots, and never refer to anything via so->ref while * clearing. */ fill = this->fill; if (table_is_malloced) EMPTY_TO_MINSIZE(this); else if (fill > 0) { /* It's a small table with something that needs to be cleared. * Afraid the only safe way is to copy the set entries into * another small table first. */ // ffao: is this really needed without reference counting? //memcpy(small_copy, table, sizeof(small_copy)); //table = small_copy; EMPTY_TO_MINSIZE(this); } /* else it's a small table that's already empty */ /* if (table_is_malloced) PyMem_DEL(table); */ return NULL; } template template void *set::update(int, U *iter) { typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,iter,1,2,3) add(e); END_FOR return NULL; } template void *set::update(int, set* other) { register int i; register setentry *entry; /* if (other == this || other->used == 0) // a.update(a) or a.update({}); nothing to do return 0; */ /* Do one big resize at the start, rather than * incrementally resizing as we insert new keys. Expect * that there will be no (or few) overlapping keys. */ if ((fill + other->used)*3 >= (mask+1)*2) resize((used + other->used)*2); for (i = 0; i <= other->mask; i++) { entry = &other->table[i]; if (entry->use == active) { insert_key(entry->key, entry->hash); } } return NULL; } template template void *set::update(int, U *iter, V *iter2) { update(1, iter); update(1, iter2); return NULL; } template template void *set::update(int, U *iter, V *iter2, W *iter3) { update(1, iter); update(1, iter2); update(1, iter3); return NULL; } template template set *set::__ss_union(int, U *other) { set *c = new set(this->frozen); *c = *this; c->update(1, other); return c; } template set *set::__ss_union(int, set *s) { set *a, *b; set *c = new set(this->frozen); if(len(s) < len(this)) { a = s; b = this; } else { a = this; b = s; } *c = *b; c->update(1, a); return c; } template template set *set::__ss_union(int, U *other, V *other2) { set *c = new set(this->frozen); *c = *this; c->update(1, other, other2); return c; } template template set *set::__ss_union(int, U *other, V *other2, W *other3) { set *c = new set(this->frozen); *c = *this; c->update(1, other, other2, other3); return c; } template set *set::symmetric_difference(set *s) { set *a, *b; set *c = new set(this->frozen); if(len(s) < len(this)) { a = s; b = this; } else { a = this; b = s; } *c = *b; int pos = 0; setentry *entry; while (a->next(&pos, &entry)) { if (b->__contains__(entry)) c->discard(entry->key); else c->add(entry); } return c; } template template set *set::intersection(int, U *iter) { set* result = new set; typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,iter,1,2,3) if (__contains__(e)) { result->add(e); } END_FOR return result; } template set *set::intersection(int, set *s) { set *a, *b; set *c = new set(this->frozen); if(len(s) < len(this)) { a = s; b = this; } else { a = this; b = s; } int pos = 0; setentry *entry; while (a->next(&pos, &entry)) { if(b->__contains__(entry)) c->add(entry); } return c; } template template set *set::intersection(int, U *iter, V *iter2) { return intersection(1, iter)->intersection(1, iter2); } template template set *set::intersection(int, U *iter, V *iter2, W *iter3) { return intersection(1, iter)->intersection(1, iter2)->intersection(1, iter3); } template template set* set::difference(int, U *other) { return difference(1, new set(other)); } template template set* set::difference(int, U *other, V *other2) { set *result = difference(1, new set(other)); return result->difference(1, new set(other2)); } template template set* set::difference(int, U *other, V *other2, W *other3) { set *result = difference(1, new set(other)); result = result->difference(1, new set(other2)); return result->difference(1, new set(other3)); } template set* set::difference(int, set *other) { set* result = new set; int pos = 0; setentry *entry; while (next(&pos, &entry)) { if (!other->__contains__(entry)) { result->add(entry); } } return result; } template set *set::__and__(set *s) { return intersection(1, s); } template set *set::__or__(set *s) { return __ss_union(1, s); } template set *set::__xor__(set *s) { return symmetric_difference(s); } template set *set::__sub__(set *s) { return difference(1, s); } template set *set::__iand__(set *s) { *this = intersection(1, s); return this; } template set *set::__ior__(set *s) { *this = __ss_union(1, s); return this; } template set *set::__ixor__(set *s) { *this = symmetric_difference(s); return this; } template set *set::__isub__(set *s) { *this = difference(1, s); return this; } template void *set::difference_update(int, set *s) { set *c = difference(1, s); *this = *c; /* XXX don't copy */ return NULL; } template template void *set::difference_update(int, U *iter) { difference_update(1, new set(iter)); return NULL; } template template void *set::difference_update(int, U *iter, V *iter2) { difference_update(1, iter); difference_update(1, iter2); return NULL; } template template void *set::difference_update(int, U *iter, V *iter2, W *iter3) { difference_update(1, iter); difference_update(1, iter2); difference_update(1, iter3); return NULL; } template void *set::symmetric_difference_update(set *s) { set *c = symmetric_difference(s); *this = *c; return NULL; } template void *set::intersection_update(int, set *s) { set *c = intersection(1, s); *this = *c; return NULL; } template template void *set::intersection_update(int, U *iter) { intersection_update(1, new set(iter)); return NULL; } template template void *set::intersection_update(int, U *iter, V *iter2) { intersection_update(1, new set(iter)); intersection_update(1, new set(iter2)); return NULL; } template template void *set::intersection_update(int, U *iter, V *iter2, W *iter3) { intersection_update(1, new set(iter)); intersection_update(1, new set(iter2)); intersection_update(1, new set(iter3)); return NULL; } template set *set::copy() { set *c = new set(this->frozen); *c = *this; return c; } template __ss_bool set::issubset(set *s) { if(__len__() > s->__len__()) { return False; } typename set::for_in_unit e; typename set::for_in_loop __3; int __2; set *__1; FOR_IN(e,this,1,2,3) if(!s->__contains__(e)) return False; END_FOR return True; } template __ss_bool set::issuperset(set *s) { if(__len__() < s->__len__()) return False; typename set::for_in_unit e; typename set::for_in_loop __3; int __2; set *__1; FOR_IN(e,s,1,2,3) if(!__contains__(e)) return False; END_FOR return True; } template __ss_bool set::isdisjoint(set *other) { int pos = 0; setentry *entry; while (next(&pos, &entry)) { if (other->__contains__(entry)) { return False; } } return True; } template __ss_bool set::issubset(pyiter *s) { return issubset(new set(s)); } template __ss_bool set::issuperset(pyiter *s) { return issuperset(new set(s)); } template __ss_bool set::isdisjoint(pyiter *s) { return isdisjoint(new set(s)); } template set *set::__copy__() { set *c = new set(); *c = *this; return c; } template set *set::__deepcopy__(dict *memo) { set *c = new set(); memo->__setitem__(this, c); typename set::for_in_unit e; typename set::for_in_loop __3; int __2; set *__1; FOR_IN(e,this,1,2,3) c->add(__deepcopy(e, memo)); END_FOR return c; } template __setiter::__setiter(set *p) { this->p = p; this->pos = 0; this->si_used = p->used; } template T __setiter::next() { if (si_used != p->used) { si_used = -1; __throw_set_changed(); } int ret = p->next(&pos, &entry); if (!ret) __throw_stop_iteration(); return entry->key; } shedskin-0.9.4/shedskin/lib/builtin/dict.hpp0000664000175000017500000004450412157270661020750 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* dict implementation, partially derived from CPython, copyright Python Software Foundation (http://www.python.org/download/releases/2.6.2/license/) */ #define INIT_NONZERO_SET_SLOTS(so) do { \ (so)->table = (so)->smalltable; \ (so)->mask = MINSIZE - 1; \ } while(0) #define EMPTY_TO_MINSIZE(so) do { \ memset((so)->smalltable, 0, sizeof((so)->smalltable)); \ (so)->used = (so)->fill = 0; \ INIT_NONZERO_SET_SLOTS(so); \ } while(0) template void *myallocate(int n) { return GC_MALLOC(n); } template <> void *myallocate<__ss_int>(int n); template void *myallocate(int n) { return GC_MALLOC(n); } template <> void *myallocate<__ss_int, __ss_int>(int n); template dict::dict() { this->__class__ = cl_dict; EMPTY_TO_MINSIZE(this); } template dict::dict(int count, ...) { this->__class__ = cl_dict; EMPTY_TO_MINSIZE(this); va_list ap; va_start(ap, count); for(int i=0; i * bert; bert t = va_arg(ap, bert); __setitem__(t->__getfirst__(), t->__getsecond__()); } va_end(ap); } template static inline void __add_to_dict(dict *d, U *iter) { __iter *it = ___iter(iter); typename U::for_in_unit a, b; a = it->next(); b = it->next(); d->__setitem__(a, b); } template static inline void __add_to_dict(dict *d, tuple2 *t) { d->__setitem__(t->__getfirst__(), t->__getsecond__()); } template template dict::dict(U *other) { this->__class__ = cl_dict; EMPTY_TO_MINSIZE(this); typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,other,1,2,3) __add_to_dict(this, e); END_FOR } template dict::dict(dict *p) { this->__class__ = cl_dict; EMPTY_TO_MINSIZE(this); *this = *p; } #ifdef __SS_BIND template dict::dict(PyObject *p) { if(!PyDict_Check(p)) throw new TypeError(new str("error in conversion to Shed Skin (dictionary expected)")); this->__class__ = cl_dict; EMPTY_TO_MINSIZE(this); PyObject *key, *value; PyObject *iter = PyObject_GetIter(p); while(key = PyIter_Next(iter)) { value = PyDict_GetItem(p, key); __setitem__(__to_ss(key), __to_ss(value)); Py_DECREF(key); } Py_DECREF(iter); } template PyObject *dict::__to_py__() { PyObject *p = PyDict_New(); __ss_int pos = 0; dictentry *entry; while(next(&pos, &entry)) { PyObject *pkey = __to_py(entry->key); PyObject *pvalue = __to_py(entry->value); PyDict_SetItem(p, pkey, pvalue); Py_DECREF(pkey); Py_DECREF(pvalue); } return p; } #endif template dict& dict::operator=(const dict& other) { memcpy(this, &other, sizeof(dict)); int table_size = sizeof(dictentry) * (other.mask+1); table = (dictentry*)myallocate(table_size); memcpy(table, other.table, table_size); return *this; } template __ss_bool dict::__eq__(pyobj *p) { /* XXX check hash */ dict *b = (dict *)p; if(b->__len__() != this->__len__()) return False; __ss_int pos = 0; dictentry *entry; while (next(&pos, &entry)) { register dictentry *entryb; entryb = b->lookup(entry->key, entry->hash); if (entryb->use != active) return False; if(!__eq(entry->value, entryb->value)) return False; } return True; } template int characterize(dict *a, dict *b, V *pval) { int i; int difference_found = 0; K akey; V aval; akey = 0; aval = 0; int cmp; for (i = 0; i <= a->mask; i++) { dictentry *entry; K thiskey; V thisaval, thisbval; if (a->table[i].use != active) continue; thiskey = a->table[i].key; if (difference_found) { cmp = __cmp(akey, thiskey); if (cmp < 0) continue; } thisaval = a->table[i].value; entry = b->lookup(thiskey, a->table[i].hash); if (entry->use != active) cmp = 1; else { thisbval = entry->value; cmp = __cmp(thisaval, thisbval); } if (cmp != 0) { difference_found = 1; akey = thiskey; aval = thisaval; } } *pval = aval; return difference_found; } template __ss_bool dict::__ge__(dict *s) { return __mbool(__cmp__(s) >= 0); } template __ss_bool dict::__le__(dict *s) { return __mbool(__cmp__(s) <= 0); } template __ss_bool dict::__lt__(dict *s) { return __mbool(__cmp__(s) < 0); } template __ss_bool dict::__gt__(dict *s) { return __mbool(__cmp__(s) > 0); } template __ss_int dict::__cmp__(pyobj *p) { dict *s = (dict *)p; int difference_found; V aval, bval; if (this->used < s->used) return -1; else if (this->used > s->used) return 1; difference_found = characterize(this, s, &aval); if (!difference_found) return 0; characterize(s, this, &bval); return __cmp(aval, bval); } template dictentry* dict::lookup(K key, long hash) const { int i = hash & mask; dictentry* entry = &table[i]; if (!(entry->use) || __eq(entry->key, key)) return entry; dictentry * freeslot; if (entry->use == dummy) freeslot = entry; else freeslot = NULL; unsigned int perturb; for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { i = (i << 2) + i + perturb + 1; entry = &table[i & mask]; if (!(entry->use)) { if (freeslot != NULL) entry = freeslot; break; } if (__eq(entry->key, key)) break; else if (entry->use == dummy && freeslot == NULL) freeslot = entry; } return entry; } template void dict::insert_key(K key, V value, long hash) { dictentry* entry; entry = lookup(key, hash); if (!(entry->use)) { fill++; entry->key = key; entry->value = value; entry->hash = hash; entry->use = active; used++; } else if (entry->use == dummy) { entry->key = key; entry->value = value; entry->hash = hash; entry->use = active; used++; } else { entry->value = value; } } template void *dict::__setitem__(K key, V value) { long hash = hasher(key); int n_used = used; insert_key(key, value, hash); if ((used > n_used && fill*3 >= (mask+1)*2)) resize(used>50000 ? used*2 : used*4); return NULL; } template T __none() { return NULL; } template<> int __none(); template<> double __none(); template V dict::__getitem__(K key) { register long hash = hasher(key); register dictentry *entry; entry = lookup(key, hash); if (entry->use != active) throw new KeyError(repr(key)); return entry->value; } template void *dict::__addtoitem__(K key, V value) { register long hash = hasher(key); register dictentry *entry; entry = lookup(key, hash); if (entry->use != active) throw new KeyError(repr(key)); entry->value = __add(entry->value, value); return NULL; } template V dict::get(K key) { register long hash = hasher(key); register dictentry *entry; entry = lookup(key, hash); if (entry->use != active) return __none(); return entry->value; } template V dict::get(K key, V d) { register long hash = hasher(key); register dictentry *entry; entry = lookup(key, hash); if (entry->use != active) return d; return entry->value; } template V dict::setdefault(K key, V value) { register long hash = hasher(key); register dictentry *entry; entry = lookup(key, hash); if (entry->use != active) __setitem__(key, value); return entry->value; } template void *dict::__delitem__(K key) { if (!do_discard(key)) throw new KeyError(repr(key)); return NULL; } template int dict::do_discard(K key) { register long hash = hasher(key); register dictentry *entry; entry = lookup(key, hash); if (entry->use != active) return DISCARD_NOTFOUND; // nothing to discard entry->use = dummy; used--; return DISCARD_FOUND; } template list *dict::keys() { __ss_int pos, i; dictentry *entry; list *ret = new list; ret->units.resize(used); pos = i = 0; while (next(&pos, &entry)) ret->units[i++] = entry->key; return ret; } template list *dict::values() { __ss_int pos, i; dictentry *entry; list *ret = new list; ret->units.resize(used); pos = i = 0; while (next(&pos, &entry)) ret->units[i++] = entry->value; return ret; } template list *> *dict::items() { __ss_int pos, i; dictentry *entry; list *> *ret = new list *>; ret->units.resize(used); pos = i = 0; while (next(&pos, &entry)) ret->units[i++] = new tuple2(2, entry->key, entry->value); return ret; } template V dict::pop(K key) { register long hash = hasher(key); register dictentry *entry; entry = lookup(key, hash); if (entry->use != active) throw new KeyError(__str(key)); entry->use = dummy; used--; return entry->value; } template tuple2 *dict::popitem() { register int i = 0; register dictentry *entry; if (used == 0) throw new KeyError(new str("popitem(): dictionary is empty")); entry = &table[0]; if (entry->use != active) { i = entry->hash; if (i > mask || i < 1) i = 1; /* skip slot 0 */ while ((entry = &table[i])->use != active) { i++; if (i > mask) i = 1; } } entry->use = dummy; used--; table[0].hash = i + 1; /* next place to start */ return new tuple2(2, entry->key, entry->value); } /* * Iterate over a dict table. Use like so: * * int pos; * dictentry *entry; * pos = 0; # important! pos should not otherwise be changed by you * while (dict_next(yourdict, &pos, &entry)) { * Refer to borrowed reference in entry->key. * } */ template int dict::next(__ss_int *pos_ptr, dictentry **entry_ptr) { int i; i = *pos_ptr; while (i <= mask && (table[i].use != active)) i++; *pos_ptr = i+1; if (i > mask) return 0; *entry_ptr = &table[i]; return 1; } /* Internal routine used by dict_table_resize() to insert an item which is known to be absent from the dict. This routine also assumes that the dict contains no deleted entries. Besides the performance benefit, using insert() in resize() is dangerous (SF bug #1456209). */ template void dict::insert_clean(K key, V value, long hash) { int i; unsigned int perturb; register dictentry *entry; i = hash & mask; entry = &table[i]; for (perturb = hash; entry->use; perturb >>= PERTURB_SHIFT) { i = (i << 2) + i + perturb + 1; entry = &table[i & mask]; } fill++; entry->key = key; entry->value = value; entry->hash = hash; entry->use = active; used++; } /* Restructure the table by allocating a new table and reinserting all keys again. When entries have been deleted, the new table may actually be smaller than the old one. */ template void dict::resize(int minused) { int newsize; dictentry *oldtable, *newtable, *entry; int i; dictentry small_copy[MINSIZE]; /* Find the smallest table size > minused. */ for (newsize = MINSIZE; newsize <= minused && newsize > 0; newsize <<= 1) ; if (newsize <= 0) { //XXX raise memory error } /* Get space for a new table. */ oldtable = table; if (newsize == MINSIZE) { /* A large table is shrinking, or we can't get any smaller. */ newtable = smalltable; if (newtable == oldtable) { if (fill == used) { /* No dummies, so no point doing anything. */ return; } /* We're not going to resize it, but rebuild the table anyway to purge old dummy entries. Subtle: This is *necessary* if fill==size, as dict_lookkey needs at least one virgin slot to terminate failing searches. If fill < size, it's merely desirable, as dummies slow searches. */ memcpy(small_copy, oldtable, sizeof(small_copy)); oldtable = small_copy; } } else { newtable = (dictentry*) myallocate(sizeof(dictentry) * newsize); } /* Make the dict empty, using the new table. */ table = newtable; mask = newsize - 1; memset(newtable, 0, sizeof(dictentry) * newsize); i = used; used = 0; fill = 0; /* Copy the data over; dummy entries aren't copied over */ for (entry = oldtable; i > 0; entry++) { if (entry->use == active) { /* ACTIVE */ --i; insert_clean(entry->key, entry->value, entry->hash); } } } template str *dict::__repr__() { str *r = new str("{"); dictentry *entry; int i = __len__(); __ss_int pos = 0; while (next(&pos, &entry)) { --i; r->unit += repr(entry->key)->unit + ": " + repr(entry->value)->unit; if( i > 0 ) r->unit += ", "; } r->unit += "}"; return r; } template __ss_int dict::__len__() { return used; } template __ss_bool dict::__contains__(K key) { long hash = hasher(key); dictentry *entry; entry = lookup(key, hash); return __mbool(entry->use==active); } template __ss_bool dict::__contains__(dictentry* entry) { entry = lookup(entry->key, entry->hash); return __mbool(entry->use == active); } template __ss_bool dict::has_key(K key) { return __contains__(key); } template void *dict::clear() { dictentry *entry, *table; int table_is_malloced; size_t fill; dictentry small_copy[MINSIZE]; table = this->table; table_is_malloced = table != smalltable; /* This is delicate. During the process of clearing the dict, * decrefs can cause the dict to mutate. To avoid fatal confusion * (voice of experience), we have to make the dict empty before * clearing the slots, and never refer to anything via so->ref while * clearing. */ fill = this->fill; if (table_is_malloced) EMPTY_TO_MINSIZE(this); else if (fill > 0) { /* It's a small table with something that needs to be cleared. * Afraid the only safe way is to copy the dict entries into * another small table first. */ // ffao: is this really needed without reference counting? //memcpy(small_copy, table, sizeof(small_copy)); //table = small_copy; EMPTY_TO_MINSIZE(this); } /* else it's a small table that's already empty */ /* if (table_is_malloced) PyMem_DEL(table); */ return NULL; } template void *dict::update(dict* other) { register int i; register dictentry *entry; /* Do one big resize at the start, rather than * incrementally resizing as we insert new keys. Expect * that there will be no (or few) overlapping keys. */ if ((fill + other->used)*3 >= (mask+1)*2) resize((used + other->used)*2); for (i = 0; i <= other->mask; i++) { entry = &other->table[i]; if (entry->use == active) { insert_key(entry->key, entry->value, entry->hash); } } return NULL; } template template void *dict::update(U *iter) { typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,iter,1,2,3) __setitem__(e->__getitem__(0), e->__getitem__(1)); END_FOR return NULL; } template dict *dict::copy() { dict *c = new dict; *c = *this; return c; } template dict *dict::__copy__() { dict *c = new dict; *c = *this; return c; } template dict *dict::__deepcopy__(dict *memo) { dict *c = new dict(); memo->__setitem__(this, c); K e; typename dict::for_in_loop __3; int __2; dict *__1; FOR_IN(e,this,1,2,3) c->__setitem__(__deepcopy(e, memo), __deepcopy(this->__getitem__(e), memo)); END_FOR return c; } /* dictiterkeys/values/items */ template __dictiterkeys::__dictiterkeys(dict *p) { this->p = p; this->pos = 0; this->si_used = p->used; } template K __dictiterkeys::next() { if (si_used != p->used) { si_used = -1; __throw_dict_changed(); } int ret = p->next(&pos, &entry); if (!ret) __throw_stop_iteration(); return entry->key; } template __dictitervalues::__dictitervalues(dict *p) { this->p = p; this->pos = 0; this->si_used = p->used; } template V __dictitervalues::next() { if (si_used != p->used) { si_used = -1; __throw_dict_changed(); } int ret = p->next(&pos, &entry); if (!ret) __throw_stop_iteration(); return entry->value; } template __dictiteritems::__dictiteritems(dict *p) { this->p = p; this->pos = 0; this->si_used = p->used; } template tuple2 *__dictiteritems::next() { if (si_used != p->used) { si_used = -1; __throw_dict_changed(); } int ret = p->next(&pos, &entry); if (!ret) __throw_stop_iteration(); return new tuple2(2, entry->key, entry->value); } /* dict.fromkeys */ namespace __dict__ { template dict *fromkeys(pyiter *f, B b) { dict *d = new dict(); typename pyiter::for_in_unit e; typename pyiter::for_in_loop __3; int __2; pyiter *__1; FOR_IN(e,f,1,2,3) d->__setitem__(e, b); END_FOR return d; } template dict *fromkeys(pyiter *f) { return fromkeys(f, (void *)0); } } shedskin-0.9.4/shedskin/lib/builtin/extmod.hpp0000664000175000017500000000307512157270661021323 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* extmod glue */ #ifdef __SS_BIND template T __to_ss(PyObject *p) { if(p==Py_None) return (T)NULL; return new (typename dereference::type)(p); /* isn't C++ pretty :-) */ } #ifdef __SS_LONG template<> __ss_int __to_ss(PyObject *p); #endif template<> int __to_ss(PyObject *p); template<> __ss_bool __to_ss(PyObject *p); template<> double __to_ss(PyObject *p); template<> void *__to_ss(PyObject *p); template PyObject *__to_py(T t) { if(!t) { Py_INCREF(Py_None); return Py_None; } return t->__to_py__(); } #ifdef __SS_LONG template<> PyObject *__to_py(__ss_int i); #endif template<> PyObject *__to_py(int i); template<> PyObject *__to_py(long i); template<> PyObject *__to_py(__ss_bool i); template<> PyObject *__to_py(double i); template<> PyObject *__to_py(void *); extern dict *__ss_proxy; #endif /* binding args */ #ifdef __SS_BIND template T __ss_arg(const char *name, int pos, int has_default, T default_value, PyObject *args, PyObject *kwargs) { PyObject *kwarg; int nrofargs = PyTuple_Size(args); if (pos < nrofargs) return __to_ss(PyTuple_GetItem(args, pos)); else if (kwargs && (kwarg = PyDict_GetItemString(kwargs, name))) return __to_ss(kwarg); else if (has_default) return default_value; else throw new TypeError(new str("missing argument")); } #endif #ifdef __SS_BIND PyObject *__ss__newobj__(PyObject *, PyObject *args, PyObject *kwargs); #endif shedskin-0.9.4/shedskin/lib/builtin/compare.hpp0000664000175000017500000001177412157270661021456 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* equality */ template inline __ss_bool __eq(T a, T b) { return ((a&&b)?(a->__eq__(b)):__mbool(a==b)); } #ifdef __SS_LONG /* XXX */ template<> inline __ss_bool __eq(__ss_int a, __ss_int b) { return __mbool(a == b); } #endif template<> inline __ss_bool __eq(str *a, str *b) { if(a&&b) { if (a->charcache && b->charcache) return __mbool(a==b); else return __mbool(a->__eq__(b)); } else return __mbool(a==b); } template<> inline __ss_bool __eq(int a, int b) { return __mbool(a == b); } template<> inline __ss_bool __eq(__ss_bool a, __ss_bool b) { return __mbool(a == b); } template<> inline __ss_bool __eq(double a, double b) { return __mbool(a == b); } template<> inline __ss_bool __eq(void *a, void *b) { return __mbool(a == b); } template inline __ss_bool __ne(T a, T b) { return ((a&&b)?(a->__ne__(b)):__mbool(a!=b)); } template<> inline __ss_bool __ne(int a, int b) { return __mbool(a != b); } template<> inline __ss_bool __ne(double a, double b) { return __mbool(a != b); } template<> inline __ss_bool __ne(void *a, void *b) { return __mbool(a != b); } template<> inline __ss_bool __ne(str *a, str *b) { return __mbool(!__eq(a, b)); } template inline __ss_bool __gt(T a, T b) { return a->__gt__(b); } template<> inline __ss_bool __gt(int a, int b) { return __mbool(a > b); } template<> inline __ss_bool __gt(double a, double b) { return __mbool(a > b); } template inline __ss_bool __ge(T a, T b) { return a->__ge__(b); } template<> inline __ss_bool __ge(int a, int b) { return __mbool(a >= b); } template<> inline __ss_bool __ge(double a, double b) { return __mbool(a >= b); } template inline __ss_bool __lt(T a, T b) { return a->__lt__(b); } template<> inline __ss_bool __lt(int a, int b) { return __mbool(a < b); } template<> inline __ss_bool __lt(double a, double b) { return __mbool(a < b); } template inline __ss_bool __le(T a, T b) { return a->__le__(b); } template<> inline __ss_bool __le(int a, int b) { return __mbool(a <= b); } template<> inline __ss_bool __le(double a, double b) { return __mbool(a <= b); } /* comparison */ template struct dereference {}; template struct dereference { typedef T type; }; /* template struct has_cmp { template struct type_check; template static char (& chk(type_check*))[1]; template static char (& chk(...))[2]; static bool const value = (sizeof(chk(0)) == 1); }; template struct has_eq { template struct type_check; template static char (& chk(type_check*))[1]; template static char (& chk(...))[2]; static bool const value = (sizeof(chk(0)) == 1); }; template inline __ss_int __cmp(T a, T b) { typedef typename dereference::type T2; if (!a) return -1; if (has_cmp::value) return a->__cmp__(b); else { if (has_eq::value and a->__eq__(b)) return 0; if(a->__lt__(b)) return -1; else return 1; } return 0; } */ template inline __ss_int __cmp(T a, T b) { if (!a) return -1; return a->__cmp__(b); } #ifdef __SS_LONG template<> inline __ss_int __cmp(__ss_int a, __ss_int b) { if(a < b) return -1; else if(a > b) return 1; return 0; } #endif template<> inline __ss_int __cmp(int a, int b) { if(a < b) return -1; else if(a > b) return 1; return 0; } template<> inline __ss_int __cmp(__ss_bool a, __ss_bool b) { return __cmp(a.value, b.value); /* XXX */ } template<> inline __ss_int __cmp(double a, double b) { if(a < b) return -1; else if(a > b) return 1; return 0; } template<> inline __ss_int __cmp(void *a, void *b) { if(a < b) return -1; else if(a > b) return 1; return 0; } template __ss_int cpp_cmp(T a, T b) { return __cmp(a, b) == -1; } template __ss_int cpp_cmp_rev(T a, T b) { return __cmp(a, b) == 1; } template class cpp_cmp_custom { typedef __ss_int (*hork)(T, T); hork cmp; public: cpp_cmp_custom(hork a) { cmp = a; } __ss_int operator()(T a, T b) const { return cmp(a,b) == -1; } }; template class cpp_cmp_custom_rev { typedef __ss_int (*hork)(T, T); hork cmp; public: cpp_cmp_custom_rev(hork a) { cmp = a; } __ss_int operator()(T a, T b) const { return cmp(a,b) == 1; } }; template class cpp_cmp_key { typedef V (*hork)(T); hork key; public: cpp_cmp_key(hork a) { key = a; } __ss_int operator()(T a, T b) const { return __cmp(key(a), key(b)) == -1; } }; template class cpp_cmp_key_rev { typedef V (*hork)(T); hork key; public: cpp_cmp_key_rev(hork a) { key = a; } __ss_int operator()(T a, T b) const { return __cmp(key(a), key(b)) == 1; } }; shedskin-0.9.4/shedskin/lib/builtin/bool.hpp0000664000175000017500000000317412157270661020756 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* bool */ inline __ss_int __ss_bool::operator+(__ss_bool b) { return value+b.value; } inline __ss_bool __ss_bool::operator==(__ss_bool b) { __ss_bool c; c.value=value==b.value; return c; } inline __ss_bool __ss_bool::operator&(__ss_bool b) { __ss_bool c; c.value=value&b.value; return c; } inline __ss_bool __ss_bool::operator|(__ss_bool b) { __ss_bool c; c.value=value|b.value; return c; } inline __ss_bool __ss_bool::operator^(__ss_bool b) { __ss_bool c; c.value=value^b.value; return c; } inline bool __ss_bool::operator!() { return !value; } inline __ss_bool::operator bool() { return bool(value); } inline __ss_bool& __ss_bool::operator=(int a) { value=a; return *this; } inline __ss_bool ___bool() { return __mbool(false); } template inline __ss_bool ___bool(T x) { return __mbool(x && x->__nonzero__()); } #ifdef __SS_LONG template<> inline __ss_bool ___bool(__ss_int x) { return __mbool(x!=0); } #endif template<> inline __ss_bool ___bool(str *s) { return __mbool(s && s->unit.size() > 0); } template<> inline __ss_bool ___bool(int x) { return __mbool(x!=0); } template<> inline __ss_bool ___bool(bool x) { return __mbool(x); } template<> inline __ss_bool ___bool(__ss_bool x) { return x; } template<> inline __ss_bool ___bool(double x) { return __mbool(x!=0); } template<> inline __ss_bool ___bool(void *) { return False; } template<> inline __ss_bool ___bool(long int) { return False; } /* XXX bool(None) 64-bit */ template inline __ss_bool ___bool(list *x) { return __mbool(x && (x->units.size() != 0)); } /* XXX more general solution */ shedskin-0.9.4/shedskin/lib/builtin/str.hpp0000664000175000017500000000400012157270661020620 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* str methods */ inline str *str::__getitem__(__ss_int i) { i = __wrap(this, i); return __char_cache[((unsigned char)(unit[i]))]; } inline str *str::__getfast__(__ss_int i) { i = __wrap(this, i); return __char_cache[((unsigned char)(unit[i]))]; } inline __ss_int str::__len__() { return unit.size(); } inline bool str::for_in_has_next(size_t i) { return i < unit.size(); /* XXX opt end cond */ } inline str *str::for_in_next(size_t &i) { return __char_cache[((unsigned char)(unit[i++]))]; } template str *str::join(U *iter) { int sz, total, __2; bool only_ones = true; typename U::for_in_unit e; typename U::for_in_loop __3; U *__1; __join_cache->units.resize(0); total = 0; FOR_IN(e,iter,1,2,3) __join_cache->units.push_back(e); sz = e->unit.size(); if(sz != 1) only_ones = false; total += sz; END_FOR int unitsize = unit.size(); int elems = len(__join_cache); if(elems==1) return __join_cache->units[0]; str *s = new str(); if(unitsize == 0 and only_ones) { s->unit.resize(total); for(int j=0; junit[j] = __join_cache->units[j]->unit[0]; } else if(elems) { total += (elems-1)*unitsize; s->unit.resize(total); int tsz; int k = 0; for(int m = 0; munits[m]; tsz = t->unit.size(); if (tsz == 1) s->unit[k] = t->unit[0]; else memcpy((void *)(s->unit.data()+k), t->unit.data(), tsz); k += tsz; if (unitsize && m < elems-1) { if (unitsize==1) s->unit[k] = unit[0]; else memcpy((void *)(s->unit.data()+k), unit.data(), unit.size()); k += unitsize; } } } return s; } shedskin-0.9.4/shedskin/lib/builtin/complex.hpp0000664000175000017500000001101512157270661021463 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* complex */ /* constructors */ inline complex mcomplex(double real, double imag) { complex c; c.real = real; c.imag = imag; return c; } template inline complex mcomplex(T t) { complex c; c.real = __float(t); c.imag = 0; return c; } /* operators */ inline complex complex::operator+(complex b) { return mcomplex(real+b.real, imag+b.imag); } inline complex complex::operator+(double b) { return (*this) + mcomplex(b); } inline complex complex::operator-(complex b) { return mcomplex(real-b.real, imag-b.imag); } inline complex complex::operator-(double b) { return (*this) - mcomplex(b); } inline complex complex::operator/(complex b) { complex c; double norm = b.real*b.real+b.imag*b.imag; c.real = (real*b.real+imag*b.imag)/norm; c.imag = (imag*b.real-b.imag*real)/norm; return c; } inline complex complex::operator/(double b) { return (*this) / mcomplex(b); } inline complex complex::operator*(complex b) { return mcomplex(real*b.real-imag*b.imag, real*b.imag+imag*b.real); } inline complex complex::operator*(double b) { return (*this) * mcomplex(b); } inline complex complex::operator%(complex b) { complex c = (*this) / b; return (*this) - (b * (((__ss_int)c.real))); } inline complex complex::operator%(double b) { return (*this) % mcomplex(b); } inline complex complex::operator-() { return mcomplex(-real, -imag); } inline complex complex::operator+() { return *this; } inline __ss_bool complex::operator==(complex b) { return __mbool(real==b.real and imag==b.imag); } inline __ss_bool complex::operator==(double b) { return (*this) == mcomplex(b); } inline __ss_bool complex::operator!=(complex b) { return __mbool(real!=b.real or imag!=b.imag); } inline __ss_bool complex::operator!=(double b) { return (*this) != mcomplex(b); } inline complex& complex::operator=(double a) { real = a; imag = 0.0; } /* floordiv */ static inline complex __complexfloordiv(complex a, complex b) { complex c = a / b; c.real = ((__ss_int)c.real); c.imag = 0; return c; } template<> inline complex __floordiv(complex a, complex b) { return __complexfloordiv(a, b); } inline complex __floordiv(complex a, double b) { return __complexfloordiv(a, mcomplex(b)); } inline complex __floordiv(double a, complex b) { return __complexfloordiv(mcomplex(a), b); } /* divmod */ static tuple2 *__complexdivmod(complex a, complex b) { return new tuple2(2, __complexfloordiv(a, b), a % b); } template<> inline tuple2 *divmod(complex a, complex b) { return __complexdivmod(a, b); } inline tuple2 *divmod(complex a, double b) { return __complexdivmod(a, mcomplex(b)); } inline tuple2 *divmod(double a, complex b) { return __complexdivmod(mcomplex(a), b); } /* str, repr */ inline str *__str(complex c) { return c.__repr__(); } inline str *repr(complex c) { return c.__repr__(); } /* comparison */ template<> inline __ss_bool __eq(complex a, complex b) { return a == b; } template<> inline __ss_bool __ne(complex a, complex b) { return a != b; } template<> inline __ss_int __cmp(complex a, complex b) {} /* unused, satisfy templates */ template<> inline __ss_bool __gt(complex a, complex b) {} template<> inline __ss_bool __ge(complex a, complex b) {} template<> inline __ss_bool __lt(complex a, complex b) {} template<> inline __ss_bool __le(complex a, complex b) {} /* copy, deepcopy */ template<> inline complex __copy(complex a) { return a; } template<> inline complex __deepcopy(complex a, dict *) { return a; } /* add */ template<> inline complex __add(complex a, complex b) { return a + b; } /* abs */ inline double __abs(complex c) { return std::sqrt(c.real*c.real+c.imag*c.imag); } /* bool */ template<> inline __ss_bool ___bool(complex c) { return __mbool(c.real != 0.0 or c.imag != 0); } /* power */ template<> complex __power(complex a, complex b); inline complex __power(complex a, double b) { return __power(a, mcomplex(b)); } inline complex __power(double a, complex b) { return __power(mcomplex(a), b); } /* hashing */ template<> inline long hasher(complex c) { return c.__hash__(); } inline long complex::__hash__() { return ((__ss_int)imag)*1000003+((__ss_int)real); } /* conjugate */ inline complex complex::conjugate() { return mcomplex(real, -imag); } /* glue */ #ifdef __SS_BIND template<> PyObject *__to_py(complex c); template<> complex __to_ss(PyObject *p); #endif shedskin-0.9.4/shedskin/lib/builtin/bool.cpp0000664000175000017500000000060712157270661020747 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* bool methods */ bool_::bool_(__ss_bool i) { unit = i; __class__ = cl_bool; } str *bool_::__repr__() { if(unit.value) return new str("True"); return new str("False"); } __ss_bool bool_::__nonzero__() { return unit; } __ss_int bool_::__index__() { return unit.value; } shedskin-0.9.4/shedskin/lib/builtin/file.cpp0000664000175000017500000001263312157270661020735 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifdef WIN32 #include // for _isatty #endif // WIN32 #include #if (_POSIX_C_SOURCE >= 1 or _XOPEN_SOURCE or _POSIX_SOURCE or _BSD_SOURCE or _SVID_SOURCE) and (_BSD_SOURCE or _SVID_SOURCE) #define HAVE_STDIO_UNLOCKED #endif #ifndef HAVE_STDIO_UNLOCKED #define GETC getc #define FWRITE fwrite #define FFLUSH fflush #define FERROR ferror #define FEOF feof #else // HAVE_STDIO_UNLOCKED #define GETC getc_unlocked #define FWRITE fwrite_unlocked #define FFLUSH fflush_unlocked #define FERROR ferror_unlocked #define FEOF feof_unlocked #endif // HAVE_STDIO_UNLOCKED file::file(str *file_name, str *flags) { if (flags) { size_t universal = flags->unit.find_first_of("Uu"); if(universal != std::string::npos) { options.universal_mode = true; flags = new str(flags->unit); flags->unit[universal] = 'b'; // force binary mode as expected by readline } } else flags = __char_cache['r']; f = fopen(file_name->unit.c_str(), flags->unit.c_str()); if(f == 0) throw new IOError(file_name); name = file_name; mode = flags; } file *open(str *name, str *flags) { return new file(name, flags); } void *file::write(str *s) { __check_closed(); if(f) { size_t size = s->unit.size(); if(FWRITE(s->unit.data(), 1, size, f) != size and __error()) throw new IOError(); } return NULL; } void *file::seek(__ss_int i, __ss_int w) { __check_closed(); if(f) { if(fseek(f, i, w) == -1) throw new IOError(); } return NULL; } __ss_int file::tell() { __check_closed(); if(f) { long status = ftell(f); if(status == -1) throw new IOError(); return __ss_int(status); } return -1; } str *file::readline(int n) { __check_closed(); __read_cache.clear(); if (options.universal_mode) { for(size_t i = 0; i < size_t(n); ++i) { int c = GETC(f); if(c == EOF) break; if(options.cr) { options.cr = false; if(c == '\n') { c = GETC(f); if(c == EOF) break; } } if(c == '\r') { options.cr = true; c = '\n'; } __read_cache.push_back(c); if(c == '\n') break; } } else { /* If not universal mode, use the normal loop */ for(size_t i = 0; i < size_t(n); ++i) { const int c = GETC(f); if(c == EOF) break; __read_cache.push_back(c); if(c == '\n') break; } } if(__error()) throw new IOError(); return new str(&__read_cache[0], __read_cache.size()); } static void __throw_io_error() { throw new IOError(); } str *file::read(int n) { __check_closed(); if(n == 1) { const int c = GETC(f); if(FERROR(f) != 0) /* avoid virtual call */ __throw_io_error(); if(c != EOF) return __char_cache[static_cast(c)]; else return new str(); } // other cases (n != 1): __read_cache.clear(); for(size_t i = 0; i < size_t(n); ++i) { const int c = GETC(f); if(c == EOF) break; __read_cache.push_back(c); } if(__error()) __throw_io_error(); return new str(&__read_cache[0], __read_cache.size()); } list *file::readlines(__ss_int /*size_hint*/) { __check_closed(); list *lines = new list(); while(not __eof()) { str *line = readline(); if(line->unit.empty()) break; lines->append(line); } return lines; } __iter *file::xreadlines() { return this->__iter__(); } void *file::close() { if(f and not closed) { flush(); if(fclose(f)) throw new IOError(); closed = 1; } return NULL; } void *file::flush() { __check_closed(); if(f) if(FFLUSH(f)) throw new IOError(); return NULL; } int file::__ss_fileno() { __check_closed(); if(f) return fileno(f); return -1; } __ss_bool file::isatty() { __check_closed(); #ifdef WIN32 return ___bool(_isatty(__ss_fileno())); #else // WIN32 return ___bool(::isatty(__ss_fileno())); #endif // WIN32 } void *file::truncate(int size) { __check_closed(); flush(); if(size == -1) size = tell(); #if(_BSD_SOURCE || _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L) if(ftruncate(__ss_fileno(), size) == -1) throw new IOError(); #endif return NULL; } str *file::__repr__() { return (new str("file '"))->__add__(name)->__add__(new str("'")); } void file::__enter__() { } void file::__exit__() { close(); } bool file::__error() { return (FERROR(f) != 0); } bool file::__eof() { return (FEOF(f) != 0); } str *file::next() { if(__eof()) throw new StopIteration(); str *line = readline(); if(__eof() and !len(line)) throw new StopIteration(); return line; } /* file iteration */ __iter *file::__iter__() { return new __fileiter(this); } __fileiter::__fileiter(file *p) { this->p = p; } str *__fileiter::next() { return p->next(); } shedskin-0.9.4/shedskin/lib/builtin/format.hpp0000664000175000017500000000313312157270661021306 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* boxing */ template T ___box(T t) { return t; } /* XXX */ #ifdef __SS_LONG int_ *___box(__ss_int); #endif int_ *___box(int); int_ *___box(unsigned int); /* XXX */ int_ *___box(long); int_ *___box(unsigned long); int_ *___box(unsigned long long); bool_ *___box(__ss_bool); float_ *___box(double); complex_ *___box(complex); /* string formatting */ int __fmtpos(str *fmt); int __fmtpos2(str *fmt); void __modfill(str **fmt, pyobj *t, str **s); str *mod_to_c2(pyobj *t); int_ *mod_to_int(pyobj *t); float_ *mod_to_float(pyobj *t); template str *__modtuple(str *fmt, tuple2 *t) { list *vals = new list(); for(int i=0;iappend(___box(t->__getitem__(i))); return __mod4(fmt, vals); } template str *__modtuple(str *fmt, tuple2 *t) { list *vals = new list(2, ___box(t->__getfirst__()), ___box(t->__getsecond__())); return __mod4(fmt, vals); } template str *__moddict(str *v, dict *d) { str *const_6 = new str(")"); int i, pos, pos2; list *names = (new list()); while((pos = __fmtpos2(v)) != -1) { pos2 = v->find(const_6, pos); names->append(v->__slice__(3, (pos+2), pos2, 0)); v = (v->__slice__(2, 0, (pos+1), 0))->__add__(v->__slice__(1, (pos2+1), 0, 0)); } list *vals = new list(); for(i=0;iappend(___box(d->__getitem__(names->__getitem__(i)))); return __mod4(v, vals); } shedskin-0.9.4/shedskin/lib/builtin/exception.cpp0000664000175000017500000000321412157270661022007 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* Exceptions */ OSError::OSError(str *filename) { this->filename = filename; this->__class__ = cl_oserror; __ss_errno = errno; message = new str(""); strerror = new str(::strerror(__ss_errno)); } str *OSError::__str__() { return __add_strs(7, new str("[Errno "), __str(__ss_errno), new str("] "), strerror, new str(": '"), filename, new str("'")); } str *OSError::__repr__() { return __add_strs(5, new str("OSError("), __str(__ss_errno), new str(", '"), strerror, new str("')")); } IOError::IOError(str *filename) { this->filename = filename; this->__class__ = cl_ioerror; __ss_errno = errno; message = new str(""); strerror = new str(::strerror(__ss_errno)); } str *IOError::__str__() { return __add_strs(7, new str("[Errno "), __str(__ss_errno), new str("] "), strerror, new str(": '"), filename, new str("'")); } str *IOError::__repr__() { return __add_strs(5, new str("IOError("), __str(__ss_errno), new str(", '"), strerror, new str("')")); } void __throw_index_out_of_range() { throw new IndexError(new str("index out of range")); } void __throw_range_step_zero() { throw new ValueError(new str("range() step argument must not be zero")); } void __throw_set_changed() { throw new RuntimeError(new str("set changed size during iteration")); } void __throw_dict_changed() { throw new RuntimeError(new str("dict changed size during iteration")); } void __throw_slice_step_zero() { throw new ValueError(new str("slice step cannot be zero")); } void __throw_stop_iteration() { throw new StopIteration(); } shedskin-0.9.4/shedskin/lib/builtin/msvc.hpp0000664000175000017500000001023712157270661020771 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #if defined(_MSC_VER) && _MSC_VER>=1400 /* logical operator keywords */ #define and && #define or || #define not ~ /* typeof */ namespace msvc_typeof_impl { /* This is a fusion of Igor Chesnokov's method (http://rsdn.ru/forum/src/1094305.aspx) and Steven Watanabe's method (http://lists.boost.org/Archives/boost/2006/12/115006.php) How it works: C++ allows template type inference for templated function parameters but nothing else. What we do is to pass the expression sent to typeof() into the templated function vartypeID() as its parameter, thus extracting its type. The big problem traditionally now is how to get that type out of the vartypeID() instance, and here's how we do it: 1. unique_type_id() returns a monotonically increasing integer for every unique type passed to it during this compilation unit. It also specialises an instance of msvc_extract_type::id2type_impl. 2. vartypeID() returns a sized for the type where sizeof(sized)==unique_type_id. We vector through sized as a means of returning the unique_type_id at compile time rather than runtime. 3. msvc_extract_type then extracts the type by using a bug in MSVC to reselect the specialised child type (id2type_impl) from within the specialisation of itself originally performed by the above instance of unique_type_id. This bug works because when MSVC calculated the signature of the specialised msvc_extract_type::id2type_impl, it does not include the value of type in the signature of id2type_impl. Therefore when we reselect msvc_extract_type::id2type_impl it erroneously returns the one already in its list of instantiated types rather than correctly generating a newly specialised msvc_extract_type::id2type_impl This bug allows the impossible and gives us a working typeof() in MSVC. Hopefully Microsoft won't fix this bug until they implement a native typeof. */ struct msvc_extract_type_default_param {}; template struct msvc_extract_type; template struct msvc_extract_type { template struct id2type_impl; typedef id2type_impl id2type; }; template struct msvc_extract_type : msvc_extract_type { template<> struct id2type_impl //VC8.0 specific bugfeature { typedef T type; }; template struct id2type_impl; typedef id2type_impl id2type; }; template class CCounter; // TUnused is required to force compiler to recompile CCountOf class template struct CCountOf { enum { __if_exists(CCounter) { count = CCountOf::count } __if_not_exists(CCounter) { count = NTested } }; }; template struct CProvideCounterValue { enum { value = NValue }; }; // type_id #define unique_type_id(type) \ (CProvideCounterValue< \ /*register TYPE--ID*/ typename msvc_extract_type::count, type>::id2type, \ /*increment compile-time Counter*/ CCounter::count>, \ /*pass value of Counter*/CCountOf::count \ >::value) // Lets type_id() be > than 0 class __Increment_type_id { enum { value = unique_type_id(__Increment_type_id) }; }; // vartypeID() returns a type with sizeof(type_id) template class sized { char m_pad[NSize]; }; template typename sized vartypeID(T&); template typename sized vartypeID(const T&); template typename sized vartypeID(volatile T&); template typename sized vartypeID(const volatile T&); } #define typeof(expression) msvc_typeof_impl::msvc_extract_type::id2type::type #endif shedskin-0.9.4/shedskin/lib/builtin/math.cpp0000664000175000017500000000075512157270661020751 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* int_ methods */ int_::int_(__ss_int i) { unit = i; __class__ = cl_int_; } str *int_::__repr__() { return __str(unit); } __ss_bool int_::__nonzero__() { return __mbool(unit); } /* float methods */ float_::float_(double f) { unit = f; __class__ = cl_float_; } str *float_::__repr__() { return __str(unit); } __ss_bool float_::__nonzero__() { return __mbool(unit); } shedskin-0.9.4/shedskin/lib/builtin/function.cpp0000664000175000017500000001237312157270661021644 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* builtin functions */ str *raw_input(str *msg) { if(msg and len(msg)) { __ss_stdout->write(msg); __ss_stdout->options.lastchar = msg->unit[len(msg)-1]; } str *s = __ss_stdin->readline(); if(len(s) and s->unit[len(s)-1] == '\n') s->unit.erase(s->unit.end()-1, s->unit.end()); if(__ss_stdin->__eof()) throw new EOFError(); return s; } __ss_int __int(str *s, __ss_int base) { char *cp; __ss_int i; #ifdef __SS_LONG i = strtoll(s->unit.c_str(), &cp, base); #else i = strtol(s->unit.c_str(), &cp, base); #endif if(*cp != '\0') { s = s->rstrip(); #ifdef __SS_LONG i = strtoll(s->unit.c_str(), &cp, base); #else i = strtol(s->unit.c_str(), &cp, base); #endif if(*cp != '\0') throw new ValueError(new str("invalid literal for int()")); } return i; } template<> double __float(str *s) { return strtod(s->unit.c_str(), NULL); } template<> __ss_int id(__ss_int) { throw new TypeError(new str("'id' called with integer")); } template<> __ss_int id(double) { throw new TypeError(new str("'id' called with float")); } template<> __ss_int id(__ss_bool) { throw new TypeError(new str("'id' called with bool")); } static int range_len(int lo, int hi, int step) { /* modified from CPython */ int n = 0; if ((lo < hi) && (step>0)) { unsigned int uhi = (unsigned int)hi; unsigned int ulo = (unsigned int)lo; unsigned int diff = uhi - ulo - 1; n = (int)(diff / (unsigned int)step + 1); } else { if ((lo > hi) && (step<0)) { unsigned int uhi = (unsigned int)lo; unsigned int ulo = (unsigned int)hi; unsigned int diff = uhi - ulo - 1; n = (int)(diff / (unsigned int)(-step) + 1); } } return n; } list<__ss_int> *range(__ss_int a, __ss_int b, __ss_int s) { list<__ss_int> *r; __ss_int i; int pos; r = new list<__ss_int>(); pos = 0; i = a; if(s==0) __throw_range_step_zero(); if(s==1) { r->units.resize(b-a); for(; iunits[pos++] = i; return r; } r->units.resize(range_len(a,b,s)); if(s>0) { while((iunits[pos++] = i; i += s; } } else { while((i>b)) { r->units[pos++] = i; i += s; } } return r; } list<__ss_int> *range(__ss_int n) { return range(0, n); } class __rangeiter : public __iter<__ss_int> { public: __ss_int i, a, b, s; __rangeiter(__ss_int a, __ss_int b, __ss_int s) { this->__class__ = cl_rangeiter; this->a = a; this->b = b; this->s = s; i = a; if(s==0) throw new ValueError(new str("xrange() arg 3 must not be zero")); } __ss_int next() { if(s>0) { if(ib) { i += s; return i-s; } throw new StopIteration(); } }; __xrange::__xrange(__ss_int a, __ss_int b, __ss_int s) { this->a = a; this->b = b; this->s = s; } __iter<__ss_int> *__xrange::__iter__() { return new __rangeiter(a, b, s); } __ss_int __xrange::__len__() { return range_len(a, b, s); } str *__xrange::__repr__() { if(s==1) { if(a==0) return __modct(new str("xrange(%d)"), 1, ___box(b)); else return __modct(new str("xrange(%d, %d)"), 2, ___box(a), ___box(b)); } return __modct(new str("xrange(%d, %d, %d)"), 3, ___box(a), ___box(b), ___box(s)); /* XXX */ } __xrange *xrange(__ss_int a, __ss_int b, __ss_int s) { return new __xrange(a,b,s); } __xrange *xrange(__ss_int n) { return new __xrange(0, n, 1); } __iter<__ss_int> *reversed(__xrange *x) { return new __rangeiter(x->a+(range_len(x->a,x->b,x->s)-1)*x->s, x->a-x->s, -x->s); } /* representation */ template<> str *repr(double d) { return __str(d); } #ifdef __SS_LONG template<> str *repr(__ss_int i) { return __str(i); } #endif template<> str *repr(int i) { return __str(i); } template<> str *repr(__ss_bool b) { return b.value?(new str("True")):(new str("False")); } template<> str *repr(void *) { return new str("None"); } str *__str(void *) { return new str("void"); } /* get class pointer */ template<> class_ *__type(int) { return cl_int_; } template<> class_ *__type(double) { return cl_float_; } /* hex, oct, bin */ template<> str *hex(int i) { if(i<0) return (new str("-0x"))->__add__(__str(-i, 16)); else return (new str("0x"))->__add__(__str(i, 16)); } template<> str *hex(__ss_bool b) { return hex(b.value); } template<> str *oct(int i) { if(i<0) return (new str("-0"))->__add__(__str(-i, 8)); else if(i>0) return (new str("0"))->__add__(__str(i, 8)); else return new str("0"); } template<> str *oct(__ss_bool b) { return oct(b.value); } template<> str *bin(int i) { if(i<0) return (new str("-0b"))->__add__(__str(-i, 2)); else return (new str("0b"))->__add__(__str(i, 2)); } template<> str *bin(__ss_bool b) { return bin(b.value); } shedskin-0.9.4/shedskin/lib/builtin/list.hpp0000664000175000017500000002762212157270661021002 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* list methods */ template list::list() { this->__class__ = cl_list; } template list::list(int count, ...) { this->__class__ = cl_list; va_list ap; va_start(ap, count); for(int i=0; iunits.push_back(t); } va_end(ap); } template template list::list(U *iter) { this->__class__ = cl_list; typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,iter,1,2,3) this->units.push_back(e); END_FOR } template list::list(list *p) { this->__class__ = cl_list; this->units = p->units; } template list::list(tuple2 *p) { this->__class__ = cl_list; this->units = p->units; } template list::list(str *s) { this->__class__ = cl_list; this->units.resize(len(s)); int sz = s->unit.size(); for(int i=0; iunits[i] = __char_cache[((unsigned char)(s->unit[i]))]; } #ifdef __SS_BIND template list::list(PyObject *p) { if(!PyList_Check(p)) throw new TypeError(new str("error in conversion to Shed Skin (list expected)")); this->__class__ = cl_list; int size = PyList_Size(p); for(int i=0; i(PyList_GetItem(p, i))); } template PyObject *list::__to_py__() { int len = this->__len__(); PyObject *p = PyList_New(len); for(int i=0; i__getitem__(i))); return p; } #endif template void list::clear() { units.resize(0); } template void list::resize(__ss_int i) { units.resize(i); } template __ss_int list::__len__() { return units.size(); } template T list::__getitem__(__ss_int i) { i = __wrap(this, i); return units[i]; } template __ss_bool list::__eq__(pyobj *p) { list *b = (list *)p; unsigned int len = this->units.size(); if(b->units.size() != len) return False; for(unsigned int i = 0; i < len; i++) if(!__eq(this->units[i], b->units[i])) return False; return True; } template void *list::append(T a) { this->units.push_back(a); return NULL; } template template void *list::extend(U *iter) { typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,iter,1,2,3) this->units.push_back(e); END_FOR return NULL; } template void *list::extend(list *p) { int l1, l2; l1 = this->__len__(); l2 = p->__len__(); this->units.resize(l1+l2); memcpy(&(this->units[l1]), &(p->units[0]), sizeof(T)*l2); return NULL; } template void *list::extend(tuple2 *p) { int l1, l2; l1 = this->__len__(); l2 = p->__len__(); this->units.resize(l1+l2); memcpy(&(this->units[l1]), &(p->units[0]), sizeof(T)*l2); return NULL; } template void *list::extend(str *s) { int sz = s->unit.size(); for(int i=0; iunits.push_back(__char_cache[((unsigned char)(s->unit[i]))]); return NULL; } template inline T list::__getfast__(__ss_int i) { i = __wrap(this, i); return this->units[i]; } template void *list::__setitem__(__ss_int i, T e) { i = __wrap(this, i); units[i] = e; return NULL; } template void *list::__delitem__(__ss_int i) { i = __wrap(this, i); units.erase(units.begin()+i,units.begin()+i+1); return NULL; } template int list::empty() { return units.empty(); } template list *list::__slice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s) { list *c = new list(); slicenr(x, l, u, s, this->__len__()); if(s == 1) { c->units.resize(u-l); memcpy(&(c->units[0]), &(this->units[l]), sizeof(T)*(u-l)); } else if(s > 0) for(int i=l; iunits.push_back(units[i]); else for(int i=l; i>u; i += s) c->units.push_back(units[i]); return c; } template void *list::__setslice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s, pyiter *b) { list *la = new list(); /* XXX avoid intermediate list */ typename pyiter::for_in_unit e; typename pyiter::for_in_loop __3; int __2; pyiter *__1; FOR_IN(e,b,1,2,3) la->units.push_back(e); END_FOR this->__setslice__(x, l, u, s, la); return NULL; } template void *list::__setslice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s, list *la) { slicenr(x, l, u, s, this->__len__()); if(x&4 && s != 1) { // x&4: extended slice (step 's' is given), check if sizes match int slicesize; if(l == u) slicesize = 0; // XXX ugly else if(s > 0 && u < l) slicesize=0; else if(s < 0 && l < u) slicesize=0; else { int slicelen = std::abs(u-l); int absstep = std::abs(s); slicesize = slicelen/absstep; if(slicelen%absstep) slicesize += 1; } if(slicesize != len(la)) throw new ValueError(__modtuple(new str("attempt to assign sequence of size %d to extended slice of size %d"), new tuple2<__ss_int,__ss_int>(2, len(la), (__ss_int)slicesize))); } if(s == 1) { if(l <= u) { this->units.erase(this->units.begin()+l, this->units.begin()+u); this->units.insert(this->units.begin()+l, la->units.begin(), la->units.end()); } else this->units.insert(this->units.begin()+l, la->units.begin(), la->units.end()); } else { int i, j; if(s > 0) for(i = 0, j = l; j < u; i++, j += s) this->units[j] = la->units[i]; else for(i = 0, j = l; j > u; i++, j += s) this->units[j] = la->units[i]; } return NULL; } template void *list::__delete__(__ss_int i) { i = __wrap(this, i); units.erase(units.begin()+i,units.begin()+i+1); return NULL; } template void *list::__delete__(__ss_int x, __ss_int l, __ss_int u, __ss_int s) { slicenr(x, l, u, s, this->__len__()); if(s == 1) __delslice__(l, u); else { __GC_VECTOR(T) v; for(int i=0; i__len__();i++) if(i < l or i >= u or (i-l)%s) v.push_back(this->units[i]); units = v; } return NULL; } template void *list::__delslice__(__ss_int a, __ss_int b) { if(a>this->__len__()) return NULL; if(b>this->__len__()) b = this->__len__(); units.erase(units.begin()+a,units.begin()+b); return NULL; } template __ss_bool list::__contains__(T a) { int size = this->units.size(); for(int i=0; iunits[i], a)) return True; return False; } template list *list::__add__(list *b) { int l1 = this->__len__(); int l2 = b->__len__(); list *c = new list(); c->units.resize(l1+l2); if(l1==1) c->units[0] = this->units[0]; else memcpy(&(c->units[0]), &(this->units[0]), sizeof(T)*l1); if(l2==1) c->units[l1] = b->units[0]; else memcpy(&(c->units[l1]), &(b->units[0]), sizeof(T)*l2); return c; } template list *list::__mul__(__ss_int b) { list *c = new list(); if(b<=0) return c; __ss_int len = this->units.size(); if(len==1) c->units.assign(b, this->units[0]); else { c->units.resize(b*len); for(__ss_int i=0; iunits[i*len]), &(this->units[0]), sizeof(T)*len); } return c; } template list *list::__copy__() { list *c = new list(); c->units = this->units; return c; } template list *list::__deepcopy__(dict *memo) { list *c = new list(); memo->__setitem__(this, c); c->units.resize(this->__len__()); for(int i=0; i__len__(); i++) c->units[i] = __deepcopy(this->units[i], memo); return c; } template template list *list::__iadd__(U *iter) { extend(iter); return this; } template list *list::__imul__(__ss_int n) { __ss_int l1 = this->__len__(); this->units.resize(l1*n); for(__ss_int i = 1; i <= n-1; i++) memcpy(&(this->units[l1*i]), &(this->units[0]), sizeof(T)*l1); return this; } template __ss_int list::index(T a) { return index(a, 0, this->__len__()); } template __ss_int list::index(T a, __ss_int s) { return index(a, s, this->__len__()); } template __ss_int list::index(T a, __ss_int s, __ss_int e) { __ss_int one = 1; slicenr(7, s, e, one, this->__len__()); for(__ss_int i = s; i __ss_int list::count(T a) { __ss_int c = 0; __ss_int len = this->__len__(); for(__ss_int i = 0; i str *list::__repr__() { str *r = new str("["); int len = this->__len__(); for(int i = 0; iunit += repr(units[i])->unit; if (iunit += ", "; } r->unit += "]"; return r; } template T list::pop(int i) { /* XXX optimize */ int len = this->__len__(); if(len==0) throw new IndexError(new str("pop from empty list")); if(i<0) i = len+i; if(i<0 or i>=len) throw new IndexError(new str("pop index out of range")); T e = units[i]; units.erase(units.begin()+i); return e; } template T list::pop() { if(this->units.size()==0) throw new IndexError(new str("pop from empty list")); T e = units.back(); units.pop_back(); return e; } template void *list::reverse() { std::reverse(this->units.begin(), this->units.end()); return NULL; } template template void *list::sort(__ss_int (*cmp)(T, T), U (*key)(T), __ss_int reverse) { if(key) { if(reverse) std::sort(units.begin(), units.end(), cpp_cmp_key_rev(key)); else std::sort(units.begin(), units.end(), cpp_cmp_key(key)); } else if(cmp) { if(reverse) std::sort(units.begin(), units.end(), cpp_cmp_custom_rev(cmp)); else std::sort(units.begin(), units.end(), cpp_cmp_custom(cmp)); } else { if(reverse) std::sort(units.begin(), units.end(), cpp_cmp_rev); else std::sort(units.begin(), units.end(), cpp_cmp); } return NULL; } template template void *list::sort(__ss_int cmp, U (*key)(T), __ss_int reverse) { return sort((__ss_int(*)(T,T))0, key, reverse); } template void *list::sort(__ss_int (*cmp)(T, T), __ss_int, __ss_int reverse) { return sort(cmp, (__ss_int(*)(T))0, reverse); } template void *list::sort(__ss_int, __ss_int, __ss_int reverse) { return sort((__ss_int(*)(T,T))0, (__ss_int(*)(T))0, reverse); } template void *list::insert(int m, T e) { int len = this->__len__(); if (m<0) m = len+m; if (m<0) m = 0; if (m>=len) m = len; units.insert(units.begin()+m, e); return NULL; } template void *list::remove(T e) { for(int i = 0; i < this->__len__(); i++) if(__eq(units[i], e)) { units.erase(units.begin()+i); return NULL; } throw new ValueError(new str("list.remove(x): x not in list")); } template inline bool list::for_in_has_next(size_t i) { return i < units.size(); /* XXX opt end cond */ } template inline T list::for_in_next(size_t &i) { return units[i++]; } shedskin-0.9.4/shedskin/lib/builtin/str.cpp0000664000175000017500000005575012157270661020635 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* str methods */ str::str() : hash(-1), charcache(0) { __class__ = cl_str_; } str::str(const char *s) : unit(s), hash(-1), charcache(0) { __class__ = cl_str_; } str::str(__GC_STRING s) : unit(s), hash(-1), charcache(0) { __class__ = cl_str_; } str::str(const char *s, int size) : unit(s, size), hash(-1), charcache(0) { /* '\0' delimiter in C */ __class__ = cl_str_; } str *str::__str__() { // weg? return this; } str *str::__repr__() { std::stringstream ss; __GC_STRING sep = "\\\n\r\t"; __GC_STRING let = "\\nrt"; const char *quote = "'"; int hasq = unit.find("'"); int hasd = unit.find("\""); if (hasq != -1 && hasd != -1) { sep += "'"; let += "'"; } if (hasq != -1 && hasd == -1) quote = "\""; ss << quote; for(unsigned int i=0; i=' ' && j<='~') ss << (char)j; else ss << "\\x" << std::hex << j; } } ss << quote; return new str(ss.str().c_str()); } __ss_int str::__int__() { return __int(this); } __ss_bool str::__contains__(str *s) { return __mbool(unit.find(s->unit) != std::string::npos); } __ss_bool str::__ctype_function(int (*cfunc)(int)) { int i, l = unit.size(); if(!l) return False; for(i = 0; i < l; i++) if(!cfunc((int)unit[i])) return False; return True; } __ss_bool str::isspace() { return __mbool(unit.size() && (unit.find_first_not_of(ws) == std::string::npos)); } __ss_bool str::isdigit() { return __ctype_function(&::isdigit); } __ss_bool str::isalpha() { return __ctype_function(&::isalpha); } __ss_bool str::isalnum() { return __ctype_function(&::isalnum); } __ss_bool str::islower() { return __ctype_function(&::islower); } __ss_bool str::isupper() { return __ctype_function(&::isupper); } str *str::ljust(int width, str *s) { if(width<=__len__()) return this; if(!s) s = sp; return __add__(s->__mul__(width-__len__())); } str *str::rjust(int width, str *s) { if(width<=__len__()) return this; if(!s) s = sp; return s->__mul__(width-__len__())->__add__(this); } str *str::zfill(int width) { if(width<=__len__()) return this; return (new str("0"))->__mul__(width-__len__())->__add__(this); } str *str::expandtabs(int width) { int i; __GC_STRING r = unit; while((i = r.find("\t")) != -1) r.replace(i, 1, (new str(" "))->__mul__(width-i%width)->unit); return new str(r); } str *str::strip(str *chars) { return lstrip(chars)->rstrip(chars); } str *str::lstrip(str *chars) { __GC_STRING remove; if(chars) remove = chars->unit; else remove = ws; int first = unit.find_first_not_of(remove); if( first == -1 ) return new str(""); return new str(unit.substr(first,unit.size()-first)); } tuple2 *str::partition(str *sep) { int i; i = unit.find(sep->unit); if(i != -1) return new tuple2(3, new str(unit.substr(0, i)), new str(sep->unit), new str(unit.substr(i + sep->unit.length()))); else return new tuple2(3, new str(unit), new str(""), new str("")); } tuple2 *str::rpartition(str *sep) { int i; i = unit.rfind(sep->unit); if(i != -1) return new tuple2(3, new str(unit.substr(0, i)), new str(sep->unit), new str(unit.substr(i + sep->unit.length()))); else return new tuple2(3, new str(unit), new str(""), new str("")); } list *str::rsplit(str *sep, int maxsep) { __GC_STRING ts; list *r = new list(); int i, j, curi, tslen; curi = 0; i = j = unit.size() - 1; //split by whitespace if(!sep) { while(i > 0 && j > 0 && (curi < maxsep || maxsep < 0)) { j = unit.find_last_not_of(ws, i); if(j == -1) break; i = unit.find_last_of(ws, j); //this works out pretty nicely; i will be -1 if no more is found, and thus i + 1 will be 0th index r->append(new str(unit.substr(i + 1, j - i))); curi++; } //thus we only bother about extra stuff here if we *have* found more whitespace if(i > 0 && j >= 0 && (j = unit.find_last_not_of(ws, i)) >= 0) r->append(new str(unit.substr(0, j))); } //split by seperator else { ts = sep->unit; tslen = ts.length(); i++; while(i > 0 && j > 0 && (curi < maxsep || maxsep < 0)) { j = i; i--; i = unit.rfind(ts, i); if(i == -1) { i = j; break; } r->append(new str(unit.substr(i + tslen, j - i - tslen))); curi++; } //either left over (beyond max) or very last match (see loop break) if(i >= 0) r->append(new str(unit.substr(0, i))); } r->reverse(); return r; } __ss_bool str::istitle() { int i, len; len = unit.size(); if(!len) return False; for(i = 0; i < len; ) { for( ; !::isalpha((int)unit[i]) && i < len; i++) ; if(i == len) break; if(!::isupper((int)unit[i])) return False; i++; for( ; ::islower((int)unit[i]) && i < len; i++) ; if(i == len) break; if(::isalpha((int)unit[i])) return False; } return True; } list *str::splitlines(int keepends) { list *r = new list(); int i, endlen; unsigned int j; const char *ends = "\r\n"; endlen = i = 0; do { j = i + endlen; i = unit.find_first_of(ends, j); if(i == -1) break; //for all we know the character sequence could change mid-way... if(unit[i] == '\r' && unit[i + 1] == '\n') endlen = 2; else endlen = 1; r->append(new str(unit.substr(j, i - j + (keepends ? endlen : 0)))); } while(i >= 0); if(j != unit.size()) r->append(new str(unit.substr(j))); return r; } str *str::rstrip(str *chars) { __GC_STRING remove; if(chars) remove = chars->unit; else remove = ws; int last = unit.find_last_not_of(remove); if( last == -1 ) return new str(""); return new str(unit.substr(0,last+1)); } list *str::split(str *sp, int max_splits) { __GC_STRING s = unit; int num_splits = 0; int sep_iter = 0, tmp, chunk_iter = 0; list *result = new list(); if (sp == NULL) { #define next_separator(iter) (s.find_first_of(ws, (iter))) #define skip_separator(iter) (s.find_first_not_of(ws, (iter))) if(skip_separator(chunk_iter) == -1) /* XXX */ return result; if(next_separator(chunk_iter) == 0) chunk_iter = skip_separator(chunk_iter); while((max_splits < 0 or num_splits < max_splits) and ((sep_iter = next_separator(chunk_iter)) != -1)) { result->append(new str(s.substr(chunk_iter, sep_iter - chunk_iter))); if((tmp = skip_separator(sep_iter)) == -1) { chunk_iter = sep_iter; break; } else chunk_iter = tmp; ++num_splits; } if(not (max_splits < 0 or num_splits < max_splits)) result->append(new str(s.substr(chunk_iter, s.size()-chunk_iter))); else if(sep_iter == -1) result->append(new str(s.substr(chunk_iter, s.size()-chunk_iter))); #undef next_separator #undef skip_separator } else { /* given separator (slightly different algorithm required) * (python is very inconsistent in this respect) */ const char *sep = sp->unit.c_str(); int sep_size = sp->unit.size(); #define next_separator(iter) s.find(sep, (iter)) #define skip_separator(iter) ((iter + sep_size) > s.size()? -1 : (iter + sep_size)) if (max_splits == 0) { result->append(this); return result; } if(next_separator(chunk_iter) == 0) { chunk_iter = skip_separator(chunk_iter); result->append(new str()); ++num_splits; } while((max_splits < 0 or num_splits < max_splits) and (sep_iter = next_separator(chunk_iter)) != -1) { result->append(new str(s.substr(chunk_iter, sep_iter - chunk_iter))); if((tmp = skip_separator(sep_iter)) == -1) { chunk_iter = sep_iter; break; } else chunk_iter = tmp; ++num_splits; } if(not (max_splits < 0 or num_splits < max_splits)) result->append(new str(s.substr(chunk_iter, s.size()-chunk_iter))); else if(sep_iter == -1) result->append(new str(s.substr(chunk_iter, s.size()-chunk_iter))); #undef next_separator #undef skip_separator } return result; } str *str::translate(str *table, str *delchars) { if(len(table) != 256) throw new ValueError(new str("translation table must be 256 characters long")); str *newstr = new str(); int self_size = unit.size(); for(int i = 0; i < self_size; i++) { char c = unit[i]; if(!delchars || delchars->unit.find(c) == std::string::npos) newstr->unit.push_back(table->unit[(unsigned char)c]); } return newstr; } str *str::swapcase() { str *r = new str(unit); int len = __len__(); for(int i = 0; i < len; i++) { char c = unit[i]; if( c >= 'a' && c <= 'z' ) r->unit[i] = ::toupper(c); else if( c >= 'A' && c <= 'Z' ) r->unit[i] = ::tolower(c); } return r; } str *str::center(int w, str *fill) { int len = __len__(); if(w<=len) return this; if(!fill) fill = sp; str *r = fill->__mul__(w); int j = (w-len)/2; for(int i=0; iunit[j+i] = unit[i]; return r; } __ss_int str::__cmp__(pyobj *p) { if (!p) return 1; str *b = (str *)p; int r = unit.compare(b->unit); if( r < 0 ) return -1; else if( r > 0 ) return 1; return 0; } __ss_bool str::__eq__(pyobj *p) { str *q = (str *)p; size_t len = unit.size(); if(len != q->unit.size() or (hash != -1 and q->hash != -1 and hash != q->hash)) return False; return __mbool(memcmp(unit.data(), q->unit.data(), len) == 0); } str *str::__mul__(__ss_int n) { /* optimize */ str *r = new str(); if(n<=0) return r; __GC_STRING &s = r->unit; __ss_int ulen = unit.size(); if(ulen == 1) r->unit = __GC_STRING(n, unit[0]); else { s.resize(ulen*n); for(__ss_int i=0; i= 4) { unsigned int k = *(unsigned int *)data; k *= m; k ^= k >> r; k *= m; x *= m; x ^= k; data += 4; len -= 4; } // Handle the last few bytes of the input array switch(len) { case 3: x ^= data[2] << 16; case 2: x ^= data[1] << 8; case 1: x ^= data[0]; x *= m; }; // Do a few final mixes of the hash to ensure the last few // bytes are well-incorporated. x ^= x >> 13; x *= m; x ^= x >> 15; #else /* modified from CPython */ x = *data << 7; while (--len >= 0) x = (1000003*x) ^ *data++; x ^= __len__(); if (x == -1) x = -2; #endif hash = x; return x; } str *str::__add__(str *b) { str *s = new str(); s->unit.reserve(unit.size()+b->unit.size()); s->unit.append(unit); s->unit.append(b->unit); return s; } str *str::__iadd__(str *b) { return __add__(b); } str *__add_strs(int, str *a, str *b, str *c) { str *result = new str(); int asize = a->unit.size(); int bsize = b->unit.size(); int csize = c->unit.size(); if(asize == 1 && bsize == 1 && csize == 1) { result->unit.resize(3); result->unit[0] = a->unit[0]; result->unit[1] = b->unit[0]; result->unit[2] = c->unit[0]; } else { result->unit.resize(asize+bsize+csize); memcpy((void *)(result->unit.data()), a->unit.data(), asize); int pos = asize; memcpy((void *)(result->unit.data()+pos), b->unit.data(), bsize); pos += bsize; memcpy((void *)(result->unit.data()+pos), c->unit.data(), csize); } return result; } str *__add_strs(int, str *a, str *b, str *c, str *d) { str *result = new str(); int asize = a->unit.size(); int bsize = b->unit.size(); int csize = c->unit.size(); int dsize = d->unit.size(); if(asize == 1 && bsize == 1 && csize == 1 && dsize == 1) { result->unit.resize(4); result->unit[0] = a->unit[0]; result->unit[1] = b->unit[0]; result->unit[2] = c->unit[0]; result->unit[3] = d->unit[0]; } else { result->unit.resize(asize+bsize+csize+dsize); memcpy((void *)(result->unit.data()), a->unit.data(), asize); int pos = asize; memcpy((void *)(result->unit.data()+pos), b->unit.data(), bsize); pos += bsize; memcpy((void *)(result->unit.data()+pos), c->unit.data(), csize); pos += csize; memcpy((void *)(result->unit.data()+pos), d->unit.data(), dsize); } return result; } str *__add_strs(int, str *a, str *b, str *c, str *d, str *e) { str *result = new str(); int asize = a->unit.size(); int bsize = b->unit.size(); int csize = c->unit.size(); int dsize = d->unit.size(); int esize = e->unit.size(); if(asize == 1 && bsize == 1 && csize == 1 && dsize == 1 && esize == 1) { result->unit.resize(5); result->unit[0] = a->unit[0]; result->unit[1] = b->unit[0]; result->unit[2] = c->unit[0]; result->unit[3] = d->unit[0]; result->unit[4] = e->unit[0]; } else { result->unit.resize(asize+bsize+csize+dsize+esize); memcpy((void *)(result->unit.data()), a->unit.data(), asize); int pos = asize; memcpy((void *)(result->unit.data()+pos), b->unit.data(), bsize); pos += bsize; memcpy((void *)(result->unit.data()+pos), c->unit.data(), csize); pos += csize; memcpy((void *)(result->unit.data()+pos), d->unit.data(), dsize); pos += dsize; memcpy((void *)(result->unit.data()+pos), e->unit.data(), esize); } return result; } str *__add_strs(int n, ...) { va_list ap; va_start(ap, n); int size; str *result = new str(); size = 0; for(int i=0; iunit.resize(size); va_start(ap, n); size = 0; int pos = 0; for(int i=0; iunit.data()+pos), s->unit.data(), s->unit.size()); pos += s->unit.size(); } va_end(ap); return result; } str *str::__slice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s) { int len = unit.size(); slicenr(x, l, u, s, len); if(s == 1) return new str(unit.data()+l, u-l); else { __GC_STRING r; if(!(x&1) && !(x&2) && s==-1) { r.resize(len); for(int i=0; i 0) for(int i=l; iu; i += s) r += unit[i]; return new str(r); } } int str::__fixstart(int a, int b) { if(a == -1) return a; return a+b; } int str::find(str *s, int a) { return __fixstart(unit.substr(a, unit.size()-a).find(s->unit), a); } int str::find(str *s, int a, int b) { return __fixstart(unit.substr(a, b-a).find(s->unit), a); } int str::rfind(str *s, int a) { return __fixstart(unit.substr(a, unit.size()-a).rfind(s->unit), a); } int str::rfind(str *s, int a, int b) { return __fixstart(unit.substr(a, b-a).rfind(s->unit), a); } int str::__checkneg(int i) { if(i == -1) throw new ValueError(new str("substring not found")); return i; } int str::index(str *s, int a) { return __checkneg(find(s, a)); } int str::index(str *s, int a, int b) { return __checkneg(find(s, a, b)); } int str::rindex(str *s, int a) { return __checkneg(find(s, a)); } int str::rindex(str *s, int a, int b) { return __checkneg(find(s, a, b)); } __ss_int str::count(str *s, __ss_int start) { return count(s, start, __len__()); } __ss_int str::count(str *s, __ss_int start, __ss_int end) { __ss_int i, count, one = 1; slicenr(7, start, end, one, __len__()); i = start; count = 0; while( ((i = unit.find(s->unit, i)) != -1) && (i <= end-len(s)) ) { i += len(s); count++; } return count; } __ss_bool str::startswith(str *s, __ss_int start) { return startswith(s, start, __len__()); } __ss_bool str::startswith(str *s, __ss_int start, __ss_int end) { __ss_int i, j, one = 1; slicenr(7, start, end, one, __len__()); for(i = start, j = 0; i < end && j < len(s); ) if (unit[i++] != s->unit[j++]) return False; return __mbool(j == len(s)); } __ss_bool str::endswith(str *s, __ss_int start) { return endswith(s, start, __len__()); } __ss_bool str::endswith(str *s, __ss_int start, __ss_int end) { __ss_int i, j, one = 1; slicenr(7, start, end, one, __len__()); for(i = end, j = len(s); i > start && j > 0; ) if (unit[--i] != s->unit[--j]) return False; return True; } str *str::replace(str *a, str *b, int c) { __GC_STRING s = unit; int i, j, p; int asize = a->unit.size(); int bsize = b->unit.size(); j = p = 0; while( ((c==-1) || (j++ != c)) && (i = s.find(a->unit, p)) != -1 ) { s.replace(i, asize, b->unit); p = i + bsize + (asize?0:1); } return new str(s); } str *str::upper() { if(unit.size() == 1) return __char_cache[((unsigned char)(::toupper(unit[0])))]; str *toReturn = new str(*this); std::transform(toReturn->unit.begin(), toReturn->unit.end(), toReturn->unit.begin(), toupper); return toReturn; } str *str::lower() { if(unit.size() == 1) return __char_cache[((unsigned char)(::tolower(unit[0])))]; str *toReturn = new str(*this); std::transform(toReturn->unit.begin(), toReturn->unit.end(), toReturn->unit.begin(), tolower); return toReturn; } str *str::title() { str *r = new str(unit); bool up = true; size_t len = this->unit.size(); for(size_t i=0; iunit[i]; if(!::isalpha(c)) up = true; else if (up) { c = ::toupper(c); up = false; } else c = ::tolower(c); r->unit[i] = c; } return r; } str *str::capitalize() { str *r = new str(unit); r->unit[0] = ::toupper(r->unit[0]); return r; } #ifdef __SS_BIND str::str(PyObject *p) : hash(-1) { if(!PyString_Check(p)) throw new TypeError(new str("error in conversion to Shed Skin (string expected)")); __class__ = cl_str_; unit = __GC_STRING(PyString_AsString(p), PyString_Size(p)); } PyObject *str::__to_py__() { return PyString_FromStringAndSize(unit.c_str(), unit.size()); } #endif #ifdef __SS_LONG str *__str(__ss_int i, __ss_int base) { if(i<10 && i>=0 && base==10) return __char_cache[((unsigned char)('0'+i))]; char buf[70]; char *psz = buf+69; /* if(i==INT_MIN) return new str("-2147483648"); */ int neg = i<0; *psz = 0; if(neg) i = -i; if(base == 10) { int pos; while(i > 999) { pos = 4*(i%1000); i = i/1000; *(--psz) = __str_cache[pos]; *(--psz) = __str_cache[pos+1]; *(--psz) = __str_cache[pos+2]; } pos = 4*i; if(i>99) { *(--psz) = __str_cache[pos]; *(--psz) = __str_cache[pos+1]; *(--psz) = __str_cache[pos+2]; } else if(i>9) { *(--psz) = __str_cache[pos]; *(--psz) = __str_cache[pos+1]; } else *(--psz) = __str_cache[pos]; } else do { *(--psz) = "0123456789abcdefghijklmnopqrstuvwxyz"[i%base]; i = i/base; } while(i); if(neg) *(--psz) = '-'; return new str(psz, buf+69-psz); } #endif str *__str(int i, int base) { if(base==10 && i<10 && i>=0) return __char_cache[((unsigned char)('0'+i))]; char buf[70]; char *psz = buf+69; if(base==10 and i==INT_MIN) return new str("-2147483648"); int neg = i<0; *psz = 0; if(neg) i = -i; if(base == 10) { int pos; while(i > 999) { pos = 4*(i%1000); i = i/1000; *(--psz) = __str_cache[pos]; *(--psz) = __str_cache[pos+1]; *(--psz) = __str_cache[pos+2]; } pos = 4*i; if(i>99) { *(--psz) = __str_cache[pos]; *(--psz) = __str_cache[pos+1]; *(--psz) = __str_cache[pos+2]; } else if(i>9) { *(--psz) = __str_cache[pos]; *(--psz) = __str_cache[pos+1]; } else *(--psz) = __str_cache[pos]; } else do { *(--psz) = "0123456789abcdefghijklmnopqrstuvwxyz"[i%base]; i = i/base; } while(i); if(neg) *(--psz) = '-'; return new str(psz, buf+69-psz); } str *__str(__ss_bool b) { if(b) return new str("True"); return new str("False"); } str *__str() { return new str(""); } /* XXX optimize */ template<> str *__str(double t) { std::stringstream ss; ss.precision(12); ss << std::showpoint << t; __GC_STRING s = ss.str().c_str(); if(s.find('e') == std::string::npos) { unsigned int j = s.find_last_not_of("0"); if( s[j] == '.') j++; s = s.substr(0, j+1); } return new str(s); } shedskin-0.9.4/shedskin/lib/builtin/iter.hpp0000664000175000017500000000240212157270661020757 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* iteration macros */ #define FAST_FOR(i, l, u, s, t1, t2) \ if(s==0) \ __throw_range_step_zero(); \ for(__ ## t1 = l, __ ## t2 = u; ; __ ## t1 += s) { \ if (s >= 0) { if (__ ## t1 >= __ ## t2) break; } \ else { if (__ ## t1 <= __ ## t2) break; } \ i=__ ## t1; \ #define FOR_IN(e, iter, temp, i, t) \ __ ## temp = iter; \ __ ## i = -1; \ __ ## t = __ ## temp->for_in_init(); \ while(__ ## temp->for_in_has_next(__ ## t)) \ { \ __ ## i ++; \ e = __ ## temp->for_in_next(__ ## t); #define FOR_IN_ZIP(a, b, k, l, t, u, n, m) \ __ ## m = __SS_MIN(k->units.size(), l->units.size()); \ __ ## t = k; \ __ ## u = l; \ for(__ ## n = 0; __ ## n < __ ## m; __ ## n ++) { \ a = (__ ## t)->units[__ ## n]; \ b = (__ ## u)->units[__ ## n]; #define FOR_IN_ENUM(i, m, temp, n) \ __ ## temp = m; \ for(__ ## n = 0; (unsigned int)__ ## n < (__ ## temp)->units.size(); __ ## n ++) { \ i = (__ ## temp)->units[__ ## n]; \ #define FOR_IN_DICT(m, temp, entry, pos) \ __ ## temp = m; \ __ ## pos = 0; \ while (__ ## temp->next(& __ ## pos, & __ ## entry)) { \ #define END_FOR } shedskin-0.9.4/shedskin/lib/builtin/math.hpp0000664000175000017500000001234512157270661020754 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* pow */ inline double __power(__ss_int a, double b) { return pow(a,b); } inline double __power(double a, __ss_int b) { if(b==2) return a*a; else if(b==3) return a*a*a; else return pow(a,b); } template A __power(A a, A b); template<> inline double __power(double a, double b) { return pow(a,b); } template<> inline __ss_int __power(__ss_int a, __ss_int b) { switch(b) { case 2: return a*a; case 3: return a*a*a; case 4: return a*a*a*a; case 5: return a*a*a*a*a; case 6: return a*a*a*a*a*a; case 7: return a*a*a*a*a*a*a; case 8: return a*a*a*a*a*a*a*a; case 9: return a*a*a*a*a*a*a*a*a; case 10: return a*a*a*a*a*a*a*a*a*a; } __ss_int res, tmp; res = 1; tmp = a; while((b>0)) { if ((b%2)) { res = (res*tmp); } tmp = (tmp*tmp); b = (b/2); } return res; } #ifdef __SS_LONG inline __ss_int __power(__ss_int a, __ss_int b, __ss_int c) { __ss_int res, tmp; res = 1; tmp = a; while((b>0)) { if ((b%2)) { res = ((res*tmp)%c); } tmp = ((tmp*tmp)%c); b = (b/2); } return res; } #endif inline int __power(int a, int b, int c) { long long res, tmp; res = 1; tmp = a; while((b>0)) { if ((b%2)) { res = ((res*tmp)%c); } tmp = ((tmp*tmp)%c); b = (b/2); } return (int)res; } /* division */ template A __divs(A a, A b); template<> inline double __divs(double a, double b) { return a/b; } #ifdef __SS_LONG template<> inline __ss_int __divs(__ss_int a, __ss_int b) { if(a<0 && b>0) return (a-b+1)/b; else if(b<0 && a>0) return (a-b-1)/b; else return a/b; } #endif template<> inline int __divs(int a, int b) { if(a<0 && b>0) return (a-b+1)/b; else if(b<0 && a>0) return (a-b-1)/b; else return a/b; } template double __divs(A a, B b); template<> inline double __divs(__ss_int a, double b) { return (double)a/b; } template<> inline double __divs(double a, __ss_int b) { return a/((double)b); } template inline A __floordiv(A a, A b) { return a->__floordiv__(b); } template<> inline double __floordiv(double a, double b) { return floor(a/b); } #ifdef __SS_LONG /* XXX */ template<> inline __ss_int __floordiv(__ss_int a, __ss_int b) { return (__ss_int)floor((double)a/b); } /* XXX */ #endif template<> inline int __floordiv(int a, int b) { return (int)floor((double)a/b); } /* XXX */ inline double __floordiv(__ss_int a, double b) { return floor((double)a/b); } inline double __floordiv(double a, __ss_int b) { return floor(a/((double)b)); } /* modulo */ template A __mods(A a, A b); #ifdef __SS_LONG /* XXX */ template<> inline __ss_int __mods(__ss_int a, __ss_int b) { int m = a%b; if((m<0 && b>0)||(m>0 && b<0)) m+=b; return m; } #endif template<> inline int __mods(int a, int b) { int m = a%b; if((m<0 && b>0)||(m>0 && b<0)) m+=b; return m; } template<> inline double __mods(double a, double b) { double f = fmod(a,b); if((f<0 && b>0)||(f>0 && b<0)) f+=b; return f; } template double __mods(A a, B b); #ifdef __SS_LONG template<> inline double __mods(__ss_int a, double b) { return __mods((double)a, b); } template<> inline double __mods(double a, __ss_int b) { return __mods(a, (double)b); } #endif template<> inline double __mods(int a, double b) { return __mods((double)a, b); } template<> inline double __mods(double a, int b) { return __mods(a, (double)b); } /* divmod */ template inline tuple2 *divmod(A a, A b) { return a->__divmod__(b); } template<> inline tuple2 *divmod(double a, double b) { return new tuple2(2, __floordiv(a,b), __mods(a,b)); } #ifdef __SS_LONG template<> inline tuple2<__ss_int, __ss_int> *divmod(__ss_int a, __ss_int b) { return new tuple2<__ss_int, __ss_int>(2, __floordiv(a,b), __mods(a,b)); } #endif template<> inline tuple2 *divmod(int a, int b) { return new tuple2(2, __floordiv(a,b), __mods(a,b)); } inline tuple2 *divmod(double a, __ss_int b) { return divmod(a, (double)b); } inline tuple2 *divmod(__ss_int a, double b) { return divmod((double)a, b); } /* add */ template inline T __add(T a, T b) { return a->__add__(b); } #ifdef __SS_LONG template<> inline __ss_int __add(__ss_int a, __ss_int b) { return a + b; } #endif template<> inline int __add(int a, int b) { return a + b; } template<> inline double __add(double a, double b) { return a + b; } /* reverse */ template U __add2(double a, U b) { return b->__add__(a); } template U __sub2(double a, U b) { return b->__rsub__(a); } template T __mul2(__ss_int n, T a) { return a->__mul__(n); } template T __mul2(__ss_bool n, T a) { return a->__mul__(n.value); } template T __mul2(double n, T a) { return a->__mul__(n); } template T __div2(__ss_int n, T a) { return a->__rdiv__(n); } template T __div2(double n, T a) { return a->__rdiv__(n); } /* float.is_integer */ inline __ss_bool __ss_is_integer(double d) { return __mbool((long long)d == d); } shedskin-0.9.4/shedskin/lib/builtin/tuple.hpp0000664000175000017500000001715312157270661021156 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* tuple2 methods */ template void tuple2::__init2__(T a, T b) { units.push_back(a); units.push_back(b); } template tuple2::tuple2() { this->__class__ = cl_tuple; } template tuple2::tuple2(int count, ...) { this->__class__ = cl_tuple; va_list ap; va_start(ap, count); for(int i=0; iunits.push_back(t); } va_end(ap); } template template tuple2::tuple2(U *iter) { this->__class__ = cl_tuple; typename U::for_in_unit e; typename U::for_in_loop __3; int __2; U *__1; FOR_IN(e,iter,1,2,3) this->units.push_back(e); END_FOR } template tuple2::tuple2(list *p) { this->__class__ = cl_tuple; this->units = p->units; } template tuple2::tuple2(tuple2 *p) { this->__class__ = cl_tuple; this->units = p->units; } template tuple2::tuple2(str *s) { this->__class__ = cl_tuple; this->units.resize(len(s)); int sz = s->unit.size(); for(int i=0; iunits[i] = __char_cache[((unsigned char)(s->unit[i]))]; } template T tuple2::__getfirst__() { return this->units[0]; } template T tuple2::__getsecond__() { return this->units[1]; } template inline T tuple2::__getfast__(__ss_int i) { i = __wrap(this, i); return this->units[i]; } template __ss_int tuple2::__len__() { return units.size(); } template T tuple2::__getitem__(__ss_int i) { i = __wrap(this, i); return units[i]; } template str *tuple2::__repr__() { str *r = new str("("); for(int i = 0; i__len__();i++) { r->unit += repr(this->units[i])->unit; if(this->__len__() == 1 ) r->unit += ","; if(i__len__()-1) r->unit += ", "; } r->unit += ")"; return r; } template tuple2 *tuple2::__add__(tuple2 *b) { tuple2 *c = new tuple2(); for(int i = 0; i__len__();i++) c->units.push_back(this->units[i]); for(int i = 0; i__len__();i++) c->units.push_back(b->units[i]); return c; } template tuple2 *tuple2::__iadd__(tuple2 *b) { return __add__(b); } template tuple2 *tuple2::__mul__(__ss_int b) { tuple2 *c = new tuple2(); if(b<=0) return c; __ss_int hop = this->__len__(); /* XXX merge with list */ if(hop==1) c->units.insert(c->units.begin(), b, this->units[0]); else for(__ss_int i=0; iunits.push_back(this->units[j]); return c; } template tuple2 *tuple2::__imul__(__ss_int b) { return __mul__(b); } template __ss_bool tuple2::__contains__(T a) { for(int i=0; i__len__(); i++) if(__eq(this->units[i], a)) return True; return False; } template __ss_bool tuple2::__eq__(pyobj *p) { tuple2 *b; b = (tuple2 *)p; unsigned int sz = this->units.size(); if(b->units.size() != sz) return False; for(unsigned int i=0; iunits[i], b->units[i])) return False; return True; } template tuple2 *tuple2::__slice__(__ss_int x, __ss_int l, __ss_int u, __ss_int s) { tuple2 *c = new tuple2(); slicenr(x, l, u, s, this->__len__()); if(s == 1) { c->units.resize(u-l); memcpy(&(c->units[0]), &(this->units[l]), sizeof(T)*(u-l)); } else if(s > 0) for(int i=l; iunits.push_back(units[i]); else for(int i=l; i>u; i += s) c->units.push_back(units[i]); return c; } template long tuple2::__hash__() { long seed = 0; size_t sz = this->units.size(); for(size_t i = 0; i(this->units[i])); return seed; } template tuple2 *tuple2::__copy__() { tuple2 *c = new tuple2(); c->units = this->units; return c; } template tuple2 *tuple2::__deepcopy__(dict *memo) { tuple2 *c = new tuple2(); memo->__setitem__(this, c); c->units.resize(this->__len__()); for(int i=0; i__len__(); i++) c->units[i] = __deepcopy(this->units[i], memo); return c; } template inline bool tuple2::for_in_has_next(size_t i) { return i < units.size(); /* XXX opt end cond */ } template inline T tuple2::for_in_next(size_t &i) { return units[i++]; } #ifdef __SS_BIND template tuple2::tuple2(PyObject *p) { if(!PyTuple_Check(p)) throw new TypeError(new str("error in conversion to Shed Skin (tuple expected)")); this->__class__ = cl_tuple; int size = PyTuple_Size(p); for(int i=0; iunits.push_back(__to_ss(PyTuple_GetItem(p, i))); } template PyObject *tuple2::__to_py__() { int len = this->__len__(); PyObject *p = PyTuple_New(len); for(int i=0; i__getitem__(i))); return p; } #endif /* tuple2 methods (binary) */ template void tuple2::__init2__(A a, B b) { first = a; second = b; } template tuple2::tuple2() { this->__class__ = cl_tuple; } template tuple2::tuple2(int, A a, B b) { this->__class__ = cl_tuple; first = a; second = b; } template A tuple2::__getfirst__() { return first; } template B tuple2::__getsecond__() { return second; } template __ss_int tuple2::__len__() { return 2; } template __ss_bool tuple2::__eq__(pyobj *p) { tuple2 *b = (tuple2 *)p; return __mbool(__eq(first, b->__getfirst__()) & __eq(second, b->__getsecond__())); } template __ss_int tuple2::__cmp__(pyobj *p) { if (!p) return 1; tuple2 *b = (tuple2 *)p; if(int c = __cmp(first, b->first)) return c; return __cmp(second, b->second); } template long tuple2::__hash__() { long seed = 0; seed = hash_combine(seed, hasher(first)); seed = hash_combine(seed, hasher(second)); return seed; } template str *tuple2::__repr__() { __GC_STRING s = "("+repr(first)->unit+", "+repr(second)->unit+")"; return new str(s); } template tuple2 *tuple2::__copy__() { return new tuple2(2, first, second); } template tuple2 *tuple2::__deepcopy__(dict *memo) { tuple2 *n = new tuple2(); memo->__setitem__(this, n); n->first = __deepcopy(first, memo); n->second = __deepcopy(second, memo); return n; } #ifdef __SS_BIND template tuple2::tuple2(PyObject *p) { if(!PyTuple_Check(p)) throw new TypeError(new str("error in conversion to Shed Skin (tuple expected)")); this->__class__ = cl_tuple; first = __to_ss(PyTuple_GetItem(p, 0)); second = __to_ss(PyTuple_GetItem(p, 1)); } template PyObject *tuple2::__to_py__() { PyObject *p = PyTuple_New(2); PyTuple_SetItem(p, 0, __to_py(first)); PyTuple_SetItem(p, 1, __to_py(second)); return p; } #endif shedskin-0.9.4/shedskin/lib/builtin/hash.hpp0000664000175000017500000000172712157270661020750 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ /* hashing */ static inline long hash_combine(long seed, long other) { return seed ^ (other + 0x9e3779b9 + (seed << 6) + (seed >> 2)); } template inline long hasher(T t) { if(t == NULL) return 0; return t->__hash__(); } #ifdef __SS_LONG template<> inline long hasher(__ss_int a) { return (a==-1)?-2:a; } #endif template<> inline long hasher(int a) { return (a==-1)?-2:a; } template<> inline long hasher(__ss_bool a) { return a.value; } template<> inline long hasher(void *a) { return (intptr_t)a; } template<> inline long hasher(double v) { long hipart, x; /* modified from CPython */ int expo; v = frexp(v, &expo); v *= 2147483648.0; /* 2**31 */ hipart = (long)v; /* take the top 32 bits */ v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */ x = hipart + (long)v + (expo << 15); if (x== -1) x = -2; return x; } shedskin-0.9.4/shedskin/lib/cStringIO.py0000664000175000017500000000023412157270661020051 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) class StringI(file): pass def StringIO(s=''): return StringI(s) shedskin-0.9.4/shedskin/lib/binascii.cpp0000664000175000017500000011310612157270661020126 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "binascii.hpp" #include namespace __binascii__ { str *__name__; void * default_4; __ss_bool default_1; __ss_bool default_0; __ss_bool default_3; __ss_bool default_2; void * default_5; // XXX #define PY_SSIZE_T_MAX INT_MAX /** class Error */ class_ *cl_Error; /** class Incomplete */ class_ *cl_Incomplete; str *hexlify(str *data) { // output will be twice as long __ss_int len = data->__len__(); __GC_STRING hexstr = __GC_STRING(data->unit); hexstr.reserve(len<<1); hexstr.resize(len<<1); str * hex = new str(hexstr); char * curdata = &data->unit[0]; char * curhex = &hex->unit[0]; char * end = curdata+len; char c; // from python's implementation (2.7.1, if it matters) while(curdata <= end) { c = (*curdata>>4) & 0xf; c = (c>9) ? c+'a'-10 : c + '0'; *(curhex++) = c; c = (*curdata) & 0xf; c = (c>9) ? c+'a'-10 : c + '0'; *(curhex++) = c; curdata++; } return hex; } /* The following table generated with: lst = [i-ord('0') if (i>=ord('0') and i<=ord('9')) else (i-ord('a')+10 if (i>=ord('a') and i<=ord('f')) else (i-ord('A')+10 if (i>=ord('A') and i<=ord('F')) else -1)) for i in xrange(256)] for i in xrange(0,256,16): # lst[i:i+16] lines sys.stdout.write(' ') for j in xrange(i,i+16,4): # lst[j:j+4] groups for k in xrange(j,j+4): # lst[k] numbers sys.stdout.write(str(lst[k]).rjust(2)+',') if j+4!=i+16: # if not last group sys.stdout.write(' ') sys.stdout.write('\n') */ static char table_a2b_hex[] = { -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1, -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, }; str *unhexlify(str *hex) { // output will be half as long __ss_int len = hex->__len__(); if ( len&1 ) throw new Error(0); //new str("Odd-length string")); str * data = new str("",len>>1); char * curdata = &data->unit[0]; char * curhex = &hex->unit[0]; char * end = curhex+len; char top,bot; // from python's implementation (2.7.1, if it matters), but way better :) while(curhex <= end-2) // must be two characters left { top = table_a2b_hex[(int)*(curhex++)]; bot = table_a2b_hex[(int)*(curhex++)]; if (top==-1 || bot==-1) throw new Error(0); //new str("Invalid hex")); *(curdata++) = (top<<4) + bot; } return data; } // from python 2.7.1 str *a2b_uu(str *string) { __ss_int ascii_len = string->__len__(); char * ascii_data = &string->unit[0]; __ss_int bin_len = (*ascii_data++ - ' ') & 077; str * binary = new str("",bin_len); char * bin_data = &binary->unit[0]; unsigned char this_ch; __ss_int leftchar=0, leftbits=0; ascii_len--; for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) { /* XXX is it really best to add NULs if there's no more data */ this_ch = (ascii_len > 0) ? *ascii_data : 0; if ( this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) { /* ** Whitespace. Assume some spaces got eaten at ** end-of-line. (We check this later) */ this_ch = 0; } else { /* Check the character for legality ** The 64 in stead of the expected 63 is because ** there are a few uuencodes out there that use ** '`' as zero instead of space. */ if ( this_ch < ' ' || this_ch > (' ' + 64)) { throw new Error(0); } this_ch = (this_ch - ' ') & 077; } /* ** Shift it in on the low end, and see if there's ** a byte ready for output. */ leftchar = (leftchar << 6) | (this_ch); leftbits += 6; if ( leftbits >= 8 ) { leftbits -= 8; *bin_data++ = (leftchar >> leftbits) & 0xff; leftchar &= ((1 << leftbits) - 1); bin_len--; } } /* ** Finally, check that if there's anything left on the line ** that it's whitespace only. */ while( ascii_len-- > 0 ) { this_ch = *ascii_data++; /* Extra '`' may be written as padding in some cases */ if ( this_ch != ' ' && this_ch != ' '+64 && this_ch != '\n' && this_ch != '\r' ) { throw new Error(0); } } return binary; } str *b2a_uu(str *binary) { __ss_int bin_len = binary->__len__(); if ( bin_len > 45 ) { /* The 45 is a limit that appears in all uuencode's */ throw new Error(0); //At most 45 bytes at once } char * bin_data = &binary->unit[0]; /* We're lazy and allocate too much (fixed up later) */ __ss_int ascii_len = 2 + (bin_len+2)/3*4; str * ascii = new str("",ascii_len); char * ascii_data = &ascii->unit[0]; char * ascii_start = ascii_data; unsigned char this_ch; __ss_int leftchar=0, leftbits=0; /* Store the length */ *ascii_data++ = ' ' + (bin_len & 077); for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) { /* Shift the data (or padding) into our buffer */ if ( bin_len > 0 ) /* Data */ leftchar = (leftchar << 8) | *bin_data; else /* Padding */ leftchar <<= 8; leftbits += 8; /* See if there are 6-bit groups ready */ while ( leftbits >= 6 ) { this_ch = (leftchar >> (leftbits-6)) & 0x3f; leftbits -= 6; *ascii_data++ = this_ch + ' '; } } *ascii_data++ = '\n'; /* Append a courtesy newline */ ascii->unit.resize( (ascii_data - ascii_start) ); return ascii; } static char table_a2b_base64[] = { -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, /* Note PAD->0 */ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1 }; #define BASE64_PAD '=' /* Max binary chunk size; limited only by available memory */ #define BASE64_MAXBIN (PY_SSIZE_T_MAX/2 - sizeof(PyStringObject) - 3) static unsigned char table_b2a_base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int find_valid(char *s, __ss_int slen, int num) { /* Finds & returns the (num+1)th ** valid character for base64, or -1 if none. */ int ret = -1; unsigned char c, b64val; while ((slen > 0) && (ret == -1)) { c = *s; b64val = table_a2b_base64[c & 0x7f]; if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) { if (num == 0) ret = *s; num--; } s++; slen--; } return ret; } // from python 2.7.1 str *a2b_base64(str *pascii) { char * ascii_data = &pascii->unit[0]; __ss_int ascii_len = pascii->__len__(); if (ascii_len > PY_SSIZE_T_MAX-3) { throw new Error(0); } int quad_pos = 0; int leftbits = 0; unsigned char this_ch; unsigned int leftchar = 0; __ss_int bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */ str * binary = new str("",bin_len); char * bin_data = &binary->unit[0]; bin_len = 0; for( ; ascii_len > 0; ascii_len--, ascii_data++) { this_ch = *ascii_data; if (this_ch > 0x7f || this_ch == '\r' || this_ch == '\n' || this_ch == ' ') continue; /* Check for pad sequences and ignore ** the invalid ones. */ if (this_ch == BASE64_PAD) { if ( (quad_pos < 2) || ((quad_pos == 2) && (find_valid(ascii_data, ascii_len, 2) != BASE64_PAD)) ) { continue; } else { /* A pad sequence means no more input. ** We've already interpreted the data ** from the quad at this point. */ leftbits = 0; break; } } this_ch = table_a2b_base64[*ascii_data]; if ( this_ch == (unsigned char) -1 ) continue; /* ** Shift it in on the low end, and see if there's ** a byte ready for output. */ quad_pos = (quad_pos + 1) & 0x03; leftchar = (leftchar << 6) | (this_ch); leftbits += 6; if ( leftbits >= 8 ) { leftbits -= 8; *bin_data++ = (leftchar >> leftbits) & 0xff; bin_len++; leftchar &= ((1 << leftbits) - 1); } } if (leftbits != 0) { throw new Error(0); // "Incorrect padding" } /* And set string size correctly. If the result string is empty ** (because the input was all invalid) return the shared empty ** string instead; _PyString_Resize() won't do this for us. */ if (bin_len > 0) { binary->unit.resize(bin_len); } else { // reset binary to "" binary = new str(""); } return binary; } str *b2a_base64(str *binary) { __ss_int bin_len = binary->__len__(); /* if (bin_len > BASE64_MAXBIN) { throw new Error(0); //Too much data for base64 line } XXX */ char * bin_data = &binary->unit[0]; __ss_int leftbits = 0,leftchar = 0; char this_ch; str * ascii = new str("",bin_len*2 + 3); char * ascii_data = &ascii->unit[0]; char * ascii_start = ascii_data; for( ; bin_len > 0 ; bin_len--, bin_data++ ) { /* Shift the data into our buffer */ leftchar = (leftchar << 8) | *bin_data; leftbits += 8; /* See if there are 6-bit groups ready */ while ( leftbits >= 6 ) { this_ch = (leftchar >> (leftbits-6)) & 0x3f; leftbits -= 6; *ascii_data++ = table_b2a_base64[this_ch]; } } if ( leftbits == 2 ) { *ascii_data++ = table_b2a_base64[(leftchar&3) << 4]; *ascii_data++ = BASE64_PAD; *ascii_data++ = BASE64_PAD; } else if ( leftbits == 4 ) { *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2]; *ascii_data++ = BASE64_PAD; } *ascii_data++ = '\n'; /* Append a courtesy newline */ // resize to ascii_data - start ascii->unit.resize(ascii_data-ascii_start); return ascii; } str *a2b_qp(str *pdata, __ss_bool header) { // from python 2.7.1 __ss_int datalen = pdata->__len__(); char * data = &pdata->unit[0]; /* We allocate the output same size as input, this is overkill. * The previous implementation used calloc() so we'll zero out the * memory here too, since PyMem_Malloc() does not guarantee that. */ str * outdata = new str("",datalen); char * odata = &outdata->unit[0]; memset(odata,0,datalen); __ss_int in,out; char hexval,top,bot; in = out = 0; while (in < datalen) { if (data[in] == '=') { in++; if (in >= datalen) break; /* Soft line breaks */ if ((data[in] == '\n') || (data[in] == '\r')) { if (data[in] != '\n') { while (in < datalen && data[in] != '\n') in++; } if (in < datalen) in++; } else if (data[in] == '=') { /* broken case from broken python qp */ odata[out++] = '='; in++; } // [fahhem] replacing hexval with table_a2b_hex else { top = table_a2b_hex[data[in]]; bot = table_a2b_hex[data[in+1]]; if (top==-1 || bot==-1) odata[out++] = '='; else { in+=2; odata[out++] = (top<<4)|bot; } } } else if (header && data[in] == '_') { odata[out++] = ' '; in++; } else { odata[out] = data[in]; in++; out++; } } outdata->unit.resize(out); return outdata; } #define MAXLINESIZE 76 static unsigned char table_b2a_hex[] = "0123456789ABCDEF"; void to_hex (unsigned char ch, unsigned char *s) { unsigned int uvalue = ch; s[1] = table_b2a_hex[uvalue & 0x0F]; uvalue >>= 4; s[0] = table_b2a_hex[uvalue & 0x0F]; } /* XXX: This is ridiculously complicated to be backward compatible * (mostly) with the quopri module. It doesn't re-create the quopri * module bug where text ending in CRLF has the CR encoded */ str *b2a_qp(str *pdata, __ss_bool quotetabs, __ss_bool istext, __ss_bool header) { __ss_int datalen = pdata->__len__(); char * data = &pdata->unit[0]; char * p = (char *) memchr(data, '\n', datalen); int crlf = 0; __ss_int in, out, odatalen=0; int linelen=0; char ch; /* See if this string is using CRLF line ends */ /* XXX: this function has the side effect of converting all of * the end of lines to be the same depending on this detection * here */ if ((p != NULL) && (p > data) && (*(p-1) == '\r')) crlf = 1; /* First, scan to see how many characters need to be encoded */ in = 0; while (in < datalen) { if ((data[in] > 126) || (data[in] == '=') || (header && data[in] == '_') || ((data[in] == '.') && (linelen == 0) && (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) || (!istext && ((data[in] == '\r') || (data[in] == '\n'))) || ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) || ((data[in] < 33) && (data[in] != '\r') && (data[in] != '\n') && (quotetabs || (!quotetabs && ((data[in] != '\t') && (data[in] != ' ')))))) { if ((linelen + 3) >= MAXLINESIZE) { linelen = 0; if (crlf) odatalen += 3; else odatalen += 2; } linelen += 3; odatalen += 3; in++; } else { if (istext && ((data[in] == '\n') || ((in+1 < datalen) && (data[in] == '\r') && (data[in+1] == '\n')))) { linelen = 0; /* Protect against whitespace on end of line */ if (in && ((data[in-1] == ' ') || (data[in-1] == '\t'))) odatalen += 2; if (crlf) odatalen += 2; else odatalen += 1; if (data[in] == '\r') in += 2; else in++; } else { if ((in + 1 != datalen) && (data[in+1] != '\n') && (linelen + 1) >= MAXLINESIZE) { linelen = 0; if (crlf) odatalen += 3; else odatalen += 2; } linelen++; odatalen++; in++; } } } /* We allocate the output same size as input, this is overkill. * The previous implementation used calloc() so we'll zero out the * memory here too, since PyMem_Malloc() does not guarantee that. */ str * outdata = new str("",odatalen); unsigned char * odata = (unsigned char *)&outdata->unit[0]; memset(odata, 0, odatalen); in = out = linelen = 0; while (in < datalen) { if ((data[in] > 126) || (data[in] == '=') || (header && data[in] == '_') || ((data[in] == '.') && (linelen == 0) && (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) || (!istext && ((data[in] == '\r') || (data[in] == '\n'))) || ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) || ((data[in] < 33) && (data[in] != '\r') && (data[in] != '\n') && (quotetabs || (!quotetabs && ((data[in] != '\t') && (data[in] != ' ')))))) { if ((linelen + 3 )>= MAXLINESIZE) { odata[out++] = '='; if (crlf) odata[out++] = '\r'; odata[out++] = '\n'; linelen = 0; } odata[out++] = '='; to_hex(data[in], &odata[out]); out += 2; in++; linelen += 3; } else { if (istext && ((data[in] == '\n') || ((in+1 < datalen) && (data[in] == '\r') && (data[in+1] == '\n')))) { linelen = 0; /* Protect against whitespace on end of line */ if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) { ch = odata[out-1]; odata[out-1] = '='; to_hex(ch, &odata[out]); out += 2; } if (crlf) odata[out++] = '\r'; odata[out++] = '\n'; if (data[in] == '\r') in += 2; else in++; } else { if ((in + 1 != datalen) && (data[in+1] != '\n') && (linelen + 1) >= MAXLINESIZE) { odata[out++] = '='; if (crlf) odata[out++] = '\r'; odata[out++] = '\n'; linelen = 0; } linelen++; if (header && data[in] == ' ') { odata[out++] = '_'; in++; } else { odata[out++] = data[in++]; } } } } outdata->unit.resize(out); return outdata; } /* ** hqx lookup table, ascii->binary. */ #define DONE 0x7F #define SKIP 0x7E #define FAIL 0x7D static unsigned char table_a2b_hqx[256] = { /* ^@ ^A ^B ^C ^D ^E ^F ^G */ /* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, /* \b \t \n ^K ^L \r ^N ^O */ /* 1*/ FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL, /* ^P ^Q ^R ^S ^T ^U ^V ^W */ /* 2*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */ /* 3*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, /* ! " # $ % & ' */ /* 4*/ FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* ( ) * + , - . / */ /* 5*/ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL, /* 0 1 2 3 4 5 6 7 */ /* 6*/ 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL, /* 8 9 : ; < = > ? */ /* 7*/ 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL, /* @ A B C D E F G */ /* 8*/ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, /* H I J K L M N O */ /* 9*/ 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL, /* P Q R S T U V W */ /*10*/ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL, /* X Y Z [ \ ] ^ _ */ /*11*/ 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL, /* ` a b c d e f g */ /*12*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL, /* h i j k l m n o */ /*13*/ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL, /* p q r s t u v w */ /*14*/ 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL, /* x y z { | } ~ ^? */ /*15*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, /*16*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, }; tuple2 *a2b_hqx(str *pascii) { __ss_int len = pascii->__len__(); if (len > PY_SSIZE_T_MAX - 2){ throw new Error(0); //No memory } unsigned char * ascii_data = (unsigned char *)&pascii->unit[0]; unsigned char * ascii_start = ascii_data; int done = 0, leftbits=0, leftchar=0; unsigned char this_ch; /* Allocate a string that is too big (fixed later) Add two to the initial length to prevent interning which would preclude subsequent resizing. */ str * outdata = new str("",len+2); unsigned char * bin_data = (unsigned char *)&outdata->unit[0]; unsigned char * bin_start = bin_data; for( ; len > 0 ; len--, ascii_data++ ) { /* Get the byte and look it up */ this_ch = table_a2b_hqx[*ascii_data]; if ( this_ch == SKIP ) continue; if ( this_ch == FAIL ) throw new Error(0); //Illegal char if ( this_ch == DONE ) { /* The terminating colon */ done = 1; break; } /* Shift it into the buffer and see if any bytes are ready */ leftchar = (leftchar << 6) | (this_ch); leftbits += 6; if ( leftbits >= 8 ) { leftbits -= 8; *(bin_data++) = (leftchar >> leftbits) & 0xff; leftchar &= ((1 << leftbits) - 1); } } if ( leftbits && !done ) throw new Error(0); //(Incomplete) String has incomplete number of bytes outdata->unit.resize(bin_data-bin_start); return new tuple2(2,outdata,done); } static unsigned char table_b2a_hqx[] = "!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr"; str *b2a_hqx(str *binary) { __ss_int bin_len = binary->__len__(); if (bin_len > PY_SSIZE_T_MAX / 2 - 2) { throw new Error(0); // out of memory } char * bin_data = &binary->unit[0]; str * ascii = new str("",(bin_len<<1)+2); char * ascii_data = &ascii->unit[0]; char * ascii_start = ascii_data; int leftbits = 0, leftchar = 0; char this_ch; for( ; bin_len > 0 ; bin_len--, bin_data++ ) { /* Shift into our buffer, and output any 6bits ready */ leftchar = (leftchar << 8) | *bin_data; leftbits += 8; while ( leftbits >= 6 ) { this_ch = (leftchar >> (leftbits-6)) & 0x3f; leftbits -= 6; *ascii_data++ = table_b2a_hqx[this_ch]; } } /* Output a possible runt byte */ if ( leftbits ) { leftchar <<= (6-leftbits); *ascii_data++ = table_b2a_hqx[leftchar & 0x3f]; } ascii->unit.resize(ascii_data-ascii_start); return ascii; } #define RUNCHAR 0x90 str *rledecode_hqx(str * in_data_str) { __ss_int in_len = in_data_str->__len__(); if ( in_len == 0 ) { return new str("",0); } else if (in_len > PY_SSIZE_T_MAX / 2) { throw new Error(0); // no memory } char * in_data = &in_data_str->unit[0]; char in_byte, in_repeat; /* Allocate a buffer of reasonable size. Resized when needed */ __ss_int out_len = in_len<<1; str * out_data_str = new str("",out_len); __ss_int out_len_left = out_len; char * out_data = &out_data_str->unit[0]; char * out_data_start = out_data; /* ** We need two macros here to get/put bytes and handle ** end-of-buffer for input and output strings. */ #define INBYTE(b) \ do { \ if ( --in_len < 0 ) { \ throw new Error(0); /* Incomplete */ \ } \ b = *in_data++; \ } while(0) #define OUTBYTE(b) \ do { \ if ( --out_len_left < 0 ) { \ if ( out_len > PY_SSIZE_T_MAX / 2) throw new Error(0); /*No Memory*/ \ out_data_str->unit.resize(out_len<<1);\ out_data = out_data_start + out_len; \ out_len_left = out_len-1; \ out_len *= 2; \ } \ *out_data++ = b; \ } while(0) /* ** Handle first byte separately (since we have to get angry ** in case of an orphaned RLE code). */ INBYTE(in_byte); if (in_byte == RUNCHAR) { INBYTE(in_repeat); if (in_repeat != 0) { /* Note Error, not Incomplete (which is at the end ** of the string only). This is a programmer error. */ throw new Error(0); // "Orphaned RLE code at start" } OUTBYTE(RUNCHAR); } else { OUTBYTE(in_byte); } while( in_len > 0 ) { INBYTE(in_byte); if (in_byte == RUNCHAR) { INBYTE(in_repeat); if ( in_repeat == 0 ) { /* Just an escaped RUNCHAR value */ OUTBYTE(RUNCHAR); } else { /* Pick up value and output a sequence of it */ in_byte = out_data[-1]; while ( --in_repeat > 0 ) OUTBYTE(in_byte); } } else { /* Normal byte */ OUTBYTE(in_byte); } } out_data_str->unit.resize(out_data-out_data_start); return out_data_str; } str *rlecode_hqx(str *data) { __ss_int len = data->__len__(); if (len > PY_SSIZE_T_MAX / 2 - 2) { throw new Error(0); // no memory } char * in_data = &data->unit[0]; str * rv = new str("", len*2+2); char * out_data = &rv->unit[0]; char * out_start = out_data; char ch; __ss_int in, inend; for( in=0; in 3 ) { /* More than 3 in a row. Output RLE. */ *out_data++ = ch; *out_data++ = RUNCHAR; *out_data++ = inend-in; in = inend-1; } else { /* Less than 3. Output the byte itself */ *out_data++ = ch; } } } rv->unit.resize(out_data-out_start); return rv; } static unsigned short crctab_hqx[256] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0, }; __ss_int crc_hqx(str *data, __ss_int crc) { __ss_int len = data->__len__(); char * bin_data = &data->unit[0]; while (len-- > 0) { crc=((crc<<8)&0xff00)^crctab_hqx[((crc>>8)&0xff)^*bin_data++]; } return crc; } /* Crc - 32 BIT ANSI X3.66 CRC checksum files Also known as: ISO 3307 **********************************************************************| * *| * Demonstration program to compute the 32-bit CRC used as the frame *| * check sequence in ADCCP (ANSI X3.66, also known as FIPS PUB 71 *| * and FED-STD-1003, the U.S. versions of CCITT's X.25 link-level *| * protocol). The 32-bit FCS was added via the Federal Register, *| * 1 June 1982, p.23798. I presume but don't know for certain that *| * this polynomial is or will be included in CCITT V.41, which *| * defines the 16-bit CRC (often called CRC-CCITT) polynomial. FIPS *| * PUB 78 says that the 32-bit FCS reduces otherwise undetected *| * errors by a factor of 10^-5 over 16-bit FCS. *| * *| **********************************************************************| Copyright (C) 1986 Gary S. Brown. You may use this program, or code or tables extracted from it, as desired without restriction. First, the polynomial itself and its table of feedback terms. The polynomial is X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 Note that we take it "backwards" and put the highest-order term in the lowest-order bit. The X^32 term is "implied"; the LSB is the X^31 term, etc. The X^0 term (usually shown as "+1") results in the MSB being 1. Note that the usual hardware shift register implementation, which is what we're using (we're merely optimizing it by doing eight-bit chunks at a time) shifts bits into the lowest-order term. In our implementation, that means shifting towards the right. Why do we do it this way? Because the calculated CRC must be transmitted in order from highest-order term to lowest-order term. UARTs transmit characters in order from LSB to MSB. By storing the CRC this way, we hand it to the UART in the order low-byte to high-byte; the UART sends each low-bit to hight-bit; and the result is transmission bit by bit from highest- to lowest-order term without requiring any bit shuffling on our part. Reception works similarly. The feedback terms table consists of 256, 32-bit entries. Notes: 1. The table can be generated at runtime if desired; code to do so is shown later. It might not be obvious, but the feedback terms simply represent the results of eight shift/xor opera- tions for all combinations of data and CRC register values. 2. The CRC accumulation logic is the same for all CRC polynomials, be they sixteen or thirty-two bits wide. You simply choose the appropriate table. Alternatively, because the table can be generated at runtime, you can start by generating the table for the polynomial in question and use exactly the same "updcrc", if your application needn't simultaneously handle two CRC polynomials. (Note, however, that XMODEM is strange.) 3. For 16-bit CRCs, the table entries need be only 16 bits wide; of course, 32-bit entries work OK if the high 16 bits are zero. 4. The values must be right-shifted by eight bits by the "updcrc" logic; the shift must be unsigned (bring in zeroes). On some hardware you could probably optimize the shift in assembler by using byte-swap instructions. ********************************************************************/ static unsigned int crc_32_tab[256] = { 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U, 0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U, 0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U, 0x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU, 0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U, 0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U, 0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U, 0xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU, 0x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U, 0x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU, 0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U, 0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U, 0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U, 0x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU, 0x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU, 0xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U, 0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU, 0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U, 0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U, 0xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U, 0x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU, 0x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U, 0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U, 0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU, 0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U, 0x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U, 0x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U, 0x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U, 0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U, 0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU, 0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU, 0x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U, 0xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U, 0xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU, 0x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU, 0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U, 0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU, 0xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U, 0x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU, 0x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U, 0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU, 0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U, 0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U, 0x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU, 0x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U, 0xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U, 0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U, 0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U, 0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U, 0xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U, 0x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU, 0x2d02ef8dU }; __ss_int crc32(str *data, __ss_int signed_crc) { __ss_int len = data->__len__(); unsigned int crc = signed_crc; crc = ~crc; char * bin_data = &data->unit[0]; while (len-- > 0) { crc = crc_32_tab[(crc ^ *bin_data++) & 0xffU] ^ (crc >> 8); } return (__ss_int)(crc ^ 0xFFFFFFFFU); } str *b2a_hex(str *data) { return hexlify(data); } str *a2b_hex(str *data) { return unhexlify(data); } void __init() { __name__ = new str("binascii"); cl_Error = new class_("Error"); cl_Incomplete = new class_("Incomplete"); default_0 = False; default_1 = False; default_2 = False; default_3 = False; } } // module namespace shedskin-0.9.4/shedskin/lib/re.hpp0000664000175000017500000001077712157270661016772 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __RE_HPP #define __RE_HPP //depending on what you want... //#define PCRE_STATIC #include "builtin.hpp" #ifndef __sun #include #else #include #endif using namespace __shedskin__; namespace __re__ { extern const __ss_int I, L, M, S, U, X, IGNORECASE, LOCALE, MULTILINE, DOTALL, __ss_UNICODE, VERBOSE; class match_object; typedef str *(*replfunc)(match_object *); extern class_ *cl_error; //re.error class error : public Exception { public: error(str *m = 0) : Exception(m) {} #ifdef __SS_BIND PyObject *__to_py__() { return PyExc_Exception; } #endif }; //we have a circular declaration, so we need to forward declare re_object class re_object; //MatchObject class match_object : public pyobj { public: //our regular expression re_object *re; //internal: captured subpatterns int *captured; //self-explanatory __ss_int pos, endpos; //last match and last named match __ss_int lastindex; //subject string str *string; //NOTE: not implemented str *lastgroup; //functions str *expand(str *tpl); str *group(__ss_int n, __ss_int m = 0); tuple2 *group(__ss_int n, __ss_int m, __ss_int o, ...); str *group(__ss_int n, str *m); tuple2 *group(__ss_int n, str *m, str *o, ...); dict *groupdict(str *defval = 0); tuple2 *groups(str *defval = 0); __ss_int __index(__ss_int matchid, char isend); __ss_int __index(str *mname, char isend); __ss_int end(__ss_int matchid = 0); __ss_int end(str *mname); __ss_int start(__ss_int matchid = 0); __ss_int start(str *mname); tuple2<__ss_int, __ss_int> *span(__ss_int matchid = 0); tuple2<__ss_int, __ss_int> *span(str *mname); str *__repr__(); }; //compiled regular expression class re_object : public pyobj { public: //named captured subpatterns dict *groupindex; //how many captured subpatterns there are __ss_int capture_count; //the original pattern str *pattern; //the flags used __ss_int flags; //internal functions __GC_STRING __group(__GC_STRING *subj, int *captured, __ss_int m); __GC_STRING __group(__GC_STRING *subj, int *captured, str *m); __GC_STRING __expand(__GC_STRING *subj, int *captured, __GC_STRING tpl); //the compiled pattern + extra info for optimization pcre *compiled_pattern; pcre_extra *study_info; match_object *__exec(str *subj, __ss_int pos = 0, __ss_int endpos = -1, __ss_int flags = 0); str *__subn(str *repl, str *subj, __ss_int maxn = -1, int *howmany = 0); list *__splitfind(str *subj, __ss_int maxn, char onlyfind, __ss_int flags); __ss_int __convert_flags(__ss_int flags); match_object *match(str *subj, __ss_int pos = 0, __ss_int endpos = -1); match_object *search(str *subj, __ss_int pos = 0, __ss_int endpos = -1); __iter *finditer(str *subj, __ss_int pos = 0, __ss_int endpos = -1, __ss_int flags = 0); list *split(str *subj, __ss_int maxn = -1); str *sub(str *repl, str *subj, __ss_int maxn = -1); str *sub(replfunc repl, str *subj, __ss_int maxn = -1); tuple2 *subn(str *repl, str *subj, __ss_int maxn = -1); list *findall(str *subj, __ss_int flags = 0); str *__repr__(); }; class match_iter : public __iter { public: re_object *ro; str *subj; __ss_int pos, endpos, flags; match_iter(re_object *ro, str *subj, __ss_int pos, __ss_int endpos, __ss_int flags); match_object *next(); }; re_object *compile(str *pat, __ss_int flags = 0); match_object *match(str *pat, str *subj, __ss_int flags = 0); match_object *search(str *pat, str *subj, __ss_int flags = 0); __iter *finditer(str *pat, str *subj, __ss_int pos = 0, __ss_int endpos = -1, __ss_int flags = 0); list *split(str *pat, str *subj, __ss_int maxn = 0); str *sub(str *pat, str *repl, str *subj, __ss_int maxn = 0); str *sub(str *pat, replfunc repl, str *subj, __ss_int maxn = 0); tuple2 *subn(str *pat, str *repl, str *subj, __ss_int maxn = 0); list *findall(str *pat, str *subj, __ss_int flags = 0); str *escape(str *s); list *__splitfind_once(str *pat, str *subj, __ss_int maxn, char onlyfind, __ss_int flags); match_object *__exec_once(str *subj, __ss_int flags); //internal functions void __init(void); void *re_malloc(size_t n); void re_free(void *o); } #endif shedskin-0.9.4/shedskin/lib/glob.py0000664000175000017500000000032712157270661017136 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) import os, os.path, fnmatch, re def iglob(s): return __iter('') def glob(s): return [''] def has_magic(s): return True shedskin-0.9.4/shedskin/lib/re.cpp0000664000175000017500000004346112157270661016761 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "re.hpp" namespace __re__ { //flags const __ss_int I = 0x02, IGNORECASE = 0x02, L = 0x04, LOCALE = 0x04, M = 0x08, MULTILINE = 0x08, S = 0x10, DOTALL = 0x10, U = 0x20, __ss_UNICODE = 0x20, X = 0x40, VERBOSE = 0x40; const unsigned char *local_table; class_ *cl_error; //match_object functions str *match_object::group(__ss_int /* n */, __ss_int matchid) { return new str(re->__group(&string->unit, captured, matchid)); } str *match_object::group(__ss_int /* n */, str *mname) { return new str(re->__group(&string->unit, captured, mname)); } tuple2 *match_object::group(__ss_int n, __ss_int m, __ss_int o, ...) { tuple2 *t = new tuple2(); va_list ap; va_start(ap, o); t->units.push_back(group(1, m)); t->units.push_back(group(1, o)); for(__ss_int i=0; iunits.push_back(group(1, va_arg(ap, __ss_int))); va_end(ap); return t; } tuple2 *match_object::group(__ss_int n, str *m, str *o, ...) { tuple2 *t = new tuple2(); va_list ap; va_start(ap, o); t->units.push_back(group(1, m)); t->units.push_back(group(1, o)); for(__ss_int i=0; iunits.push_back(group(1, va_arg(ap, str *))); va_end(ap); return t; } //index functions __ss_int match_object::__index(__ss_int matchid, char isend) { if(matchid > lastindex) throw new error(new str("group does not exist or is unmatched")); return captured[matchid * 2 + isend]; } __ss_int match_object::__index(str *mname, char isend) { if(!re->groupindex->has_key(mname)) throw new error(new str("no such group exists")); return __index(re->groupindex->__getitem__(mname), isend); } __ss_int match_object::end(__ss_int matchid) { return __index(matchid, 1); } __ss_int match_object::end(str *mname) { return __index(mname, 1); } __ss_int match_object::start(__ss_int matchid) { return __index(matchid, 0); } __ss_int match_object::start(str *mname) { return __index(mname, 0); } tuple2<__ss_int, __ss_int> *match_object::span(__ss_int matchid) { return new tuple2<__ss_int, __ss_int>(2, start(matchid), end(matchid)); } tuple2<__ss_int, __ss_int> *match_object::span(str *mname) { return new tuple2<__ss_int, __ss_int>(2, start(mname), end(mname)); } str *match_object::expand(str *tpl) { return new str(re->__expand(&string->unit, captured, tpl->unit)); } tuple2 *match_object::groups(str *defval) { tuple2 *r; int i; r = new tuple2(); for(i = 1; i <= re->capture_count; i++) { if(captured[i * 2] != -1) r->units.push_back(new str(string->unit.substr(captured[i * 2], captured[i * 2 + 1] - captured[i * 2]))); else r->units.push_back(defval ? new str(defval->unit) : 0); } return r; } dict *match_object::groupdict(str *defval) { dict *r; int t; r = new dict(); str *k; dict::for_in_loop __3; int __2; dict *__1; FOR_IN(k,re->groupindex,1,2,3) t = re->groupindex->__getitem__(k); if(captured[t * 2] != -1) r->__setitem__(new str(k->unit), new str(string->unit.substr(captured[t * 2], captured[t * 2 + 1] - captured[t * 2]))); else r->__setitem__(new str(k->unit), defval ? new str(defval->unit) : 0); END_FOR return r; } str *match_object::__repr__() { return new str(""); } str *re_object::__repr__() { return new str(""); } //these are for internal use __GC_STRING re_object::__group(__GC_STRING *subj, int *captured, __ss_int matchid) { if(matchid > capture_count || matchid < 0) throw new error(new str("group does not exist")); if(captured[matchid * 2] == -1) throw new error(new str("group is unmatched")); return subj->substr(captured[matchid * 2], captured[matchid * 2 + 1] - captured[matchid * 2]); } __GC_STRING re_object::__group(__GC_STRING *subj, int *captured, str *mname) { if(!groupindex->has_key(mname)) throw new error(new str("no such group exists")); return __group(subj, captured, groupindex->__getitem__(mname)); } __GC_STRING re_object::__expand(__GC_STRING *subj, int *captured, __GC_STRING tpl) { __GC_STRING out; int i, j, len, ref; char c; out = ""; len = tpl.length(); for(i = 0; i < len; i++) { //zip past anything that we don't need to worry about j = i; while(tpl[i] != '\\' && i < len) i++; if(i - j) out += tpl.substr(j, i - j); if(i == len) break; //we've hit a backslash switch(tpl[++i]) { //reference case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : j = i; while(isdigit(tpl[i]) && i < len) i++; ref = strtol(tpl.substr(j, i - j).c_str(), 0, 10); out += __group(subj, captured, ref); i--; continue; //named reference case 'g' : if(tpl[++i] != '<') throw new error(new str("invalid name group")); i++; j = i; c = 1; while(tpl[i] != '>' && i < len) c = c && ::isdigit((int)tpl[i]), i++; if(tpl[i] != '>') throw new error(new str("unterminated name group")); if(::isdigit((int)tpl[j]) && !c) throw new error(new str("invalid first character in name group")); if(c) out += __group(subj, captured, strtol(tpl.substr(j, i - j).c_str(), 0, 10)); else out += __group(subj, captured, new str(tpl.substr(j, i - j))); continue; //escape char case 'n' : c = '\n'; break; case 'v' : c = '\v'; break; case 'a' : c = '\a'; break; case 'b' : c = '\b'; break; case 'f' : c = '\f'; break; case 't' : c = '\t'; break; case 'r' : c = '\r'; break; //nothing meaningful here, ignore default : c = 0; } if(c) out += c; else { out += '\\'; i--; } } return out; } //replacing pcre's allocation functions with ours using the garbage collector void *re_malloc(size_t n) { return GC_MALLOC(n); } void re_free(void *o) { GC_FREE(o); } str *re_object::__subn(str *repl, str *subj, __ss_int maxn, int *howmany) { __GC_STRING *s, out; int *captured, clen, i, cur; const char *c_subj; //temporary data clen = (capture_count + 1) * 2 * 3; captured = (int *)GC_MALLOC(clen * sizeof(int)); out = ""; s = &subj->unit; c_subj = s->c_str(); for(cur = i = 0; maxn <= 0 || cur < maxn; cur++) { //get a match if(pcre_exec( compiled_pattern, study_info, c_subj, s->size(), i, 0, captured, clen ) <= 0) break; //append stuff we skipped out += s->substr(i, captured[0] - i); //replace section out += __expand(s, captured, repl->unit); //move our index if(i == captured[1]) i++; else i = captured[1]; } //extra out += s->substr(i); if(howmany) *howmany = cur; GC_FREE(captured); return new str(out); } str *re_object::sub(str *repl, str *subj, __ss_int maxn) { return __subn(repl, subj, maxn, 0); } str *re_object::sub(replfunc func, str *string, __ss_int maxn) { list *l; int at; at = 0; l = (new list()); __re__::match_object *match; __iter<__re__::match_object *>::for_in_loop __3; int __2; __iter<__re__::match_object *> *__1; FOR_IN(match,finditer(string),1,2,3) l->append(string->__slice__(3, at, match->start(), 0)); l->append(func(match)); at = match->end(); if ((maxn>0)) { maxn = (maxn-1); if ((maxn==0)) { break; } } END_FOR l->append(string->__slice__(1, at, 0, 0)); return (new str(""))->join(l); } tuple2 *re_object::subn(str *repl, str *subj, __ss_int maxn) { str *r; int n; r = __subn(repl, subj, maxn, &n); return new tuple2(2, r, n); } list *re_object::__splitfind(str *subj, __ss_int maxn, char onlyfind, __ss_int flags) { __GC_STRING *subjs; list *r; int *captured, clen, i, j, cur; const char *c_subj; //temporary data clen = (capture_count + 1) * 2 * 3; captured = (int *)GC_MALLOC(clen * sizeof(int)); //'permanent' (in respect to the lifetime of this function) r = new list(); subjs = &subj->unit; c_subj = subjs->c_str(); for(cur = i = 0; maxn <= 0 || cur < maxn; cur++) { //get a match if(pcre_exec( compiled_pattern, study_info, c_subj, subjs->size(), i, flags, captured, clen ) <= 0) break; //this whole subroutine is very similar to findall, so we might as well save some code and merge them... if(onlyfind) { r->append(new str(subjs->substr(captured[0], captured[1] - captured[0]))); //for split we ignore zero-length matches, but findall dosn't if(captured[1] == captured[0]) captured[1]++; } else { //is it worth it? if(captured[1] == i) { cur--; i++; continue; } //append block of text r->append(new str(subjs->substr(i, captured[0] - i))); //append all the submatches to list for(j = 1; j <= capture_count; j++) { if(captured[j * 2] != -1) r->append(new str(subjs->substr(captured[j * 2], captured[j * 2 + 1] - captured[j * 2]))); else r->append(0); //should this be new str() ? } } //move our index i = captured[1]; } if(!onlyfind) r->append(new str(subjs->substr(i))); GC_FREE(captured); return r; } list *re_object::split(str *subj, __ss_int maxn) { return __splitfind(subj, maxn, 0, 0); } list *re_object::findall(str *subj, __ss_int flags) { return __splitfind(subj, -1, 1, flags); } match_iter::match_iter(re_object *ro, str *subj, __ss_int pos, __ss_int endpos, __ss_int flags) { this->subj = subj; this->pos = pos; this->endpos = endpos; this->flags = flags; this->ro = ro; } match_object *match_iter::next(void) { match_object *mobj; if((pos > endpos && endpos != -1) || (unsigned int)pos >= subj->unit.size()) throw new StopIteration(); //get next match mobj = ro->__exec(subj, pos, endpos, flags); if(!mobj) throw new StopIteration(); if(mobj->captured[1] == pos) pos++; else pos = mobj->captured[1]; return mobj; } __iter *re_object::finditer(str *subj, __ss_int pos, __ss_int endpos, __ss_int flags) { if(endpos < pos && endpos != -1) throw new error(new str("end position less than initial")); if((unsigned int)pos >= subj->unit.size()) throw new error(new str("starting position >= string length")); return new match_iter(this, subj, pos, endpos, flags); } match_object *re_object::__exec(str *subj, __ss_int pos, __ss_int endpos, __ss_int flags) { match_object *mobj; int *captured, clen, r, t, mx_i, nendpos; str *mx_s; mx_s = NULL; //allocate captured array clen = (capture_count + 1) * 2 * 3; captured = (int *)GC_MALLOC(clen * sizeof(int)); //sanity checking if(endpos == -1) nendpos = subj->unit.size() - 1; else if(endpos < pos) throw new error(new str("end position less than initial")); else nendpos = endpos; if(subj->unit.size()!=0 and (unsigned int)pos >= subj->unit.size()) throw new error(new str("starting position >= string length")); r = pcre_exec( compiled_pattern, study_info, subj->unit.c_str(), nendpos + 1, pos, flags, captured, clen ); //no match was found (dont have to worry about freeing thanks to the garbage collector) if(r < 0) return (match_object *)NULL; //create object now that we know we're successful mobj = new match_object(); mobj->re = this; //extra info mobj->captured = captured; mobj->pos = pos; mobj->endpos = endpos; mobj->string = subj; mobj->lastindex = r - 1; //find lastgroup mx_i = -1; str *k; dict::for_in_loop __3; int __2; dict *__1; FOR_IN(k,groupindex,1,2,3) t = groupindex->__getitem__(k); if(captured[t * 2] != -1 && t > mx_i) { mx_s = k; mx_i = t; } END_FOR if(mx_i != -1) mobj->lastgroup = mx_s; else mobj->lastgroup = 0; return mobj; } match_object *re_object::match(str *subj, __ss_int pos, __ss_int endpos) { return __exec(subj, pos, endpos, PCRE_ANCHORED); } match_object *re_object::search(str *subj, __ss_int pos, __ss_int endpos) { return __exec(subj, pos, endpos, 0); } //re.* functions __ss_int __convert_flags(__ss_int flags) { int ta[] = {IGNORECASE, MULTILINE, DOTALL, __ss_UNICODE, VERBOSE}, tb[] = {PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, PCRE_UTF8, PCRE_EXTENDED}; int i, r; r = 0; for(i = sizeof(ta) / sizeof(ta[0]) - 1; i >= 0; i--) if(flags & ta[i]) r |= tb[i]; return r; } re_object *compile(str *pat, __ss_int flags) { re_object *reobj; __GC_STRING fullerr; pcre *cpat; char *errmsg, *nametable; int options, erroff, ntlen, nteach, i; //convert flags options = __convert_flags(flags); //attempt a compilation cpat = pcre_compile( pat->unit.c_str(), options, (const char **)&errmsg, &erroff, (flags & LOCALE ? local_table : 0) ); //... if(!cpat) { fullerr = "char " + erroff; fullerr += ":"; fullerr += errmsg; throw new error(new str(fullerr)); } //everythings ok, create object reobj = new re_object(); reobj->compiled_pattern = cpat; //might as well study it reobj->study_info = pcre_study(cpat, 0, (const char **)&errmsg); //any named indices? reobj->groupindex = new dict(); pcre_fullinfo(cpat, reobj->study_info, PCRE_INFO_NAMECOUNT, (void *)&ntlen); pcre_fullinfo(cpat, reobj->study_info, PCRE_INFO_NAMEENTRYSIZE, (void *)&nteach); pcre_fullinfo(cpat, reobj->study_info, PCRE_INFO_NAMETABLE, (void *)&nametable); for(i = 0; i < ntlen; i++) { //first 2 bytes = number //rest = name reobj->groupindex->__setitem__(new str((char *)&nametable[i * nteach + 2]), (short)nametable[i * nteach] << 8 | (short)nametable[i * nteach + 1]); } //extra info reobj->pattern = new str(pat->unit); reobj->flags = flags; pcre_fullinfo(cpat, 0, PCRE_INFO_CAPTURECOUNT, &reobj->capture_count); return reobj; } str *escape(str *s) { __GC_STRING *ps, out; int i, j, len; ps = &s->unit; len = ps->size(); out = ""; for(i = 0; i < len; i++) { //skip alphanumerics for(j = i; ::isalnum((int)(*ps)[j]) && j < len; j++) ; if(j != i) { out += ps->substr(i, j - i); i = j; } //now process potential metachars while(!::isalnum((int)(*ps)[i]) && i < len) { out += "\\"; out += (*ps)[i]; i++; } } return new str(out); } match_object *__exec_once(str *pat, str *subj, __ss_int flags) { re_object *r; match_object *mo; r = compile(pat, flags); mo = r->__exec(subj, 0, -1, 0); if(!mo) delete r; return mo; } match_object *search(str *pat, str *subj, __ss_int flags) { return __exec_once(pat, subj, flags); } match_object *match(str *pat, str *subj, __ss_int flags) { return __exec_once(pat, subj, flags | PCRE_ANCHORED); } __iter *finditer(str *pat, str *subj, __ss_int pos, __ss_int endpos, __ss_int flags) { re_object *ro; __iter *r; ro = compile(pat, flags); r = ro->finditer(subj, pos, endpos, 0); return r; } str *sub(str *pat, str *repl, str *subj, __ss_int maxn) { re_object *ro; str *r; ro = compile(pat, 0); r = ro->sub(repl, subj, maxn); return r; } str *sub(str *pat, replfunc func, str *subj, __ss_int maxn) { re_object *ro; str *r; ro = compile(pat, 0); r = ro->sub(func, subj, maxn); return r; } tuple2 *subn(str *pat, str *repl, str *subj, __ss_int maxn) { re_object *ro; tuple2 *r; ro = compile(pat, 0); r = ro->subn(repl, subj, maxn); return r; } list *__splitfind_once(str *pat, str *subj, __ss_int maxn, char onlyfind, __ss_int flags) { re_object *ro; list *r; ro = compile(pat, flags); r = ro->__splitfind(subj, maxn, onlyfind, 0); //return subj->substr(captured[matchid * 2], captured[matchid * 2 + 1] - captured[matchid * 2]); return r; } list *split(str *pat, str *subj, __ss_int maxn) { return __splitfind_once(pat, subj, maxn, 0, 0); } list *findall(str *pat, str *subj, __ss_int flags) { return __splitfind_once(pat, subj, -1, 1, flags); } void __init(void) { cl_error = new class_("error"); pcre_malloc = &re_malloc; pcre_free = &re_free; local_table = pcre_maketables(); } } shedskin-0.9.4/shedskin/lib/string.py0000664000175000017500000000262512157270661017524 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) ascii_letters = letters = ascii_lowercase = lowercase = ascii_uppercase = uppercase = whitespace = '' printable = punctuation = '' digits = hexdigits = octdigits = '' def join(a, b=''): return '' def joinfields(a, b=''): return '' def find(s, sub, start=0, end=0): return 1 def rfind(s, sub, start=0, end=0): return 1 def index(s, sub, start=0, end=0): return 1 def rindex(s, sub, start=0, end=0): return 1 def rsplit(s, sep='', c=-1): return [''] def split(s, sep='', c=-1): return [''] def splitfields(s, sep='', c=-1): return [''] def replace(s, a, b, c=-1): return '' def translate(s, table, delchars=''): return '' def count(s, sub, start=0, end=0): return 1 def expandtabs(s, width=8): return '' def lower(s): return '' def upper(s): return '' def zfill(s, width): return '' def strip(s, chars=''): return '' def lstrip(s, chars=''): return '' def rstrip(s, chars=''): return '' def ljust(s, width, chars=''): return '' def rjust(s, width, chars=''): return '' def maketrans(frm, to): return '' def capitalize(s): return '' def capwords(s, sep=''): return '' def swapcase(s): return '' def center(s, w, fill=''): return '' def atoi(s, base=10): return 1 def atol(s, base=10): return 1 def atof(s): return 1.0 shedskin-0.9.4/shedskin/lib/math.hpp0000664000175000017500000000440412157270661017303 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #ifndef __MATH_HPP #define __MATH_HPP #include "builtin.hpp" using namespace __shedskin__; namespace __math__ { extern double pi; extern double e; void __init(); inline double ceil(double x) { return std::ceil(x); } inline double fabs(double x) { return std::fabs(x); } inline __ss_int factorial(__ss_int x) { if (x < 0) throw new ValueError(new str("factorial() not defined for negative values")); __ss_int result = 1; for (__ss_int i = 1; i <= x; ++i) { result *= i; } return result; } inline double floor(double x) { return std::floor(x); } inline double fmod(double x, double y) { return std::fmod(x, y); } inline tuple2 *modf(double x) { return (new tuple2(2, x-(__ss_int)x, (double)(__ss_int)x)); } inline double ldexp(double x, __ss_int i) { return std::ldexp(x, i); } inline double exp(double x) { return std::exp(x); } inline double log(double x) { return std::log(x); } inline double log(double x, double base) { return std::log(x) / std::log(base); } inline double log10(double x) { return std::log10(x); } inline double sqrt(double x) { return std::sqrt(x); } inline double acos(double x) { return std::acos(x); } inline double asin(double x) { return std::asin(x); } inline double atan(double x) { return std::atan(x); } inline double atan2(double x, double y) { return std::atan2(x, y); } inline double cos(double x) { return std::cos(x); } inline double hypot(double x, double y) { return sqrt(x*x+y*y); } inline double sin(double x) { return std::sin(x); } inline double tan(double x) { return std::tan(x); } inline double degrees(double x) { return x*(180.0/pi); } inline double radians(double x) { return x/(180.0/pi); } inline double cosh(double x) { return std::cosh(x); } inline double sinh(double x) { return std::sinh(x); } inline double tanh(double x) { return std::tanh(x); } inline double pow(double x, double y) { return std::pow(x,y); } } // module namespace #endif shedskin-0.9.4/shedskin/lib/mmap.cpp0000664000175000017500000005356012157270661017306 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include #include #include #include #ifndef WIN32 #include // mmap, munmap, msync, mremap #include #include // fstat #include // sysconf #define MMAP_PUSH(constant) __##constant = (constant) #define HAVE_MREMAP #else /* WIN32 */ #include // lseek #define MMAP_PUSH(constant) __##constant = -1 #endif /* WIN32 */ namespace __mmap__ { enum { MMAP_PUSH(PROT_READ), MMAP_PUSH(PROT_WRITE), MMAP_PUSH(PROT_EXEC), MMAP_PUSH(MAP_SHARED), MMAP_PUSH(MAP_PRIVATE), MMAP_PUSH(MAP_ANONYMOUS), MMAP_PUSH(MAP_ANON) }; } // __mmap__ namespace #undef MMAP_PUSH #undef PROT_READ #undef PROT_WRITE #undef PROT_EXEC #undef MAP_SHARED #undef MAP_PRIVATE #undef MAP_ANONYMOUS #undef MAP_ANON #include "mmap.hpp" namespace __mmap__ { #ifdef WIN32 const __ss_int PAGESIZE = 0x10000; __ss_int ALLOCATIONGRANULARITY = PAGESIZE; #else // UNIX const __ss_int PAGESIZE = sysconf(_SC_PAGE_SIZE); __ss_int ALLOCATIONGRANULARITY = PAGESIZE; #endif const __ss_int ACCESS_DEFAULT = 0, ACCESS_READ = 1, ACCESS_WRITE = 2, ACCESS_COPY = 3, PROT_READ = __PROT_READ, PROT_WRITE = __PROT_WRITE, PROT_EXEC = __PROT_EXEC, MAP_SHARED = __MAP_SHARED, MAP_PRIVATE = __MAP_PRIVATE, MAP_ANONYMOUS = __MAP_ANONYMOUS, MAP_ANON = __MAP_ANON; // Default parameters. #ifndef WIN32 /* UNIX */ __ss_int default_0 = MAP_SHARED, default_1 = PROT_READ | PROT_WRITE; #else str *default_2; #endif /* WIN32 */ // Error messages. str *const_0, *const_1, *const_2, *const_3, *const_4, *const_5, *const_6, *const_8, *const_9, *const_10, *const_11, *const_12, *const_13, *const_14, *const_15, *const_16, *const_17; str *__name__; class_ *cl_mmap; #ifndef WIN32 void *mmap::__init__(int __ss_fileno_, __ss_int length_, __ss_int flags_, __ss_int prot_, __ss_int access_, __ss_int offset_) { if (length_ < 0) { throw new OverflowError(const_2); } if (offset_ < 0) { throw new OverflowError(const_3); } if ((access_ != ACCESS_DEFAULT and (flags_ != MAP_SHARED or (prot_ != (PROT_WRITE | PROT_READ))))) { throw new ValueError(const_4); } if (access_ == ACCESS_READ) { flags_ = MAP_SHARED; prot_ = PROT_READ; } else if (access_ == ACCESS_WRITE) { flags_ = MAP_SHARED; prot_ = (PROT_READ|PROT_WRITE); } else if (access_ == ACCESS_COPY) { flags_ = MAP_PRIVATE; prot_ = (PROT_READ | PROT_WRITE); } else if (access_ == ACCESS_DEFAULT) { /* Nothing */; } else { throw new ValueError(const_5); } if (prot_ == PROT_READ) { access_ = ACCESS_READ; } if (__ss_fileno_ == -1) { flags_ |= MAP_ANONYMOUS; assert(fd == -1); } else { fd = dup(__ss_fileno_); if (fd == -1) { throw new IOError(); } if(length_ == 0) { struct stat buf; if (fstat(fd, &buf) == -1) { throw new IOError(); } length_ = buf.st_size; } } m_begin = static_cast(::mmap(0, length_, prot_, flags_, fd, offset_)); if (m_begin == iterator(-1)) { throw IOError(); } m_position = m_begin; m_end = m_begin + length_; flags = flags_; prot = prot_; access = access_; return NULL; } void *mmap::close() { if (not closed) { if (fd >= 0) ::close(fd); ::msync(m_begin, __size(), MS_SYNC); ::munmap(m_begin, __size()); closed = true; } return NULL; } __ss_int mmap::flush(__ss_int offset, __ss_int size) { __raise_if_closed(); if (::msync(m_begin + offset, __subscript(size), MS_SYNC) == -1) { throw new IOError(); } return 0; } void *mmap::resize(__ss_int new_size) { __raise_if_closed(); #ifdef HAVE_MREMAP #if defined(__NetBSD__) m_begin = static_cast(::mremap(m_begin, __size(), m_begin, size_t(new_size), 0)); #else // !__NetBSD__ m_begin = static_cast(::mremap(m_begin, __size(), size_t(new_size), 0)); #endif // __NetBSD__ if (m_begin == iterator(-1)) { throw new IOError(); } m_end = m_begin + size_t(new_size); m_position = std::min(m_position, m_end); #else // !HAVE_MREMAP throw new NotImplementedError(const_15); #endif // HAVE_MREMAP return NULL; } #else /* WIN32*/ void *mmap::__init__(int __ss_fileno_, __ss_int length_, str *tagname_, __ss_int access_, __ss_int offset_) { if (length_ < 0) { throw new OverflowError(const_2); } if (offset_ < 0) { throw new OverflowError(const_3); } // Taken from Python 2.7 DWORD flProtect, dwDesiredAccess; DWORD off_hi; /* upper 32 bits of offset */ DWORD off_lo; /* lower 32 bits of offset */ DWORD size_hi; /* upper 32 bits of size */ DWORD size_lo; /* lower 32 bits of size */ DWORD dwErr = 0; HANDLE fh = 0; size_t size = 0; const char *tagname = tagname_ ? tagname_->unit.c_str() : 0; switch (access_) { case ACCESS_READ: flProtect = PAGE_READONLY; dwDesiredAccess = FILE_MAP_READ; break; case ACCESS_DEFAULT: case ACCESS_WRITE: flProtect = PAGE_READWRITE; dwDesiredAccess = FILE_MAP_WRITE; break; case ACCESS_COPY: flProtect = PAGE_WRITECOPY; dwDesiredAccess = FILE_MAP_COPY; break; default: throw new ValueError(const_5); } if (__ss_fileno_ != -1 and __ss_fileno_ != 0) { fh = HANDLE(_get_osfhandle(__ss_fileno_)); if (fh == HANDLE(-1)) { throw new ValueError(const_16); } /* Win9x appears to need us seeked to zero */ lseek(__ss_fileno_, 0, SEEK_SET); } if (fh == 0) { size = length_; } else { /* It is necessary to duplicate the handle, so the Python code can close it on us */ if (!DuplicateHandle( GetCurrentProcess(), /* source process handle */ fh, /* handle to be duplicated */ GetCurrentProcess(), /* target proc handle */ (LPHANDLE)&file_handle, /* result */ 0, /* access - ignored due to options value */ FALSE, /* inherited by child processes? */ DUPLICATE_SAME_ACCESS)) /* options */ { throw new IOError(); } if (length_) { size = length_; } else { DWORD low, high; low = GetFileSize(fh, &high); /* low might just happen to have the value INVALID_FILE_SIZE; so we need to check the last error also. */ if (low == INVALID_FILE_SIZE and (dwErr = GetLastError()) != NO_ERROR) { throw new ValueError(const_17); } #if SIZEOF_SIZE_T > 4 size = (size_t(high)<<32) + low; #else // SIZEOF_SIZE_T <= 4 if (high) /* File is too large to map completely */ size = size_t(-1); else size = low; #endif // SIZEOF_SIZE_T > 4 } } access = access_; /* DWORD is a 4-byte int. If we're on a box where size_t consumes * more than 4 bytes, we need to break it apart. Else (size_t * consumes 4 bytes), C doesn't define what happens if we shift * right by 32, so we need different code. */ #if SIZEOF_SIZE_T > 4 size_hi = (DWORD)((offset_ + size) >> 32); size_lo = (DWORD)((offset_ + size) & 0xFFFFFFFF); off_hi = (DWORD)(offset_ >> 32); off_lo = (DWORD)(offset_ & 0xFFFFFFFF); #else // SIZEOF_SIZE_T <= 4 size_hi = 0; size_lo = (DWORD)(offset_ + size); off_hi = 0; off_lo = (DWORD)offset_; #endif // SIZEOF_SIZE_T > 4 /* For files, it would be sufficient to pass 0 as size. For anonymous maps, we have to pass the size explicitly. */ map_handle = CreateFileMapping(file_handle, NULL, flProtect, size_hi, size_lo, tagname); if (map_handle == NULL) { throw new IOError(); } m_begin = static_cast(MapViewOfFile(map_handle, dwDesiredAccess, off_hi, off_lo, size)); if (m_begin == NULL) { throw new IOError(); } /* set the initial position */ m_position = m_begin; m_end = m_begin + size_lo; offset = offset_; return NULL; } void *mmap::close() { if (not closed) { UnmapViewOfFile (m_begin); CloseHandle (map_handle); if (file_handle != INVALID_HANDLE_VALUE) CloseHandle (file_handle); closed = true; } return NULL; } __ss_int mmap::flush(__ss_int offset, __ss_int size) { __raise_if_closed(); return __ss_int(FlushViewOfFile(m_begin + offset, __subscript(size))); } void *mmap::resize(__ss_int new_size) { __raise_if_closed(); DWORD dwErrCode = 0; DWORD off_hi, off_lo; LONG newSizeLow, newSizeHigh; /* First, unmap the file view */ UnmapViewOfFile(m_begin); m_begin = NULL; /* Close the mapping object */ CloseHandle(map_handle); map_handle = NULL; /* Move to the desired EOF position */ #if SIZEOF_SIZE_T > 4 newSizeHigh = (DWORD)((offset + new_size) >> 32); newSizeLow = (DWORD)((offset + new_size) & 0xFFFFFFFF); off_hi = (DWORD)(offset >> 32); off_lo = (DWORD)(offset & 0xFFFFFFFF); #else // SIZEOF_SIZE_T <= 4 newSizeHigh = 0; newSizeLow = (DWORD)(offset + new_size); off_hi = 0; off_lo = (DWORD)offset; #endif // SIZEOF_SIZE_T > 4 SetFilePointer(file_handle, newSizeLow, &newSizeHigh, FILE_BEGIN); /* Change the size of the file */ SetEndOfFile(file_handle); /* Create another mapping object and remap the file view */ map_handle = CreateFileMapping( file_handle, NULL, PAGE_READWRITE, 0, 0, tagname); if (map_handle != NULL) { throw new IOError(); } m_begin = static_cast(MapViewOfFile(map_handle, FILE_MAP_WRITE, off_hi, off_lo, new_size)); if (m_begin == NULL) { CloseHandle(map_handle); throw new IOError(); } m_end = m_begin + size_t(new_size); m_position = std::min(m_position, m_end); return NULL; } #endif /* WIN32 */ __ss_int mmap::find(str *needle, __ss_int start, __ss_int end) { __raise_if_closed_or_not_readable(); if (start == -1) { start = __tell(); } if( end == -1) { end = __size(); } return __find(needle->unit, start, end); } void *mmap::move(__ss_int destination, __ss_int source, __ss_int count) { __raise_if_closed_or_not_readable(); __ss_int length = size(); // Taken from Python 2.7 if (count < 0 or (count + destination) < count or (count + source) < count or source < 0 or source > length or (source + count) > length or destination < 0 or destination > length or (destination + count) > length) { throw new ValueError(const_1); } memmove(m_begin + destination, m_begin + source, count); return NULL; } str *mmap::read(__ss_int size) { __raise_if_closed_or_not_readable(); const iterator at = m_position; if (size == all) { m_position = m_end; } else { m_position += size; if (m_position < at) { throw new OverflowError(const_14); } if (m_position >= m_end) { m_position = m_end; } } return new str(at, m_position - at); } str *mmap::read_byte() { __raise_if_closed_or_not_readable(); if(m_position < m_end) { return __char_cache[(unsigned char)(*m_position++)]; } else { m_position = m_end; return new str(""); } } str *mmap::readline(__ss_int size, const char eol) { __raise_if_closed_or_not_readable(); const iterator at = m_position; m_position = __next_line(eol); if (m_position == 0) { m_position = m_end; } else // Line found. { ++m_position; // Include the newline } if (size != all and m_position > at + size) { m_position = at + size; } return new str(at, m_position - at); } __ss_int mmap::rfind(str *needle, __ss_int start, __ss_int end) { __raise_if_closed_or_not_readable(); if (start == -1) { start = __tell(); } if( end == -1) { end = __size(); } return __find(needle->unit, start, end, true); } void *mmap::seek(__ss_int offset, __ss_int whence) { __raise_if_closed(); const iterator restore = m_begin + tell(); switch (whence) { case 0: /* SEEK_SET: relative to start.*/ if (offset < 0L or size_t(offset) > __size()) { __seek_failed(); } m_position = m_begin + offset; break; case 1: /* SEEK_CUR: relative to current position. */ m_position += offset; if (m_position < m_begin or m_position > m_end) { m_position = restore; __seek_failed(); } break; case 2: /* SEEK_END: relative to end */ if (offset > 0L or offset < -int(__size())) { __seek_failed(); } m_position = m_end + offset; break; default: // Error throw new ValueError(const_12); } return NULL; } __ss_int mmap::size() { __raise_if_closed(); #ifdef WIN32 if (file_handle != INVALID_HANDLE_VALUE) { DWORD low, high; uint64_t size; low = GetFileSize(file_handle, &high); if (low == INVALID_FILE_SIZE) { /* It might be that the function appears to have failed, when indeed its size equals INVALID_FILE_SIZE */ DWORD error = GetLastError(); if (error != NO_ERROR) throw IOError(); } size = (((uint64_t)high)<<32) + low; return __ss_int(size); } else { return __size(); } #else /* UNIX */ if(fd == -1 ) { return __size(); } else { struct stat buf; if (fstat(fd, &buf) == -1) { throw new IOError(); } return buf.st_size; } #endif /* WIN32 */ } __ss_int mmap::tell() { __raise_if_closed(); return __tell(); } void *mmap::write(str *string) { __raise_if_closed_or_not_writable(); size_t length = string->unit.size(); if (m_position + length > m_end) { throw new ValueError(const_14); } memcpy(m_position, string->unit.data(), length); m_position += length; return NULL; } void *mmap::write_byte(str *string) { __raise_if_closed_or_not_writable(); if (string == 0 or string->unit.size() != 1) { throw new ValueError(const_8); } if (m_position + 1 > m_end) { throw new ValueError(const_14); } *m_position++ = string->unit[0]; return NULL; } __ss_bool mmap::__contains__(str *string) { __raise_if_closed_or_not_readable(); if (string == 0 or string->unit.size() != 1) { throw new ValueError(const_8); } return __mbool(find(string, 0) != -1); } __iter *mmap::__iter__() { __raise_if_closed(); return new __mmapiter(this); } __ss_int mmap::__len__() { return size(); } str *mmap::__getitem__(__ss_int index) { __raise_if_closed_or_not_readable(); iterator position = m_begin + __subscript(index); return new str(position, 1); } void *mmap::__setitem__(__ss_int index, str *character) { __raise_if_closed_or_not_writable(); size_t id = __subscript(index); if (character->unit.size() != 1) { throw new IndexError(const_8); } m_begin[id] = character->unit[0]; return NULL; } str *mmap::__slice__(__ss_int kind, __ss_int lower, __ss_int upper, __ss_int) { __raise_if_closed_or_not_readable(); lower = __clamp(lower); upper = __clamp(upper); iterator start = m_begin; size_t size = 0; switch (kind) { case 1: // step[x:] start = m_begin + __subscript(lower); size = m_end - start; break; case 2: // step[:x] start = m_begin; size = __subscript(upper); break; case 3: // step[x:y] start = m_begin + __subscript(lower); size = __subscript(upper) - __subscript(lower); break; default: assert(false); } return new str(start, size); } void *mmap::__setslice__(__ss_int kind, __ss_int lower, __ss_int upper, __ss_int, str *sequence) { __raise_if_closed_or_not_writable(); iterator start = m_end; iterator finish = m_end; switch (kind) { case 1: // step[x:] start = m_begin + __subscript(lower, true); finish= m_end; break; case 2: // step[:x] start = m_begin; finish= m_begin + __subscript(upper, true); break; case 3: // step[x:y] start = m_begin + __subscript(lower, true); finish= m_begin + __subscript(upper, true); break; default: assert(false); } memcpy(start, sequence->unit.data(), finish - start); return NULL; } void *mmap::__raise_if_closed() { if (closed) { throw new ValueError(const_11); } return NULL; } void *mmap::__raise_if_closed_or_not_readable() { __raise_if_closed(); #ifndef WIN32 if ((prot & PROT_READ) == 0) #else /* WIN32 */ if (access and access != ACCESS_READ) #endif /* WIN32 */ { throw new TypeError(const_0); } return NULL; } void *mmap::__raise_if_closed_or_not_writable() { __raise_if_closed(); #ifndef WIN32 if ((prot & PROT_WRITE) == 0) #else /* WIN32 */ if (access == ACCESS_READ) #endif /* WIN32 */ { throw new TypeError(const_9); } return NULL; } void *mmap::__seek_failed() { throw new ValueError(const_14); return NULL; } size_t mmap::__subscript(__ss_int index, bool include_end) const { if (index < 0) { index += __size(); } if (index < 0 or size_t(index) >= __size()) { if (not include_end or size_t(index) != __size()) { throw new IndexError(const_13); } } return index; } __ss_int mmap::__clamp(__ss_int index) const { __ss_int length = __size(); return std::min(std::max(index, -length), length); } mmap::iterator mmap::__next_line(const char eol) { return static_cast( memchr(m_position, eol, m_end - m_position)); } __ss_int mmap::__find(const __GC_STRING& needle, __ss_int start, __ss_int end, bool reverse) { if (end == 0) { end = __size(); } size_t length = needle.size(); // Taken from Python 3.2. const char *p, *start_p, *end_p; int sign = reverse ? -1 : 1; if (start < 0) start += __size(); if (start < 0) start = 0; else if (size_t(start) > __size()) start = __size(); if (end < 0) end += __size(); if (end < 0) end = 0; else if (size_t(end) > __size()) end = __size(); start_p = m_begin + start; end_p = m_begin + end; for (p = (reverse ? end_p - length : start_p); (p >= start_p) and (p + length <= end_p); p += sign) { size_t i; for (i = 0; i < length and needle[i] == p[i]; ++i) { /* Nothing */; } if (i == length) { return (p - m_begin); } } return -1; } str *__mmapiter::next() { if (map->__eof()) throw new StopIteration(); str* byte = map->read_byte(); if (map->__eof()) throw new StopIteration(); return byte; } void __init() { static bool __initialized = false; if(not __initialized) { #ifdef WIN32 SYSTEM_INFO si; GetSystemInfo(&si); ALLOCATIONGRANULARITY = si.dwAllocationGranularity; default_2 = new str(""); #endif /* WIN32 */ const_0 = new str("mmap object is not open for reading"); const_1 = new str("source, destination, or count out of range"); const_2 = new str("memory mapped size must be positive"); const_3 = new str("memory mapped offset must be positive"); const_4 = new str("mmap can't specify both access and flags, prot"); const_5 = new str("mmap invalid access parameter"); const_8 = new str("mmap assignment must be single-character string"); const_9 = new str("mmap object is not open for writing"); const_10 = new str("mmap slice assignment is wrong size"); const_11 = new str("mmap closed or invalid"); const_12 = new str("unknown seek type"); const_13 = new str("mmap index out of range"); const_14 = new str("data out of range"); const_15 = new str("mmap: resizing not available--no mremap()"); const_16 = new str("mmap invalid file handle"); const_17 = new str("mmap invalid file size"); __name__ = new str("mmap"); cl_mmap = new class_("mmap"); __initialized = true; } } // __init } // __mmap__ namespace shedskin-0.9.4/shedskin/lib/array.py0000664000175000017500000000345312157270661017334 0ustar srepmubsrepmub# Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) class array: def __init__(self, flag, it=None): self.typecode = '' self.itemsize = 0 def __init_int__(self, flag, it=None): self.unit = 1 self.__init__(flag) def __init_float__(self, flag, it=None): self.unit = 1.0 self.__init__(flag) def __init_str__(self, flag, it=None): self.unit = '' self.__init__(flag) def tolist(self): return [self.unit] def tostring(self): return '' def fromlist(self, l): pass def fromstring(self, s): pass def extend(self, it): pass def pop(self, i=-1): return self.unit def remove(self, e): pass def __getitem__(self, i): return self.unit def __setitem__(self, i, e): pass def __delitem__(self, i): pass def insert(self, i, e): pass def append(self, e): pass def tofile(self, f): pass def fromfile(self, f, n): pass def count(self, e): return 1 def index(self, e): return 1 def __contains__(self, e): return True def byteswap(self): pass def reverse(self): pass def __add__(self, b): return self def __mul__(self, b): return self def __with_int__(self): # int.__mul__ etc. return self def __len__(self): return 1 def __iter__(self): return __iter(self.unit) def __slice__(self, x, lower, upper, step): return self def __delslice__(self, a, b): pass def __setslice__(self, x, lower, upper, step, r): pass def __delete__(self, x, a=1, b=1, s=1): pass def __copy__(self): return self shedskin-0.9.4/shedskin/lib/time.cpp0000664000175000017500000005323312157270661017307 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "time.hpp" #include "time.h" #include namespace __time__ { clock_t start; __ss_int timezone; tuple2 *tzname; #ifdef WIN32 //#include double clock() { return ((double) (std::clock()-start)) / CLOCKS_PER_SEC; /* static LARGE_INTEGER ctrStart; static double divisor = 0.0; LARGE_INTEGER now; double diff; if (divisor == 0.0) { LARGE_INTEGER freq; QueryPerformanceCounter(&ctrStart); if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) { // Unlikely to happen - this works on all intel // machines at least! Revert to clock() return ((double) (std::clock()-start)) / CLOCKS_PER_SEC; } divisor = (double)freq.QuadPart; } QueryPerformanceCounter(&now); diff = (double)(now.QuadPart - ctrStart.QuadPart); return diff / divisor; */ } #ifdef WIN32 __ss_int gettimeofday (struct timeval *tv, struct __ss_timezone *tz) { struct _timeb tb; if (!tv) return -1; _ftime (&tb); tv->tv_sec = (long) tb.time; tv->tv_usec = tb.millitm * 1000 + 500; if (tz) { tz->tz_minuteswest = -60 * _timezone; tz->tz_dsttime = _daylight; } return 0; } #endif #else double clock() { return ((double) (std::clock()-start)) / CLOCKS_PER_SEC; } #endif str *const_0, *const_1; class_ *cl_struct_time; __ss_int struct_time::__getitem__(__ss_int n) { return ((new tuple2<__ss_int, __ss_int>(9, this->tm_year, this->tm_mon, this->tm_mday, this->tm_hour, this->tm_min, this->tm_sec, this->tm_wday, this->tm_yday, this->tm_isdst)))->__getitem__(n); } struct_time::struct_time(tuple2<__ss_int, __ss_int> *_tuple) { this->__class__ = cl_struct_time; if ((len(_tuple)!=9)) { throw ((new TypeError(const_0))); } this->tm_year = _tuple->__getitem__(0); this->tm_mon = _tuple->__getitem__(1); this->tm_mday = _tuple->__getitem__(2); this->tm_hour = _tuple->__getitem__(3); this->tm_min = _tuple->__getitem__(4); this->tm_sec = _tuple->__getitem__(5); this->tm_wday = _tuple->__getitem__(6); this->tm_yday = _tuple->__getitem__(7); this->tm_isdst = _tuple->__getitem__(8); } str *struct_time::__repr__() { return __modct(const_1, 9, ___box(this->tm_year), ___box(this->tm_mon), ___box(this->tm_mday), ___box(this->tm_hour), ___box(this->tm_min), ___box(this->tm_sec), ___box(this->tm_wday), ___box(this->tm_yday), ___box(this->tm_isdst)); } tm* tuple2tm(struct_time* tuple) { tm *time_tuple = new tm; time_tuple->tm_sec = tuple->tm_sec; time_tuple->tm_min = tuple->tm_min; time_tuple->tm_hour = tuple->tm_hour; time_tuple->tm_mday = tuple->tm_mday; time_tuple->tm_mon = tuple->tm_mon - 1; time_tuple->tm_year = tuple->tm_year - 1900; time_tuple->tm_wday = tuple->tm_wday == 6 ? 0 : tuple->tm_wday + 1; time_tuple->tm_yday = tuple->tm_yday - 1 ; time_tuple->tm_isdst = tuple->tm_isdst; return time_tuple; } struct_time *tm2tuple(tm* tm_time) { struct_time *time_tuple = new struct_time(new tuple2<__ss_int, __ss_int>(9, (__ss_int)(tm_time->tm_year + 1900), (__ss_int)(tm_time->tm_mon + 1), (__ss_int)(tm_time->tm_mday), (__ss_int)(tm_time->tm_hour), (__ss_int)(tm_time->tm_min), (__ss_int)(tm_time->tm_sec), (__ss_int)(tm_time->tm_wday == 0 ? 6 : tm_time->tm_wday - 1), (__ss_int)(tm_time->tm_yday + 1), (__ss_int)(tm_time->tm_isdst))); return time_tuple; } double time() { timeval tim; gettimeofday(&tim, 0); return tim.tv_sec+tim.tv_usec/1000000.0; } #ifndef WIN32 void *sleep(double s) { struct timespec time; time_t seconds = (int) s; long nanosecs = (double)(s - seconds)*1000000000; time.tv_sec = seconds; time.tv_nsec = nanosecs; nanosleep(&time, NULL); return NULL; } #else // TOFIX void *sleep(double s) { return NULL; } #endif double mktime(struct_time *tuple) { return (double)::mktime(tuple2tm(tuple)); } double mktime(tuple2<__ss_int, __ss_int> *tuple) { struct_time *st; try { st = new struct_time(tuple); } catch(...) { throw; } return (double)::mktime(tuple2tm(st)); } struct_time *localtime() { time_t time = ::time(NULL); return localtime((double)time); } struct_time *localtime(const double timep) { time_t timet = static_cast(timep); tm *tm_time = ::localtime(&timet); return tm2tuple(tm_time); } struct_time *gmtime() { time_t time = ::time(NULL); return gmtime((double)time); } struct_time *gmtime(const double seconds) { time_t timet = static_cast(seconds); tm *tm_time = ::gmtime(&timet); return tm2tuple(tm_time); } str *asctime() { struct_time *tuple = localtime(); return asctime(tuple); } str *asctime(struct_time *tuple) { return (new str(::asctime(tuple2tm(tuple))))->__slice__(2, 0, -1, 0); } str *ctime() { return asctime(localtime()); } str *ctime(const double seconds) { return asctime(localtime(seconds)); } str *strftime(str *format, struct_time* tuple) { tm *time_tuple = tuple2tm(tuple); unsigned int size = format->__len__(); unsigned int n; char *buf; if(size==0) return new str(); do { size *= 2; buf = new char[size]; n = ::strftime(buf, size, format->unit.c_str(), time_tuple); } while(n == 0); return new str(buf); } str *strftime(str *format) { return strftime(format, localtime()); } str *strftime(str *format, tuple2<__ss_int, __ss_int> *tuple) { struct_time *st; try { st = new struct_time(tuple); } catch (...) { throw; } return strftime(format, st); } #ifdef WIN32 /****************************************************************************** * STRPTIME FOR WINDOWS *****************************************************************************/ /* $NetBSD: strptime.c,v 1.35 2009/12/14 20:45:02 matt Exp $ */ /*- * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc. * All rights reserved. * * This code was contributed to The NetBSD Foundation by Klaus Klein. * Heavily optimised by David Laight * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* Original code modified to work with MSVC May 2010 with help from http://www.opensource.apple.com/source/gdb/gdb-282/src/tcl/compat/strftime.c */ #include #include #include typedef struct { const char *abday[7]; const char *day[7]; const char *abmon[12]; const char *mon[12]; const char *am_pm[2]; const char *d_t_fmt; const char *d_fmt; const char *t_fmt; const char *t_fmt_ampm; } _TimeLocale; /* * This is the C locale default. On Windows, if we wanted to make this * localized, we would use GetLocaleInfo to get the correct values. * It may be acceptable to do localization of month/day names, as the * numerical values would be considered the locale-independent versions. */ static const _TimeLocale _DefaultTimeLocale = { { "Sun","Mon","Tue","Wed","Thu","Fri","Sat", }, { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }, { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }, { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }, { "AM", "PM" }, "%a %b %d %H:%M:%S %Y", "%m/%d/%y", "%H:%M:%S", "%I:%M:%S %p" }; static const _TimeLocale *_CurrentTimeLocale = &_DefaultTimeLocale; #define TM_YEAR_BASE 1900 #define IsLeapYear(x) ((x % 4 == 0) && (x % 100 != 0 || x % 400 == 0)) #define _ctloc(x) (_CurrentTimeLocale->x) #define u_char unsigned char #define uint unsigned int #define __UNCONST /* * We do not implement alternate representations. However, we always * check whether a given modifier is allowed for a certain conversion. */ #define ALT_E 0x01 #define ALT_O 0x02 #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; } static char gmt[] = { "GMT" }; static char utc[] = { "UTC" }; /* RFC-822/RFC-2822 */ static const char * const nast[5] = { "EST", "CST", "MST", "PST", "\0\0\0" }; static const char * const nadt[5] = { "EDT", "CDT", "MDT", "PDT", "\0\0\0" }; static const u_char *conv_num(const unsigned char *, int *, uint, uint); static const u_char *find_string(const u_char *, int *, const char * const *, const char * const *, int); char * strptime(const char *buf, const char *fmt, struct tm *tm) { unsigned char c; const unsigned char *bp, *ep; int alt_format, i, split_year = 0, neg = 0, offs; const char *new_fmt; bp = (const u_char *)buf; while (bp != NULL && (c = *fmt++) != '\0') { /* Clear `alternate' modifier prior to new conversion. */ alt_format = 0; i = 0; /* Eat up white-space. */ if (isspace(c)) { while (isspace(*bp)) bp++; continue; } if (c != '%') goto literal; again: switch (c = *fmt++) { case '%': /* "%%" is converted to "%". */ literal: if (c != *bp++) return NULL; LEGAL_ALT(0); continue; /* * "Alternative" modifiers. Just set the appropriate flag * and start over again. */ case 'E': /* "%E?" alternative conversion modifier. */ LEGAL_ALT(0); alt_format |= ALT_E; goto again; case 'O': /* "%O?" alternative conversion modifier. */ LEGAL_ALT(0); alt_format |= ALT_O; goto again; /* * "Complex" conversion rules, implemented through recursion. */ case 'c': /* Date and time, using the locale's format. */ new_fmt = _ctloc(d_t_fmt); goto recurse; case 'D': /* The date as "%m/%d/%y". */ new_fmt = "%m/%d/%y"; LEGAL_ALT(0); goto recurse; case 'F': /* The date as "%Y-%m-%d". */ new_fmt = "%Y-%m-%d"; LEGAL_ALT(0); goto recurse; case 'R': /* The time as "%H:%M". */ new_fmt = "%H:%M"; LEGAL_ALT(0); goto recurse; case 'r': /* The time in 12-hour clock representation. */ new_fmt =_ctloc(t_fmt_ampm); LEGAL_ALT(0); goto recurse; case 'T': /* The time as "%H:%M:%S". */ new_fmt = "%H:%M:%S"; LEGAL_ALT(0); goto recurse; case 'X': /* The time, using the locale's format. */ new_fmt =_ctloc(t_fmt); goto recurse; case 'x': /* The date, using the locale's format. */ new_fmt =_ctloc(d_fmt); recurse: bp = (const u_char *)strptime((const char *)bp, new_fmt, tm); LEGAL_ALT(ALT_E); continue; /* * "Elementary" conversion rules. */ case 'A': /* The day of week, using the locale's form. */ case 'a': bp = find_string(bp, &tm->tm_wday, _ctloc(day), _ctloc(abday), 7); LEGAL_ALT(0); continue; case 'B': /* The month, using the locale's form. */ case 'b': case 'h': bp = find_string(bp, &tm->tm_mon, _ctloc(mon), _ctloc(abmon), 12); LEGAL_ALT(0); continue; case 'C': /* The century number. */ i = 20; bp = conv_num(bp, &i, 0, 99); i = i * 100 - TM_YEAR_BASE; if (split_year) i += tm->tm_year % 100; split_year = 1; tm->tm_year = i; LEGAL_ALT(ALT_E); continue; case 'd': /* The day of month. */ case 'e': bp = conv_num(bp, &tm->tm_mday, 1, 31); LEGAL_ALT(ALT_O); continue; case 'k': /* The hour (24-hour clock representation). */ LEGAL_ALT(0); /* FALLTHROUGH */ case 'H': bp = conv_num(bp, &tm->tm_hour, 0, 23); LEGAL_ALT(ALT_O); continue; case 'l': /* The hour (12-hour clock representation). */ LEGAL_ALT(0); /* FALLTHROUGH */ case 'I': bp = conv_num(bp, &tm->tm_hour, 1, 12); if (tm->tm_hour == 12) tm->tm_hour = 0; LEGAL_ALT(ALT_O); continue; case 'j': /* The day of year. */ i = 1; bp = conv_num(bp, &i, 1, 366); tm->tm_yday = i - 1; LEGAL_ALT(0); continue; case 'M': /* The minute. */ bp = conv_num(bp, &tm->tm_min, 0, 59); LEGAL_ALT(ALT_O); continue; case 'm': /* The month. */ i = 1; bp = conv_num(bp, &i, 1, 12); tm->tm_mon = i - 1; LEGAL_ALT(ALT_O); continue; case 'p': /* The locale's equivalent of AM/PM. */ bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2); if (tm->tm_hour > 11) return NULL; tm->tm_hour += i * 12; LEGAL_ALT(0); continue; case 'S': /* The seconds. */ bp = conv_num(bp, &tm->tm_sec, 0, 61); LEGAL_ALT(ALT_O); continue; case 's': /* seconds since the epoch */ { time_t sse = 0; uint64_t rulim = LLONG_MAX; struct tm *tm2; if (*bp < '0' || *bp > '9') { bp = NULL; continue; } do { sse *= 10; sse += *bp++ - '0'; rulim /= 10; } while ((sse * 10 <= LLONG_MAX) && rulim && *bp >= '0' && *bp <= '9'); if (sse < 0 || (uint64_t)sse > LLONG_MAX) { bp = NULL; continue; } if ((tm2 = ::localtime(&sse)) == NULL) bp = NULL; else *tm = *tm2; } continue; case 'U': /* The week of year, beginning on sunday. */ case 'W': /* The week of year, beginning on monday. */ /* * XXX This is bogus, as we can not assume any valid * information present in the tm structure at this * point to calculate a real value, so just check the * range for now. */ bp = conv_num(bp, &i, 0, 53); LEGAL_ALT(ALT_O); continue; case 'w': /* The day of week, beginning on sunday. */ bp = conv_num(bp, &tm->tm_wday, 0, 6); LEGAL_ALT(ALT_O); continue; case 'u': /* The day of week, monday = 1. */ bp = conv_num(bp, &i, 1, 7); tm->tm_wday = i % 7; LEGAL_ALT(ALT_O); continue; case 'g': /* The year corresponding to the ISO week * number but without the century. */ bp = conv_num(bp, &i, 0, 99); continue; case 'G': /* The year corresponding to the ISO week * number with century. */ do bp++; while (isdigit(*bp)); continue; case 'V': /* The ISO 8601:1988 week number as decimal */ bp = conv_num(bp, &i, 0, 53); continue; case 'Y': /* The year. */ i = TM_YEAR_BASE; /* just for data sanity... */ bp = conv_num(bp, &i, 0, 9999); tm->tm_year = i - TM_YEAR_BASE; LEGAL_ALT(ALT_E); continue; case 'y': /* The year within 100 years of the epoch. */ /* LEGAL_ALT(ALT_E | ALT_O); */ bp = conv_num(bp, &i, 0, 99); if (split_year) /* preserve century */ i += (tm->tm_year / 100) * 100; else { split_year = 1; if (i <= 68) i = i + 2000 - TM_YEAR_BASE; else i = i + 1900 - TM_YEAR_BASE; } tm->tm_year = i; continue; case 'Z': tzset(); if (strncmp((const char *)bp, gmt, 3) == 0) { tm->tm_isdst = 0; #ifdef TM_GMTOFF tm->TM_GMTOFF = 0; #endif #ifdef TM_ZONE tm->TM_ZONE = gmt; #endif bp += 3; } else { ep = find_string(bp, &i, (const char * const *)tzname, NULL, 2); if (ep != NULL) { tm->tm_isdst = i; #ifdef TM_GMTOFF tm->TM_GMTOFF = -(timezone); #endif #ifdef TM_ZONE tm->TM_ZONE = tzname[i]; #endif } bp = ep; } continue; case 'z': /* * We recognize all ISO 8601 formats: * Z = Zulu time/UTC * [+-]hhmm * [+-]hh:mm * [+-]hh * We recognize all RFC-822/RFC-2822 formats: * UT|GMT * North American : UTC offsets * E[DS]T = Eastern : -4 | -5 * C[DS]T = Central : -5 | -6 * M[DS]T = Mountain: -6 | -7 * P[DS]T = Pacific : -7 | -8 * Military * [A-IL-M] = -1 ... -9 (J not used) * [N-Y] = +1 ... +12 */ while (isspace(*bp)) bp++; switch (*bp++) { case 'G': if (*bp++ != 'M') return NULL; /*FALLTHROUGH*/ case 'U': if (*bp++ != 'T') return NULL; /*FALLTHROUGH*/ case 'Z': tm->tm_isdst = 0; #ifdef TM_GMTOFF tm->TM_GMTOFF = 0; #endif #ifdef TM_ZONE tm->TM_ZONE = utc; #endif continue; case '+': neg = 0; break; case '-': neg = 1; break; default: --bp; ep = find_string(bp, &i, nast, NULL, 4); if (ep != NULL) { #ifdef TM_GMTOFF tm->TM_GMTOFF = -5 - i; #endif #ifdef TM_ZONE tm->TM_ZONE = __UNCONST(nast[i]); #endif bp = ep; continue; } ep = find_string(bp, &i, nadt, NULL, 4); if (ep != NULL) { tm->tm_isdst = 1; #ifdef TM_GMTOFF tm->TM_GMTOFF = -4 - i; #endif #ifdef TM_ZONE tm->TM_ZONE = __UNCONST(nadt[i]); #endif bp = ep; continue; } if ((*bp >= 'A' && *bp <= 'I') || (*bp >= 'L' && *bp <= 'Y')) { #ifdef TM_GMTOFF /* Argh! No 'J'! */ if (*bp >= 'A' && *bp <= 'I') tm->TM_GMTOFF = ('A' - 1) - (int)*bp; else if (*bp >= 'L' && *bp <= 'M') tm->TM_GMTOFF = 'A' - (int)*bp; else if (*bp >= 'N' && *bp <= 'Y') tm->TM_GMTOFF = (int)*bp - 'M'; #endif #ifdef TM_ZONE tm->TM_ZONE = NULL; /* XXX */ #endif bp++; continue; } return NULL; } offs = 0; for (i = 0; i < 4; ) { if (isdigit(*bp)) { offs = offs * 10 + (*bp++ - '0'); i++; continue; } if (i == 2 && *bp == ':') { bp++; continue; } break; } switch (i) { case 2: offs *= 100; break; case 4: i = offs % 100; if (i >= 60) return NULL; /* Convert minutes into decimal */ offs = (offs / 100) * 100 + (i * 50) / 30; break; default: return NULL; } if (neg) offs = -offs; tm->tm_isdst = 0; /* XXX */ #ifdef TM_GMTOFF tm->TM_GMTOFF = offs; #endif #ifdef TM_ZONE tm->TM_ZONE = NULL; /* XXX */ #endif continue; /* * Miscellaneous conversions. */ case 'n': /* Any kind of white-space. */ case 't': while (isspace(*bp)) bp++; LEGAL_ALT(0); continue; default: /* Unknown/unsupported conversion. */ return NULL; } } return (char *)(bp); } static const u_char * conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim) { uint result = 0; unsigned char ch; /* The limit also determines the number of valid digits. */ uint rulim = ulim; ch = *buf; if (ch < '0' || ch > '9') return NULL; do { result *= 10; result += ch - '0'; rulim /= 10; ch = *++buf; } while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9'); if (result < llim || result > ulim) return NULL; *dest = result; return buf; } int strncasecmp(char *s1, char *s2, size_t n) { if (n == 0) return 0; while (n-- != 0 && tolower(*s1) == tolower(*s2)) { if (n == 0 || *s1 == '\0' || *s2 == '\0') break; s1++; s2++; } return tolower(*(unsigned char *) s1) - tolower(*(unsigned char *) s2); } static const u_char * find_string(const u_char *bp, int *tgt, const char * const *n1, const char * const *n2, int c) { int i; unsigned int len; /* check full name - then abbreviated ones */ for (; n1 != NULL; n1 = n2, n2 = NULL) { for (i = 0; i < c; i++, n1++) { len = strlen(*n1); if (strncasecmp((char *)*n1, (char *)bp, len) == 0) { *tgt = i; return bp + len; } } } /* Nothing matched */ return NULL; } /****************************************************************************** * END OF STRPTIME FOR WINDOWS *****************************************************************************/ #endif struct_time *strptime(str *string, str *format) { #ifdef WIN32 tm time_tuple = {0, 0, 0, 1, 0, 0, 0, 1, -1}; /* XXX check if newer MinGW supports this */ if(!strptime(string->unit.c_str(), format->unit.c_str(), &time_tuple)) throw new ValueError(new str("time data did not match format: data="+string->unit+" fmt="+format->unit)); #else tm time_tuple = {0, 0, 0, 1, 0, 0, 0, 1, -1, 0, 0}; if(!::strptime(string->unit.c_str(), format->unit.c_str(), &time_tuple)) throw new ValueError(new str("time data did not match format: data="+string->unit+" fmt="+format->unit)); #endif return tm2tuple(&time_tuple); } void __init() { start = std::clock(); const_0 = new str("time.struct_time() takes a 9-sequence"); const_1 = new str("time.struct_time(tm_year=%d, tm_mon=%d, tm_mday=%d, tm_hour=%d, tm_min=%d, tm_sec=%d, tm_wday=%d, tm_yday=%d, tm_isdst=%d)"); struct_time* gmt = gmtime(); struct_time* localt = localtime(); __ss_int gmt_hour = gmt->tm_hour; __ss_int localt_hour = localt->tm_hour; if (gmt->tm_mday > localt->tm_mday) { localt_hour -= 24; } else if (gmt->tm_mday < localt->tm_mday) { localt_hour += 24; } timezone = (gmt_hour - localt_hour) * 3600; tzname = new tuple2(2, new str(::tzname[0]), new str(::tzname[1])); } } // module namespace shedskin-0.9.4/shedskin/lib/struct.cpp0000664000175000017500000003350712157270661017677 0ustar srepmubsrepmub/* Copyright 2005-2011 Mark Dufour and contributors; License Expat (See LICENSE) */ #include "struct.hpp" #include namespace __struct__ { __GC_STRING ordering; void *buffy; class_ *cl_error; bool little_endian; static inline bool swap_endian(char o) { return (little_endian and (o=='>' or o=='!')) or (not little_endian and o=='<'); } int get_itemsize(char order, char c) { switch(c) { case 'c': return 1; case 's': return 1; case 'p': return 1; case '?': return 1; case 'x': return 1; } if(order == '@') { switch(c) { case 'b': return sizeof(signed char); case 'B': return sizeof(unsigned char); case 'h': return sizeof(short); case 'H': return sizeof(unsigned short); case 'i': return sizeof(int); case 'I': return sizeof(unsigned int); case 'l': return sizeof(long); case 'L': return sizeof(unsigned long); case 'q': return sizeof(long long); case 'Q': return sizeof(unsigned long long); case 'f': return sizeof(float); case 'd': return sizeof(double); } } else { switch(c) { case 'b': return 1; case 'B': return 1; case 'h': return 2; case 'H': return 2; case 'i': return 4; case 'I': return 4; case 'l': return 4; case 'L': return 4; case 'q': return 8; case 'Q': return 8; case 'f': return 4; case 'd': return 8; } } return 0; } int padding(char o, int pos, unsigned int itemsize) { if(sizeof(void *) == 4) { #ifndef WIN32 if(itemsize == 8) itemsize = 4; #endif } if(o == '@' and pos % itemsize) return itemsize - (pos % itemsize); return 0; } __ss_int unpack_int(char o, char c, unsigned int d, str *data, __ss_int *pos) { unsigned long long result; unsigned int itemsize = get_itemsize(o, c); *pos += padding(o, *pos, itemsize); if(d==0) return 0; result = 0; for(unsigned int i=0; iunit[*pos+i]; if(swap_endian(o)) result |= (c << 8*(itemsize-i-1)); else result |= (c << 8*i); } *pos += itemsize; if(c == 'h') return (short)result; return result; } str * unpack_str(char, char c, unsigned int d, str *data, __ss_int *pos) { str *result = 0; unsigned int len; switch(c) { case 'c': result = __char_cache[(unsigned char)(data->unit[*pos])]; break; case 's': result = new str(); for(unsigned int i=0; iunit += data->unit[*pos+i]; break; case 'p': result = new str(); len = data->unit[*pos]; for(unsigned int i=0; iunit += data->unit[*pos+i+1]; break; } *pos += d; return result; } __ss_bool unpack_bool(char, char, unsigned int d, str *data, __ss_int *pos) { __ss_bool result; if(data->unit[*pos] == '\x00') result = False; else result = True; if(d!=0) *pos += 1; return result; } double unpack_float(char o, char c, unsigned int d, str *data, __ss_int *pos) { double result; unsigned int itemsize = get_itemsize(o, c); *pos += padding(o, *pos, itemsize); if(d==0) return 0; if(swap_endian(o)) for(unsigned int i=0; iunit[*pos+i]; else for(unsigned int i=0; iunit[*pos+i]; if(c == 'f') result = *((float *)(buffy)); else result = *((double *)(buffy)); *pos += itemsize; return result; } void unpack_pad(char, char, unsigned int d, str *, __ss_int *pos) { *pos += d; } __ss_int calcsize(str *fmt) { __ss_int result = 0; str *digits = new str(); char order = '@'; unsigned int itemsize; for(unsigned int i=0; i<(unsigned int)len(fmt); i++) { char c = fmt->unit[i]; if(ordering.find(c) != std::string::npos) { order = c; continue; } if(::isdigit(c)) { digits->unit += c; continue; } unsigned int ndigits = 1; if(len(digits)) { ndigits = __int(digits); digits = new str(); } itemsize = get_itemsize(order, c); switch(c) { case 'b': case 'B': case 'h': case 'H': case 'i': case 'I': case 'l': case 'L': case 'q': case 'Q': case 'd': case 'f': result += padding(order, result, itemsize); case 'c': case 's': case 'p': case '?': case 'x': break; case 'P': throw new error(new str("unsupported 'P' char in struct format")); default: throw new error(new str("bad char in struct format")); } result += ndigits * itemsize; } return result; } void fillbuf(char c, __ss_int t, char order, unsigned int itemsize) { if(order == '@') { switch(c) { case 'b': *((signed char *)buffy) = t; break; case 'B': *((unsigned char *)buffy) = t; break; case 'h': *((short *)buffy) = t; break; case 'H': *((unsigned short *)buffy) = t; break; case 'i': *((int *)buffy) = t; break; case 'I': *((unsigned int *)buffy) = t; break; case 'l': *((long *)buffy) = t; break; case 'L': *((unsigned long *)buffy) = t; break; case 'q': *((long long *)buffy) = t; break; case 'Q': *((unsigned long long *)buffy) = t; break; } } else { if(swap_endian(order)) { for(int i=itemsize-1; i>=0; i--) { ((char *)buffy)[i] = (unsigned char)(t & 0xff); t >>= 8; } } else { for(unsigned int i=0; i>= 8; } } } } void fillbuf2(char c, double t, char, unsigned int) { switch(c) { case 'f': *((float *)buffy) = t; break; case 'd': *((double *)buffy) = t; break; } } str *pack(int, str *fmt, ...) { pyobj *arg; va_list args; va_start(args, fmt); str *result = new str(); char order = '@'; str *digits = new str(); int pos=0; unsigned int itemsize, pad; unsigned int fmtlen = fmt->__len__(); str *strarg; int pascal_ff = 0; for(unsigned int j=0; junit[j]; if(ordering.find(c) != std::string::npos) { order = c; continue; } if(::isdigit(c)) { digits->unit += c; continue; } unsigned int ndigits = 1; if(len(digits)) { ndigits = __int(digits); digits = new str(); } switch(c) { case 'b': case 'B': case 'h': case 'H': case 'i': case 'I': case 'l': case 'L': case 'q': case 'Q': itemsize = get_itemsize(order, c); pad = padding(order, pos, itemsize); for(unsigned int j=0; junit += '\xff'; pascal_ff = 0; } else result->unit += '\x00'; } pos += pad; for(unsigned int j=0; j__class__ == cl_int_) value = ((int_ *)arg)->unit; else if(arg->__class__ == cl_float_) value = ((float_ *)arg)->unit; else { try { value = arg->__index__(); } catch(Exception *e) { throw new error(new str("cannot convert argument to integer")); } } fillbuf(c, value, order, itemsize); for(unsigned int k=0; kunit += ((char *)buffy)[k]; pos += itemsize; } if(ndigits) pascal_ff = 0; break; case 'd': case 'f': itemsize = get_itemsize(order, c); pad = padding(order, pos, itemsize); for(unsigned int j=0; junit += '\xff'; pascal_ff = 0; } else result->unit += '\x00'; } pos += pad; for(unsigned int j=0; j__class__ == cl_float_) value = ((float_ *)arg)->unit; else if(arg->__class__ == cl_int_) value = ((int_ *)arg)->unit; else throw new error(new str("required argument is not a float")); fillbuf2(c, value, order, itemsize); if(swap_endian(order)) for(int i=itemsize-1; i>=0; i--) result->unit += ((char *)buffy)[i]; else for(unsigned int i=0; iunit += ((char *)buffy)[i]; pos += itemsize; } if(ndigits) pascal_ff = 0; break; case 'c': for(unsigned int j=0; j__class__ != cl_str_) throw new error(new str("char format require string of length 1")); strarg = ((str *)(arg)); unsigned int len = strarg->__len__(); if(len != 1) throw new error(new str("char format require string of length 1")); result->unit += strarg->unit[0]; pos += 1; } if(ndigits) pascal_ff = 0; break; case 'p': arg = va_arg(args, pyobj *); if(arg->__class__ != cl_str_) throw new error(new str("argument for 'p' must be a string")); if(ndigits) { strarg = ((str *)(arg)); unsigned int len = strarg->__len__(); if(len+1 > ndigits) len = ndigits-1; result->unit += (unsigned char)(len); for(unsigned int j=0; junit += strarg->unit[j]; for(unsigned int j=0; junit += '\x00'; pos += ndigits; pascal_ff = 0; } else pascal_ff = 1; break; case 's': arg = va_arg(args, pyobj *); if(arg->__class__ != cl_str_) throw new error(new str("argument for 's' must be a string")); if(ndigits) { strarg = ((str *)(arg)); unsigned int len = strarg->__len__(); if(len > ndigits) len = ndigits; for(unsigned int j=0; junit += strarg->unit[j]; for(unsigned int j=0; junit += '\xff'; pascal_ff = 0; } else result->unit += '\x00'; } pos += ndigits; pascal_ff = 0; } break; case '?': for(unsigned int j=0; j__nonzero__()) result->unit += '\x01'; else result->unit += '\x00'; pos += 1; } if(ndigits) pascal_ff = 0; break; case 'x': for(unsigned int j=0; junit += '\xff'; pascal_ff = 0; } else result->unit += '\x00'; } pos += ndigits; break; case 'P': throw new error(new str("unsupported 'P' char in struct format")); default: throw new error(new str("bad char in struct format")); } } va_end(args); return result; } void __init() { ordering = "@<>!="; cl_error = new class_("error"); int num = 1; little_endian = (*(char *)&num == 1); buffy = malloc(8); } } // module namespace shedskin-0.9.4/shedskin/lib/itertools.cpp0000664000175000017500000000025212157270661020366 0ustar srepmubsrepmub/* Copyright (c) 2009 Jérémie Roquet ; License Expat (See LICENSE) */ #include "itertools.hpp" namespace __itertools__ { void __init() { } } shedskin-0.9.4/shedskin/config.py0000664000175000017500000000553412157270661016717 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) ''' import os import sys class GlobalInfo: # XXX add comments, split up def __init__(self): self.constraints = set() self.allvars = set() self.allfuncs = set() self.allclasses = set() self.cnode = {} self.types = {} self.templates = 0 self.modules = {} self.inheritance_relations = {} self.inheritance_temp_vars = {} self.parent_nodes = {} self.inherited = set() self.nrcltypes = 8 self.empty_constructors = set() self.sig_nr = {} self.nameclasses = {} self.module = None self.builtins = ['none', 'str_', 'float_', 'int_', 'class_', 'list', 'tuple', 'tuple2', 'dict', 'set', 'frozenset', 'bool_'] self.assign_target = {} # instance node for instance Variable assignment self.alloc_info = {} # allocation site type information across iterations self.iterations = 0 self.total_iterations = 0 self.lambdawrapper = {} self.init_directories() illegal_file = file(os.path.join(self.sysdir, 'illegal')) self.cpp_keywords = set(line.strip() for line in illegal_file) self.ss_prefix = '__ss_' self.list_types = {} self.loopstack = [] # track nested loops self.comments = {} self.import_order = 0 # module import order self.from_module = {} self.class_def_order = 0 # command-line options self.wrap_around_check = True self.bounds_checking = True self.fast_random = False self.assertions = True self.extension_module = False self.longlong = False self.flags = None self.annotation = False self.msvc = False self.gcwarns = True self.pypy = False self.silent = False self.backtrace = False self.makefile_name = 'Makefile' # XXX other default? self.item_rvalue = {} self.genexp_to_lc = {} self.bool_test_only = set() self.tempcount = {} self.fast_hash = False self.struct_unpack = {} self.debug_level = 0 self.maxhits = 0 # XXX amaze.py termination self.gc_cleanup = False def init_directories(self): shedskin_directory = os.sep.join(__file__.split(os.sep)[:-1]) shedskin_libdir = os.path.join(shedskin_directory, 'lib') system_libdir = '/usr/share/shedskin/lib' self.sysdir = shedskin_directory if os.path.isdir(shedskin_libdir): self.libdirs = [shedskin_libdir] elif os.path.isdir(system_libdir): self.libdirs = [system_libdir] else: print '*ERROR* Could not find lib directory.\n' sys.exit(1) shedskin-0.9.4/shedskin/extmod.py0000664000175000017500000005222112157270661016745 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) extmod.py: extension module support ''' from infer import called from python import Class, def_class, Module from typestr import ExtmodError, nodetypestr, singletype2 OVERLOAD_SINGLE = ['__neg__', '__pos__', '__abs__', '__nonzero__'] OVERLOAD = ['__add__', '__sub__', '__mul__', '__div__', '__mod__', '__divmod__', '__pow__'] + OVERLOAD_SINGLE def do_init_mods(gx, gv, what): for module in gx.modules.values(): if not module.builtin and not module is gv.module: print >>gv.out, ' %s::%s%s();' % (module.full_path(), what, '_'.join(module.name_list)) def clname(cl): return '__ss_%s_%s' % ('_'.join(cl.mv.module.name_list), cl.ident) def supported_vars(gx, gv, vars): # XXX virtuals? supported = [] for var in vars: if not var in gx.merged_inh or not gx.merged_inh[var]: continue if var.name.startswith('__'): # XXX continue if var.invisible or singletype2(gx.merged_inh[var], Module): continue try: nodetypestr(gx, var, var.parent, check_extmod=True, mv=gv.mv) except ExtmodError: if isinstance(var.parent, Class): print "*WARNING* '%s' variable not exported (cannot convert)" % (var.parent.ident + '.' + var.name) else: print "*WARNING* '%s' variable not exported (cannot convert)" % var.name continue supported.append(var) return supported def supported_funcs(gx, gv, funcs): supported = [] for func in funcs: if func.isGenerator or not called(func): continue if func.ident in ['__setattr__', '__getattr__', '__iadd__', '__isub__', '__imul__']: # XXX continue if isinstance(func.parent, Class): if func.invisible or func.inherited or not gv.inhcpa(func): continue if isinstance(func.parent, Class) and func.ident in func.parent.staticmethods: print "*WARNING* '%s' method not exported (staticmethod)" % (func.parent.ident + '.' + func.ident) continue builtins = True for formal in func.formals: try: nodetypestr(gx, func.vars[formal], func, check_extmod=True, mv=gv.mv) except ExtmodError: builtins = False reason = "cannot convert argument '%s'" % formal try: nodetypestr(gx, func.retnode.thing, func, check_extmod=True, check_ret=True, mv=gv.mv) except ExtmodError: builtins = False reason = 'cannot convert return value' if builtins: supported.append(func) else: if isinstance(func.parent, Class): print "*WARNING* '%s' method not exported (%s)" % (func.parent.ident + '.' + func.ident, reason) else: print "*WARNING* '%s' function not exported (%s)" % (func.ident, reason) return supported def has_method(cl, name): # XXX shared.py return name in cl.funcs and not cl.funcs[name].invisible and not cl.funcs[name].inherited and called(cl.funcs[name]) def do_add_globals(gx, gv, classes, __ss_mod): # global variables for var in supported_vars(gx, gv, gv.mv.globals.values()): if [1 for t in gv.mergeinh[var] if t[0].ident in ['int_', 'float_', 'bool_']]: print >>gv.out, ' PyModule_AddObject(%(ssmod)s, (char *)"%(name)s", __to_py(%(var)s));' % {'name': var.name, 'var': '__' + gv.module.ident + '__::' + gv.cpp_name(var), 'ssmod': __ss_mod} else: print >>gv.out, ' PyModule_AddObject(%(ssmod)s, (char *)"%(name)s", __to_py(%(var)s));' % {'name': var.name, 'var': '__' + gv.module.ident + '__::' + gv.cpp_name(var), 'ssmod': __ss_mod} def do_reduce_setstate(gx, gv, cl, vars): if def_class(gx, 'Exception') in cl.ancestors(): # XXX return print >>gv.out, 'PyObject *%s__reduce__(PyObject *self, PyObject *args, PyObject *kwargs) {' % clname(cl) print >>gv.out, ' PyObject *t = PyTuple_New(3);' print >>gv.out, ' PyTuple_SetItem(t, 0, PyObject_GetAttrString(__ss_mod_%s, "__newobj__"));' % '_'.join(gv.module.name_list) print >>gv.out, ' PyObject *a = PyTuple_New(1);' print >>gv.out, ' Py_INCREF((PyObject *)&%sObjectType);' % clname(cl) print >>gv.out, ' PyTuple_SetItem(a, 0, (PyObject *)&%sObjectType);' % clname(cl) print >>gv.out, ' PyTuple_SetItem(t, 1, a);' print >>gv.out, ' PyObject *b = PyTuple_New(%d);' % len(vars) for i, var in enumerate(vars): print >>gv.out, ' PyTuple_SetItem(b, %d, __to_py(((%sObject *)self)->__ss_object->%s));' % (i, clname(cl), gv.cpp_name(var)) print >>gv.out, ' PyTuple_SetItem(t, 2, b);' print >>gv.out, ' return t;' print >>gv.out, '}\n' print >>gv.out, 'PyObject *%s__setstate__(PyObject *self, PyObject *args, PyObject *kwargs) {' % clname(cl) print >>gv.out, ' int l = PyTuple_Size(args);' print >>gv.out, ' PyObject *state = PyTuple_GetItem(args, 0);' for i, var in enumerate(vars): vartype = nodetypestr(gx, var, var.parent, mv=gv.mv) print >>gv.out, ' ((%sObject *)self)->__ss_object->%s = __to_ss<%s>(PyTuple_GetItem(state, %d));' % (clname(cl), gv.cpp_name(var), vartype, i) print >>gv.out, ' Py_INCREF(Py_None);' print >>gv.out, ' return Py_None;' print >>gv.out, '}\n' def convert_methods(gx, gv, cl, declare): if def_class(gx, 'Exception') in cl.ancestors(): return if declare: print >>gv.out, ' virtual PyObject *__to_py__();' else: for n in cl.module.name_list: print >>gv.out, 'namespace __%s__ { /* XXX */' % n print >>gv.out print >>gv.out, 'PyObject *%s::__to_py__() {' % gv.cpp_name(cl) print >>gv.out, ' PyObject *p;' print >>gv.out, ' if(__ss_proxy->has_key(this))' print >>gv.out, ' p = (PyObject *)(__ss_proxy->__getitem__(this));' print >>gv.out, ' else {' print >>gv.out, ' %sObject *self = (%sObject *)(%sObjectType.tp_alloc(&%sObjectType, 0));' % (4 * (clname(cl),)) print >>gv.out, ' self->__ss_object = this;' print >>gv.out, ' __ss_proxy->__setitem__(self->__ss_object, self);' print >>gv.out, ' p = (PyObject *)self;' print >>gv.out, ' }' print >>gv.out, ' Py_INCREF(p);' print >>gv.out, ' return p;' print >>gv.out, '}\n' for n in cl.module.name_list: print >>gv.out, '} // module namespace' print >>gv.out print >>gv.out, 'namespace __shedskin__ { /* XXX */\n' print >>gv.out, 'template<> %s::%s *__to_ss(PyObject *p) {' % (cl.module.full_path(), gv.cpp_name(cl)) print >>gv.out, ' if(p == Py_None) return NULL;' print >>gv.out, ' if(PyObject_IsInstance(p, (PyObject *)&%s::%sObjectType)!=1)' % (cl.module.full_path(), clname(cl)) print >>gv.out, ' throw new TypeError(new str("error in conversion to Shed Skin (%s expected)"));' % cl.ident print >>gv.out, ' return ((%s::%sObject *)p)->__ss_object;' % (cl.module.full_path(), clname(cl)) print >>gv.out, '}\n}' def do_extmod_methoddef(gx, gv, ident, funcs, cl): if cl: ident = clname(cl) print >>gv.out, 'static PyNumberMethods %s_as_number = {' % ident for overload in OVERLOAD: if [f for f in funcs if f.ident == overload]: if overload == '__nonzero__': # XXX print >>gv.out, ' (int (*)(PyObject *))%s_%s,' % (clname(f.parent), overload) elif overload in OVERLOAD_SINGLE: print >>gv.out, ' (PyObject *(*)(PyObject *))%s_%s,' % (clname(f.parent), overload) else: print >>gv.out, ' (PyCFunction)%s_%s,' % (clname(f.parent), overload) else: print >>gv.out, ' 0,' print >>gv.out, '};\n' if cl and not def_class(gx, 'Exception') in cl.ancestors(): print >>gv.out, 'PyObject *%s__reduce__(PyObject *self, PyObject *args, PyObject *kwargs);' % ident print >>gv.out, 'PyObject *%s__setstate__(PyObject *self, PyObject *args, PyObject *kwargs);\n' % ident print >>gv.out, 'static PyMethodDef %sMethods[] = {' % ident if not cl: print >>gv.out, ' {(char *)"__newobj__", (PyCFunction)__ss__newobj__, METH_VARARGS | METH_KEYWORDS, (char *)""},' elif cl and not def_class(gx, 'Exception') in cl.ancestors(): print >>gv.out, ' {(char *)"__reduce__", (PyCFunction)%s__reduce__, METH_VARARGS | METH_KEYWORDS, (char *)""},' % ident print >>gv.out, ' {(char *)"__setstate__", (PyCFunction)%s__setstate__, METH_VARARGS | METH_KEYWORDS, (char *)""},' % ident for func in funcs: if isinstance(func.parent, Class): id = clname(func.parent) + '_' + func.ident else: id = 'Global_' + '_'.join(gv.module.name_list) + '_' + func.ident print >>gv.out, ' {(char *)"%(id)s", (PyCFunction)%(id2)s, METH_VARARGS | METH_KEYWORDS, (char *)""},' % {'id': func.ident, 'id2': id} print >>gv.out, ' {NULL}\n};\n' def do_extmod_method(gx, gv, func): is_method = isinstance(func.parent, Class) if is_method: formals = func.formals[1:] else: formals = func.formals if isinstance(func.parent, Class): id = clname(func.parent) + '_' + func.ident else: id = 'Global_' + '_'.join(gv.module.name_list) + '_' + func.ident print >>gv.out, 'PyObject *%s(PyObject *self, PyObject *args, PyObject *kwargs) {' % id print >>gv.out, ' try {' for i, formal in enumerate(formals): gv.start('') typ = nodetypestr(gx, func.vars[formal], func, mv=gv.mv) if func.ident in OVERLOAD: print >>gv.out, ' %(type)sarg_%(num)d = __to_ss<%(type)s>(args);' % {'type': typ, 'num': i} continue gv.append(' %(type)sarg_%(num)d = __ss_arg<%(type)s>("%(name)s", %(num)d, ' % {'type': typ, 'num': i, 'name': formal}) if i >= len(formals) - len(func.defaults): gv.append('1, ') defau = func.defaults[i - (len(formals) - len(func.defaults))] if defau in func.mv.defaults: if gv.mergeinh[defau] == set([(def_class(gx, 'none'), 0)]): gv.append('0') else: gv.append('%s::default_%d' % ('__' + func.mv.module.ident + '__', func.mv.defaults[defau][0])) else: gv.visit(defau, func) elif typ.strip() == '__ss_bool': gv.append('0, False') else: gv.append('0, 0') gv.append(', args, kwargs)') gv.eol() print >>gv.out # call if is_method: where = '((%sObject *)self)->__ss_object->' % clname(func.parent) else: where = '__' + gv.module.ident + '__::' print >>gv.out, ' return __to_py(' + where + gv.cpp_name(func.ident) + '(' + ', '.join('arg_%d' % i for i in range(len(formals))) + '));\n' # convert exceptions print >>gv.out, ' } catch (Exception *e) {' print >>gv.out, ' PyErr_SetString(__to_py(e), ((e->message)?(e->message->unit.c_str()):""));' print >>gv.out, ' return 0;' print >>gv.out, ' }' print >>gv.out, '}\n' def do_extmod(gx, gv): print >>gv.out, '/* extension module glue */\n' print >>gv.out, 'extern "C" {' print >>gv.out, '#include ' print >>gv.out, '#include \n' print >>gv.out, 'PyObject *__ss_mod_%s;\n' % '_'.join(gv.module.name_list) # classes classes = exported_classes(gx, gv, warns=True) for cl in classes: do_extmod_class(gx, gv, cl) for n in gv.module.name_list: print >>gv.out, 'namespace __%s__ { /* XXX */' % n # global functions funcs = supported_funcs(gx, gv, gv.module.mv.funcs.values()) for func in funcs: do_extmod_method(gx, gv, func) do_extmod_methoddef(gx, gv, 'Global_' + '_'.join(gv.module.name_list), funcs, None) # module init function print >>gv.out, 'PyMODINIT_FUNC init%s(void) {' % '_'.join(gv.module.name_list) # initialize modules __ss_mod = '__ss_mod_%s' % '_'.join(gv.module.name_list) if gv.module == gx.main_module: gv.do_init_modules() print >>gv.out, ' __' + gv.module.ident + '__::__init();' print >>gv.out, '\n %s = Py_InitModule((char *)"%s", Global_%sMethods);' % (__ss_mod, gv.module.ident, '_'.join(gv.module.name_list)) print >>gv.out, ' if(!%s)' % __ss_mod print >>gv.out, ' return;\n' # add types to module for cl in classes: print >>gv.out, ' if (PyType_Ready(&%sObjectType) < 0)' % clname(cl) print >>gv.out, ' return;\n' print >>gv.out, ' PyModule_AddObject(%s, "%s", (PyObject *)&%sObjectType);' % (__ss_mod, cl.ident, clname(cl)) print >>gv.out if gv.module == gx.main_module: do_init_mods(gx, gv, 'init') do_init_mods(gx, gv, 'add') print >>gv.out, ' add%s();' % gv.module.ident print >>gv.out, '\n}\n' print >>gv.out, 'PyMODINIT_FUNC add%s(void) {' % '_'.join(gv.module.name_list) do_add_globals(gx, gv, classes, __ss_mod) print >>gv.out, '\n}' for n in gv.module.name_list: print >>gv.out, '\n} // namespace __%s__' % n print >>gv.out, '\n} // extern "C"' # conversion methods to/from CPython/Shedskin for cl in classes: convert_methods(gx, gv, cl, False) def do_extmod_class(gx, gv, cl): for n in cl.module.name_list: print >>gv.out, 'namespace __%s__ { /* XXX */' % n print >>gv.out # determine methods, vars to expose funcs = supported_funcs(gx, gv, cl.funcs.values()) vars = supported_vars(gx, gv, cl.vars.values()) # python object print >>gv.out, '/* class %s */\n' % cl.ident print >>gv.out, 'typedef struct {' print >>gv.out, ' PyObject_HEAD' print >>gv.out, ' %s::%s *__ss_object;' % (cl.module.full_path(), gv.cpp_name(cl)) print >>gv.out, '} %sObject;\n' % clname(cl) print >>gv.out, 'static PyMemberDef %sMembers[] = {' % clname(cl) print >>gv.out, ' {NULL}\n};\n' # methods for func in funcs: do_extmod_method(gx, gv, func) do_extmod_methoddef(gx, gv, cl.ident, funcs, cl) # tp_init if has_method(cl, '__init__') and cl.funcs['__init__'] in funcs: print >>gv.out, 'int %s___tpinit__(PyObject *self, PyObject *args, PyObject *kwargs) {' % clname(cl) print >>gv.out, ' if(!%s___init__(self, args, kwargs))' % clname(cl) print >>gv.out, ' return -1;' print >>gv.out, ' return 0;' print >>gv.out, '}\n' # tp_new print >>gv.out, 'PyObject *%sNew(PyTypeObject *type, PyObject *args, PyObject *kwargs) {' % clname(cl) print >>gv.out, ' %sObject *self = (%sObject *)type->tp_alloc(type, 0);' % (clname(cl), clname(cl)) print >>gv.out, ' self->__ss_object = new %s::%s();' % (cl.module.full_path(), gv.cpp_name(cl)) print >>gv.out, ' self->__ss_object->__class__ = %s::cl_%s;' % (cl.module.full_path(), cl.ident) print >>gv.out, ' __ss_proxy->__setitem__(self->__ss_object, self);' print >>gv.out, ' return (PyObject *)self;' print >>gv.out, '}\n' # tp_dealloc print >>gv.out, 'void %sDealloc(%sObject *self) {' % (clname(cl), clname(cl)) print >>gv.out, ' self->ob_type->tp_free((PyObject *)self);' print >>gv.out, ' __ss_proxy->__delitem__(self->__ss_object);' print >>gv.out, '}\n' # getset for var in vars: print >>gv.out, 'PyObject *__ss_get_%s_%s(%sObject *self, void *closure) {' % (clname(cl), var.name, clname(cl)) print >>gv.out, ' return __to_py(self->__ss_object->%s);' % gv.cpp_name(var) print >>gv.out, '}\n' print >>gv.out, 'int __ss_set_%s_%s(%sObject *self, PyObject *value, void *closure) {' % (clname(cl), var.name, clname(cl)) print >>gv.out, ' try {' typ = nodetypestr(gx, var, var.parent, mv=gv.mv) if typ == 'void *': # XXX investigate print >>gv.out, ' self->__ss_object->%s = NULL;' % gv.cpp_name(var) else: print >>gv.out, ' self->__ss_object->%s = __to_ss<%s>(value);' % (gv.cpp_name(var), typ) print >>gv.out, ' } catch (Exception *e) {' print >>gv.out, ' PyErr_SetString(__to_py(e), ((e->message)?(e->message->unit.c_str()):""));' print >>gv.out, ' return -1;' print >>gv.out, ' }' print >>gv.out, ' return 0;' print >>gv.out, '}\n' print >>gv.out, 'PyGetSetDef %sGetSet[] = {' % clname(cl) for var in vars: print >>gv.out, ' {(char *)"%s", (getter)__ss_get_%s_%s, (setter)__ss_set_%s_%s, (char *)"", NULL},' % (var.name, clname(cl), var.name, clname(cl), var.name) print >>gv.out, ' {NULL}\n};\n' # python type print >>gv.out, 'PyTypeObject %sObjectType = {' % clname(cl) print >>gv.out, ' PyObject_HEAD_INIT(NULL)' print >>gv.out, ' 0, /* ob_size */' print >>gv.out, ' "%s.%s", /* tp_name */' % (cl.module.ident, cl.ident) print >>gv.out, ' sizeof(%sObject), /* tp_basicsize */' % clname(cl) print >>gv.out, ' 0, /* tp_itemsize */' print >>gv.out, ' (destructor)%sDealloc, /* tp_dealloc */' % clname(cl) print >>gv.out, ' 0, /* tp_print */' print >>gv.out, ' 0, /* tp_getattr */' print >>gv.out, ' 0, /* tp_setattr */' print >>gv.out, ' 0, /* tp_compare */' if has_method(cl, '__repr__'): print >>gv.out, ' (PyObject *(*)(PyObject *))%s___repr__, /* tp_repr */' % clname(cl) else: print >>gv.out, ' 0, /* tp_repr */' print >>gv.out, ' &%s_as_number, /* tp_as_number */' % clname(cl) print >>gv.out, ' 0, /* tp_as_sequence */' print >>gv.out, ' 0, /* tp_as_mapping */' print >>gv.out, ' 0, /* tp_hash */' print >>gv.out, ' 0, /* tp_call */' if has_method(cl, '__str__'): print >>gv.out, ' (PyObject *(*)(PyObject *))%s___str__, /* tp_str */' % clname(cl) else: print >>gv.out, ' 0, /* tp_str */' print >>gv.out, ' 0, /* tp_getattro */' print >>gv.out, ' 0, /* tp_setattro */' print >>gv.out, ' 0, /* tp_as_buffer */' print >>gv.out, ' Py_TPFLAGS_DEFAULT, /* tp_flags */' print >>gv.out, ' 0, /* tp_doc */' print >>gv.out, ' 0, /* tp_traverse */' print >>gv.out, ' 0, /* tp_clear */' print >>gv.out, ' 0, /* tp_richcompare */' print >>gv.out, ' 0, /* tp_weaklistoffset */' print >>gv.out, ' 0, /* tp_iter */' print >>gv.out, ' 0, /* tp_iternext */' print >>gv.out, ' %sMethods, /* tp_methods */' % clname(cl) print >>gv.out, ' %sMembers, /* tp_members */' % clname(cl) print >>gv.out, ' %sGetSet, /* tp_getset */' % clname(cl) if cl.bases and not cl.bases[0].mv.module.builtin: print >>gv.out, ' &%sObjectType, /* tp_base */' % clname(cl.bases[0]) else: print >>gv.out, ' 0, /* tp_base */' print >>gv.out, ' 0, /* tp_dict */' print >>gv.out, ' 0, /* tp_descr_get */' print >>gv.out, ' 0, /* tp_descr_set */' print >>gv.out, ' 0, /* tp_dictoffset */' if has_method(cl, '__init__') and cl.funcs['__init__'] in funcs: print >>gv.out, ' %s___tpinit__, /* tp_init */' % clname(cl) else: print >>gv.out, ' 0, /* tp_init */' print >>gv.out, ' 0, /* tp_alloc */' print >>gv.out, ' %sNew, /* tp_new */' % clname(cl) print >>gv.out, '};\n' do_reduce_setstate(gx, gv, cl, vars) for n in cl.module.name_list: print >>gv.out, '} // namespace __%s__' % n print >>gv.out def pyinit_func(gv): for what in ('init', 'add'): print >>gv.out, 'PyMODINIT_FUNC %s%s(void);\n' % (what, '_'.join(gv.module.name_list)) def exported_classes(gx, gv, warns=False): classes = [] for cl in gv.module.mv.classes.values(): if def_class(gx, 'Exception') in cl.ancestors(): if warns: print "*WARNING* '%s' class not exported (inherits from Exception)" % cl.ident else: classes.append(cl) return sorted(classes, key=lambda x: x.def_order) def convert_methods2(gx, gv): for cl in exported_classes(gx, gv): print >>gv.out, 'extern PyTypeObject %sObjectType;' % clname(cl) print >>gv.out, 'namespace __shedskin__ { /* XXX */\n' for cl in exported_classes(gx, gv): print >>gv.out, 'template<> %s::%s *__to_ss(PyObject *p);' % (cl.module.full_path(), gv.cpp_name(cl)) print >>gv.out, '}' shedskin-0.9.4/shedskin/FLAGS.osx0000664000175000017500000000011512157270661016455 0ustar srepmubsrepmubCC=g++ CCFLAGS=-O2 -Wno-deprecated $(CPPFLAGS) LFLAGS=-lgc -lpcre $(LDFLAGS) shedskin-0.9.4/shedskin/FLAGS0000664000175000017500000000013312157270661015645 0ustar srepmubsrepmubCC=g++ CCFLAGS=-O2 -march=native -Wno-deprecated $(CPPFLAGS) LFLAGS=-lgc -lpcre $(LDFLAGS) shedskin-0.9.4/shedskin/error.py0000664000175000017500000000204112157270661016571 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) ''' import sys import infer ERRORS = set() def error(msg, gx, node=None, warning=False, mv=None): if warning: kind = '*WARNING*' else: kind = '*ERROR*' if not mv and node and (node, 0, 0) in gx.cnode: mv = infer.inode(gx, node).mv filename = lineno = None if mv: filename = mv.module.relative_filename if node and hasattr(node, 'lineno'): lineno = node.lineno result = (kind, filename, lineno, msg) if result not in ERRORS: ERRORS.add(result) if not warning: print format_error(result) sys.exit(1) def format_error(error): (kind, filename, lineno, msg) = error result = kind if filename: result += ' %s:' % filename if lineno is not None: result += '%d:' % lineno return result + ' ' + msg def print_errors(): for error in sorted(ERRORS): print format_error(error) shedskin-0.9.4/shedskin/cpp.py0000664000175000017500000033647712157270661016251 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) cpp.py: output C++ code output equivalent C++ code, using templates and virtuals to support data and OO polymorphism. class GenerateVisitor: inherits visitor pattern from compiler.visitor.ASTVisitor, to recursively generate C++ code for each syntactical Python construct. the constraint graph, with inferred types, is first 'merged' back to program dimensions (gx.merged_inh). ''' import string import struct import textwrap from compiler import walk from compiler.ast import Const, AssTuple, AssList, From, Add, Stmt, AssAttr, \ Keyword, AssName, CallFunc, Slice, Getattr, Dict, Subscript, \ Function as FunctionNode, Return, Class as ClassNode, Name, List, Discard, Sliceobj, Tuple from compiler.visitor import ASTVisitor from error import error from extmod import convert_methods, convert_methods2, do_extmod, pyinit_func from infer import analyze_callfunc, callfunc_targets, connect_actual_formal, \ called, inode, var_types from makefile import generate_makefile from python import assign_rec, aug_msg, Class, def_class, \ is_enum, is_fastfor, is_literal, is_zip2, \ lookup_class, lookup_class_module, lookup_var, lookup_module, \ Function, Module, Variable, StaticClass from typestr import incompatible_assignment_rec, lowest_common_parents, \ nodetypestr, polymorphic_t, singletype, unboxable, typestr from virtual import virtuals class CPPNamer(object): def __init__(self, gx, mv): self.gx = gx self.class_names = [cl.ident for cl in self.gx.allclasses] self.cpp_keywords = self.gx.cpp_keywords self.ss_prefix = self.gx.ss_prefix self.name_by_type = { str: self.name_str, Class: self.name_class, Function: self.name_function, Variable: self.name_variable, } self.mv = mv def nokeywords(self, name): if name in self.cpp_keywords: return self.ss_prefix + name return name def namespace_class(self, cl, add_cl=''): module = cl.mv.module if module.ident != 'builtin' and module != self.mv.module and module.name_list: return module.full_path() + '::' + add_cl + self.name(cl) else: return add_cl + self.name(cl) def name(self, obj): get_name = self.name_by_type[type(obj)] name = get_name(obj) return self.nokeywords(name) def name_variable(self, var): if var.masks_global(): return '_' + var.name return self.name_str(var.name) def name_function(self, func): return self.name_str(func.ident) def name_class(self, obj): return obj.ident def name_str(self, name): if [x for x in ('init', 'add') if name == x + self.gx.main_module.ident] or \ name in self.class_names or name + '_' in self.class_names: name = '_' + name return name # --- code generation visitor; use type information class GenerateVisitor(ASTVisitor): def __init__(self, gx, module): self.gx = gx self.output_base = module.filename[:-3] self.out = file(self.output_base + '.cpp', 'w') self.indentation = '' self.consts = {} self.mergeinh = self.gx.merged_inh self.module = module self.mv = module.mv self.name = module.ident self.filling_consts = False self.with_count = 0 self.bool_wrapper = {} self.namer = CPPNamer(self.gx, self) def cpp_name(self, obj): return self.namer.name(obj) def insert_consts(self, declare): # XXX ugly if not self.consts: return self.filling_consts = True if declare: suffix = '.hpp' else: suffix = '.cpp' lines = file(self.output_base + suffix, 'r').readlines() newlines = [] j = -1 for (i, line) in enumerate(lines): if line.startswith('namespace ') and not 'XXX' in line: # XXX j = i + 1 newlines.append(line) if i == j: pairs = [] done = set() for (node, name) in self.consts.items(): if not name in done and node in self.mergeinh and self.mergeinh[node]: # XXX ts = nodetypestr(self.gx, node, inode(self.gx, node).parent, mv=self.mv) if declare: ts = 'extern ' + ts pairs.append((ts, name)) done.add(name) newlines.extend(self.group_declarations(pairs)) newlines.append('\n') newlines2 = [] j = -1 for (i, line) in enumerate(newlines): if line.startswith('void __init() {'): j = i newlines2.append(line) if i == j: todo = {} for (node, name) in self.consts.items(): if not name in todo: todo[int(name[6:])] = node todolist = todo.keys() todolist.sort() for number in todolist: if self.mergeinh[todo[number]]: # XXX name = 'const_' + str(number) self.start(' ' + name + ' = ') if isinstance(todo[number], Const) and isinstance(todo[number].value, str) and len(todo[number].value) == 1: self.append("__char_cache[%d];" % ord(todo[number].value)) else: self.visit(todo[number], inode(self.gx, todo[number]).parent) newlines2.append(self.line + ';\n') newlines2.append('\n') file(self.output_base + suffix, 'w').writelines(newlines2) self.filling_consts = False def insert_extras(self, suffix): lines = file(self.output_base + suffix, 'r').readlines() newlines = [] for line in lines: newlines.append(line) if suffix == '.cpp' and line.startswith('#include'): newlines.extend(self.include_files()) elif suffix == '.hpp' and line.startswith('using namespace'): newlines.extend(self.fwd_class_refs()) file(self.output_base + suffix, 'w').writelines(newlines) def fwd_class_refs(self): lines = [] for _module in self.module.prop_includes: if _module.builtin: continue for name in _module.name_list: lines.append('namespace __%s__ { /* XXX */\n' % name) for cl in _module.mv.classes.values(): lines.append('class %s;\n' % self.cpp_name(cl)) for name in _module.name_list: lines.append('}\n') if lines: lines.insert(0, '\n') return lines def include_files(self): # find all (indirect) dependencies includes = set() includes.add(self.module) changed = True while changed: size = len(includes) for module in list(includes): includes.update(module.prop_includes) includes.update(module.mv.imports.values()) includes.update(module.mv.fake_imports.values()) changed = (size != len(includes)) includes = set(i for i in includes if i.ident != 'builtin') # order by cross-file inheritance dependencies for include in includes: include.deps = set() for include in includes: for cl in include.mv.classes.values(): if cl.bases: module = cl.bases[0].mv.module if module.ident != 'builtin' and module != include: include.deps.add(module) includes1 = [i for i in includes if i.builtin] includes2 = [i for i in includes if not i.builtin] includes = includes1 + self.includes_rec(set(includes2)) return ['#include "%s"\n' % module.include_path() for module in includes] def includes_rec(self, includes): # XXX should be recursive!? ugh todo = includes.copy() result = [] while todo: for include in todo: if not include.deps - set(result): todo.remove(include) result.append(include) break else: # XXX circular dependency warning? result.append(todo.pop()) return result # --- group pairs of (type, name) declarations, while paying attention to '*' def group_declarations(self, pairs): group = {} for (type, name) in pairs: group.setdefault(type, []).append(name) result = [] for (type, names) in group.items(): names.sort() if type.endswith('*'): result.append(type + (', *'.join(names)) + ';\n') else: result.append(type + (', '.join(names)) + ';\n') return result def header_file(self): self.out = file(self.output_base + '.hpp', 'w') self.visit(self.module.ast, True) self.out.close() def output(self, text): print >>self.out, self.indentation + text def start(self, text=None): self.line = self.indentation if text: self.line += text def append(self, text): self.line += text def eol(self, text=None): if text: self.append(text) if self.line.strip(): print >>self.out, self.line + ';' def indent(self): self.indentation += 4 * ' ' def deindent(self): self.indentation = self.indentation[:-4] def visitm(self, *args): func = None if args and isinstance(args[-1], (Function, Class)): func = args[-1] for arg in args[:-1]: if isinstance(arg, str): self.append(arg) else: self.visit(arg, func) def connector(self, node, func): if singletype(self.gx, node, Module): return '::' elif unboxable(self.gx, self.mergeinh[node]): return '.' else: return '->' def declare_defs(self, vars, declare): pairs = [] for (name, var) in vars: if singletype(self.gx, var, Module) or var.invisible: continue ts = nodetypestr(self.gx, var, var.parent, mv=self.mv) if declare: if 'for_in_loop' in ts: # XXX continue ts = 'extern ' + ts if not var.name in ['__exception', '__exception2']: # XXX pairs.append((ts, self.cpp_name(var))) return ''.join(self.group_declarations(pairs)) def get_constant(self, node): parent = inode(self.gx, node).parent while isinstance(parent, Function) and parent.listcomp: # XXX parent = parent.parent if isinstance(parent, Function) and (parent.inherited or not self.inhcpa(parent)): # XXX return for other in self.consts: # XXX use mapping if node.value == other.value: return self.consts[other] self.consts[node] = 'const_' + str(len(self.consts)) return self.consts[node] def module_hpp(self, node): define = '_'.join(self.module.name_list).upper() + '_HPP' print >>self.out, '#ifndef __' + define print >>self.out, '#define __' + define + '\n' # --- namespaces print >>self.out, 'using namespace __shedskin__;' for n in self.module.name_list: print >>self.out, 'namespace __' + n + '__ {' print >>self.out # class declarations for child in node.node.getChildNodes(): if isinstance(child, ClassNode): cl = def_class(self.gx, child.name, mv=self.mv) print >>self.out, 'class ' + self.cpp_name(cl) + ';' print >>self.out # --- lambda typedefs self.func_pointers() # globals defs = self.declare_defs(list(self.mv.globals.items()), declare=True) if defs: self.output(defs) print >>self.out # --- class definitions for child in node.node.getChildNodes(): if isinstance(child, ClassNode): self.class_hpp(child) # --- defaults self.defaults(declare=True) # function declarations if self.module != self.gx.main_module: print >>self.out, 'void __init();' for child in node.node.getChildNodes(): if isinstance(child, FunctionNode): func = self.mv.funcs[child.name] if self.inhcpa(func): self.visitFunction(func.node, declare=True) print >>self.out if self.gx.extension_module: print >>self.out, 'extern "C" {' pyinit_func(self) print >>self.out, '}' for n in self.module.name_list: print >>self.out, '} // module namespace' self.rich_comparison() if self.gx.extension_module: convert_methods2(self.gx, self) print >>self.out, '#endif' def defaults(self, declare): if self.module.mv.defaults: extern = ['', 'extern '][declare] for default, (nr, func, func_def_nr) in self.module.mv.defaults.items(): formal = func.formals[len(func.formals) - len(func.defaults) + func_def_nr] var = func.vars[formal] print >>self.out, extern + typestr(self.gx, self.mergeinh[var], func, mv=self.mv) + ' ' + ('default_%d;' % nr) print >>self.out def init_defaults(self, func): for default in func.defaults: if default in self.mv.defaults: nr, func, func_def_nr = self.mv.defaults[default] formal = func.formals[len(func.formals) - len(func.defaults) + func_def_nr] var = func.vars[formal] if self.mergeinh[var]: ts = [t for t in self.mergeinh[default] if isinstance(t[0], Function)] if not ts or [t for t in ts if called(t[0])]: self.start('default_%d = ' % nr) self.visit_conv(default, self.mergeinh[var], None) self.eol() def rich_comparison(self): cmp_cls, lt_cls, gt_cls, le_cls, ge_cls = [], [], [], [], [] for cl in self.mv.classes.values(): if not '__cmp__' in cl.funcs and [f for f in ('__eq__', '__lt__', '__gt__') if f in cl.funcs]: cmp_cls.append(cl) if not '__lt__' in cl.funcs and '__gt__' in cl.funcs: lt_cls.append(cl) if not '__gt__' in cl.funcs and '__lt__' in cl.funcs: gt_cls.append(cl) if not '__le__' in cl.funcs and '__ge__' in cl.funcs: le_cls.append(cl) if not '__ge__' in cl.funcs and '__le__' in cl.funcs: ge_cls.append(cl) if cmp_cls or lt_cls or gt_cls or le_cls or ge_cls: print >>self.out, 'namespace __shedskin__ { /* XXX */' for cl in cmp_cls: t = '__%s__::%s *' % (self.mv.module.ident, self.cpp_name(cl)) print >>self.out, 'template<> inline __ss_int __cmp(%sa, %sb) {' % (t, t) print >>self.out, ' if (!a) return -1;' if '__eq__' in cl.funcs: print >>self.out, ' if(a->__eq__(b)) return 0;' if '__lt__' in cl.funcs: print >>self.out, ' return (a->__lt__(b))?-1:1;' elif '__gt__' in cl.funcs: print >>self.out, ' return (a->__gt__(b))?1:-1;' else: print >>self.out, ' return __cmp(a, b);' print >>self.out, '}' self.rich_compare(lt_cls, 'lt', 'gt') self.rich_compare(gt_cls, 'gt', 'lt') self.rich_compare(le_cls, 'le', 'ge') self.rich_compare(ge_cls, 'ge', 'le') print >>self.out, '}' def rich_compare(self, cls, msg, fallback_msg): for cl in cls: t = '__%s__::%s *' % (self.mv.module.ident, self.cpp_name(cl)) print >>self.out, 'template<> inline __ss_bool __%s(%sa, %sb) {' % (msg, t, t) # print >>self.out, ' if (!a) return -1;' # XXX check print >>self.out, ' return b->__%s__(a);' % fallback_msg print >>self.out, '}' def module_cpp(self, node): print >>self.out, '#include "builtin.hpp"\n' # --- comments if node.doc: self.do_comment(node.doc) print >>self.out # --- namespace fun for n in self.module.name_list: print >>self.out, 'namespace __' + n + '__ {' print >>self.out for child in node.node.getChildNodes(): if isinstance(child, From) and child.modname != '__future__': module = self.gx.from_module[child] using = 'using ' + module.full_path() + '::' for (name, pseudonym) in child.names: pseudonym = pseudonym or name if name == '*': for func in module.mv.funcs.values(): if func.cp: # XXX print >>self.out, using + self.cpp_name(func) + ';' for cl in module.mv.classes.values(): print >>self.out, using + self.cpp_name(cl) + ';' elif pseudonym not in self.module.mv.globals: if name in module.mv.funcs: func = module.mv.funcs[name] if func.cp: print >>self.out, using + self.cpp_name(func) + ';' else: print >>self.out, using + self.namer.nokeywords(name) + ';' print >>self.out # --- globals defs = self.declare_defs(list(self.mv.globals.items()), declare=False) if defs: self.output(defs) print >>self.out # --- defaults self.defaults(declare=False) # --- declarations self.listcomps = {} for (listcomp, lcfunc, func) in self.mv.listcomps: self.listcomps[listcomp] = (lcfunc, func) self.do_listcomps(True) self.do_lambdas(True) print >>self.out # --- definitions self.do_listcomps(False) self.do_lambdas(False) for child in node.node.getChildNodes(): if isinstance(child, ClassNode): self.class_cpp(child) elif isinstance(child, FunctionNode): self.do_comments(child) self.visit(child) # --- __init self.output('void __init() {') self.indent() if self.module == self.gx.main_module and not self.gx.extension_module: self.output('__name__ = new str("__main__");\n') else: self.output('__name__ = new str("%s");\n' % self.module.ident) for child in node.node.getChildNodes(): if isinstance(child, FunctionNode): self.init_defaults(child) elif isinstance(child, ClassNode): for child2 in child.code.getChildNodes(): if isinstance(child2, FunctionNode): self.init_defaults(child2) if child.name in self.mv.classes: cl = self.mv.classes[child.name] self.output('cl_' + cl.ident + ' = new class_("%s");' % (cl.ident)) if cl.parent.static_nodes: self.output('%s::__static__();' % self.cpp_name(cl)) elif isinstance(child, Discard): self.visit_discard(child) elif isinstance(child, From) and child.modname != '__future__': module = self.gx.from_module[child] for (name, pseudonym) in child.names: pseudonym = pseudonym or name if name == '*': for var in module.mv.globals.values(): if not var.invisible and not var.imported and not var.name.startswith('__') and var_types(self.gx, var): self.start(self.namer.nokeywords(var.name) + ' = ' + module.full_path() + '::' + self.namer.nokeywords(var.name)) self.eol() elif pseudonym in self.module.mv.globals and not [t for t in var_types(self.gx, self.module.mv.globals[pseudonym]) if isinstance(t[0], Module)]: self.start(self.namer.nokeywords(pseudonym) + ' = ' + module.full_path() + '::' + self.namer.nokeywords(name)) self.eol() elif not isinstance(child, (ClassNode, FunctionNode)): self.do_comments(child) self.visit(child) self.deindent() self.output('}\n') # --- close namespace for n in self.module.name_list: print >>self.out, '} // module namespace' print >>self.out # --- c++ main/extension module setup if self.gx.extension_module: do_extmod(self.gx, self) if self.module == self.gx.main_module: self.do_main() def visit_discard(self, node, func=None): if isinstance(node.expr, Const) and node.expr.value is None: # XXX merge with visitStmt pass elif isinstance(node.expr, Const) and type(node.expr.value) == str: self.do_comment(node.expr.value) else: self.start('') self.visit(node, func) self.eol() def visitModule(self, node, declare=False): if declare: self.module_hpp(node) else: self.module_cpp(node) def do_main(self): modules = self.gx.modules.values() if any(module.builtin and module.ident == 'sys' for module in modules): print >>self.out, 'int main(int __ss_argc, char **__ss_argv) {' else: print >>self.out, 'int main(int, char **) {' self.do_init_modules() print >>self.out, ' __shedskin__::__start(__%s__::__init);' % self.module.ident print >>self.out, '}' def do_init_modules(self): print >>self.out, ' __shedskin__::__init();' for module in sorted(self.gx.modules.values(), key=lambda x: x.import_order): if module != self.gx.main_module and module.ident != 'builtin': if module.ident == 'sys': if self.gx.extension_module: print >>self.out, ' __sys__::__init(0, 0);' else: print >>self.out, ' __sys__::__init(__ss_argc, __ss_argv);' else: print >>self.out, ' ' + module.full_path() + '::__init();' def do_comment(self, s): if not s: return doc = s.replace('/*', '//').replace('*/', '//').split('\n') self.output('/**') if doc[0].strip(): self.output(doc[0]) rest = textwrap.dedent('\n'.join(doc[1:])).splitlines() for l in rest: self.output(l) self.output('*/') def do_comments(self, child): if child in self.gx.comments: for n in self.gx.comments[child]: self.do_comment(n) def visitContinue(self, node, func=None): self.output('continue;') def visitWith(self, node, func=None): self.start() if node.vars: self.visitm('WITH_VAR(', node.expr, ',', node.vars, func) else: self.visitm('WITH(', node.expr, func) self.append(',%d)' % self.with_count) self.with_count += 1 print >>self.out, self.line self.indent() self.visit(node.body, func) self.deindent() self.output('END_WITH') def visitWhile(self, node, func=None): print >>self.out if node.else_: self.output('%s = 0;' % self.mv.tempcount[node.else_]) self.start('while (') self.bool_test(node.test, func) self.append(') {') print >>self.out, self.line self.indent() self.gx.loopstack.append(node) self.visit(node.body, func) self.gx.loopstack.pop() self.deindent() self.output('}') if node.else_: self.output('if (!%s) {' % self.mv.tempcount[node.else_]) self.indent() self.visit(node.else_, func) self.deindent() self.output('}') def copy_method(self, cl, name, declare): class_name = self.cpp_name(cl) header = class_name + ' *' if not declare: header += class_name + '::' header += name + '(' self.start(header) if name == '__deepcopy__': self.append('dict *memo') self.append(')') if not declare: print >>self.out, self.line + ' {' self.indent() self.output(class_name + ' *c = new ' + class_name + '();') if name == '__deepcopy__': self.output('memo->__setitem__(this, c);') for var in cl.vars.values(): if not var.invisible and var in self.gx.merged_inh and self.gx.merged_inh[var]: varname = self.cpp_name(var) if name == '__deepcopy__': self.output('c->%s = __deepcopy(%s);' % (varname, varname)) else: self.output('c->%s = %s;' % (varname, varname)) self.output('return c;') self.deindent() self.output('}\n') else: self.eol() def copy_methods(self, cl, declare): if cl.has_copy: self.copy_method(cl, '__copy__', declare) if cl.has_deepcopy: self.copy_method(cl, '__deepcopy__', declare) def class_hpp(self, node): cl = self.mv.classes[node.name] self.output('extern class_ *cl_' + cl.ident + ';') # --- header clnames = [self.namer.namespace_class(b) for b in cl.bases] if not clnames: clnames = ['pyobj'] if '__iter__' in cl.funcs: # XXX get return type of 'next' ts = nodetypestr(self.gx, cl.funcs['__iter__'].retnode.thing, mv=self.mv) if ts.startswith('__iter<'): ts = ts[ts.find('<') + 1:ts.find('>')] clnames = ['pyiter<%s>' % ts] # XXX use iterable interface if '__call__' in cl.funcs: callfunc = cl.funcs['__call__'] r_typestr = nodetypestr(self.gx, callfunc.retnode.thing, mv=self.mv).strip() nargs = len(callfunc.formals) - 1 argtypes = [nodetypestr(self.gx, callfunc.vars[callfunc.formals[i + 1]], mv=self.mv).strip() for i in range(nargs)] clnames = ['pycall%d<%s,%s>' % (nargs, r_typestr, ','.join(argtypes))] self.output('class ' + self.cpp_name(cl) + ' : ' + ', '.join(['public ' + clname for clname in clnames]) + ' {') self.do_comment(node.doc) self.output('public:') self.indent() self.class_variables(cl) # --- constructor need_init = False if '__init__' in cl.funcs: initfunc = cl.funcs['__init__'] if self.inhcpa(initfunc): need_init = True # --- default constructor if need_init: self.output(self.cpp_name(cl) + '() {}') else: self.output(self.cpp_name(cl) + '() { this->__class__ = cl_' + cl.ident + '; }') # --- init constructor if need_init: self.func_header(initfunc, declare=True, is_init=True) self.indent() self.output('this->__class__ = cl_' + cl.ident + ';') self.output('__init__(' + ', '.join(self.cpp_name(initfunc.vars[f]) for f in initfunc.formals[1:]) + ');') self.deindent() self.output('}') # --- destructor call if '__del__' in cl.funcs and self.inhcpa(cl.funcs['__del__']): self.output('~%s() { this->__del__(); }' % self.cpp_name(cl)) # --- static code if cl.parent.static_nodes: self.output('static void __static__();') # --- methods virtuals(self, cl, True) for func in cl.funcs.values(): if func.node and not (func.ident == '__init__' and func.inherited): self.visitFunction(func.node, cl, True) self.copy_methods(cl, True) if self.gx.extension_module: convert_methods(self.gx, self, cl, True) self.deindent() self.output('};\n') def class_cpp(self, node): cl = self.mv.classes[node.name] if node in self.gx.comments: self.do_comments(node) else: self.output('/**\nclass %s\n*/\n' % cl.ident) self.output('class_ *cl_' + cl.ident + ';\n') # --- methods virtuals(self, cl, False) for func in cl.funcs.values(): if func.node and not (func.ident == '__init__' and func.inherited): self.visitFunction(func.node, cl, False) self.copy_methods(cl, False) # --- class variable declarations if cl.parent.vars: # XXX merge with visitModule for var in cl.parent.vars.values(): if var in self.gx.merged_inh and self.gx.merged_inh[var]: self.start(nodetypestr(self.gx, var, cl.parent, mv=self.mv) + cl.ident + '::' + self.cpp_name(var)) self.eol() print >>self.out # --- static init if cl.parent.static_nodes: self.output('void %s::__static__() {' % self.cpp_name(cl)) self.indent() for node in cl.parent.static_nodes: self.visit(node, cl.parent) self.deindent() self.output('}') print >>self.out def class_variables(self, cl): # --- class variables if cl.parent.vars: for var in cl.parent.vars.values(): if var in self.gx.merged_inh and self.gx.merged_inh[var]: self.output('static ' + nodetypestr(self.gx, var, cl.parent, mv=self.mv) + self.cpp_name(var) + ';') print >>self.out # --- instance variables for var in cl.vars.values(): if var.invisible: continue # var.name in cl.virtualvars: continue # var is masked by ancestor var vars = set() for ancestor in cl.ancestors(): vars.update(ancestor.vars) if var.name in vars: continue if var in self.gx.merged_inh and self.gx.merged_inh[var]: self.output(nodetypestr(self.gx, var, cl, mv=self.mv) + self.cpp_name(var) + ';') if [v for v in cl.vars if not v.startswith('__')]: print >>self.out def nothing(self, types): if def_class(self.gx, 'complex') in (t[0] for t in types): return 'mcomplex(0.0, 0.0)' elif def_class(self.gx, 'bool_') in (t[0] for t in types): return 'False' else: return '0' def inhcpa(self, func): return called(func) or (func in self.gx.inheritance_relations and [1 for f in self.gx.inheritance_relations[func] if called(f)]) def visitSlice(self, node, func=None): if node.flags == 'OP_DELETE': self.start() self.visit(inode(self.gx, node.expr).fakefunc, func) self.eol() else: self.visit(inode(self.gx, node.expr).fakefunc, func) def visitLambda(self, node, parent=None): self.append(self.mv.lambdaname[node]) def subtypes(self, types, varname): subtypes = set() for t in types: if isinstance(t[0], Class): var = t[0].vars.get(varname) if var and (var, t[1], 0) in self.gx.cnode: # XXX yeah? subtypes.update(self.gx.cnode[var, t[1], 0].types()) return subtypes def bin_tuple(self, types): for t in types: if isinstance(t[0], Class) and t[0].ident == 'tuple2': var1 = t[0].vars.get('first') var2 = t[0].vars.get('second') if var1 and var2: if (var1, t[1], 0) in self.gx.cnode and (var2, t[1], 0) in self.gx.cnode: if self.gx.cnode[var1, t[1], 0].types() != self.gx.cnode[var2, t[1], 0].types(): return True return False def instance_new(self, node, argtypes): if argtypes is None: argtypes = self.gx.merged_inh[node] ts = typestr(self.gx, argtypes, mv=self.mv) if ts.startswith('pyseq') or ts.startswith('pyiter'): # XXX argtypes = self.gx.merged_inh[node] ts = typestr(self.gx, argtypes, mv=self.mv) self.append('(new ' + ts[:-2] + '(') return argtypes def visitDict(self, node, func=None, argtypes=None): argtypes = self.instance_new(node, argtypes) if node.items: self.append(str(len(node.items)) + ', ') ts_key = typestr(self.gx, self.subtypes(argtypes, 'unit'), mv=self.mv) ts_value = typestr(self.gx, self.subtypes(argtypes, 'value'), mv=self.mv) for (key, value) in node.items: self.visitm('(new tuple2<%s, %s>(2,' % (ts_key, ts_value), func) type_child = self.subtypes(argtypes, 'unit') self.visit_conv(key, type_child, func) self.append(',') type_child = self.subtypes(argtypes, 'value') self.visit_conv(value, type_child, func) self.append('))') if (key, value) != node.items[-1]: self.append(',') self.append('))') def visit_tuple_list(self, node, func=None, argtypes=None): if isinstance(func, Class): # XXX func = None argtypes = self.instance_new(node, argtypes) children = node.getChildNodes() if children: self.append(str(len(children)) + ',') if len(children) >= 2 and self.bin_tuple(argtypes): # XXX >=2? type_child = self.subtypes(argtypes, 'first') self.visit_conv(children[0], type_child, func) self.append(',') type_child = self.subtypes(argtypes, 'second') self.visit_conv(children[1], type_child, func) else: for child in children: type_child = self.subtypes(argtypes, 'unit') self.visit_conv(child, type_child, func) if child != children[-1]: self.append(',') self.append('))') def visitTuple(self, node, func=None, argtypes=None): if len(node.nodes) > 2: types = set() for child in node.nodes: types.update(self.mergeinh[child]) typestr(self.gx, types, node=child, tuple_check=True, mv=self.mv) self.visit_tuple_list(node, func, argtypes) def visitList(self, node, func=None, argtypes=None): self.visit_tuple_list(node, func, argtypes) def visitAssert(self, node, func=None): self.start('ASSERT(') self.visitm(node.test, ', ', func) if len(node.getChildNodes()) > 1: self.visit(node.getChildNodes()[1], func) else: self.append('0') self.eol(')') def visitRaise(self, node, func=None): cl = None # XXX sep func t = [t[0] for t in self.mergeinh[node.expr1]] if len(t) == 1: cl = t[0] self.start('throw (') # --- raise class [, constructor args] if isinstance(node.expr1, Name) and not lookup_var(node.expr1.name, func, mv=self.mv): # XXX lookup_class self.append('new %s(' % node.expr1.name) if node.expr2: if isinstance(node.expr2, Tuple) and node.expr2.nodes: for n in node.expr2.nodes: self.visit(n, func) if n != node.expr2.nodes[-1]: self.append(', ') # XXX visitcomma(nodes) else: self.visit(node.expr2, func) self.append(')') # --- raise instance elif isinstance(cl, Class) and cl.mv.module.ident == 'builtin' and not [a for a in cl.ancestors_upto(None) if a.ident == 'BaseException']: self.append('new Exception()') else: self.visit(node.expr1, func) self.eol(')') def visitTryExcept(self, node, func=None): # try self.start('try {') print >>self.out, self.line self.indent() if node.else_: self.output('%s = 0;' % self.mv.tempcount[node.else_]) self.visit(node.body, func) if node.else_: self.output('%s = 1;' % self.mv.tempcount[node.else_]) self.deindent() self.start('}') # except for handler in node.handlers: if isinstance(handler[0], Tuple): pairs = [(n, handler[1], handler[2]) for n in handler[0].nodes] else: pairs = [(handler[0], handler[1], handler[2])] for (h0, h1, h2) in pairs: if isinstance(h0, Name) and h0.name in ['int', 'float', 'str', 'class']: continue # XXX lookup_class elif h0: cl = lookup_class(h0, self.mv) if cl.mv.module.builtin and cl.ident in ['KeyboardInterrupt', 'FloatingPointError', 'OverflowError', 'ZeroDivisionError', 'SystemExit']: error("system '%s' is not caught" % cl.ident, self.gx, h0, warning=True, mv=self.mv) arg = self.namer.namespace_class(cl) + ' *' else: arg = 'Exception *' if h1: arg += h1.name self.append(' catch (%s) {' % arg) print >>self.out, self.line self.indent() self.visit(h2, func) self.deindent() self.start('}') print >>self.out, self.line # else if node.else_: self.output('if(%s) { // else' % self.mv.tempcount[node.else_]) self.indent() self.visit(node.else_, func) self.deindent() self.output('}') def do_fastfor(self, node, qual, quals, iter, func, genexpr): if len(qual.list.args) == 3 and not is_literal(qual.list.args[2]): for arg in qual.list.args: # XXX simplify if arg in self.mv.tempcount: self.start() self.visitm(self.mv.tempcount[arg], ' = ', arg, func) self.eol() self.fastfor(qual, iter, func) self.forbody(node, quals, iter, func, False, genexpr) def visit_temp(self, node, func): # XXX generalize? if node in self.mv.tempcount: self.append(self.mv.tempcount[node]) else: self.visit(node, func) def fastfor(self, node, assname, func=None): # --- for i in range(..) -> for( i=l, u=expr; i < u; i++ ) .. ivar, evar = self.mv.tempcount[node.assign], self.mv.tempcount[node.list] self.start('FAST_FOR(%s,' % assname) if len(node.list.args) == 1: self.append('0,') self.visit_temp(node.list.args[0], func) self.append(',') else: self.visit_temp(node.list.args[0], func) self.append(',') self.visit_temp(node.list.args[1], func) self.append(',') if len(node.list.args) != 3: self.append('1') else: self.visit_temp(node.list.args[2], func) self.append(',%s,%s)' % (ivar[2:], evar[2:])) print >>self.out, self.line def fastenum(self, node): return is_enum(node) and self.only_classes(node.list.args[0], ('tuple', 'list')) def fastzip2(self, node): names = ('tuple', 'list') return is_zip2(node) and self.only_classes(node.list.args[0], names) and self.only_classes(node.list.args[1], names) def fastdictiter(self, node): return isinstance(node.list, CallFunc) and isinstance(node.assign, (AssList, AssTuple)) and self.only_classes(node.list.node, ('dict',)) and isinstance(node.list.node, Getattr) and node.list.node.attrname == 'iteritems' def only_classes(self, node, names): if node not in self.mergeinh: return False classes = [def_class(self.gx, name, mv=self.mv) for name in names] + [def_class(self.gx, 'none')] return not [t for t in self.mergeinh[node] if t[0] not in classes] def visitFor(self, node, func=None): if isinstance(node.assign, AssName): assname = node.assign.name elif isinstance(node.assign, AssAttr): self.start('') self.visitAssAttr(node.assign, func) assname = self.line.strip() # XXX yuck else: assname = self.mv.tempcount[node.assign] assname = self.cpp_name(assname) print >>self.out if node.else_: self.output('%s = 0;' % self.mv.tempcount[node.else_]) if is_fastfor(node): self.do_fastfor(node, node, None, assname, func, False) elif self.fastenum(node): self.do_fastenum(node, func, False) self.forbody(node, None, assname, func, True, False) elif self.fastzip2(node): self.do_fastzip2(node, func, False) self.forbody(node, None, assname, func, True, False) elif self.fastdictiter(node): self.do_fastdictiter(node, func, False) self.forbody(node, None, assname, func, True, False) else: pref, tail = self.forin_preftail(node) self.start('FOR_IN%s(%s,' % (pref, assname)) self.visit(node.list, func) print >>self.out, self.line + ',' + tail + ')' self.forbody(node, None, assname, func, False, False) print >>self.out def do_fastzip2(self, node, func, genexpr): self.start('FOR_IN_ZIP(') left, right = node.assign.nodes self.do_fastzip2_one(left, func) self.do_fastzip2_one(right, func) self.visitm(node.list.args[0], ',', node.list.args[1], ',', func) tail1 = self.mv.tempcount[(node, 2)][2:] + ',' + self.mv.tempcount[(node, 3)][2:] + ',' tail2 = self.mv.tempcount[(node.list)][2:] + ',' + self.mv.tempcount[(node, 4)][2:] print >>self.out, self.line + tail1 + tail2 + ')' self.indent() if isinstance(left, (AssTuple, AssList)): self.tuple_assign(left, self.mv.tempcount[left], func) if isinstance(right, (AssTuple, AssList)): self.tuple_assign(right, self.mv.tempcount[right], func) def do_fastzip2_one(self, node, func): if isinstance(node, (AssTuple, AssList)): self.append(self.mv.tempcount[node]) else: self.visit(node, func) self.append(',') def do_fastenum(self, node, func, genexpr): self.start('FOR_IN_ENUM(') left, right = node.assign.nodes self.do_fastzip2_one(right, func) self.visit(node.list.args[0], func) tail = self.mv.tempcount[(node, 2)][2:] + ',' + self.mv.tempcount[node.list][2:] print >>self.out, self.line + ',' + tail + ')' self.indent() self.start() self.visitm(left, ' = ' + self.mv.tempcount[node.list], func) self.eol() if isinstance(right, (AssTuple, AssList)): self.tuple_assign(right, self.mv.tempcount[right], func) def do_fastdictiter(self, node, func, genexpr): self.start('FOR_IN_DICT(') left, right = node.assign.nodes tail = self.mv.tempcount[node, 7][2:] + ',' + self.mv.tempcount[node, 6][2:] + ',' + self.mv.tempcount[node.list][2:] self.visit(node.list.node.expr, func) print >>self.out, self.line + ',' + tail + ')' self.indent() self.start() if left in self.mv.tempcount: # XXX not for zip, enum..? self.visitm('%s = %s->key' % (self.mv.tempcount[left], self.mv.tempcount[node, 6]), func) else: self.visitm(left, ' = %s->key' % self.mv.tempcount[node, 6], func) self.eol() self.start() if right in self.mv.tempcount: self.visitm('%s = %s->value' % (self.mv.tempcount[right], self.mv.tempcount[node, 6]), func) else: self.visitm(right, ' = %s->value' % self.mv.tempcount[node, 6], func) self.eol() if isinstance(left, (AssTuple, AssList)): self.tuple_assign(left, self.mv.tempcount[left], func) if isinstance(right, (AssTuple, AssList)): self.tuple_assign(right, self.mv.tempcount[right], func) def forin_preftail(self, node): tail = self.mv.tempcount[node][2:] + ',' + self.mv.tempcount[node.list][2:] tail += ',' + self.mv.tempcount[(node, 5)][2:] return '', tail def forbody(self, node, quals, iter, func, skip, genexpr): if quals is not None: self.listcompfor_body(node, quals, iter, func, False, genexpr) return if not skip: self.indent() if isinstance(node.assign, (AssTuple, AssList)): self.tuple_assign(node.assign, self.mv.tempcount[node.assign], func) self.gx.loopstack.append(node) self.visit(node.body, func) self.gx.loopstack.pop() self.deindent() self.output('END_FOR') if node.else_: self.output('if (!%s) {' % self.mv.tempcount[node.else_]) self.indent() self.visit(node.else_, func) self.deindent() self.output('}') def func_pointers(self): for func in self.mv.lambdas.values(): argtypes = [nodetypestr(self.gx, func.vars[formal], func, mv=self.mv).rstrip() for formal in func.formals] if func.largs is not None: argtypes = argtypes[:func.largs] rettype = nodetypestr(self.gx, func.retnode.thing, func, mv=self.mv) print >>self.out, 'typedef %s(*lambda%d)(' % (rettype, func.lambdanr) + ', '.join(argtypes) + ');' print >>self.out # --- function/method header def func_header(self, func, declare, is_init=False): method = isinstance(func.parent, Class) if method: formals = [f for f in func.formals if f != 'self'] else: formals = [f for f in func.formals] if func.largs is not None: formals = formals[:func.largs] if is_init: ident = self.cpp_name(func.parent) else: ident = self.cpp_name(func) self.start() # --- return expression header = '' if is_init: pass elif func.ident in ['__hash__']: header += 'long ' # XXX __ss_int leads to problem with virtual parent elif func.returnexpr: header += nodetypestr(self.gx, func.retnode.thing, func, mv=self.mv) # XXX mult else: header += 'void ' ftypes = [nodetypestr(self.gx, func.vars[f], func, mv=self.mv) for f in formals] # if arguments type too precise (e.g. virtually called) cast them back oldftypes = ftypes if func.ftypes: ftypes = func.ftypes[1:] # --- method header if method and not declare: header += self.cpp_name(func.parent) + '::' header += ident # --- cast arguments if necessary (explained above) casts = [] casters = set() if func.ftypes: for i in range(min(len(oldftypes), len(ftypes))): # XXX this is 'cast on specialize'.. how about generalization? if oldftypes[i] != ftypes[i]: casts.append(oldftypes[i] + formals[i] + ' = (' + oldftypes[i] + ')__' + formals[i] + ';') if not declare: casters.add(i) formals2 = formals[:] for (i, f) in enumerate(formals2): # XXX formals2[i] = self.cpp_name(func.vars[f]) if i in casters: formals2[i] = '__' + formals2[i] formaldecs = [o + f for (o, f) in zip(ftypes, formals2)] if declare and isinstance(func.parent, Class) and func.ident in func.parent.staticmethods: header = 'static ' + header if is_init and not formaldecs: formaldecs = ['int __ss_init'] if func.ident.startswith('__lambda'): # XXX header = 'static inline ' + header # --- output self.append(header + '(' + ', '.join(formaldecs) + ')') if is_init: print >>self.out, self.line + ' {' elif declare: self.eol() else: print >>self.out, self.line + ' {' self.indent() if not declare and func.doc: self.do_comment(func.doc) for cast in casts: self.output(cast) self.deindent() def visitFunction(self, node, parent=None, declare=False): # locate right func instance if parent and isinstance(parent, Class): func = parent.funcs[node.name] elif node.name in self.mv.funcs: func = self.mv.funcs[node.name] else: func = self.mv.lambdas[node.name] if func.invisible or (func.inherited and not func.ident == '__init__'): return if declare and func.declared: # XXX return # check whether function is called at all (possibly via inheritance) if not self.inhcpa(func): if func.ident in ['__iadd__', '__isub__', '__imul__']: return if func.lambdanr is None and not repr(node.code).startswith("Stmt([Raise(CallFunc(Name('NotImplementedError')"): error(repr(func) + ' not called!', self.gx, node, warning=True, mv=self.mv) if not (declare and func.parent and func.ident in func.parent.virtuals): return if func.isGenerator and not declare: self.generator_class(func) self.func_header(func, declare) if declare: return self.indent() if func.isGenerator: self.generator_body(func) return # --- local declarations self.local_defs(func) # --- function body for fake_unpack in func.expand_args.values(): self.visit(fake_unpack, func) self.visit(node.code, func) if func.fakeret: self.visit(func.fakeret, func) # --- add Return(None) (sort of) if function doesn't already end with a Return if node.getChildNodes(): lastnode = node.getChildNodes()[-1] if not func.ident == '__init__' and not func.fakeret and not isinstance(lastnode, Return) and not (isinstance(lastnode, Stmt) and isinstance(lastnode.nodes[-1], Return)): # XXX use Stmt in moduleVisitor self.output('return %s;' % self.nothing(self.mergeinh[func.retnode.thing])) self.deindent() self.output('}\n') def generator_ident(self, func): # XXX merge? if func.parent: return func.parent.ident + '_' + func.ident return func.ident def generator_class(self, func): ident = self.generator_ident(func) self.output('class __gen_%s : public %s {' % (ident, nodetypestr(self.gx, func.retnode.thing, func, mv=self.mv)[:-2])) self.output('public:') self.indent() pairs = [(nodetypestr(self.gx, func.vars[f], func, mv=self.mv), self.cpp_name(func.vars[f])) for f in func.vars] self.output(self.indentation.join(self.group_declarations(pairs))) self.output('int __last_yield;\n') args = [] for f in func.formals: args.append(nodetypestr(self.gx, func.vars[f], func, mv=self.mv) + self.cpp_name(func.vars[f])) self.output(('__gen_%s(' % ident) + ','.join(args) + ') {') self.indent() for f in func.formals: self.output('this->%s = %s;' % (self.cpp_name(func.vars[f]), self.cpp_name(func.vars[f]))) for fake_unpack in func.expand_args.values(): self.visit(fake_unpack, func) self.output('__last_yield = -1;') self.deindent() self.output('}\n') self.output('%s __get_next() {' % nodetypestr(self.gx, func.retnode.thing, func, mv=self.mv)[7:-3]) self.indent() self.output('switch(__last_yield) {') self.indent() for (i, n) in enumerate(func.yieldNodes): self.output('case %d: goto __after_yield_%d;' % (i, i)) self.output('default: break;') self.deindent() self.output('}') for child in func.node.code.getChildNodes(): if isinstance(child, Discard): self.visit_discard(child, func) else: self.visit(child, func) self.output('__stop_iteration = true;') self.deindent() self.output('}\n') self.deindent() self.output('};\n') def generator_body(self, func): ident = self.generator_ident(func) if not (func.isGenerator and func.parent): formals = [self.cpp_name(func.vars[f]) for f in func.formals] else: formals = ['this'] + [self.cpp_name(func.vars[f]) for f in func.formals if f != 'self'] self.output('return new __gen_%s(%s);\n' % (ident, ','.join(formals))) self.deindent() self.output('}\n') def visitYield(self, node, func): self.output('__last_yield = %d;' % func.yieldNodes.index(node)) self.start('__result = ') self.visit_conv(node.value, self.mergeinh[func.yieldnode.thing], func) self.eol() self.output('return __result;') self.output('__after_yield_%d:;' % func.yieldNodes.index(node)) self.start() def visitNot(self, node, func=None): self.append('__NOT(') self.bool_test(node.expr, func) self.append(')') def visitBackquote(self, node, func=None): self.visitm('repr(', inode(self.gx, node.expr).fakefunc.node.expr, ')', func) def visitIf(self, node, func=None): for test in node.tests: self.start() if test != node.tests[0]: self.append('else ') self.append('if (') self.bool_test(test[0], func) print >>self.out, self.line + ') {' self.indent() self.visit(test[1], func) self.deindent() self.output('}') if node.else_: self.output('else {') self.indent() self.visit(node.else_, func) self.deindent() self.output('}') def visitIfExp(self, node, func=None): types = self.mergeinh[node] self.append('((') self.bool_test(node.test, func) self.append(')?(') self.visit_conv(node.then, types, func) self.append('):(') self.visit_conv(node.else_, types, func) self.append('))') def visit_conv(self, node, argtypes, func, check_temp=True): # convert/cast node to type it is assigned to actualtypes = self.mergeinh[node] if check_temp and node in self.mv.tempcount: # XXX self.append(self.mv.tempcount[node]) elif isinstance(node, Dict): self.visitDict(node, func, argtypes=argtypes) elif isinstance(node, Tuple): self.visitTuple(node, func, argtypes=argtypes) elif isinstance(node, List): self.visitList(node, func, argtypes=argtypes) elif isinstance(node, CallFunc) and isinstance(node.node, Name) and node.node.name in ('list', 'tuple', 'dict', 'set'): self.visitCallFunc(node, func, argtypes=argtypes) elif isinstance(node, Name) and node.name == 'None': self.visit(node, func) else: # XXX messy cast = '' if actualtypes and argtypes and typestr(self.gx, actualtypes, mv=self.mv) != typestr(self.gx, argtypes, mv=self.mv) and typestr(self.gx, actualtypes, mv=self.mv) != 'str *': # XXX if incompatible_assignment_rec(self.gx, actualtypes, argtypes): error("incompatible types", self.gx, node, warning=True, mv=self.mv) else: cast = '(' + typestr(self.gx, argtypes, mv=self.mv).strip() + ')' if cast == '(complex)': cast = 'mcomplex' if cast: self.append('(' + cast + '(') self.visit(node, func) if cast: self.append('))') def visitBreak(self, node, func=None): if self.gx.loopstack[-1].else_ in self.mv.tempcount: self.output('%s = 1;' % self.mv.tempcount[self.gx.loopstack[-1].else_]) self.output('break;') def visitStmt(self, node, func=None): for b in node.nodes: if isinstance(b, Discard): self.visit_discard(b, func) else: self.visit(b, func) def visitOr(self, node, func=None): self.visit_and_or(node, node.nodes, '__OR', 'or', func) def visitAnd(self, node, func=None): self.visit_and_or(node, node.nodes, '__AND', 'and', func) def visit_and_or(self, node, nodes, op, mix, func=None): if node in self.gx.bool_test_only: self.append('(') for n in nodes: self.bool_test(n, func) if n != node.nodes[-1]: self.append(' ' + mix + ' ') self.append(')') else: child = nodes[0] if len(nodes) > 1: self.append(op + '(') self.visit_conv(child, self.mergeinh[node], func, check_temp=False) if len(nodes) > 1: self.append(', ') self.visit_and_or(node, nodes[1:], op, mix, func) self.append(', ' + self.mv.tempcount[child][2:] + ')') def visitCompare(self, node, func=None, wrapper=True): if not node in self.bool_wrapper: self.append('___bool(') self.done = set() mapping = { '>': ('__gt__', '>', None), '<': ('__lt__', '<', None), '!=': ('__ne__', '!=', None), '==': ('__eq__', '==', None), '<=': ('__le__', '<=', None), '>=': ('__ge__', '>=', None), 'is': (None, '==', None), 'is not': (None, '!=', None), 'in': ('__contains__', None, None), 'not in': ('__contains__', None, '!'), } left = node.expr for op, right in node.ops: msg, short, pre = mapping[op] if msg == '__contains__': self.do_compare(right, left, msg, short, func, pre) else: self.do_compare(left, right, msg, short, func, pre) if right != node.ops[-1][1]: self.append('&&') left = right if not node in self.bool_wrapper: self.append(')') def visitAugAssign(self, node, func=None): if isinstance(node.node, Subscript): self.start() if set([t[0].ident for t in self.mergeinh[node.node.expr] if isinstance(t[0], Class)]) in [set(['dict']), set(['defaultdict'])] and node.op == '+=': self.visitm(node.node.expr, '->__addtoitem__(', inode(self.gx, node).subs, ', ', node.expr, ')', func) self.eol() return self.visitm(inode(self.gx, node).temp1 + ' = ', node.node.expr, func) self.eol() self.start() self.visitm(inode(self.gx, node).temp2 + ' = ', inode(self.gx, node).subs, func) self.eol() self.visit(inode(self.gx, node).assignhop, func) def visitAdd(self, node, func=None): str_nodes = self.rec_string_addition(node) if str_nodes and len(str_nodes) > 2: self.append('__add_strs(%d, ' % len(str_nodes)) for (i, node) in enumerate(str_nodes): self.visit(node, func) if i < len(str_nodes) - 1: self.append(', ') self.append(')') else: self.visitBinary(node.left, node.right, aug_msg(node, 'add'), '+', func) def rec_string_addition(self, node): if isinstance(node, Add): l, r = self.rec_string_addition(node.left), self.rec_string_addition(node.right) if l and r: return l + r elif self.mergeinh[node] == set([(def_class(self.gx, 'str_'), 0)]): return [node] def visitBitand(self, node, func=None): self.visit_bitop(node, aug_msg(node, 'and'), '&', func) def visitBitor(self, node, func=None): self.visit_bitop(node, aug_msg(node, 'or'), '|', func) def visitBitxor(self, node, func=None): self.visit_bitop(node, aug_msg(node, 'xor'), '^', func) def visit_bitop(self, node, msg, inline, func=None): ltypes = self.mergeinh[node.nodes[0]] ul = unboxable(self.gx, ltypes) self.append('(') for child in node.nodes: self.append('(') self.visit(child, func) self.append(')') if child is not node.nodes[-1]: if ul: self.append(inline) else: self.append('->' + msg) self.append(')') def visitRightShift(self, node, func=None): self.visitBinary(node.left, node.right, aug_msg(node, 'rshift'), '>>', func) def visitLeftShift(self, node, func=None): self.visitBinary(node.left, node.right, aug_msg(node, 'lshift'), '<<', func) def visitMul(self, node, func=None): self.visitBinary(node.left, node.right, aug_msg(node, 'mul'), '*', func) def visitDiv(self, node, func=None): self.visitBinary(node.left, node.right, aug_msg(node, 'div'), '/', func) def visitInvert(self, node, func=None): # XXX visitUnarySub merge, template function __invert? if unboxable(self.gx, self.mergeinh[node.expr]): self.visitm('~', node.expr, func) else: self.visitCallFunc(inode(self.gx, node.expr).fakefunc, func) def visitFloorDiv(self, node, func=None): self.visitBinary(node.left, node.right, aug_msg(node, 'floordiv'), '//', func) def visitPower(self, node, func=None): self.power(node.left, node.right, None, func) def power(self, left, right, mod, func=None): inttype = set([(def_class(self.gx, 'int_'), 0)]) # XXX merge if self.mergeinh[left] == inttype and self.mergeinh[right] == inttype: if not isinstance(right, Const): error("pow(int, int) returns int after compilation", self.gx, left, warning=True, mv=self.mv) if mod: self.visitm('__power(', left, ', ', right, ', ', mod, ')', func) else: self.visitm('__power(', left, ', ', right, ')', func) def visitSub(self, node, func=None): self.visitBinary(node.left, node.right, aug_msg(node, 'sub'), '-', func) def visitBinary(self, left, right, middle, inline, func=None): # XXX cleanup please ltypes = self.mergeinh[left] rtypes = self.mergeinh[right] ul, ur = unboxable(self.gx, ltypes), unboxable(self.gx, rtypes) inttype = set([(def_class(self.gx, 'int_'), 0)]) # XXX new type? floattype = set([(def_class(self.gx, 'float_'), 0)]) # XXX new type? # --- inline mod/div # XXX C++ knows %, /, so we can overload? if (floattype.intersection(ltypes) or inttype.intersection(ltypes)): if inline in ['%'] or (inline in ['/'] and not (floattype.intersection(ltypes) or floattype.intersection(rtypes))): if not def_class(self.gx, 'complex') in (t[0] for t in rtypes): # XXX self.append({'%': '__mods', '/': '__divs'}[inline] + '(') self.visit(left, func) self.append(', ') self.visit(right, func) self.append(')') return # --- inline floordiv if (inline and ul and ur) and inline in ['//']: self.append({'//': '__floordiv'}[inline] + '(') self.visit(left, func) self.append(',') self.visit(right, func) self.append(')') return # --- beauty fix for '1 +- nj' notation if inline in ['+', '-'] and isinstance(right, Const) and isinstance(right.value, complex): if floattype.intersection(ltypes) or inttype.intersection(ltypes): self.append('mcomplex(') self.visit(left, func) self.append(', ' + {'+': '', '-': '-'}[inline] + str(right.value.imag) + ')') return # --- inline other if inline and ((ul and ur) or not middle or (isinstance(left, Name) and left.name == 'None') or (isinstance(right, Name) and right.name == 'None')): # XXX not middle, cleanup? self.append('(') self.visit(left, func) self.append(inline) self.visit(right, func) self.append(')') return # --- 'a.__mul__(b)': use template to call to b.__mul__(a), while maintaining evaluation order if inline in ['+', '*', '-', '/'] and ul and not ur: self.append('__' + {'+': 'add', '*': 'mul', '-': 'sub', '/': 'div'}[inline] + '2(') self.visit(left, func) self.append(', ') self.visit(right, func) self.append(')') return # --- default: left, connector, middle, right argtypes = ltypes | rtypes self.append('(') if middle == '__add__': self.visit_conv(left, argtypes, func) else: self.visit(left, func) self.append(')') self.append(self.connector(left, func) + middle + '(') if middle == '__add__': self.visit_conv(right, argtypes, func) else: self.visit(right, func) self.append(')') def do_compare(self, left, right, middle, inline, func=None, prefix=''): ltypes = self.mergeinh[left] rtypes = self.mergeinh[right] argtypes = ltypes | rtypes ul, ur = unboxable(self.gx, ltypes), unboxable(self.gx, rtypes) # --- inline other if inline and ((ul and ur) or not middle or (isinstance(left, Name) and left.name == 'None') or (isinstance(right, Name) and right.name == 'None')): # XXX not middle, cleanup? self.append('(') self.visit2(left, argtypes, middle, func) self.append(inline) self.visit2(right, argtypes, middle, func) self.append(')') return # --- prefix '!' postfix = '' if prefix: self.append('(' + prefix) postfix = ')' # --- comparison if middle in ['__eq__', '__ne__', '__gt__', '__ge__', '__lt__', '__le__']: self.append(middle[:-2] + '(') self.visit2(left, argtypes, middle, func) self.append(', ') self.visit2(right, argtypes, middle, func) self.append(')' + postfix) return # --- default: left, connector, middle, right self.append('(') self.visit2(left, argtypes, middle, func) self.append(')') if middle == '==': self.append('==(') else: self.append(self.connector(left, func) + middle + '(') self.visit2(right, argtypes, middle, func) self.append(')' + postfix) def visit2(self, node, argtypes, middle, func): # XXX use temp vars in comparisons, e.g. (t1=fun()) if node in self.mv.tempcount: if node in self.done: self.append(self.mv.tempcount[node]) else: self.visitm('(' + self.mv.tempcount[node] + '=', node, ')', func) self.done.add(node) elif middle == '__contains__': self.visit(node, func) else: self.visit_conv(node, argtypes, func) def visitUnarySub(self, node, func=None): self.visitm('(', func) if unboxable(self.gx, self.mergeinh[node.expr]): self.visitm('-', node.expr, func) else: self.visitCallFunc(inode(self.gx, node.expr).fakefunc, func) self.visitm(')', func) def visitUnaryAdd(self, node, func=None): self.visitm('(', func) if unboxable(self.gx, self.mergeinh[node.expr]): self.visitm('+', node.expr, func) else: self.visitCallFunc(inode(self.gx, node.expr).fakefunc, func) self.visitm(')', func) def library_func(self, funcs, modname, clname, funcname): for func in funcs: if not func.mv.module.builtin or func.mv.module.ident != modname: continue if clname is not None: if not func.parent or func.parent.ident != clname: continue return func.ident == funcname def add_args_arg(self, node, funcs): ''' append argument that describes which formals are actually filled in ''' if self.library_func(funcs, 'datetime', 'time', 'replace') or \ self.library_func(funcs, 'datetime', 'datetime', 'replace'): formals = funcs[0].formals[1:] # skip self formal_pos = dict((v, k) for k, v in enumerate(formals)) positions = [] for i, arg in enumerate(node.args): if isinstance(arg, Keyword): positions.append(formal_pos[arg.name]) else: positions.append(i) if positions: self.append(str(reduce(lambda a, b: a | b, ((1 << x) for x in positions))) + ', ') else: self.append('0, ') def visitCallFunc(self, node, func=None, argtypes=None): objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func = analyze_callfunc(self.gx, node, merge=self.gx.merged_inh) funcs = callfunc_targets(self.gx, node, self.gx.merged_inh) if self.library_func(funcs, 're', None, 'findall') or \ self.library_func(funcs, 're', 're_object', 'findall'): error("'findall' does not work with groups (use 'finditer' instead)", self.gx, node, warning=True, mv=self.mv) if self.library_func(funcs, 'socket', 'socket', 'settimeout') or \ self.library_func(funcs, 'socket', 'socket', 'gettimeout'): error("socket.set/gettimeout do not accept/return None", self.gx, node, warning=True, mv=self.mv) if self.library_func(funcs, 'builtin', None, 'map') and len(node.args) > 2: error("default fillvalue for 'map' becomes 0 for integers", self.gx, node, warning=True, mv=self.mv) if self.library_func(funcs, 'itertools', None, 'izip_longest'): error("default fillvalue for 'izip_longest' becomes 0 for integers", self.gx, node, warning=True, mv=self.mv) if self.library_func(funcs, 'struct', None, 'unpack'): error("struct.unpack should be used as follows: 'a, .. = struct.unpack(..)'", self.gx, node, warning=True, mv=self.mv) if self.library_func(funcs, 'array', 'array', '__init__'): if not node.args or not isinstance(node.args[0], Const) or node.args[0].value not in 'cbBhHiIlLfd': error("non-constant or unsupported type code", self.gx, node, warning=True, mv=self.mv) if self.library_func(funcs, 'builtin', None, 'id'): if struct.calcsize("P") == 8 and struct.calcsize('i') == 4 and not self.gx.longlong: error("return value of 'id' does not fit in 32-bit integer (try shedskin -l)", self.gx, node, warning=True, mv=self.mv) nrargs = len(node.args) if isinstance(func, Function) and func.largs: nrargs = func.largs # --- target expression if node.node in self.mergeinh and [t for t in self.mergeinh[node.node] if isinstance(t[0], Function)]: # anonymous function self.visitm(node.node, '(', func) elif constructor: ts = self.namer.nokeywords(nodetypestr(self.gx, node, func, mv=self.mv)) if ts == 'complex ': self.append('mcomplex(') constructor = False # XXX else: if argtypes is not None: # XXX merge instance_new ts = typestr(self.gx, argtypes, mv=self.mv) if ts.startswith('pyseq') or ts.startswith('pyiter'): # XXX argtypes = self.gx.merged_inh[node] ts = typestr(self.gx, argtypes, mv=self.mv) self.append('(new ' + ts[:-2] + '(') if funcs and len(funcs[0].formals) == 1 and not funcs[0].mv.module.builtin: self.append('1') # don't call default constructor elif parent_constr: cl = lookup_class(node.node.expr, self.mv) self.append(self.namer.namespace_class(cl) + '::' + node.node.attrname + '(') elif direct_call: # XXX no namespace (e.g., math.pow), check nr of args if ident == 'float' and node.args and self.mergeinh[node.args[0]] == set([(def_class(self.gx, 'float_'), 0)]): self.visit(node.args[0], func) return if ident in ['abs', 'int', 'float', 'str', 'dict', 'tuple', 'list', 'type', 'cmp', 'sum', 'zip']: self.append('__' + ident + '(') elif ident in ['min', 'max', 'iter', 'round']: self.append('___' + ident + '(') elif ident == 'bool': self.bool_test(node.args[0], func, always_wrap=True) return elif ident == 'pow' and direct_call.mv.module.ident == 'builtin': if nrargs == 3: third = node.args[2] else: third = None self.power(node.args[0], node.args[1], third, func) return elif ident == 'hash': self.append('hasher(') # XXX cleanup elif ident == '__print': # XXX self.append('print(') elif ident == 'isinstance': self.append('True') return else: if isinstance(node.node, Name): if isinstance(func, Function) and isinstance(func.parent, Class) and ident in func.parent.funcs: # masked by method self.append(funcs[0].mv.module.full_path() + '::') self.append(self.cpp_name(funcs[0])) else: self.visit(node.node) self.append('(') elif method_call: for cl, _ in self.mergeinh[objexpr]: if isinstance(cl, Class) and cl.ident != 'none' and ident not in cl.funcs: conv = {'int_': 'int', 'float_': 'float', 'str_': 'str', 'class_': 'class', 'none': 'none'} clname = conv.get(cl.ident, cl.ident) error("class '%s' has no method '%s'" % (clname, ident), self.gx, node, warning=True, mv=self.mv) if isinstance(cl, Class) and ident in cl.staticmethods: error("staticmethod '%s' called without using class name" % ident, self.gx, node, warning=True, mv=self.mv) return # tuple2.__getitem -> __getfirst__/__getsecond if ident == '__getitem__' and isinstance(node.args[0], Const) and node.args[0].value in (0, 1) and self.only_classes(objexpr, ('tuple2',)): self.visit(node.node.expr, func) self.append('->%s()' % ['__getfirst__', '__getsecond__'][node.args[0].value]) return if ident == '__call__': self.visitm(node.node, '->__call__(', func) elif ident == 'is_integer' and (def_class(self.gx, 'float_'), 0) in self.mergeinh[node.node.expr]: self.visitm('__ss_is_integer(', node.node.expr, ')', func) return else: self.visitm(node.node, '(', func) else: if ident: error("unresolved call to '" + ident + "'", self.gx, node, mv=self.mv, warning=True) else: error("unresolved call (possibly caused by method passing, which is currently not allowed)", self.gx, node, mv=self.mv, warning=True) return if not funcs: if constructor: self.append(')') self.append(')') return self.visit_callfunc_args(funcs, node, func) self.append(')') if constructor: self.append(')') def bool_test(self, node, func, always_wrap=False): wrapper = always_wrap or not self.only_classes(node, ('int_', 'bool_')) if node in self.gx.bool_test_only: self.visit(node, func) elif wrapper: self.append('___bool(') self.visit(node, func) is_func = bool([1 for t in self.mergeinh[node] if isinstance(t[0], Function)]) self.append(('', '!=NULL')[is_func] + ')') # XXX else: self.bool_wrapper[node] = True self.visit(node, func) def visit_callfunc_args(self, funcs, node, func): objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func = analyze_callfunc(self.gx, node, merge=self.gx.merged_inh) target = funcs[0] # XXX print_function = self.library_func(funcs, 'builtin', None, '__print') castnull = False # XXX if (self.library_func(funcs, 'random', None, 'seed') or self.library_func(funcs, 'random', None, 'triangular') or self.library_func(funcs, 'random', 'Random', 'seed') or self.library_func(funcs, 'random', 'Random', 'triangular')): castnull = True for itertools_func in ['islice', 'izip_longest', 'permutations']: if self.library_func(funcs, 'itertools', None, itertools_func): castnull = True break for f in funcs: if len(f.formals) != len(target.formals): error('calling functions with different numbers of arguments', self.gx, node, warning=True, mv=self.mv) self.append(')') return if target.inherited_from: target = target.inherited_from pairs, rest, err = connect_actual_formal(self.gx, node, target, parent_constr, merge=self.mergeinh) if err and not target.mv.module.builtin: # XXX error('call with incorrect number of arguments', self.gx, node, warning=True, mv=self.mv) if isinstance(func, Function) and func.lambdawrapper: rest = func.largs if target.node.varargs: self.append('%d' % rest) if rest or pairs: self.append(', ') double = False if ident in ['min', 'max']: for arg in node.args: if arg in self.mergeinh and (def_class(self.gx, 'float_'), 0) in self.mergeinh[arg]: double = True self.add_args_arg(node, funcs) if isinstance(func, Function) and func.largs is not None: kw = [p for p in pairs if p[1].name.startswith('__kw_')] nonkw = [p for p in pairs if not p[1].name.startswith('__kw_')] pairs = kw + nonkw[:func.largs] for (arg, formal) in pairs: cast = False builtin_types = self.cast_to_builtin(arg, func, formal, target, method_call, objexpr) formal_types = builtin_types or self.mergeinh[formal] if double and self.mergeinh[arg] == set([(def_class(self.gx, 'int_'), 0)]): cast = True self.append('((double)(') elif castnull and isinstance(arg, Name) and arg.name == 'None': cast = True self.append('((void *)(') if (print_function or self.library_func(funcs, 'struct', None, 'pack')) and not formal.name.startswith('__kw_'): types = [t[0].ident for t in self.mergeinh[arg]] if 'float_' in types or 'int_' in types or 'bool_' in types or 'complex' in types: cast = True self.append('___box((') if arg in target.mv.defaults: if self.mergeinh[arg] == set([(def_class(self.gx, 'none'), 0)]): self.append('NULL') elif target.mv.module == self.mv.module: self.append('default_%d' % (target.mv.defaults[arg][0])) else: self.append('%s::default_%d' % (target.mv.module.full_path(), target.mv.defaults[arg][0])) elif arg in self.consts: self.append(self.consts[arg]) else: if constructor and ident in ['set', 'frozenset'] and nodetypestr(self.gx, arg, func, mv=self.mv) in ['list *', 'tuple *', 'pyiter *', 'pyseq *', 'pyset']: # XXX pass elif not builtin_types and target.mv.module.builtin: self.visit(arg, func) else: self.visit_conv(arg, formal_types, func) if cast: self.append('))') if (arg, formal) != pairs[-1]: self.append(', ') if constructor and ident == 'frozenset': if pairs: self.append(',') self.append('1') def cast_to_builtin(self, arg, func, formal, target, method_call, objexpr): # type inference cannot deduce all necessary casts to builtin formals vars = {'u': 'unit', 'v': 'value', 'o': None} if target.mv.module.builtin and method_call and formal.name in vars and target.parent.ident in ('list', 'dict', 'set'): subtypes = self.subtypes(self.mergeinh[objexpr], vars[formal.name]) if nodetypestr(self.gx, arg, func, mv=self.mv) != typestr(self.gx, subtypes, mv=self.mv): return subtypes def cast_to_builtin2(self, arg, func, objexpr, msg, formal_nr): # shortcut for outside of visitCallFunc XXX merge with visitCallFunc? cls = [t[0] for t in self.mergeinh[objexpr] if isinstance(t[0], Class)] if cls: cl = cls.pop() if msg in cl.funcs: target = cl.funcs[msg] if formal_nr < len(target.formals): formal = target.vars[target.formals[formal_nr]] builtin_types = self.cast_to_builtin(arg, func, formal, target, True, objexpr) if builtin_types: return typestr(self.gx, builtin_types, mv=self.mv) def visitReturn(self, node, func=None): if func.isGenerator: self.output('__stop_iteration = true;') self.output('return __zero<%s>();' % nodetypestr(self.gx, func.retnode.thing, mv=self.mv)[7:-3]) # XXX meugh return self.start('return ') self.visit_conv(node.value, self.mergeinh[func.retnode.thing], func) self.eol() def tuple_assign(self, lvalue, rvalue, func): temp = self.mv.tempcount[lvalue] if isinstance(lvalue, tuple): nodes = lvalue else: nodes = lvalue.nodes # --- nested unpacking assignment: a, (b,c) = d, e if [item for item in nodes if not isinstance(item, AssName)]: self.start(temp + ' = ') if isinstance(rvalue, str): self.append(rvalue) else: self.visit(rvalue, func) self.eol() for i, item in enumerate(nodes): selector = self.get_selector(temp, item, i) if isinstance(item, AssName): self.output('%s = %s;' % (item.name, selector)) elif isinstance(item, (AssTuple, AssList)): # recursion self.tuple_assign(item, selector, func) elif isinstance(item, Subscript): self.assign_pair(item, selector, func) elif isinstance(item, AssAttr): self.assign_pair(item, selector, func) self.eol(' = ' + selector) # --- non-nested unpacking assignment: a,b,c = d else: self.start() self.visitm(temp, ' = ', rvalue, func) self.eol() for i, item in enumerate(lvalue.nodes): self.start() self.visitm(item, ' = ', self.get_selector(temp, item, i), func) self.eol() def one_class(self, node, names): for clname in names: if self.only_classes(node, (clname,)): return True return False def get_selector(self, temp, item, i): rvalue_node = self.gx.item_rvalue[item] sel = '__getitem__(%d)' % i if i < 2 and self.only_classes(rvalue_node, ('tuple2',)): sel = ['__getfirst__()', '__getsecond__()'][i] elif self.one_class(rvalue_node, ('list', 'str_', 'tuple')): sel = '__getfast__(%d)' % i return '%s->%s' % (temp, sel) def subs_assign(self, lvalue, func): if len(lvalue.subs) > 1: subs = inode(self.gx, lvalue.expr).faketuple else: subs = lvalue.subs[0] self.visitm(lvalue.expr, self.connector(lvalue.expr, func), '__setitem__(', subs, ', ', func) def struct_unpack_cpp(self, node, func): struct_unpack = self.gx.struct_unpack.get(node) if struct_unpack: sinfo, tvar, tvar_pos = struct_unpack self.start() self.visitm(tvar, ' = ', node.expr.args[1], func) self.eol() self.output('%s = 0;' % tvar_pos) hop = 0 for (o, c, t, d) in sinfo: self.start() expr = "__struct__::unpack_%s('%c', '%c', %d, %s, &%s)" % (t, o, c, d, tvar, tvar_pos) if c == 'x' or (d == 0 and c != 's'): self.visitm(expr, func) else: n = list(node.nodes[0])[hop] hop += 1 if isinstance(n, Subscript): # XXX merge self.subs_assign(n, func) self.visitm(expr, ')', func) elif isinstance(n, AssName): self.visitm(n, ' = ', expr, func) elif isinstance(n, AssAttr): self.visitAssAttr(n, func) self.visitm(' = ', expr, func) self.eol() return True return False def visitAssign(self, node, func=None): if self.struct_unpack_cpp(node, func): return # temp vars if len(node.nodes) > 1 or isinstance(node.expr, Tuple): if isinstance(node.expr, Tuple): if [n for n in node.nodes if isinstance(n, AssTuple)]: # XXX a,b=d[i,j]=..? for child in node.expr.nodes: if not (child, 0, 0) in self.gx.cnode: # (a,b) = (1,2): (1,2) never visited continue if not isinstance(child, Const) and not (isinstance(child, Name) and child.name == 'None'): self.start(self.mv.tempcount[child] + ' = ') self.visit(child, func) self.eol() elif not isinstance(node.expr, Const) and not (isinstance(node.expr, Name) and node.expr.name == 'None'): self.start(self.mv.tempcount[node.expr] + ' = ') self.visit(node.expr, func) self.eol() # a = (b,c) = .. = expr for left in node.nodes: pairs = assign_rec(left, node.expr) for (lvalue, rvalue) in pairs: self.start('') # XXX remove? # expr[expr] = expr if isinstance(lvalue, Subscript) and not isinstance(lvalue.subs[0], Sliceobj): self.assign_pair(lvalue, rvalue, func) # expr.attr = expr elif isinstance(lvalue, AssAttr): lcp = lowest_common_parents(polymorphic_t(self.gx, self.mergeinh[lvalue.expr])) # property if len(lcp) == 1 and isinstance(lcp[0], Class) and lvalue.attrname in lcp[0].properties: self.visitm(lvalue.expr, '->' + self.cpp_name(lcp[0].properties[lvalue.attrname][1]) + '(', rvalue, ')', func) elif lcp and isinstance(lcp[0], Class): var = lookup_var(lvalue.attrname, lcp[0], mv=self.mv) vartypes = set() if var: vartypes = self.mergeinh[var] self.visit(lvalue, func) self.append(' = ') self.visit_conv(rvalue, vartypes, func) else: self.visitm(lvalue, ' = ', rvalue, func) self.eol() # name = expr elif isinstance(lvalue, AssName): vartypes = self.mergeinh[lookup_var(lvalue.name, func, mv=self.mv)] self.visit(lvalue, func) self.append(' = ') self.visit_conv(rvalue, vartypes, func) self.eol() # (a,(b,c), ..) = expr elif isinstance(lvalue, (AssTuple, AssList)): self.tuple_assign(lvalue, rvalue, func) # expr[a:b] = expr elif isinstance(lvalue, Slice): if isinstance(rvalue, Slice) and lvalue.upper == rvalue.upper == None and lvalue.lower == rvalue.lower == None: self.visitm(lvalue.expr, self.connector(lvalue.expr, func), 'units = ', rvalue.expr, self.connector(rvalue.expr, func), 'units', func) else: # XXX let visitCallFunc(fakefunc) use cast_to_builtin fakefunc = inode(self.gx, lvalue.expr).fakefunc self.visitm('(', fakefunc.node.expr, ')->__setslice__(', fakefunc.args[0], ',', fakefunc.args[1], ',', fakefunc.args[2], ',', fakefunc.args[3], ',', func) self.visit_conv(fakefunc.args[4], self.mergeinh[lvalue.expr], func) self.append(')') self.eol() # expr[a:b:c] = expr elif isinstance(lvalue, Subscript) and isinstance(lvalue.subs[0], Sliceobj): # XXX see comment above fakefunc = inode(self.gx, lvalue.expr).fakefunc self.visitm('(', fakefunc.node.expr, ')->__setslice__(', fakefunc.args[0], ',', fakefunc.args[1], ',', fakefunc.args[2], ',', fakefunc.args[3], ',', func) self.visit_conv(fakefunc.args[4], self.mergeinh[lvalue.expr], func) self.append(')') self.eol() def assign_pair(self, lvalue, rvalue, func): self.start('') # expr[expr] = expr if isinstance(lvalue, Subscript) and not isinstance(lvalue.subs[0], Sliceobj): self.subs_assign(lvalue, func) if isinstance(rvalue, str): self.append(rvalue) elif rvalue in self.mv.tempcount: self.append(self.mv.tempcount[rvalue]) else: cast = self.cast_to_builtin2(rvalue, func, lvalue.expr, '__setitem__', 2) if cast: self.append('((%s)' % cast) self.visit(rvalue, func) if cast: self.append(')') self.append(')') self.eol() # expr.x = expr elif isinstance(lvalue, AssAttr): self.visitAssAttr(lvalue, func) def do_lambdas(self, declare): for l in self.mv.lambdas.values(): if l.ident not in self.mv.funcs: self.visitFunction(l.node, declare=declare) def do_listcomps(self, declare): for (listcomp, lcfunc, func) in self.mv.listcomps: # XXX cleanup if lcfunc.mv.module.builtin: continue parent = func while isinstance(parent, Function) and parent.listcomp: parent = parent.parent if isinstance(parent, Function): if not self.inhcpa(parent) or parent.inherited: continue genexpr = listcomp in self.gx.genexp_to_lc.values() if declare: self.listcomp_head(listcomp, True, genexpr) elif genexpr: self.genexpr_class(listcomp, declare) else: self.listcomp_func(listcomp) def listcomp_head(self, node, declare, genexpr): lcfunc, func = self.listcomps[node] args = [a + b for a, b in self.lc_args(lcfunc, func)] ts = nodetypestr(self.gx, node, lcfunc, mv=self.mv) if not ts.endswith('*'): ts += ' ' if genexpr: self.genexpr_class(node, declare) else: self.output('static inline ' + ts + lcfunc.ident + '(' + ', '.join(args) + ')' + [' {', ';'][declare]) def lc_args(self, lcfunc, func): args = [] for name in lcfunc.misses: if lookup_var(name, func, mv=self.mv).parent: args.append((nodetypestr(self.gx, lookup_var(name, lcfunc, mv=self.mv), lcfunc, mv=self.mv), self.cpp_name(name))) return args def listcomp_func(self, node): lcfunc, func = self.listcomps[node] self.listcomp_head(node, False, False) self.indent() self.local_defs(lcfunc) self.output(nodetypestr(self.gx, node, lcfunc, mv=self.mv) + '__ss_result = new ' + nodetypestr(self.gx, node, lcfunc, mv=self.mv)[:-2] + '();\n') self.listcomp_rec(node, node.quals, lcfunc, False) self.output('return __ss_result;') self.deindent() self.output('}\n') def genexpr_class(self, node, declare): lcfunc, func = self.listcomps[node] args = self.lc_args(lcfunc, func) func1 = lcfunc.ident + '(' + ', '.join(a + b for a, b in args) + ')' func2 = nodetypestr(self.gx, node, lcfunc, mv=self.mv)[7:-3] if declare: ts = nodetypestr(self.gx, node, lcfunc, mv=self.mv) if not ts.endswith('*'): ts += ' ' self.output('class ' + lcfunc.ident + ' : public ' + ts[:-2] + ' {') self.output('public:') self.indent() self.local_defs(lcfunc) for a, b in args: self.output(a + b + ';') self.output('int __last_yield;\n') self.output(func1 + ';') self.output(func2 + ' __get_next();') self.deindent() self.output('};\n') else: self.output(lcfunc.ident + '::' + func1 + ' {') for a, b in args: self.output(' this->%s = %s;' % (b, b)) self.output(' __last_yield = -1;') self.output('}\n') self.output(func2 + ' ' + lcfunc.ident + '::__get_next() {') self.indent() self.output('if(!__last_yield) goto __after_yield_0;') self.output('__last_yield = 0;\n') self.listcomp_rec(node, node.quals, lcfunc, True) self.output('__stop_iteration = true;') self.deindent() self.output('}\n') def local_defs(self, func): pairs = [] for (name, var) in func.vars.items(): if not var.invisible and (not hasattr(func, 'formals') or name not in func.formals): # XXX pairs.append((nodetypestr(self.gx, var, func, mv=self.mv), self.cpp_name(var))) self.output(self.indentation.join(self.group_declarations(pairs))) # --- nested for loops: loop headers, if statements def listcomp_rec(self, node, quals, lcfunc, genexpr): if not quals: if genexpr: self.start('__result = ') self.visit(node.expr, lcfunc) self.eol() self.output('return __result;') self.start('__after_yield_0:') elif len(node.quals) == 1 and not is_fastfor(node.quals[0]) and not self.fastenum(node.quals[0]) and not self.fastzip2(node.quals[0]) and not node.quals[0].ifs and self.one_class(node.quals[0].list, ('tuple', 'list', 'str_', 'dict', 'set')): self.start('__ss_result->units[' + self.mv.tempcount[node.quals[0].list] + '] = ') self.visit(node.expr, lcfunc) else: self.start('__ss_result->append(') self.visit(node.expr, lcfunc) self.append(')') self.eol() return qual = quals[0] # iter var if isinstance(qual.assign, AssName): var = lookup_var(qual.assign.name, lcfunc, mv=self.mv) else: var = lookup_var(self.mv.tempcount[qual.assign], lcfunc, mv=self.mv) iter = self.cpp_name(var) if is_fastfor(qual): self.do_fastfor(node, qual, quals, iter, lcfunc, genexpr) elif self.fastenum(qual): self.do_fastenum(qual, lcfunc, genexpr) self.listcompfor_body(node, quals, iter, lcfunc, True, genexpr) elif self.fastzip2(qual): self.do_fastzip2(qual, lcfunc, genexpr) self.listcompfor_body(node, quals, iter, lcfunc, True, genexpr) elif self.fastdictiter(qual): self.do_fastdictiter(qual, lcfunc, genexpr) self.listcompfor_body(node, quals, iter, lcfunc, True, genexpr) else: if not isinstance(qual.list, Name): itervar = self.mv.tempcount[qual] self.start('') self.visitm(itervar, ' = ', qual.list, lcfunc) self.eol() else: itervar = self.cpp_name(qual.list.name) pref, tail = self.forin_preftail(qual) if len(node.quals) == 1 and not qual.ifs and not genexpr: if self.one_class(qual.list, ('list', 'tuple', 'str_', 'dict', 'set')): self.output('__ss_result->resize(len(' + itervar + '));') self.start('FOR_IN' + pref + '(' + iter + ',' + itervar + ',' + tail) print >>self.out, self.line + ')' self.listcompfor_body(node, quals, iter, lcfunc, False, genexpr) def listcompfor_body(self, node, quals, iter, lcfunc, skip, genexpr): qual = quals[0] if not skip: self.indent() if isinstance(qual.assign, (AssTuple, AssList)): self.tuple_assign(qual.assign, iter, lcfunc) # if statements if qual.ifs: self.start('if (') self.indent() for cond in qual.ifs: self.bool_test(cond.test, lcfunc) if cond != qual.ifs[-1]: self.append(' && ') self.append(') {') print >>self.out, self.line # recurse self.listcomp_rec(node, quals[1:], lcfunc, genexpr) # --- nested for loops: loop tails if qual.ifs: self.deindent() self.output('}') self.deindent() self.output('END_FOR\n') def visitGenExpr(self, node, func=None): self.visit(self.gx.genexp_to_lc[node], func) def visitListComp(self, node, func=None): lcfunc, _ = self.listcomps[node] args = [] temp = self.line for name in lcfunc.misses: var = lookup_var(name, func, mv=self.mv) if var.parent: if name == 'self' and not func.listcomp: # XXX parent? args.append('this') else: args.append(self.cpp_name(var)) self.line = temp if node in self.gx.genexp_to_lc.values(): self.append('new ') self.append(lcfunc.ident + '(' + ', '.join(args) + ')') def visitSubscript(self, node, func=None): if node.flags == 'OP_DELETE': self.start() if isinstance(node.subs[0], Sliceobj): self.visitCallFunc(inode(self.gx, node.expr).fakefunc, func) else: self.visitCallFunc(inode(self.gx, node.expr).fakefunc, func) self.eol() else: self.visitCallFunc(inode(self.gx, node.expr).fakefunc, func) def visitMod(self, node, func=None): # --- non-str % .. if [t for t in self.gx.merged_inh[node.left] if t[0].ident != 'str_']: self.visitBinary(node.left, node.right, '__mod__', '%', func) return # --- str % non-constant dict/tuple if not isinstance(node.right, (Tuple, Dict)) and node.right in self.gx.merged_inh: # XXX if [t for t in self.gx.merged_inh[node.right] if t[0].ident == 'dict']: self.visitm('__moddict(', node.left, ', ', node.right, ')', func) return elif [t for t in self.gx.merged_inh[node.right] if t[0].ident in ['tuple', 'tuple2']]: self.visitm('__modtuple(', node.left, ', ', node.right, ')', func) return # --- str % constant-dict: if isinstance(node.right, Dict): # XXX geen str keys self.visitm('__modcd(', node.left, ', ', 'new list(%d, ' % len(node.right.items), func) self.append(', '.join(('new str("%s")' % key.value) for key, value in node.right.items)) self.append(')') nodes = [value for (key, value) in node.right.items] else: self.visitm('__modct(', node.left, func) # --- str % constant-tuple if isinstance(node.right, Tuple): nodes = node.right.nodes # --- str % non-tuple/non-dict else: nodes = [node.right] self.append(', %d' % len(nodes)) # --- visit nodes, boxing scalars for n in nodes: if [clname for clname in ('float_', 'int_', 'bool_', 'complex') if def_class(self.gx, clname) in [t[0] for t in self.mergeinh[n]]]: self.visitm(', ___box(', n, ')', func) else: self.visitm(', ', n, func) self.append(')') def visitPrintnl(self, node, func=None): self.visitPrint(node, func, print_space=False) def visitPrint(self, node, func=None, print_space=True): self.start('print2(') if node.dest: self.visitm(node.dest, ', ', func) else: self.append('NULL,') if print_space: self.append('1,') else: self.append('0,') self.append(str(len(node.nodes))) for n in node.nodes: types = [t[0].ident for t in self.mergeinh[n]] if 'float_' in types or 'int_' in types or 'bool_' in types or 'complex' in types: self.visitm(', ___box(', n, ')', func) else: self.visitm(', ', n, func) self.eol(')') def visitGetattr(self, node, func=None): cl, module = lookup_class_module(node.expr, inode(self.gx, node).mv, func) # module.attr if module: self.append(module.full_path() + '::') # class.attr: staticmethod elif cl and node.attrname in cl.staticmethods: ident = cl.ident if cl.ident in ['dict', 'defaultdict']: # own namespace because of template vars self.append('__' + cl.ident + '__::') elif isinstance(node.expr, Getattr): submodule = lookup_module(node.expr.expr, inode(self.gx, node).mv) self.append(submodule.full_path() + '::' + ident + '::') else: self.append(ident + '::') # class.attr elif cl: # XXX merge above? ident = cl.ident if isinstance(node.expr, Getattr): submodule = lookup_module(node.expr.expr, inode(self.gx, node).mv) self.append(submodule.full_path() + '::' + cl.ident + '::') else: self.append(ident + '::') # obj.attr else: checkcls = [] # XXX better to just inherit vars? for t in self.mergeinh[node.expr]: if isinstance(t[0], Class): checkcls.extend(t[0].ancestors(True)) for cl in checkcls: if not node.attrname in t[0].funcs and node.attrname in cl.parent.vars: # XXX error("class attribute '" + node.attrname + "' accessed without using class name", self.gx, node, warning=True, mv=self.mv) break else: if not self.mergeinh[node.expr] and not node.attrname.startswith('__'): # XXX error('expression has no type', self.gx, node, warning=True, mv=self.mv) elif not self.mergeinh[node] and not [cl for cl in checkcls if node.attrname in cl.funcs] and not node.attrname.startswith('__'): # XXX error('expression has no type', self.gx, node, warning=True, mv=self.mv) if not isinstance(node.expr, Name): self.append('(') if isinstance(node.expr, Name) and not lookup_var(node.expr.name, func, mv=self.mv): # XXX XXX self.append(node.expr.name) else: self.visit(node.expr, func) if not isinstance(node.expr, (Name)): self.append(')') self.append(self.connector(node.expr, func)) ident = node.attrname # property lcp = lowest_common_parents(polymorphic_t(self.gx, self.mergeinh[node.expr])) if len(lcp) == 1 and node.attrname in lcp[0].properties: self.append(self.cpp_name(lcp[0].properties[node.attrname][0]) + '()') return # getfast if ident == '__getitem__' and self.one_class(node.expr, ('list', 'str_', 'tuple')): ident = '__getfast__' elif ident == '__getitem__' and len(lcp) == 1 and lcp[0].ident == 'array': # XXX merge into above ident = '__getfast__' self.append(self.attr_var_ref(node, ident)) def attr_var_ref(self, node, ident): # XXX blegh lcp = lowest_common_parents(polymorphic_t(self.gx, self.mergeinh[node.expr])) if len(lcp) == 1 and isinstance(lcp[0], Class) and node.attrname in lcp[0].vars and not node.attrname in lcp[0].funcs: return self.cpp_name(lcp[0].vars[node.attrname]) return self.cpp_name(ident) def visitAssAttr(self, node, func=None): # XXX merge with visitGetattr if node.flags == 'OP_DELETE': error("'del' has no effect without refcounting", self.gx, node, warning=True, mv=self.mv) return cl, module = lookup_class_module(node.expr, inode(self.gx, node).mv, func) # module.attr if module: self.append(module.full_path() + '::') # class.attr elif cl: if isinstance(node.expr, Getattr): submodule = lookup_module(node.expr.expr, inode(self.gx, node).mv) self.append(submodule.full_path() + '::' + cl.ident + '::') else: self.append(cl.ident + '::') # obj.attr else: if isinstance(node.expr, Name) and not lookup_var(node.expr.name, func, mv=self.mv): # XXX self.append(node.expr.name) else: self.visit(node.expr, func) self.append(self.connector(node.expr, func)) # XXX '->' self.append(self.attr_var_ref(node, node.attrname)) def visitAssName(self, node, func=None): if node.flags == 'OP_DELETE': error("'del' has no effect without refcounting", self.gx, node, warning=True, mv=self.mv) return self.append(self.cpp_name(node.name)) def visitName(self, node, func=None, add_cl=True): map = {'True': 'True', 'False': 'False'} if node in self.mv.lwrapper: self.append(self.mv.lwrapper[node]) elif node.name == 'None': self.append('NULL') elif node.name == 'self': lcp = lowest_common_parents(polymorphic_t(self.gx, self.mergeinh[node])) if ((not func or func.listcomp or not isinstance(func.parent, Class)) or (func and func.parent and func.isGenerator)): # XXX lookup_var? self.append('self') elif len(lcp) == 1 and not (lcp[0] is func.parent or lcp[0] in func.parent.ancestors()): # see test 160 self.mv.module.prop_includes.add(lcp[0].module) # XXX generalize self.append('((' + self.namer.namespace_class(lcp[0]) + ' *)this)') else: self.append('this') elif node.name in map: self.append(map[node.name]) else: # XXX clean up if not self.mergeinh[node] and not inode(self.gx, node).parent in self.gx.inheritance_relations: error("variable '" + node.name + "' has no type", self.gx, node, warning=True, mv=self.mv) self.append(node.name) elif singletype(self.gx, node, Module): self.append('__' + singletype(self.gx, node, Module).ident + '__') else: if ((def_class(self.gx, 'class_'), 0) in self.mergeinh[node] or (add_cl and [t for t in self.mergeinh[node] if isinstance(t[0], StaticClass)])): cl = lookup_class(node, self.mv) if cl: self.append(self.namer.namespace_class(cl, add_cl='cl_')) else: self.append(self.cpp_name(node.name)) else: if isinstance(func, Class) and node.name in func.parent.vars: # XXX self.append(func.ident + '::') var = lookup_var(node.name, func, mv=self.mv) if var: self.append(self.cpp_name(var)) else: self.append(node.name) # XXX def expand_special_chars(self, value): value = list(value) replace = dict(['\\\\', '\nn', '\tt', '\rr', '\ff', '\bb', '\vv', '""']) for i in range(len(value)): if value[i] in replace: value[i] = '\\' + replace[value[i]] elif value[i] not in string.printable: value[i] = '\\' + oct(ord(value[i])).zfill(4)[1:] return ''.join(value) def visitConst(self, node, func=None): if not self.filling_consts and isinstance(node.value, str): self.append(self.get_constant(node)) return if node.value is None: self.append('NULL') return t = list(inode(self.gx, node).types())[0] if t[0].ident == 'int_': self.append(str(node.value)) if self.gx.longlong: self.append('LL') elif t[0].ident == 'float_': if str(node.value) in ['inf', '1.#INF', 'Infinity']: self.append('INFINITY') elif str(node.value) in ['-inf', '-1.#INF', 'Infinity']: self.append('-INFINITY') else: self.append(str(node.value)) elif t[0].ident == 'str_': self.append('new str("%s"' % self.expand_special_chars(node.value)) if '\0' in node.value: # '\0' delimiter in C self.append(', %d' % len(node.value)) self.append(')') elif t[0].ident == 'complex': self.append('mcomplex(%s, %s)' % (node.value.real, node.value.imag)) else: self.append('new %s(%s)' % (t[0].ident, node.value)) def generate_code(gx): for module in gx.modules.values(): if not module.builtin: gv = GenerateVisitor(gx, module) walk(module.ast, gv) gv.out.close() gv.header_file() gv.out.close() gv.insert_consts(declare=False) gv.insert_consts(declare=True) gv.insert_extras('.hpp') gv.insert_extras('.cpp') generate_makefile(gx) shedskin-0.9.4/shedskin/typestr.py0000664000175000017500000002715212157270661017164 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) typestr.py: generate type declarations ''' import error import python import infer class ExtmodError(Exception): pass def types_var_types(gx, types, varname): subtypes = set() for t in types: if not varname in t[0].vars: continue var = t[0].vars[varname] if (var, t[1], 0) in gx.cnode: subtypes.update(gx.cnode[var, t[1], 0].types()) return subtypes def types_classes(types): return set(t[0] for t in types if isinstance(t[0], python.Class)) def unboxable(gx, types): if not isinstance(types, set): types = infer.inode(gx, types).types() classes = set(t[0] for t in types) if [cl for cl in classes if cl.ident not in ['int_', 'float_', 'bool_', 'complex']]: return None else: if classes: return classes.pop().ident return None def singletype(gx, node, type): types = [t[0] for t in infer.inode(gx, node).types()] if len(types) == 1 and isinstance(types[0], type): return types[0] def singletype2(types, type): ltypes = list(types) if len(types) == 1 and isinstance(ltypes[0][0], type): return ltypes[0][0] def polymorphic_t(gx, types): return polymorphic_cl(gx, (t[0] for t in types)) def polymorphic_cl(gx, classes): cls = set(cl for cl in classes) if len(cls) > 1 and python.def_class(gx, 'none') in cls and not python.def_class(gx, 'int_') in cls and not python.def_class(gx, 'float_') in cls and not python.def_class(gx, 'bool_') in cls: cls.remove(python.def_class(gx, 'none')) if python.def_class(gx, 'tuple2') in cls and python.def_class(gx, 'tuple') in cls: # XXX hmm cls.remove(python.def_class(gx, 'tuple2')) return cls # --- determine lowest common parent classes (inclusive) def lowest_common_parents(classes): classes = [cl for cl in classes if isinstance(cl, python.Class)] # collect all possible parent classes parents = set() for parent in classes: while parent: parent.lcpcount = 0 parents.add(parent) if parent.bases: parent = parent.bases[0] else: parent = None # count how many descendants in 'classes' each has for parent in classes: while parent: parent.lcpcount += 1 if parent.bases: parent = parent.bases[0] else: parent = None # remove those that don't add anything useless = set() for parent in parents: orig = parent while parent: if parent != orig: if parent.lcpcount > orig.lcpcount: useless.add(orig) elif parent.lcpcount == orig.lcpcount: useless.add(parent) if parent.bases: parent = parent.bases[0] else: parent = None return list(parents - useless) def nodetypestr(gx, node, parent=None, cplusplus=True, check_extmod=False, check_ret=False, var=None, mv=None): # XXX minimize if cplusplus and isinstance(node, python.Variable) and node.looper: # XXX to declaredefs? return nodetypestr(gx, node.looper, None, cplusplus, mv=mv)[:-2] + '::for_in_loop ' if cplusplus and isinstance(node, python.Variable) and node.wopper: # XXX to declaredefs? ts = nodetypestr(gx, node.wopper, None, cplusplus, mv=mv) if ts.startswith('dict<'): return 'dictentry' + ts[4:] types = gx.merged_inh[node] return typestr(gx, types, None, cplusplus, node, check_extmod, 0, check_ret, var, mv=mv) def typestr(gx, types, parent=None, cplusplus=True, node=None, check_extmod=False, depth=0, check_ret=False, var=None, tuple_check=False, mv=None): try: ts = typestrnew(gx, types, cplusplus, node, check_extmod, depth, check_ret, var, tuple_check, mv=mv) except RuntimeError: if not mv.module.builtin and isinstance(node, python.Variable) and not node.name.startswith('__'): # XXX startswith if node.parent: varname = repr(node) else: varname = "'%s'" % node.name error.error("Variable %s has dynamic (sub)type" % varname, gx, node, warning=True) ts = 'ERROR' if cplusplus: if not ts.endswith('*'): ts += ' ' return ts return '[' + ts + ']' def dynamic_variable_error(gx, node, types, conv2): if not node.name.startswith('__'): # XXX startswith classes = polymorphic_cl(gx, types_classes(types)) lcp = lowest_common_parents(classes) if node.parent: varname = "%s" % node else: varname = "'%s'" % node if [t for t in types if isinstance(t[0], python.Function)]: error.error("Variable %s has dynamic (sub)type: {%s, function}" % (varname, ', '.join(sorted(conv2.get(cl.ident, cl.ident) for cl in lcp))), gx, node, warning=True) else: error.error("Variable %s has dynamic (sub)type: {%s}" % (varname, ', '.join(sorted(conv2.get(cl.ident, cl.ident) for cl in lcp))), gx, node, warning=True) def typestrnew(gx, types, cplusplus=True, node=None, check_extmod=False, depth=0, check_ret=False, var=None, tuple_check=False, mv=None): if depth == 10: raise RuntimeError() # --- annotation or c++ code conv1 = {'int_': '__ss_int', 'float_': 'double', 'str_': 'str', 'none': 'int', 'bool_': '__ss_bool', 'complex': 'complex'} conv2 = {'int_': 'int', 'float_': 'float', 'str_': 'str', 'class_': 'class', 'none': 'None', 'bool_': 'bool', 'complex': 'complex'} if cplusplus: sep, ptr, conv = '<>', ' *', conv1 else: sep, ptr, conv = '()', '', conv2 def map(ident): if cplusplus: return ident + ' *' return conv.get(ident, ident) anon_funcs = set(t[0] for t in types if isinstance(t[0], python.Function)) static_cls = set(t[0] for t in types if isinstance(t[0], python.StaticClass)) if (anon_funcs or static_cls) and check_extmod: raise ExtmodError() if anon_funcs: if [t for t in types if not isinstance(t[0], python.Function) and t[0] is not python.def_class(gx, 'none')]: if isinstance(node, python.Variable): dynamic_variable_error(gx, node, types, conv2) else: error.error("function mixed with non-function", gx, node, warning=True) f = anon_funcs.pop() if f.mv != mv: return f.mv.module.full_path() + '::' + 'lambda%d' % f.lambdanr return 'lambda%d' % f.lambdanr classes = polymorphic_cl(gx, types_classes(types)) lcp = lowest_common_parents(classes) # --- multiple parent classes if len(lcp) > 1: if set(lcp) == set([python.def_class(gx, 'int_'), python.def_class(gx, 'float_')]): return conv['float_'] elif not node or infer.inode(gx, node).mv.module.builtin: if python.def_class(gx, 'complex') in lcp: # XXX return conv['complex'] elif python.def_class(gx, 'float_') in lcp: return conv['float_'] elif python.def_class(gx, 'int_') in lcp: return conv['int_'] else: return '***ERROR*** ' elif isinstance(node, python.Variable): dynamic_variable_error(gx, node, types, conv2) elif node not in gx.bool_test_only: if tuple_check: error.error("tuple with length > 2 and different types of elements", gx, node, warning=True, mv=mv) else: error.error("expression has dynamic (sub)type: {%s}" % ', '.join(sorted(conv2.get(cl.ident, cl.ident) for cl in lcp)), gx, node, warning=True) elif not classes: if cplusplus: return 'void *' return '' cl = lcp.pop() if check_ret and cl.mv.module.ident == 'collections' and cl.ident == 'defaultdict': print '*WARNING* defaultdicts are returned as dicts' elif check_extmod and cl.mv.module.builtin and not (cl.mv.module.ident == 'builtin' and cl.ident in ['int_', 'float_', 'complex', 'str_', 'list', 'tuple', 'tuple2', 'dict', 'set', 'frozenset', 'none', 'bool_']) and not (cl.mv.module.ident == 'collections' and cl.ident == 'defaultdict'): raise ExtmodError() # --- simple built-in types if cl.ident in ['int_', 'float_', 'bool_', 'complex']: return conv[cl.ident] elif cl.ident == 'str_': return 'str' + ptr elif cl.ident == 'none': if cplusplus: return 'void *' return 'None' # --- namespace prefix namespace = '' if cl.module not in [mv.module, gx.modules['builtin']]: if cplusplus: namespace = cl.module.full_path() + '::' else: namespace = '::'.join(cl.module.name_list) + '::' if cplusplus: mv.module.prop_includes.add(cl.module) template_vars = cl.tvar_names() if template_vars: subtypes = [] for tvar in template_vars: vartypes = types_var_types(gx, types, tvar) ts = typestrnew(gx, vartypes, cplusplus, node, check_extmod, depth + 1, tuple_check=tuple_check, mv=mv) if tvar == var: return ts if [t[0] for t in vartypes if isinstance(t[0], python.Function)]: ident = cl.ident if ident == 'tuple2': ident = 'tuple' error.error("'%s' instance containing function reference" % ident, gx, node, warning=True) # XXX test subtypes.append(ts) else: if cl.ident in gx.cpp_keywords: return namespace + gx.ss_prefix + map(cl.ident) return namespace + map(cl.ident) ident = cl.ident # --- binary tuples if ident == 'tuple2': if subtypes[0] == subtypes[1]: ident, subtypes = 'tuple', [subtypes[0]] if ident == 'tuple2' and not cplusplus: ident = 'tuple' elif ident == 'tuple' and cplusplus: return namespace + 'tuple2' + sep[0] + subtypes[0] + ', ' + subtypes[0] + sep[1] + ptr if ident in ['frozenset', 'pyset'] and cplusplus: ident = 'set' if ident in gx.cpp_keywords: ident = gx.ss_prefix + ident # --- final type representation return namespace + ident + sep[0] + ', '.join(subtypes) + sep[1] + ptr def incompatible_assignment_rec(gx, argtypes, formaltypes, depth=0): if depth == 10: return False argclasses = types_classes(argtypes) formalclasses = types_classes(formaltypes) inttype = (python.def_class(gx, 'int_'), 0) booltype = (python.def_class(gx, 'bool_'), 0) floattype = (python.def_class(gx, 'float_'), 0) # int -> float if depth > 0 and (argtypes == set([inttype]) and floattype in formaltypes): return True # bool -> int if depth > 0 and (argtypes == set([booltype]) and inttype in formaltypes): return True # void * -> non-pointer if not argclasses and [cl for cl in formalclasses if cl.ident in ['int_', 'float_', 'bool_', 'complex']]: return True # None -> anything if len(argclasses) == 1 and python.def_class(gx, 'none') in argclasses: return False # recurse on subvars lcp = lowest_common_parents(polymorphic_cl(gx, formalclasses)) if len(lcp) != 1: # XXX return False tvars = lcp[0].tvar_names() for tvar in tvars: argvartypes = types_var_types(gx, argtypes, tvar) formalvartypes = types_var_types(gx, formaltypes, tvar) if incompatible_assignment_rec(gx, argvartypes, formalvartypes, depth + 1): return True return False shedskin-0.9.4/shedskin/FLAGS.mingw0000664000175000017500000000016412157270661016771 0ustar srepmubsrepmubCC=g++ CCFLAGS=-O2 -march=native -Wno-deprecated -Wl,--enable-auto-import $(CPPFLAGS) LFLAGS=-lgc -lpcre $(LDFLAGS) shedskin-0.9.4/shedskin/annotate.py0000664000175000017500000001112012157270661017247 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) annotate.py: annotate source code with inferred types, as *.ss.py (shedskin -a) ''' import re from compiler.ast import Const, AssTuple, AssList, Assign, AugAssign, \ Getattr, Dict, Print, Return, Printnl, Name, List, Tuple, ListComp from infer import inode from python import assign_rec from typestr import nodetypestr RE_COMMENT = re.compile(r'#[^\"\']*$') def paste(gx, source, expr, text, mv): if not expr.lineno: return if (expr, 0, 0) not in gx.cnode or inode(gx, expr).mv != mv: return # XXX line = source[expr.lineno - 1] match = RE_COMMENT.search(line) if match: line = line[:match.start()] if text: text = '# ' + text line = line.rstrip() if text and len(line) < 40: line += (40 - len(line)) * ' ' source[expr.lineno - 1] = line if text: source[expr.lineno - 1] += ' ' + text source[expr.lineno - 1] += '\n' def annotate(gx): if not gx.annotation: return for module in gx.modules.values(): if module.builtin: continue mv = module.mv merge = dict((k, v) for k, v in gx.merged_inh.items() if (k, 0, 0) in gx.cnode and inode(gx, k).mv == mv) source = open(module.filename).readlines() # --- constants/names/attributes for expr in merge: if isinstance(expr, (Const, Name)): paste(gx, source, expr, nodetypestr(gx, expr, inode(gx, expr).parent, False, mv=mv), mv) for expr in merge: if isinstance(expr, Getattr): paste(gx, source, expr, nodetypestr(gx, expr, inode(gx, expr).parent, False, mv=mv), mv) for expr in merge: if isinstance(expr, (Tuple, List, Dict)): paste(gx, source, expr, nodetypestr(gx, expr, inode(gx, expr).parent, False, mv=mv), mv) # --- instance variables funcs = mv.funcs.values() for cl in mv.classes.values(): labels = [var.name + ': ' + nodetypestr(gx, var, cl, False, mv=mv) for var in cl.vars.values() if var in merge and merge[var] and not var.name.startswith('__')] if labels: paste(gx, source, cl.node, ', '.join(labels), mv) funcs += cl.funcs.values() # --- function variables for func in funcs: if not func.node or func.node in gx.inherited: continue vars = [func.vars[f] for f in func.formals] labels = [var.name + ': ' + nodetypestr(gx, var, func, False, mv=mv) for var in vars if not var.name.startswith('__')] paste(gx, source, func.node, ', '.join(labels), mv) # --- callfuncs for callfunc, _ in mv.callfuncs: if isinstance(callfunc.node, Getattr): if not callfunc.node.__class__.__name__.startswith('FakeGetattr'): # XXX paste(gx, source, callfunc.node.expr, nodetypestr(gx, callfunc, inode(gx, callfunc).parent, False, mv=mv), mv) else: paste(gx, source, callfunc.node, nodetypestr(gx, callfunc, inode(gx, callfunc).parent, False, mv=mv), mv) # --- higher-level crap (listcomps, returns, assignments, prints) for expr in merge: if isinstance(expr, ListComp): paste(gx, source, expr, nodetypestr(gx, expr, inode(gx, expr).parent, False, mv=mv), mv) elif isinstance(expr, Return): paste(gx, source, expr, nodetypestr(gx, expr.value, inode(gx, expr).parent, False, mv=mv), mv) elif isinstance(expr, (AssTuple, AssList)): paste(gx, source, expr, nodetypestr(gx, expr, inode(gx, expr).parent, False, mv=mv), mv) elif isinstance(expr, (Print, Printnl)): paste(gx, source, expr, ', '.join(nodetypestr(gx, child, inode(gx, child).parent, False, mv=mv) for child in expr.nodes), mv) # --- assignments for expr in merge: if isinstance(expr, Assign): pairs = assign_rec(expr.nodes[0], expr.expr) paste(gx, source, expr, ', '.join(nodetypestr(gx, r, inode(gx, r).parent, False, mv=mv) for (l, r) in pairs), mv) elif isinstance(expr, AugAssign): paste(gx, source, expr, nodetypestr(gx, expr.expr, inode(gx, expr).parent, False, mv=mv), mv) # --- output annotated file (skip if no write permission) try: out = open(module.filename[:-3] + '.ss.py', 'w') out.write(''.join(source)) out.close() except IOError: pass shedskin-0.9.4/shedskin/__init__.py0000664000175000017500000001137612157270661017212 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) ''' import getopt import os.path import struct import sys import time import traceback import graph from annotate import annotate from config import GlobalInfo from cpp import generate_code from error import print_errors from infer import analyze def usage(): print """Usage: shedskin [OPTION]... FILE -a --ann Output annotated source code (.ss.py) -b --nobounds Disable bounds checking -e --extmod Generate extension module -f --flags Provide alternate Makefile flags -g --nogcwarns Disable runtime GC warnings -l --long Use long long ("64-bit") integers -m --makefile Specify alternate Makefile name -n --silent Silent mode, only show warnings -o --noassert Disable assert statements -r --random Use fast random number generator (rand()) -s --strhash Use fast string hashing algorithm (murmur) -w --nowrap Disable wrap-around checking -x --traceback Print traceback for uncaught exceptions -L --lib Add a library directory """ # -p --pypy Make extension module PyPy-compatible # -v --msvc Output MSVC-style Makefile sys.exit(1) def parse_command_line_options(): gx = GlobalInfo() # --- command-line options try: opts, args = getopt.getopt(sys.argv[1:], 'vbchef:wad:m:rolspxngL:', ['help', 'extmod', 'nobounds', 'nowrap', 'flags=', 'debug=', 'makefile=', 'random', 'noassert', 'long', 'msvc', 'ann', 'strhash', 'pypy', 'traceback', 'silent', 'nogcwarns', 'lib']) except getopt.GetoptError: usage() for opt, value in opts: if opt in ['-h', '--help']: usage() if opt in ['-b', '--nobounds']: gx.bounds_checking = False if opt in ['-e', '--extmod']: gx.extension_module = True if opt in ['-a', '--ann']: gx.annotation = True if opt in ['-d', '--debug']: gx.debug_level = int(value) if opt in ['-l', '--long']: gx.longlong = True if opt in ['-g', '--nogcwarns']: gx.gcwarns = False if opt in ['-w', '--nowrap']: gx.wrap_around_check = False if opt in ['-r', '--random']: gx.fast_random = True if opt in ['-o', '--noassert']: gx.assertions = False if opt in ['-p', '--pypy']: gx.pypy = True if opt in ['-m', '--makefile']: gx.makefile_name = value if opt in ['-n', '--silent']: gx.silent = True if opt in ['-s', '--strhash']: gx.fast_hash = True if opt in ['-v', '--msvc']: gx.msvc = True if opt in ['-x', '--traceback']: gx.backtrace = True if opt in ['-L', '--lib']: gx.libdirs = [value] + gx.libdirs if opt in ['-f', '--flags']: if not os.path.isfile(value): print "*ERROR* no such file: '%s'" % value sys.exit(1) gx.flags = value if not gx.silent: print '*** SHED SKIN Python-to-C++ Compiler 0.9.4 ***' print 'Copyright 2005-2011 Mark Dufour; License GNU GPL version 3 (See LICENSE)' print # --- some checks major, minor = sys.version_info[:2] if (major, minor) not in [(2, 4), (2, 5), (2, 6), (2, 7)]: print '*ERROR* Shed Skin is not compatible with this version of Python' sys.exit(1) if sys.platform == 'win32' and os.path.isdir('c:/mingw'): print '*ERROR* please rename or remove c:/mingw, as it conflicts with Shed Skin' sys.exit() if sys.platform == 'win32' and struct.calcsize('P') == 8 and gx.extension_module: print '*WARNING* 64-bit python may not come with necessary file to build extension module' # --- argument if len(args) != 1: usage() name = args[0] if not name.endswith('.py'): name += '.py' if not os.path.isfile(name): print "*ERROR* no such file: '%s'" % name sys.exit(1) main_module_name = os.path.splitext(name)[0] return gx, main_module_name def start(gx, main_module_name): # --- analyze & annotate t0 = time.time() analyze(gx, main_module_name) annotate(gx) generate_code(gx) print_errors() if not gx.silent: print '[elapsed time: %.2f seconds]' % (time.time() - t0) def main(): sys.setrecursionlimit(100000) gx, main_module_name = parse_command_line_options() try: start(gx, main_module_name) except KeyboardInterrupt, e: if gx.debug_level > 0: print traceback.format_exc(e) sys.exit(1) if __name__ == '__main__': main() shedskin-0.9.4/shedskin/infer.py0000664000175000017500000016015112157270661016552 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) infer.py: perform iterative type analysis we combine two techniques from the literature, to analyze both parametric polymorphism and data polymorphism adaptively. these techniques are agesen's cartesian product algorithm and plevyak's iterative flow analysis (the data polymorphic part). for details about these algorithms, see ole agesen's excellent Phd thesis. for details about the Shed Skin implementation, see mark dufour's MsC thesis. the cartesian product algorithm duplicates functions (or their graph counterpart), based on the cartesian product of possible argument types, whereas iterative flow analysis duplicates classes based on observed imprecisions at assignment points. the two integers mentioned in the graph.py description are used to keep track of duplicates along these dimensions (first class duplicate nr, then function duplicate nr). the combined technique scales reasonably well, but can explode in many cases. there are many ways to improve this. some ideas: -an iterative deepening approach, merging redundant duplicates after each deepening -add and propagate filters across variables. e.g. 'a+1; a=b' implies that a and b must be of a type that implements '__add__'. a complementary but very practical approach to (greatly) improve scalability would be to profile programs before compiling them, resulting in quite precise (lower bound) type information. type inference can then be used to 'fill in the gaps'. iterative_dataflow_analysis(): (FORWARD PHASE) -propagate types along constraint graph (propagate()) -all the while creating function duplicates using the cartesian product algorithm(cpa()) -when creating a function duplicate, fill in allocation points with correct type (ifa_seed_template()) (BACKWARD PHASE) -determine classes to be duplicated, according to found imprecision points (ifa()) -from imprecision points, follow the constraint graph (backwards) to find involved allocation points -duplicate classes, and spread them over these allocation points (CLEANUP) -quit if no further imprecision points (ifa() did not find anything) -otherwise, restore the constraint graph to its original state and restart -all the while maintaining types for each allocation point in gx.alloc_info update: we now analyze programs incrementally, adding several functions and redoing the full analysis each time. this seems to greatly help the CPA from exploding early on. ''' import random import sys from compiler.ast import Const, Node, AssAttr, Keyword, CallFunc, Getattr, Dict, List, Tuple, ListComp, Not, Compare, Name import error import graph from python import StaticClass, lookup_class_module, Function, \ Variable, lookup_var, Class, lookup_implementor, def_class from typestr import nodetypestr from virtual import analyze_virtuals INCREMENTAL = True INCREMENTAL_FUNCS = 5 INCREMENTAL_DATA = True INCREMENTAL_ALLOCS = 20 MAXITERS = 30 CPA_LIMIT = 10 class CNode: __slots__ = ['thing', 'dcpa', 'cpa', 'fakefunc', 'parent', 'defnodes', 'mv', 'constructor', 'copymetoo', 'fakert', 'in_', 'out', 'fout', 'in_list', 'callfuncs', 'nodecp'] def __init__(self, gx, thing, dcpa=0, cpa=0, parent=None, mv=None): self.gx = gx self.thing = thing self.dcpa = dcpa self.cpa = cpa self.fakefunc = None if isinstance(parent, Class): # XXX parent = None self.parent = parent self.defnodes = False # if callnode, notification nodes were made for default arguments self.mv = mv self.constructor = False # allocation site self.copymetoo = False self.fakert = False self.lambdawrapper = None self.gx.cnode[self.thing, self.dcpa, self.cpa] = self # --- in, outgoing constraints self.in_ = set() # incoming nodes self.out = set() # outgoing nodes self.fout = set() # unreal outgoing edges, used in ifa # --- iterative dataflow analysis self.in_list = 0 # node in work-list self.callfuncs = [] # callfuncs to which node is object/argument self.nodecp = set() # already analyzed cp's # XXX kill!? # --- add node to surrounding non-listcomp function if parent: # do this only once! (not when copying) while parent and isinstance(parent, Function) and parent.listcomp: parent = parent.parent if isinstance(parent, Function): if self not in parent.nodes: parent.nodes.add(self) parent.nodes_ordered.append(self) def copy(self, dcpa, cpa, worklist=None): # XXX to infer.py # if not self.mv.module.builtin: print 'copy', self if (self.thing, dcpa, cpa) in self.gx.cnode: return self.gx.cnode[self.thing, dcpa, cpa] newnode = CNode(self.gx, self.thing, dcpa, cpa, mv=self.mv) newnode.callfuncs = self.callfuncs[:] # XXX no copy? newnode.constructor = self.constructor newnode.copymetoo = self.copymetoo newnode.parent = self.parent add_to_worklist(worklist, newnode) if self.constructor or self.copymetoo or isinstance(self.thing, (Not, Compare)): # XXX XXX self.gx.types[newnode] = self.gx.types[self].copy() else: self.gx.types[newnode] = set() return newnode def types(self): if self in self.gx.types: return self.gx.types[self] else: return set() # XXX def __repr__(self): return repr((self.thing, self.dcpa, self.cpa)) def DEBUG(gx, level): return gx.debug_level >= level def nrargs(gx, node): if inode(gx, node).lambdawrapper: return inode(gx, node).lambdawrapper.largs return len(node.args) def called(func): return bool([cpas for cpas in func.cp.values() if cpas]) def get_types(gx, expr, node, merge): types = set() if merge: if expr.node in merge: types = merge[expr.node] elif node: node = (expr.node, node.dcpa, node.cpa) if node in gx.cnode: types = gx.cnode[node].types() return types def is_anon_callable(gx, expr, node, merge=None): types = get_types(gx, expr, node, merge) anon = bool([t for t in types if isinstance(t[0], Function)]) call = bool([t for t in types if isinstance(t[0], Class) and '__call__' in t[0].funcs]) return anon, call def parent_func(gx, thing): parent = inode(gx, thing).parent while parent: if not isinstance(parent, Function) or not parent.listcomp: if not isinstance(parent, StaticClass): return parent parent = parent.parent def analyze_args(gx, expr, func, node=None, skip_defaults=False, merge=None): objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func = analyze_callfunc(gx, expr, node, merge) args = [] kwdict = {} for a in expr.args: if isinstance(a, Keyword): kwdict[a.name] = a.expr else: args.append(a) formal_args = func.formals[:] if func.node.varargs: formal_args = formal_args[:-1] default_start = len(formal_args) - len(func.defaults) if ident in ['__getattr__', '__setattr__']: # property? args = args[1:] if (method_call or constructor) and not (parent_constr or anon_func): # XXX args = [None] + args argnr = 0 actuals, formals, defaults = [], [], [] missing = False for i, formal in enumerate(formal_args): if formal in kwdict: actuals.append(kwdict[formal]) formals.append(formal) elif formal.startswith('__kw_') and formal[5:] in kwdict: actuals.insert(0, kwdict[formal[5:]]) formals.insert(0, formal) elif argnr < len(args) and not formal.startswith('__kw_'): actuals.append(args[argnr]) argnr += 1 formals.append(formal) elif i >= default_start: if not skip_defaults: default = func.defaults[i - default_start] if formal.startswith('__kw_'): actuals.insert(0, default) formals.insert(0, formal) else: actuals.append(default) formals.append(formal) defaults.append(default) else: missing = True extra = args[argnr:] _error = (missing or extra) and not func.node.varargs and not func.node.kwargs and not expr.star_args and func.lambdanr is None and expr not in gx.lambdawrapper # XXX if func.node.varargs: for arg in extra: actuals.append(arg) formals.append(func.formals[-1]) return actuals, formals, defaults, extra, _error def connect_actual_formal(gx, expr, func, parent_constr=False, merge=None): pairs = [] actuals = [a for a in expr.args if not isinstance(a, Keyword)] if isinstance(func.parent, Class): formals = [f for f in func.formals if f != 'self'] else: formals = [f for f in func.formals] if parent_constr: actuals = actuals[1:] skip_defaults = False # XXX investigate and further narrow down cases where we want to skip if (func.mv.module.ident in ['time', 'string', 'collections', 'bisect', 'array', 'math', 'cStringIO', 'getopt']) or \ (func.mv.module.ident == 'random' and func.ident == 'randrange') or\ (func.mv.module.ident == 'builtin' and func.ident not in ('sort', 'sorted', 'min', 'max', '__print')): skip_defaults = True actuals, formals, _, extra, _error = analyze_args(gx, expr, func, skip_defaults=skip_defaults, merge=merge) for (actual, formal) in zip(actuals, formals): if not (isinstance(func.parent, Class) and formal == 'self'): pairs.append((actual, func.vars[formal])) return pairs, len(extra), _error # --- return list of potential call targets def callfunc_targets(gx, node, merge): objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func = analyze_callfunc(gx, node, merge=merge) funcs = [] if node.node in merge and [t for t in merge[node.node] if isinstance(t[0], Function)]: # anonymous function call funcs = [t[0] for t in merge[node.node] if isinstance(t[0], Function)] elif constructor: if ident in ('list', 'tuple', 'set', 'frozenset') and nrargs(gx, node) == 1: funcs = [constructor.funcs['__inititer__']] elif (ident, nrargs(gx, node)) in (('dict', 1), ('defaultdict', 2)): # XXX merge infer.redirect funcs = [constructor.funcs['__initdict__']] # XXX __inititer__? elif sys.platform == 'win32' and '__win32__init__' in constructor.funcs: funcs = [constructor.funcs['__win32__init__']] elif '__init__' in constructor.funcs: funcs = [constructor.funcs['__init__']] elif parent_constr: if ident != '__init__': cl = inode(gx, node).parent.parent funcs = [cl.funcs[ident]] elif direct_call: funcs = [direct_call] elif method_call: classes = set(t[0] for t in merge[objexpr] if isinstance(t[0], Class)) funcs = [cl.funcs[ident] for cl in classes if ident in cl.funcs] return funcs # --- analyze call expression: namespace, method call, direct call/constructor.. def analyze_callfunc(gx, node, node2=None, merge=None): # XXX generate target list XXX uniform Variable system! XXX node2, merge? # print 'analyze callnode', node, inode(gx, node).parent cnode = inode(gx, node) mv = cnode.mv namespace, objexpr, method_call, parent_constr = mv.module, None, False, False constructor, direct_call, ident = None, None, None # anon func call XXX refactor as __call__ method call below anon_func, is_callable = is_anon_callable(gx, node, node2, merge) if is_callable: method_call, objexpr, ident = True, node.node, '__call__' return objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func # method call if isinstance(node.node, Getattr): objexpr, ident = node.node.expr, node.node.attrname cl, module = lookup_class_module(objexpr, mv, cnode.parent) if cl: # staticmethod call if ident in cl.staticmethods: direct_call = cl.funcs[ident] return objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func # ancestor call elif ident not in ['__setattr__', '__getattr__'] and cnode.parent: thiscl = cnode.parent.parent if isinstance(thiscl, Class) and cl.ident in (x.ident for x in thiscl.ancestors_upto(None)): # XXX if lookup_implementor(cl, ident): parent_constr = True ident = ident + lookup_implementor(cl, ident) + '__' # XXX change data structure return objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func if module: # XXX elif? namespace, objexpr = module, None else: method_call = True elif isinstance(node.node, Name): ident = node.node.name # direct [constructor] call if isinstance(node.node, Name) or namespace != mv.module: if isinstance(node.node, Name): if lookup_var(ident, cnode.parent, mv=mv): return objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func if ident in namespace.mv.classes: constructor = namespace.mv.classes[ident] elif ident in namespace.mv.funcs: direct_call = namespace.mv.funcs[ident] elif ident in namespace.mv.ext_classes: constructor = namespace.mv.ext_classes[ident] elif ident in namespace.mv.ext_funcs: direct_call = namespace.mv.ext_funcs[ident] else: if namespace != mv.module: return objexpr, ident, None, False, None, False, False return objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func # --- merge constraint network along combination of given dimensions (dcpa, cpa, inheritance) # e.g. for annotation we merge everything; for code generation, we might want to create specialized code def merged(gx, nodes, inheritance=False): merge = {} if inheritance: # XXX do we really need this crap mergeinh = merged(gx, [n for n in nodes if n.thing in gx.inherited]) mergenoinh = merged(gx, [n for n in nodes if not n.thing in gx.inherited]) for node in nodes: # --- merge node types sortdefault = merge.setdefault(node.thing, set()) sortdefault.update(gx.types[node]) # --- merge inheritance nodes if inheritance: inh = gx.inheritance_relations.get(node.thing, []) # merge function variables with their inherited versions (we don't customize!) if isinstance(node.thing, Variable) and isinstance(node.thing.parent, Function): var = node.thing for inhfunc in gx.inheritance_relations.get(var.parent, []): if var.name in inhfunc.vars: if inhfunc.vars[var.name] in mergenoinh: sortdefault.update(mergenoinh[inhfunc.vars[var.name]]) for inhvar in gx.inheritance_temp_vars.get(var, []): # XXX more general if inhvar in mergenoinh: sortdefault.update(mergenoinh[inhvar]) # node is not a function variable else: for n in inh: if n in mergeinh: # XXX ook mergenoinh? sortdefault.update(mergeinh[n]) return merge def inode(gx, node): return gx.cnode[node, 0, 0] def add_constraint(gx, a, b, worklist=None): gx.constraints.add((a, b)) in_out(a, b) add_to_worklist(worklist, a) def in_out(a, b): a.out.add(b) b.in_.add(a) def add_to_worklist(worklist, node): # XXX to infer.py if worklist is not None and not node.in_list: worklist.append(node) node.in_list = 1 def class_copy(gx, cl, dcpa): for var in cl.vars.values(): # XXX if not inode(gx, var) in gx.types: continue # XXX research later inode(gx, var).copy(dcpa, 0) gx.types[gx.cnode[var, dcpa, 0]] = inode(gx, var).types().copy() for n in inode(gx, var).in_: # XXX if isinstance(n.thing, Const): add_constraint(gx, n, gx.cnode[var, dcpa, 0]) for func in cl.funcs.values(): if cl.mv.module.ident == 'builtin' and cl.ident != '__iter' and func.ident == '__iter__': # XXX hack for __iter__:__iter() itercl = def_class(gx, '__iter') gx.alloc_info[func.ident, ((cl, dcpa),), func.returnexpr[0]] = (itercl, itercl.dcpa) class_copy(gx, itercl, dcpa) itercl.dcpa += 1 func_copy(gx, func, dcpa, 0) # --- use dcpa=0,cpa=0 mold created by module visitor to duplicate function def func_copy(gx, func, dcpa, cpa, worklist=None, cart=None): # print 'funccopy', func, cart, dcpa, cpa # --- copy local end points of each constraint for (a, b) in func.constraints: if not (isinstance(a.thing, Variable) and parent_func(gx, a.thing) != func) and a.dcpa == 0: a = a.copy(dcpa, cpa, worklist) if not (isinstance(b.thing, Variable) and parent_func(gx, b.thing) != func) and b.dcpa == 0: b = b.copy(dcpa, cpa, worklist) add_constraint(gx, a, b, worklist) # --- copy other nodes for node in func.nodes: node.copy(dcpa, cpa, worklist) # --- iterative flow analysis: seed allocation sites in new template ifa_seed_template(gx, func, cart, dcpa, cpa, worklist) def print_typeset(types): l = list(types.items()) l.sort(lambda x, y: cmp(repr(x[0]), repr(y[0]))) for uh in l: if not uh[0].mv.module.builtin: print repr(uh[0]) + ':', uh[1] # , uh[0].parent print def print_state(gx): # print 'state:' print_typeset(gx.types) def print_constraints(gx): # print 'constraints:' l = list(gx.constraints) l.sort(lambda x, y: cmp(repr(x[0]), repr(y[0]))) for (a, b) in l: if not (a.mv.module.builtin and b.mv.module.builtin): print a, '->', b if not a in gx.types or not b in gx.types: print 'NOTYPE', a in gx.types, b in gx.types print # --- iterative dataflow analysis def propagate(gx): if DEBUG(gx, 1): print 'propagate' # --- initialize working sets worklist = [] changed = set() for node in gx.types: if gx.types[node]: add_to_worklist(worklist, node) expr = node.thing if (isinstance(expr, CallFunc) and not expr.args) or expr in gx.lambdawrapper: # XXX changed.add(node) for node in changed: cpa(gx, node, worklist) builtins = set(gx.builtins) types = gx.types # --- iterative dataflow analysis while worklist: callnodes = set() while worklist: a = worklist.pop(0) a.in_list = 0 for callfunc in a.callfuncs: t = (callfunc, a.dcpa, a.cpa) if t in gx.cnode: callnodes.add(gx.cnode[t]) for b in a.out.copy(): # XXX can change...? # for builtin types, the set of instance variables is known, so do not flow into non-existent ones # XXX ifa if isinstance(b.thing, Variable) and isinstance(b.thing.parent, Class): parent_ident = b.thing.parent.ident if parent_ident in builtins: if parent_ident in ['int_', 'float_', 'str_', 'none', 'bool_']: continue elif parent_ident in ['list', 'tuple', 'frozenset', 'set', 'file', '__iter', 'deque', 'array'] and b.thing.name != 'unit': continue elif parent_ident in ('dict', 'defaultdict') and b.thing.name not in ['unit', 'value']: continue elif parent_ident == 'tuple2' and b.thing.name not in ['unit', 'first', 'second']: continue typesa = types[a] typesb = types[b] oldsize = len(typesb) typesb.update(typesa) if len(typesb) > oldsize: add_to_worklist(worklist, b) for callnode in callnodes: cpa(gx, callnode, worklist) # --- determine cartesian product of possible function and argument types def possible_functions(gx, node, analysis): expr = node.thing # --- determine possible target functions objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func = analysis funcs = [] if anon_func: # anonymous call types = gx.cnode[expr.node, node.dcpa, node.cpa].types() types = [t for t in types if isinstance(t[0], Function)] # XXX XXX analyse per t, sometimes class, sometimes function.. if list(types)[0][0].parent: # method reference XXX merge below? funcs = [(f[0], f[1], (f[0].parent, f[1])) for f in types] # node.dcpa: connect to right dcpa duplicate version else: # function reference funcs = [(f[0], f[1], None) for f in types] # function call: only one version; no objtype elif constructor: funcs = [(t[0].funcs['__init__'], t[1], t) for t in node.types() if '__init__' in t[0].funcs] elif parent_constr: objtypes = gx.cnode[lookup_var('self', node.parent, mv=node.mv), node.dcpa, node.cpa].types() funcs = [(t[0].funcs[ident], t[1], None) for t in objtypes if ident in t[0].funcs] elif direct_call: funcs = [(direct_call, 0, None)] elif method_call: objtypes = gx.cnode[objexpr, node.dcpa, node.cpa].types() objtypes = [t for t in objtypes if not isinstance(t[0], Function)] # XXX funcs = [(t[0].funcs[ident], t[1], t) for t in objtypes if ident in t[0].funcs and not (isinstance(t[0], Class) and ident in t[0].staticmethods)] return funcs def possible_argtypes(gx, node, funcs, analysis, worklist): expr = node.thing objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func = analysis if funcs: func = funcs[0][0] # XXX args = [] if expr.star_args: # XXX args = [expr.star_args] elif funcs and not func.node: # XXX getattr, setattr args = expr.args elif funcs: actuals, formals, used_defaults, varargs, _ = analyze_args(gx, expr, func, node) if not node.defnodes: for i, default in enumerate(used_defaults): defnode = CNode(gx, (inode(gx, node.thing), i), node.dcpa, node.cpa, parent=func, mv=node.mv) gx.types[defnode] = set() defnode.callfuncs.append(node.thing) add_constraint(gx, gx.cnode[default, 0, 0], defnode, worklist) # XXX bad place node.defnodes = True for act, form in zip(actuals, formals): if parent_constr or not (isinstance(func.parent, Class) and form == 'self'): # XXX merge args.append(act) argtypes = [] for arg in args: if (arg, node.dcpa, node.cpa) in gx.cnode: argtypes.append(gx.cnode[arg, node.dcpa, node.cpa].types()) else: argtypes.append(inode(gx, arg).types()) # XXX def arg? # store arg count for wrappers to builtin refs if funcs and (func.lambdawrapper or node.thing in gx.lambdawrapper): while argtypes and not argtypes[-1]: argtypes = argtypes[:-1] if func.lambdawrapper: if expr.star_args and node.parent and node.parent.node.varargs: func.largs = node.parent.xargs[node.dcpa, node.cpa] - len(node.parent.formals) + 1 else: func.largs = len(argtypes) return argtypes def product(*lists): if not lists: return [()] result = [] prod = product(*lists[:-1]) for x in prod: for y in lists[-1]: result.append(x + (y,)) return result def cartesian_product(gx, node, analysis, worklist): funcs = possible_functions(gx, node, analysis) if not funcs: return [] argtypes = possible_argtypes(gx, node, funcs, analysis, worklist) alltypes = [funcs] + argtypes return product(*alltypes) def redirect(gx, c, dcpa, func, callfunc, ident, callnode, direct_call, constructor): # redirect based on number of arguments (__%s%d syntax in builtins) if func.mv.module.builtin: if isinstance(func.parent, Class): funcs = func.parent.funcs else: funcs = func.mv.funcs redir = '__%s%d' % (func.ident, len([kwarg for kwarg in callfunc.args if not isinstance(kwarg, Keyword)])) func = funcs.get(redir, func) # filter if direct_call and ident == 'filter': clnames = [x[0].ident for x in c if isinstance(x[0], Class)] if 'str_' in clnames or 'tuple' in clnames or 'tuple2' in clnames: func = func.mv.funcs['__' + ident] # staticmethod if isinstance(func.parent, Class) and func.ident in func.parent.staticmethods: dcpa = 1 # dict.__init__ if constructor and (ident, nrargs(gx, callfunc)) in (('dict', 1), ('defaultdict', 2)): clnames = [x[0].ident for x in c if isinstance(x[0], Class)] if 'dict' in clnames or 'defaultdict' in clnames: func = list(callnode.types())[0][0].funcs['__initdict__'] else: func = list(callnode.types())[0][0].funcs['__inititer__'] # dict.update if func.ident == 'update' and isinstance(func.parent, Class) and func.parent.ident in ('dict', 'defaultdict'): clnames = [x[0].ident for x in c if isinstance(x[0], Class)] if not ('dict' in clnames or 'defaultdict' in clnames): func = func.parent.funcs['updateiter'] # list, tuple if constructor and ident in ('list', 'tuple', 'set', 'frozenset') and nrargs(gx, callfunc) == 1: func = list(callnode.types())[0][0].funcs['__inititer__'] # XXX use __init__? # array if constructor and ident == 'array' and isinstance(callfunc.args[0], Const): typecode = callfunc.args[0].value array_type = None if typecode in 'bBhHiIlL': array_type = 'int' elif typecode == 'c': array_type = 'str' elif typecode in 'fd': array_type = 'float' if array_type is not None: func = list(callnode.types())[0][0].funcs['__init_%s__' % array_type] # tuple2.__getitem__(0/1) -> __getfirst__/__getsecond__ if (isinstance(callfunc.node, Getattr) and callfunc.node.attrname == '__getitem__' and isinstance(callfunc.args[0], Const) and callfunc.args[0].value in (0, 1) and func.parent.mv.module.builtin and func.parent.ident == 'tuple2'): if callfunc.args[0].value == 0: func = func.parent.funcs['__getfirst__'] else: func = func.parent.funcs['__getsecond__'] # property if isinstance(callfunc.node, Getattr) and callfunc.node.attrname in ['__setattr__', '__getattr__']: if isinstance(func.parent, Class) and callfunc.args and callfunc.args[0].value in func.parent.properties: arg = callfunc.args[0].value if callfunc.node.attrname == '__setattr__': func = func.parent.funcs[func.parent.properties[arg][1]] else: func = func.parent.funcs[func.parent.properties[arg][0]] c = c[1:] # win32 if sys.platform == 'win32' and func.mv.module.builtin and isinstance(func.parent, Class) and '__win32' + func.ident in func.parent.funcs: func = func.parent.funcs['__win32' + func.ident] return c, dcpa, func # --- cartesian product algorithm; adds interprocedural constraints def cpa(gx, callnode, worklist): analysis = analyze_callfunc(gx, callnode.thing, callnode) cp = cartesian_product(gx, callnode, analysis, worklist) if not cp: return if len(cp) > gx.cpa_limit and not gx.cpa_clean: gx.cpa_limited = True return [] objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func = analysis # --- iterate over argument type combinations for c in cp: (func, dcpa, objtype), c = c[0], c[1:] if INCREMENTAL: if not func.mv.module.builtin and func not in gx.added_funcs_set and not func.ident in ['__getattr__', '__setattr__']: if INCREMENTAL_DATA: if gx.added_allocs >= INCREMENTAL_ALLOCS: continue else: if gx.added_funcs >= INCREMENTAL_FUNCS: continue gx.added_funcs += 1 gx.added_funcs_set.add(func) if DEBUG(gx, 1): print 'adding', func if objtype: objtype = (objtype,) else: objtype = () # redirect in special cases callfunc = callnode.thing c, dcpa, func = redirect(gx, c, dcpa, func, callfunc, ident, callnode, direct_call, constructor) # already connected to template if (func,) + objtype + c in callnode.nodecp: continue callnode.nodecp.add((func,) + objtype + c) # create new template if not dcpa in func.cp or not c in func.cp[dcpa]: create_template(gx, func, dcpa, c, worklist) cpa = func.cp[dcpa][c] func.xargs[dcpa, cpa] = len(c) # __getattr__, __setattr__ if connect_getsetattr(gx, func, callnode, callfunc, dcpa, worklist): continue # connect actuals and formals actuals_formals(gx, callfunc, func, callnode, dcpa, cpa, objtype + c, analysis, worklist) # connect call and return expressions if func.retnode and not constructor: retnode = gx.cnode[func.retnode.thing, dcpa, cpa] add_constraint(gx, retnode, callnode, worklist) def connect_getsetattr(gx, func, callnode, callfunc, dcpa, worklist): if (isinstance(callfunc.node, Getattr) and callfunc.node.attrname in ['__setattr__', '__getattr__'] and not (isinstance(func.parent, Class) and callfunc.args and callfunc.args[0].value in func.parent.properties)): varname = callfunc.args[0].value parent = func.parent var = default_var(gx, varname, parent, worklist, mv=parent.module.mv) # XXX always make new var?? inode(gx, var).copy(dcpa, 0, worklist) if not gx.cnode[var, dcpa, 0] in gx.types: gx.types[gx.cnode[var, dcpa, 0]] = set() gx.cnode[var, dcpa, 0].mv = parent.module.mv # XXX move into default_var if callfunc.node.attrname == '__setattr__': add_constraint(gx, gx.cnode[callfunc.args[1], callnode.dcpa, callnode.cpa], gx.cnode[var, dcpa, 0], worklist) else: add_constraint(gx, gx.cnode[var, dcpa, 0], callnode, worklist) return True return False def create_template(gx, func, dcpa, c, worklist): # --- unseen cartesian product: create new template if not dcpa in func.cp: func.cp[dcpa] = {} func.cp[dcpa][c] = cpa = len(func.cp[dcpa]) # XXX +1 if DEBUG(gx, 2) and not func.mv.module.builtin and not func.ident in ['__getattr__', '__setattr__']: print 'template', (func, dcpa), c gx.templates += 1 func_copy(gx, func, dcpa, cpa, worklist, c) def actuals_formals(gx, expr, func, node, dcpa, cpa, types, analysis, worklist): objexpr, ident, direct_call, method_call, constructor, parent_constr, anon_func = analysis if expr.star_args: # XXX only in lib/ formals = func.formals actuals = len(formals) * [expr.star_args] types = len(formals) * types else: actuals, formals, _, varargs, _error = analyze_args(gx, expr, func, node) if _error: return for (actual, formal, formaltype) in zip(actuals, formals, types): formalnode = gx.cnode[func.vars[formal], dcpa, cpa] if formaltype[1] != 0: # ifa: remember dataflow information for non-simple types if actual is None: if constructor: objexpr = node.thing if method_call or constructor: formalnode.in_.add(gx.cnode[objexpr, node.dcpa, node.cpa]) else: if actual in func.defaults: formalnode.in_.add(gx.cnode[actual, 0, 0]) else: formalnode.in_.add(gx.cnode[actual, node.dcpa, node.cpa]) gx.types[formalnode].add(formaltype) add_to_worklist(worklist, formalnode) # --- iterative flow analysis: after each iteration, detect imprecisions, and split involved contours def ifa(gx): if DEBUG(gx, 1): print 'ifa' split = [] # [(set of creation nodes, new type number), ..] allcsites = {} for n, types in gx.types.iteritems(): if not n.in_: for (cl, dcpa) in types: allcsites.setdefault((cl, dcpa), set()).add(n) for cl in ifa_classes_to_split(gx): if DEBUG(gx, 3): print 'IFA: --- class %s ---' % cl.ident cl.newdcpa = cl.dcpa vars = [cl.vars[name] for name in cl.tvar_names() if name in cl.vars] classes_nr, nr_classes = ifa_class_types(gx, cl, vars) for dcpa in range(1, cl.dcpa): if ifa_split_vars(gx, cl, dcpa, vars, nr_classes, classes_nr, split, allcsites) is not None: if DEBUG(gx, 3): print 'IFA found splits, return' return split if DEBUG(gx, 3): print 'IFA final return' return split def ifa_split_vars(gx, cl, dcpa, vars, nr_classes, classes_nr, split, allcsites): for (varnum, var) in enumerate(vars): if not (var, dcpa, 0) in gx.cnode: continue node = gx.cnode[var, dcpa, 0] creation_points, paths, assignsets, allnodes, csites, emptycsites = ifa_flow_graph(gx, cl, dcpa, node, allcsites) if DEBUG(gx, 3): print 'IFA visit var %s.%s, %d, csites %d' % (cl.ident, var.name, dcpa, len(csites)) if len(csites) + len(emptycsites) == 1: continue if ((len(merge_simple_types(gx, gx.types[node])) > 1 and len(assignsets) > 1) or (assignsets and emptycsites)): # XXX move to split_no_conf ifa_split_no_confusion(gx, cl, dcpa, varnum, classes_nr, nr_classes, csites, emptycsites, allnodes, split) if split: break for node in allnodes: if not ifa_confluence_point(node, creation_points): continue if not node.thing.formal_arg and not isinstance(node.thing.parent, Class): continue remaining = ifa_determine_split(node, allnodes) if len(remaining) < 2 or len(remaining) >= 10: continue # --- if it exists, perform actual splitting if DEBUG(gx, 3): print 'IFA normal split, remaining:', len(remaining) for splitsites in remaining[1:]: ifa_split_class(cl, dcpa, splitsites, split) return split # --- try to partition csites across paths prt = {} for c in csites: ts = set() for p in c.paths: ts.update(p) ts = frozenset(ts) if ts not in prt: prt[ts] = [] prt[ts].append(c) if len(prt) > 1: if DEBUG(gx, 3): print 'IFA partition csites:', prt.values()[0] ifa_split_class(cl, dcpa, prt.values()[0], split) # --- if all else fails, perform wholesale splitting elif len(paths) > 1 and 1 < len(csites) < 10: if DEBUG(gx, 3): print 'IFA wholesale splitting, csites:', len(csites) for csite in csites[1:]: ifa_split_class(cl, dcpa, [csite], split) return split def ifa_split_no_confusion(gx, cl, dcpa, varnum, classes_nr, nr_classes, csites, emptycsites, allnodes, split): '''creation sites on single path: split them off, possibly reusing contour''' attr_types = list(nr_classes[dcpa]) noconf = set([n for n in csites if len(n.paths) == 1] + emptycsites) others = len(csites) + len(emptycsites) - len(noconf) subtype_csites = {} for node in noconf: if node.paths: assign_set = node.paths[0] else: assign_set = frozenset() if attr_types[varnum] == assign_set: others += 1 else: subtype = attr_types[:] subtype[varnum] = assign_set subtype = tuple(subtype) try: subtype_csites[subtype].append(node) except KeyError: subtype_csites[subtype] = [node] items = subtype_csites.items() if not others: items = items[1:] for subtype, csites in subtype_csites.iteritems(): # XXX items? if subtype in classes_nr: # reuse contour nr = classes_nr[subtype] split.append((cl, dcpa, csites, nr)) cl.splits[nr] = dcpa else: # create new contour classes_nr[subtype] = cl.newdcpa ifa_split_class(cl, dcpa, csites, split) if DEBUG(gx, 3) and subtype_csites: print 'IFA found simple split', subtype_csites.keys() def ifa_class_types(gx, cl, vars): ''' create table for previously deduced types ''' classes_nr, nr_classes = {}, {} for dcpa in range(1, cl.dcpa): attr_types = [] # XXX merge with ifa_merge_contours.. sep func? for var in vars: if (var, dcpa, 0) in gx.cnode: attr_types.append(merge_simple_types(gx, gx.cnode[var, dcpa, 0].types())) else: attr_types.append(frozenset()) attr_types = tuple(attr_types) if DEBUG(gx, 3) and [x for x in attr_types if x]: print 'IFA', str(dcpa) + ':', zip([var.name for var in vars], map(list, attr_types)) nr_classes[dcpa] = attr_types classes_nr[attr_types] = dcpa return classes_nr, nr_classes def ifa_determine_split(node, allnodes): ''' determine split along incoming dataflow edges ''' remaining = [incoming.csites.copy() for incoming in node.in_ if incoming in allnodes] # --- try to clean out larger collections, if subsets are in smaller ones for (i, seti) in enumerate(remaining): for setj in remaining[i + 1:]: in_both = seti.intersection(setj) if in_both: if len(seti) > len(setj): seti -= in_both else: setj -= in_both remaining = [setx for setx in remaining if setx] return remaining def ifa_classes_to_split(gx): ''' setup classes to perform splitting on ''' classes = [] for ident in ['list', 'tuple', 'tuple2', 'dict', 'set', 'frozenset', 'deque', 'defaultdict', '__iter', 'array']: for cl in gx.allclasses: if cl.mv.module.builtin and cl.ident == ident: cl.splits = {} classes.append(cl) break random.shuffle(classes) return classes def ifa_confluence_point(node, creation_points): ''' determine if node is confluence point ''' if len(node.in_) > 1 and isinstance(node.thing, Variable): for csite in node.csites: occ = [csite in crpoints for crpoints in creation_points.values()].count(True) if occ > 1: return True return False def ifa_flow_graph(gx, cl, dcpa, node, allcsites): creation_points, paths, assignsets = {}, {}, {} allnodes = set() csites = [] # --- determine assignment sets for a in node.in_: types = gx.types[a] if types: if a.thing in gx.assign_target: # XXX *args target = gx.cnode[gx.assign_target[a.thing], a.dcpa, a.cpa] # print 'target', a, target, types assignsets.setdefault(merge_simple_types(gx, types), []).append(target) # --- determine backflow paths and creation points per assignment set for assign_set, targets in assignsets.iteritems(): path = backflow_path(gx, targets, (cl, dcpa)) paths[assign_set] = path allnodes.update(path) alloc = [n for n in path if not n.in_] creation_points[assign_set] = alloc # --- per node, determine paths it is located on for n in allnodes: n.paths = [] for assign_set, path in paths.iteritems(): for n in path: n.paths.append(assign_set) # --- for each node, determine creation points that 'flow' through it for n in allnodes: n.csites = set() if not n.in_: n.csites.add(n) csites.append(n) flow_creation_sites(csites, allnodes) # csites not flowing to any assignment allcsites2 = allcsites.get((cl, dcpa), set()) emptycsites = list(allcsites2 - set(csites)) for n in emptycsites: n.paths = [] return creation_points, paths, assignsets, allnodes, csites, emptycsites def ifa_split_class(cl, dcpa, things, split): split.append((cl, dcpa, things, cl.newdcpa)) cl.splits[cl.newdcpa] = dcpa cl.newdcpa += 1 def update_progressbar(gx, perc): if not gx.silent: print '\r%s%d%%' % (int(perc * 32) * '*', 100 * perc), if DEBUG(gx, 1): print else: sys.stdout.flush() # --- cartesian product algorithm (cpa) & iterative flow analysis (ifa) def iterative_dataflow_analysis(gx): if not gx.silent: print '[analyzing types..]' backup = backup_network(gx) gx.orig_types = {} for n, t in gx.types.iteritems(): gx.orig_types[n] = t if INCREMENTAL: update_progressbar(gx, 0) gx.added_funcs = INCREMENTAL_FUNCS # analyze root of callgraph in first round gx.added_funcs_set = set() gx.added_allocs = 0 gx.added_allocs_set = set() gx.cpa_limit = CPA_LIMIT gx.cpa_clean = False while True: gx.iterations += 1 gx.total_iterations += 1 maxiter = (gx.iterations == MAXITERS) if DEBUG(gx, 1): print '\n*** iteration %d ***' % gx.iterations # --- propagate using cartesian product algorithm gx.new_alloc_info = {} # print 'table' # print '\n'.join([repr(e)+': '+repr(l) for e,l in gx.alloc_info.items()]) gx.cpa_limited = False propagate(gx) gx.alloc_info = gx.new_alloc_info if gx.cpa_limited: if DEBUG(gx, 1): print 'CPA limit %d reached!' % gx.cpa_limit else: gx.cpa_clean = True # --- ifa: detect conflicting assignments to instance variables, and split contours to resolve these split = ifa(gx) if split: if DEBUG(gx, 1): print '%d splits' % len(split) elif DEBUG(gx, 3): print 'IFA splits', [(s[0], s[1], s[3]) for s in split] if not split or maxiter: if DEBUG(gx, 1) and not maxiter: print 'no splits' if INCREMENTAL: allfuncs = len([f for f in gx.allfuncs if not f.mv.module.builtin and not [start for start in ('__iadd__', '__imul__', '__str__', '__hash__') if f.ident.startswith(start)]]) perc = 1.0 if allfuncs: perc = min(len(gx.added_funcs_set) / float(allfuncs), 1.0) update_progressbar(gx, perc) if maxiter: print '\n*WARNING* reached maximum number of iterations' gx.maxhits += 1 if gx.maxhits == 3: return gx.cpa_clean = False if INCREMENTAL and (gx.added_funcs or gx.added_allocs): gx.added_funcs = 0 gx.added_allocs = 0 gx.iterations = 0 elif gx.cpa_limited: gx.cpa_limit *= 2 gx.iterations = 0 else: if INCREMENTAL: update_progressbar(gx, 1.0) if DEBUG(gx, 1): print '\niterations:', gx.total_iterations, 'templates:', gx.templates elif not gx.silent: print return if not INCREMENTAL and not DEBUG(gx, 1): sys.stdout.write('*') sys.stdout.flush() # --- update alloc info table for split contours for cl, dcpa, nodes, newnr in split: for n in nodes: parent = parent_func(gx, n.thing) if parent: if n.dcpa in parent.cp: for cart, cpa in parent.cp[n.dcpa].items(): # XXX not very fast if cpa == n.cpa: if parent.parent and isinstance(parent.parent, Class): # self cart = ((parent.parent, n.dcpa),) + cart gx.alloc_info[parent.ident, cart, n.thing] = (cl, newnr) break beforetypes = backup[0] # --- clean out constructor node types in functions, possibly to be seeded again for node in beforetypes: func = parent_func(gx, node.thing) if isinstance(func, Function): if node.constructor and isinstance(node.thing, (List, Dict, Tuple, ListComp, CallFunc)): beforetypes[node] = set() # --- create new class types, and seed global nodes for cl, dcpa, nodes, newnr in split: if newnr == cl.dcpa: class_copy(gx, cl, newnr) cl.dcpa += 1 # print 'split off', nodes, newnr for n in nodes: if not parent_func(gx, n.thing): beforetypes[n] = set([(cl, newnr)]) # --- restore network restore_network(gx, backup) # --- seed allocation sites in newly created templates (called by function.copy()) def ifa_seed_template(gx, func, cart, dcpa, cpa, worklist): if cart is not None: # (None means we are not in the process of propagation) # print 'funccopy', func.ident #, func.nodes if isinstance(func.parent, Class): # self cart = ((func.parent, dcpa),) + cart added = gx.added_allocs_set added_new = 0 for node in func.nodes_ordered: if node.constructor and isinstance(node.thing, (List, Dict, Tuple, ListComp, CallFunc)): if node.thing not in added: if INCREMENTAL_DATA and not func.mv.module.builtin: if gx.added_allocs >= INCREMENTAL_ALLOCS: continue added_new += 1 gx.added_allocs += 1 added.add(node.thing) # --- contour is specified in alloc_info parent = node.parent while isinstance(parent.parent, Function): parent = parent.parent alloc_id = (parent.ident, cart, node.thing) # XXX ident? alloc_node = gx.cnode[node.thing, dcpa, cpa] if alloc_id in gx.alloc_info: pass # print 'specified' # print 'specified', func.ident, cart, alloc_node, alloc_node.callfuncs, gx.alloc_info[alloc_id] # --- contour is newly split: copy allocation type for 'mother' contour; modify alloc_info else: mother_alloc_id = alloc_id for (id, c, thing) in gx.alloc_info: if id == parent.ident and thing is node.thing: for a, b in zip(cart, c): if a != b and not (isinstance(a[0], Class) and a[0] is b[0] and a[1] in a[0].splits and a[0].splits[a[1]] == b[1]): break else: mother_alloc_id = (id, c, thing) break # print 'not specified.. mother id:', mother_alloc_id if mother_alloc_id in gx.alloc_info: gx.alloc_info[alloc_id] = gx.alloc_info[mother_alloc_id] # print 'mothered', alloc_node, gx.alloc_info[mother_alloc_id] elif gx.orig_types[node]: # empty constructors that do not flow to assignments have no type # print 'no mother', func.ident, cart, mother_alloc_id, alloc_node, gx.types[node] gx.alloc_info[alloc_id] = list(gx.orig_types[node])[0] else: # print 'oh boy' for (id, c, thing) in gx.alloc_info: # XXX vhy? if id == parent.ident and thing is node.thing: mother_alloc_id = (id, c, thing) gx.alloc_info[alloc_id] = gx.alloc_info[mother_alloc_id] break if alloc_id in gx.alloc_info: gx.new_alloc_info[alloc_id] = gx.alloc_info[alloc_id] gx.types[alloc_node] = set() # print 'seeding..', alloc_node, gx.alloc_info[alloc_id], alloc_node.thing in gx.empty_constructors gx.types[alloc_node].add(gx.alloc_info[alloc_id]) add_to_worklist(worklist, alloc_node) if DEBUG(gx, 1) and added_new and not func.mv.module.builtin: print '%d seed(s)' % added_new, func # --- for a set of target nodes of a specific type of assignment (e.g. int to (list,7)), flow back to creation points def backflow_path(gx, worklist, t): path = set(worklist) while worklist: new = set() for node in worklist: for incoming in node.in_: if t in gx.types[incoming]: incoming.fout.add(node) if not incoming in path: path.add(incoming) new.add(incoming) worklist = new return path def flow_creation_sites(worklist, allnodes): while worklist: new = set() for node in worklist: for out in node.fout: if out in allnodes: oldsize = len(out.csites) out.csites.update(node.csites) if len(out.csites) > oldsize: new.add(out) worklist = new # --- backup constraint network def backup_network(gx): beforetypes = {} for node, typeset in gx.types.items(): beforetypes[node] = typeset.copy() beforeconstr = gx.constraints.copy() beforeinout = {} for node in gx.types: beforeinout[node] = (node.in_.copy(), node.out.copy()) beforecnode = gx.cnode.copy() return (beforetypes, beforeconstr, beforeinout, beforecnode) # --- restore constraint network, introducing new types def restore_network(gx, backup): beforetypes, beforeconstr, beforeinout, beforecnode = backup gx.types = {} for node, typeset in beforetypes.items(): gx.types[node] = typeset.copy() gx.constraints = beforeconstr.copy() gx.cnode = beforecnode.copy() for node, typeset in gx.types.items(): node.nodecp = set() node.defnodes = False befinout = beforeinout[node] node.in_, node.out = befinout[0].copy(), befinout[1].copy() node.fout = set() # XXX ? for var in gx.allvars: # XXX we have to restore some variable constraint nodes.. remove vars? if not (var, 0, 0) in gx.cnode: CNode(gx, var, parent=var.parent) for func in gx.allfuncs: func.cp = {} def merge_simple_types(gx, types): merge = types.copy() if len(types) > 1 and (def_class(gx, 'none'), 0) in types: if not (def_class(gx, 'int_'), 0) in types and not (def_class(gx, 'float_'), 0) in types and not (def_class(gx, 'bool_'), 0) in types: merge.remove((def_class(gx, 'none'), 0)) return frozenset(merge) def get_classes(gx, var): return set(t[0] for t in gx.merged_inh[var] if isinstance(t[0], Class) and not t[0].mv.module.builtin) def deepcopy_classes(gx, classes): changed = True while changed: changed = False for cl in classes.copy(): for var in cl.vars.values(): if var not in gx.merged_inh: continue newcl = get_classes(gx, var) if newcl - classes: changed = True classes.update(newcl) return classes def determine_classes(gx): # XXX modeling..? if 'copy' not in gx.modules: return func = gx.modules['copy'].mv.funcs['copy'] var = func.vars[func.formals[0]] for cl in get_classes(gx, var): cl.has_copy = True func = gx.modules['copy'].mv.funcs['deepcopy'] var = func.vars[func.formals[0]] for cl in deepcopy_classes(gx, get_classes(gx, var)): cl.has_deepcopy = True def analyze(gx, module_name): # --- build dataflow graph from source code gx.main_module = graph.parse_module(module_name, gx) # --- seed class_.__name__ attributes.. for cl in gx.allclasses: if cl.ident == 'class_': var = default_var(gx, '__name__', cl) gx.types[inode(gx, var)] = set([(def_class(gx, 'str_'), 0)]) # --- non-ifa: copy classes for each allocation site for cl in gx.allclasses: if cl.ident in ['int_', 'float_', 'none', 'class_', 'str_', 'bool_']: continue if cl.ident == 'list': cl.dcpa = len(gx.list_types) + 2 elif cl.ident != '__iter': # XXX huh cl.dcpa = 2 for dcpa in range(1, cl.dcpa): class_copy(gx, cl, dcpa) var = default_var(gx, 'unit', def_class(gx, 'str_')) gx.types[inode(gx, var)] = set([(def_class(gx, 'str_'), 0)]) # --- cartesian product algorithm & iterative flow analysis iterative_dataflow_analysis(gx) if not gx.silent: print '[generating c++ code..]' for cl in gx.allclasses: for name in cl.vars: if name in cl.parent.vars and not name.startswith('__'): error.error("instance variable '%s' of class '%s' shadows class variable" % (name, cl.ident), gx, warning=True) gx.merged_inh = merged(gx, gx.types, inheritance=True) analyze_virtuals(gx) determine_classes(gx) # --- add inheritance relationships for non-original Nodes (and temp_vars?); XXX register more, right solution? for func in gx.allfuncs: if func in gx.inheritance_relations: for inhfunc in gx.inheritance_relations[func]: for a, b in zip(func.registered, inhfunc.registered): graph.inherit_rec(gx, a, b, func.mv) for a, b in zip(func.registered_temp_vars, inhfunc.registered_temp_vars): # XXX more general gx.inheritance_temp_vars.setdefault(a, []).append(b) gx.merged_inh = merged(gx, gx.types, inheritance=True) # error for dynamic expression without explicit type declaration for node in gx.merged_inh: if isinstance(node, Node) and not isinstance(node, AssAttr) and not inode(gx, node).mv.module.builtin: nodetypestr(gx, node, inode(gx, node).parent, mv=inode(gx, node).mv) return gx def register_temp_var(var, parent): if isinstance(parent, Function): parent.registered_temp_vars.append(var) def default_var(gx, name, parent, worklist=None, mv=None): if parent: mv = parent.mv var = lookup_var(name, parent, local=True, mv=mv) if not var: var = Variable(name, parent) if parent: # XXX move to Variable? parent.vars[name] = var else: mv.globals[name] = var gx.allvars.add(var) if (var, 0, 0) not in gx.cnode: newnode = CNode(gx, var, parent=parent, mv=mv) if parent: newnode.mv = parent.mv else: newnode.mv = mv add_to_worklist(worklist, newnode) gx.types[newnode] = set() if isinstance(parent, Function) and parent.listcomp and not var.registered: while isinstance(parent, Function) and parent.listcomp: # XXX parent = parent.parent register_temp_var(var, parent) return var def var_types(gx, var): return inode(gx, var).types() shedskin-0.9.4/shedskin/illegal0000664000175000017500000000437212157270661016433 0ustar srepmubsrepmubabstract AF_INET AF_INET6 AF_UNIX AI_PASSIVE asm auto bool case catch char const const_cast CPU_ALLOC CPU_ALLOC_SIZE CPU_AND CPU_AND_S CPU_CLR CPU_CLR_S CPU_COUNT CPU_COUNT_S CPU_EQUAL CPU_EQUAL_S CPU_FREE CPU_ISSET CPU_ISSET_S CPU_OR CPU_OR_S CPU_SET CPU_SET_S CPU_XOR CPU_XOR_S CPU_ZERO CPU_ZERO_S default delete do double dynamic_cast END_FOR END_WITH enum environ errno EX_CANTCREAT EX_CONFIG EX_DATAERR EX_IOERR exit EX_NOHOST EX_NOINPUT EX_NOPERM EX_NOTFOUND EX_NOUSER EX_OK EX_OSERR EX_OSFILE explicit export EX_PROTOCOL EX_SOFTWARE EX_TEMPFAIL extern EX_UNAVAILABLE EX_USAGE false FAST_FOR fileno float F_OK FOR_IN FOR_IN_ENUM FOR_IN_ZIP friend goto htonl htons INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE inline int IP_TOS IP_TTL long main major makedev MAXINT minor mutable namespace new NGROUPS_MAX ntohl ntohs O_APPEND O_ASYNC O_BINARY O_CREAT O_DIRECT O_DIRECTORY O_DSYNC O_EXCL O_EXLOCK O_LARGEFILE O_NDELAY O_NOATIME O_NOCTTY O_NOFOLLOW O_NOINHERIT O_NONBLOCK operator optarg opterr optind optopt O_RANDOM O_RDONLY O_RDWR O_RSYNC O_SEQUENTIAL O_SHLOCK _O_SHORT_LIVED O_SYNC O_TEMPORARY O_TEXT O_TRUNC O_WRONLY P_DETACH P_NOWAIT P_NOWAITO P_OVERLAY private protected public P_WAIT rand register reinterpret_cast R_OK SEEK_CUR SEEK_END SEEK_SET S_ENFMT short SHUT_RD SHUT_RDWR SHUT_WR S_IEXEC S_IFBLK S_IFCHR S_IFDIR S_IFIFO S_IFLNK S_IFMT S_IFREG S_IFSOCK SIGABRT SIGALRM SIGBUS SIGCHLD SIGCLD SIGCONT SIG_DFL SIGFPE SIGHUP SIG_IGN SIGILL SIGINT SIGIO SIGIOT SIGKILL SIGBREAK signed SIGPIPE SIGPOLL SIGPROF SIGPWR SIGQUIT SIGRTMAX SIGRTMIN SIGSEGV SIGSTOP SIGSYS SIGTERM SIGTRAP SIGTSTP SIGTTIN SIGTTOU SIGURG SIGUSR1 SIGUSR2 SIGVTALRM SIGWINCH SIGXCPU SIGXFSZ S_IMODE S_IREAD S_IRGRP S_IROTH S_IRUSR S_IRWXG S_IRWXO S_IRWXU S_ISBLK S_ISCHR S_ISDIR S_ISFIFO S_ISGID S_ISLNK S_ISREG S_ISSOCK S_ISUID S_ISVTX S_IWGRP S_IWOTH S_IWRITE S_IWUSR S_IXGRP S_IXOTH S_IXUSR sizeof SOCK_DGRAM SOCK_STREAM SOL_IP SOL_SOCKET SOMAXCONN SO_REUSEADDR static static_cast st_atime ST_ATIME st_ctime ST_CTIME std stderr ST_DEV stdin stdout ST_GID ST_INO ST_MODE st_mtime ST_MTIME ST_NLINK struct ST_SIZE ST_UID sun switch template this throw TMP_MAX true typedef typeid typename UNICODE union unsigned using virtual void volatile wchar_t WCONTINUED WITH WITH_VAR WNOHANG W_OK WUNTRACED X_OK shedskin-0.9.4/shedskin/graph.py0000664000175000017500000021215312157270661016550 0ustar srepmubsrepmub''' *** SHED SKIN Python-to-C++ Compiler *** Copyright 2005-2013 Mark Dufour; License GNU GPL version 3 (See LICENSE) graph.py: build constraint graph used in dataflow analysis constraint graph: graph along which possible types 'flow' during an 'abstract execution' of a program (a dataflow analysis). consider the assignment statement 'a = b'. it follows that the set of possible types of b is smaller than or equal to that of a (a constraint). we can determine possible types of a, by 'flowing' the types from b to a, in other words, along the constraint. constraint graph nodes are stored in gx.cnode, and the set of types of for each node in gx.types. nodes are identified by an AST Node, and two integers. the integers are used in py to duplicate parts of the constraint graph along two dimensions. in the initial constraint graph, these integers are always 0. class ModuleVisitor: inherits visitor pattern from compiler.visitor.ASTVisitor, to recursively generate constraints for each syntactical Python construct. for example, the visitFor method is called in case of a for-loop. temporary variables are introduced in many places, to enable translation to a lower-level language. parse_module(): locate module by name (e.g. 'os.path'), and use ModuleVisitor if not cached ''' import copy import os import re import sys from compiler.ast import Const, AssTuple, AssList, From, Add, ListCompFor, \ UnaryAdd, Import, Bitand, Stmt, Assign, FloorDiv, Not, Mod, AssAttr, \ Keyword, GenExpr, LeftShift, AssName, Div, Or, Lambda, And, CallFunc, \ Global, Slice, RightShift, Sub, Getattr, Dict, Ellipsis, Mul, \ Subscript, Function as FunctionNode, Return, Power, Bitxor, Class as ClassNode, Name, List, \ Discard, Sliceobj, Tuple, Pass, UnarySub, Bitor, ListComp from compiler.visitor import ASTVisitor from error import error from infer import inode, in_out, CNode, default_var, register_temp_var from python import StaticClass, lookup_func, Function, is_zip2, \ lookup_class, is_method, is_literal, is_enum, lookup_var, assign_rec, \ Class, is_property_setter, is_fastfor, aug_msg, \ Module, def_class, parse_file, find_module # --- global variable mv _mv = None def setmv(mv): global _mv _mv = mv return _mv def getmv(): return _mv class FakeGetattr3(Getattr): pass class FakeGetattr2(Getattr): pass class FakeGetattr(Getattr): pass # XXX ugly def check_redef(gx, node, s=None, onlybuiltins=False): # XXX to modvisitor, rewrite if not getmv().module.builtin: existing = [getmv().ext_classes, getmv().ext_funcs] if not onlybuiltins: existing += [getmv().classes, getmv().funcs] for whatsit in existing: if s is not None: name = s else: name = node.name if name in whatsit: error("function/class redefinition is not supported", gx, node, mv=getmv()) # --- maintain inheritance relations between copied AST nodes def inherit_rec(gx, original, copy, mv): gx.inheritance_relations.setdefault(original, []).append(copy) gx.inherited.add(copy) gx.parent_nodes[copy] = original for (a, b) in zip(original.getChildNodes(), copy.getChildNodes()): inherit_rec(gx, a, b, mv) def register_node(node, func): if func: func.registered.append(node) def slice_nums(nodes): nodes2 = [] x = 0 for i, n in enumerate(nodes): if not n or (isinstance(n, Const) and n.value is None): nodes2.append(Const(0)) else: nodes2.append(n) x |= (1 << i) return [Const(x)] + nodes2 # --- module visitor; analyze program, build constraint graph class ModuleVisitor(ASTVisitor): def __init__(self, module, gx): ASTVisitor.__init__(self) self.module = module self.gx = gx self.classes = {} self.funcs = {} self.globals = {} self.lambdas = {} self.imports = {} self.fake_imports = {} self.ext_classes = {} self.ext_funcs = {} self.lambdaname = {} self.lwrapper = {} self.tempcount = self.gx.tempcount self.callfuncs = [] self.for_in_iters = [] self.listcomps = [] self.defaults = {} self.importnodes = [] def dispatch(self, node, *args): if (node, 0, 0) not in self.gx.cnode: ASTVisitor.dispatch(self, node, *args) def fake_func(self, node, objexpr, attrname, args, func): if (node, 0, 0) in self.gx.cnode: # XXX newnode = self.gx.cnode[node, 0, 0] else: newnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[newnode] = set() fakefunc = CallFunc(Getattr(objexpr, attrname), args) fakefunc.lineno = objexpr.lineno self.visit(fakefunc, func) self.add_constraint((inode(self.gx, fakefunc), newnode), func) inode(self.gx, objexpr).fakefunc = fakefunc return fakefunc # simple heuristic for initial list split: count nesting depth, first constant child type def list_type(self, node): count = 0 child = node while isinstance(child, (List, ListComp)): if not child.getChildNodes(): return None child = child.getChildNodes()[0] count += 1 if isinstance(child, (UnarySub, UnaryAdd)): child = child.expr if isinstance(child, CallFunc) and isinstance(child.node, Name): map = {'int': int, 'str': str, 'float': float} if child.node.name in ('range'): # ,'xrange'): count, child = count + 1, int elif child.node.name in map: child = map[child.node.name] elif child.node.name in (cl.ident for cl in self.gx.allclasses) or child.node.name in getmv().classes: # XXX getmv().classes child = child.node.name else: if count == 1: return None child = None elif isinstance(child, Const): child = type(child.value) elif isinstance(child, Name) and child.name in ('True', 'False'): child = bool elif isinstance(child, Tuple): child = tuple elif isinstance(child, Dict): child = dict else: if count == 1: return None child = None self.gx.list_types.setdefault((count, child), len(self.gx.list_types) + 2) # print 'listtype', node, self.gx.list_types[count, child] return self.gx.list_types[count, child] def instance(self, node, cl, func=None): if (node, 0, 0) in self.gx.cnode: # XXX to create_node() func newnode = self.gx.cnode[node, 0, 0] else: newnode = CNode(self.gx, node, parent=func, mv=getmv()) newnode.constructor = True if cl.ident in ['int_', 'float_', 'str_', 'none', 'class_', 'bool_']: self.gx.types[newnode] = set([(cl, cl.dcpa - 1)]) else: if cl.ident == 'list' and self.list_type(node): self.gx.types[newnode] = set([(cl, self.list_type(node))]) else: self.gx.types[newnode] = set([(cl, cl.dcpa)]) def constructor(self, node, classname, func): cl = def_class(self.gx, classname) self.instance(node, cl, func) default_var(self.gx, 'unit', cl) if classname in ['list', 'tuple'] and not node.nodes: self.gx.empty_constructors.add(node) # ifa disables those that flow to instance variable assignments # --- internally flow binary tuples if cl.ident == 'tuple2': default_var(self.gx, 'first', cl) default_var(self.gx, 'second', cl) elem0, elem1 = node.nodes self.visit(elem0, func) self.visit(elem1, func) self.add_dynamic_constraint(node, elem0, 'unit', func) self.add_dynamic_constraint(node, elem1, 'unit', func) self.add_dynamic_constraint(node, elem0, 'first', func) self.add_dynamic_constraint(node, elem1, 'second', func) return # --- add dynamic children constraints for other types if classname == 'dict': # XXX filter children default_var(self.gx, 'unit', cl) default_var(self.gx, 'value', cl) for child in node.getChildNodes(): self.visit(child, func) for (key, value) in node.items: # XXX filter self.add_dynamic_constraint(node, key, 'unit', func) self.add_dynamic_constraint(node, value, 'value', func) else: for child in node.nodes: self.visit(child, func) for child in self.filter_redundant_children(node): self.add_dynamic_constraint(node, child, 'unit', func) # --- for compound list/tuple/dict constructors, we only consider a single child node for each subtype def filter_redundant_children(self, node): done = set() nonred = [] for child in node.nodes: type = self.child_type_rec(child) if not type or not type in done: done.add(type) nonred.append(child) return nonred # --- determine single constructor child node type, used by the above def child_type_rec(self, node): if isinstance(node, (UnarySub, UnaryAdd)): node = node.expr if isinstance(node, (List, Tuple)): if isinstance(node, List): cl = def_class(self.gx, 'list') elif len(node.nodes) == 2: cl = def_class(self.gx, 'tuple2') else: cl = def_class(self.gx, 'tuple') merged = set() for child in node.nodes: merged.add(self.child_type_rec(child)) if len(merged) == 1: return (cl, merged.pop()) elif isinstance(node, Const): return (list(inode(self.gx, node).types())[0][0],) # --- add dynamic constraint for constructor argument, e.g. '[expr]' becomes [].__setattr__('unit', expr) def add_dynamic_constraint(self, parent, child, varname, func): # print 'dynamic constr', child, parent self.gx.assign_target[child] = parent cu = Const(varname) self.visit(cu, func) fakefunc = CallFunc(FakeGetattr2(parent, '__setattr__'), [cu, child]) self.visit(fakefunc, func) fakechildnode = CNode(self.gx, (child, varname), parent=func, mv=getmv()) # create separate 'fake' CNode per child, so we can have multiple 'callfuncs' self.gx.types[fakechildnode] = set() self.add_constraint((inode(self.gx, parent), fakechildnode), func) # add constraint from parent to fake child node. if parent changes, all fake child nodes change, and the callfunc for each child node is triggered fakechildnode.callfuncs.append(fakefunc) # --- add regular constraint to function def add_constraint(self, constraint, func): in_out(constraint[0], constraint[1]) self.gx.constraints.add(constraint) while isinstance(func, Function) and func.listcomp: func = func.parent # XXX if isinstance(func, Function): func.constraints.add(constraint) def struct_unpack(self, rvalue, func): if isinstance(rvalue, CallFunc): if isinstance(rvalue.node, Getattr) and isinstance(rvalue.node.expr, Name) and rvalue.node.expr.name == 'struct' and rvalue.node.attrname == 'unpack' and lookup_var('struct', func, mv=self).imported: # XXX imported from where? return True elif isinstance(rvalue.node, Name) and rvalue.node.name == 'unpack' and 'unpack' in self.ext_funcs and not lookup_var('unpack', func, mv=self): # XXX imported from where? return True def struct_info(self, node, func): if isinstance(node, Name): var = lookup_var(node.name, func, mv=self) # XXX fwd ref? if not var or len(var.const_assign) != 1: error('non-constant format string', self.gx, node, mv=self) error('assuming constant format string', self.gx, node, mv=self, warning=True) fmt = var.const_assign[0].value elif isinstance(node, Const): fmt = node.value else: error('non-constant format string', self.gx, node, mv=self) char_type = dict(['xx', 'cs', 'bi', 'Bi', '?b', 'hi', 'Hi', 'ii', 'Ii', 'li', 'Li', 'qi', 'Qi', 'ff', 'df', 'ss', 'ps']) ordering = '@' if fmt and fmt[0] in '@<>!=': ordering, fmt = fmt[0], fmt[1:] result = [] digits = '' for i, c in enumerate(fmt): if c.isdigit(): digits += c elif c in char_type: rtype = {'i': 'int', 's': 'str', 'b': 'bool', 'f': 'float', 'x': 'pad'}[char_type[c]] if rtype == 'str' and c != 'c': result.append((ordering, c, 'str', int(digits or '1'))) elif digits == '0': result.append((ordering, c, rtype, 0)) else: result.extend(int(digits or '1') * [(ordering, c, rtype, 1)]) digits = '' else: error('bad or unsupported char in struct format: ' + repr(c), self.gx, node, mv=self) digits = '' return result def struct_faketuple(self, info): result = [] for o, c, t, d in info: if d != 0 or c == 's': if t == 'int': result.append(Const(1)) elif t == 'str': result.append(Const('')) elif t == 'float': result.append(Const(1.0)) elif t == 'bool': result.append(Name('True')) return Tuple(result) def visitExec(self, node, func=None): error("'exec' is not supported", self.gx, node, mv=getmv()) def visitGenExpr(self, node, func=None): newnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[newnode] = set() lc = ListComp(node.code.expr, [ListCompFor(qual.assign, qual.iter, qual.ifs, qual.lineno) for qual in node.code.quals], lineno=node.lineno) register_node(lc, func) self.gx.genexp_to_lc[node] = lc self.visit(lc, func) self.add_constraint((inode(self.gx, lc), newnode), func) def visitStmt(self, node, func=None): comments = [] for b in node.nodes: if isinstance(b, Discard): self.bool_test_add(b.expr) if isinstance(b, Discard) and isinstance(b.expr, Const) and type(b.expr.value) == str: comments.append(b.expr.value) elif comments: self.gx.comments[b] = comments comments = [] self.visit(b, func) def visitModule(self, node): # --- bootstrap built-in classes if self.module.ident == 'builtin': for dummy in self.gx.builtins: self.visit(ClassNode(dummy, [], None, Pass())) if self.module.ident != 'builtin': n = From('builtin', [('*', None)], None) # Python2.5+ getmv().importnodes.append(n) self.visit(n) # --- __name__ if self.module.ident != 'builtin': namevar = default_var(self.gx, '__name__', None, mv=getmv()) self.gx.types[inode(self.gx, namevar)] = set([(def_class(self.gx, 'str_'), 0)]) self.forward_references(node) # --- visit children for child in node.getChildNodes(): if isinstance(child, Stmt): getmv().importnodes.extend(n for n in child.nodes if isinstance(n, (Import, From))) self.visit(child, None) # --- register classes for cl in getmv().classes.values(): self.gx.allclasses.add(cl) # --- inheritance expansion # determine base classes for cl in self.classes.values(): for base in cl.node.bases: if not (isinstance(base, Name) and base.name == 'object'): ancestor = lookup_class(base, getmv()) cl.bases.append(ancestor) ancestor.children.append(cl) # for each base class, duplicate methods for cl in self.classes.values(): for ancestor in cl.ancestors_upto(None)[1:]: cl.staticmethods.extend(ancestor.staticmethods) cl.properties.update(ancestor.properties) for func in ancestor.funcs.values(): if not func.node or func.inherited: continue ident = func.ident if ident in cl.funcs: ident += ancestor.ident + '__' # deep-copy AST function nodes func_copy = copy.deepcopy(func.node) inherit_rec(self.gx, func.node, func_copy, func.mv) tempmv, mv = getmv(), func.mv setmv(mv) self.visitFunction(func_copy, cl, inherited_from=ancestor) mv = tempmv setmv(mv) # maintain relation with original self.gx.inheritance_relations.setdefault(func, []).append(cl.funcs[ident]) cl.funcs[ident].inherited = func.node cl.funcs[ident].inherited_from = func func_copy.name = ident if ident == func.ident: cl.funcs[ident + ancestor.ident + '__'] = cl.funcs[ident] def stmt_nodes(self, node, cl): result = [] for child in node.getChildNodes(): if isinstance(child, Stmt): for n in child.nodes: if isinstance(n, cl): result.append(n) return result def forward_references(self, node): getmv().classnodes = [] # classes for n in self.stmt_nodes(node, ClassNode): check_redef(self.gx, n) getmv().classnodes.append(n) newclass = Class(self.gx, n, getmv()) self.classes[n.name] = newclass getmv().classes[n.name] = newclass newclass.module = self.module newclass.parent = StaticClass(newclass, getmv()) # methods for m in self.stmt_nodes(n, FunctionNode): if hasattr(m, 'decorators') and m.decorators and [dec for dec in m.decorators if is_property_setter(dec)]: m.name = m.name + '__setter__' if m.name in newclass.funcs: # and func.ident not in ['__getattr__', '__setattr__']: # XXX error("function/class redefinition is not allowed", self.gx, m, mv=getmv()) func = Function(self.gx, m, newclass, mv=getmv()) newclass.funcs[func.ident] = func self.set_default_vars(m, func) # functions getmv().funcnodes = [] for n in self.stmt_nodes(node, FunctionNode): check_redef(self.gx, n) getmv().funcnodes.append(n) func = getmv().funcs[n.name] = Function(self.gx, n, mv=getmv()) self.set_default_vars(n, func) # global variables XXX visitGlobal for assname in self.local_assignments(node, global_=True): default_var(self.gx, assname.name, None, mv=getmv()) def set_default_vars(self, node, func): globals = set(self.get_globals(node)) for assname in self.local_assignments(node): if assname.name not in globals: default_var(self.gx, assname.name, func) def get_globals(self, node): if isinstance(node, Global): result = node.names else: result = [] for child in node.getChildNodes(): result.extend(self.get_globals(child)) return result def local_assignments(self, node, global_=False): if global_ and isinstance(node, (ClassNode, FunctionNode)): return [] elif isinstance(node, (ListComp, GenExpr)): return [] elif isinstance(node, AssName): result = [node] else: result = [] for child in node.getChildNodes(): result.extend(self.local_assignments(child, global_)) return result def visitImport(self, node, func=None): if not node in getmv().importnodes: error("please place all imports (no 'try:' etc) at the top of the file", self.gx, node, mv=getmv()) for (name, pseudonym) in node.names: if pseudonym: # --- import a.b as c: don't import a self.import_module(name, pseudonym, node, False) else: self.import_modules(name, node, False) def import_modules(self, name, node, fake): # --- import a.b.c: import a, then a.b, then a.b.c split = name.split('.') module = getmv().module for i in range(len(split)): subname = '.'.join(split[:i + 1]) parent = module module = self.import_module(subname, subname, node, fake) if module.ident not in parent.mv.imports: # XXX if not fake: parent.mv.imports[module.ident] = module return module def import_module(self, name, pseudonym, node, fake): module = self.analyze_module(name, pseudonym, node, fake) if not fake: var = default_var(self.gx, pseudonym or name, None, mv=getmv()) var.imported = True self.gx.types[inode(self.gx, var)] = set([(module, 0)]) return module def visitFrom(self, node, parent=None): if not node in getmv().importnodes: # XXX use (func, node) as parent.. error("please place all imports (no 'try:' etc) at the top of the file", self.gx, node, mv=getmv()) if hasattr(node, 'level') and node.level: error("relative imports are not supported", self.gx, node, mv=getmv()) if node.modname == '__future__': for name, _ in node.names: if name not in ['with_statement', 'print_function']: error("future '%s' is not yet supported" % name, self.gx, node, mv=getmv()) return module = self.import_modules(node.modname, node, True) self.gx.from_module[node] = module for name, pseudonym in node.names: if name == '*': self.ext_funcs.update(module.mv.funcs) self.ext_classes.update(module.mv.classes) for import_name, import_module in module.mv.imports.items(): var = default_var(self.gx, import_name, None, mv=getmv()) # XXX merge var.imported = True self.gx.types[inode(self.gx, var)] = set([(import_module, 0)]) self.imports[import_name] = import_module for name, extvar in module.mv.globals.items(): if not extvar.imported and not name in ['__name__']: var = default_var(self.gx, name, None, mv=getmv()) # XXX merge var.imported = True self.add_constraint((inode(self.gx, extvar), inode(self.gx, var)), None) continue path = module.path pseudonym = pseudonym or name if name in module.mv.funcs: self.ext_funcs[pseudonym] = module.mv.funcs[name] elif name in module.mv.classes: self.ext_classes[pseudonym] = module.mv.classes[name] elif name in module.mv.globals and not module.mv.globals[name].imported: # XXX extvar = module.mv.globals[name] var = default_var(self.gx, pseudonym, None, mv=getmv()) var.imported = True self.add_constraint((inode(self.gx, extvar), inode(self.gx, var)), None) elif os.path.isfile(os.path.join(path, name + '.py')) or \ os.path.isfile(os.path.join(path, name, '__init__.py')): modname = '.'.join(module.name_list + [name]) self.import_module(modname, name, node, False) else: error("no identifier '%s' in module '%s'" % (name, node.modname), self.gx, node, mv=getmv()) def analyze_module(self, name, pseud, node, fake): module = parse_module(name, self.gx, getmv().module, node) if not fake: self.imports[pseud] = module else: self.fake_imports[pseud] = module return module def visitFunction(self, node, parent=None, is_lambda=False, inherited_from=None): if not getmv().module.builtin and (node.varargs or node.kwargs): error('argument (un)packing is not supported', self.gx, node, mv=getmv()) if not parent and not is_lambda and node.name in getmv().funcs: func = getmv().funcs[node.name] elif isinstance(parent, Class) and not inherited_from and node.name in parent.funcs: func = parent.funcs[node.name] else: func = Function(self.gx, node, parent, inherited_from, mv=getmv()) if inherited_from: self.set_default_vars(node, func) if not is_method(func): if not getmv().module.builtin and not node in getmv().funcnodes and not is_lambda: error("non-global function '%s'" % node.name, self.gx, node, mv=getmv()) if hasattr(node, 'decorators') and node.decorators: for dec in node.decorators.nodes: if isinstance(dec, Name) and dec.name == 'staticmethod': parent.staticmethods.append(node.name) elif isinstance(dec, Name) and dec.name == 'property': parent.properties[node.name] = [node.name, None] elif is_property_setter(dec): parent.properties[dec.expr.name][1] = node.name else: error("unsupported type of decorator", self.gx, dec, mv=getmv()) if parent: if not inherited_from and not func.ident in parent.staticmethods and (not func.formals or func.formals[0] != 'self'): error("formal arguments of method must start with 'self'", self.gx, node, mv=getmv()) if not func.mv.module.builtin and func.ident in ['__new__', '__getattr__', '__setattr__', '__radd__', '__rsub__', '__rmul__', '__rdiv__', '__rtruediv__', '__rfloordiv__', '__rmod__', '__rdivmod__', '__rpow__', '__rlshift__', '__rrshift__', '__rand__', '__rxor__', '__ror__', '__iter__', '__call__', '__enter__', '__exit__', '__del__', '__copy__', '__deepcopy__']: error("'%s' is not supported" % func.ident, self.gx, node, warning=True, mv=getmv()) if is_lambda: self.lambdas[node.name] = func # --- add unpacking statement for tuple formals func.expand_args = {} for i, formal in enumerate(func.formals): if isinstance(formal, tuple): tmp = self.temp_var((node, i), func) func.formals[i] = tmp.name fake_unpack = Assign([self.unpack_rec(formal)], Name(tmp.name)) func.expand_args[tmp.name] = fake_unpack self.visit(fake_unpack, func) func.defaults = node.defaults for formal in func.formals: var = default_var(self.gx, formal, func) var.formal_arg = True # --- flow return expressions together into single node func.retnode = retnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[retnode] = set() func.yieldnode = yieldnode = CNode(self.gx, (node, 'yield'), parent=func, mv=getmv()) self.gx.types[yieldnode] = set() self.visit(node.code, func) for i, default in enumerate(func.defaults): if not is_literal(default): self.defaults[default] = (len(self.defaults), func, i) self.visit(default, None) # defaults are global # --- add implicit 'return None' if no return expressions if not func.returnexpr: func.fakeret = Return(Name('None')) self.visit(func.fakeret, func) # --- register function if isinstance(parent, Class): if func.ident not in parent.staticmethods: # XXX use flag default_var(self.gx, 'self', func) if func.ident == '__init__' and '__del__' in parent.funcs: # XXX what if no __init__ self.visit(CallFunc(Getattr(Name('self'), '__del__'), []), func) self.gx.gc_cleanup = True parent.funcs[func.ident] = func def unpack_rec(self, formal): if isinstance(formal, str): return AssName(formal, 'OP_ASSIGN') else: return AssTuple([self.unpack_rec(elem) for elem in formal]) def visitLambda(self, node, func=None): lambdanr = len(self.lambdas) name = '__lambda%d__' % lambdanr fakenode = FunctionNode(None, name, node.argnames, node.defaults, node.flags, None, Return(node.code)) self.visit(fakenode, None, True) f = self.lambdas[name] f.lambdanr = lambdanr self.lambdaname[node] = name newnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[newnode] = set([(f, 0)]) newnode.copymetoo = True def visitAnd(self, node, func=None): self.visit_and_or(node, func) def visitOr(self, node, func=None): self.visit_and_or(node, func) def visit_and_or(self, node, func): newnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[newnode] = set() for child in node.getChildNodes(): if node in self.gx.bool_test_only: self.bool_test_add(child) self.visit(child, func) self.add_constraint((inode(self.gx, child), newnode), func) self.temp_var2(child, newnode, func) def visitIf(self, node, func=None): for test, code in node.tests: self.bool_test_add(test) faker = CallFunc(Name('bool'), [test]) self.visit(faker, func) self.visit(code, func) if node.else_: self.visit(node.else_, func) def visitIfExp(self, node, func=None): newnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[newnode] = set() for child in node.getChildNodes(): self.visit(child, func) self.add_constraint((inode(self.gx, node.then), newnode), func) self.add_constraint((inode(self.gx, node.else_), newnode), func) def visitGlobal(self, node, func=None): func.globals += node.names def visitList(self, node, func=None): self.constructor(node, 'list', func) def visitDict(self, node, func=None): self.constructor(node, 'dict', func) if node.items: # XXX library bug node.lineno = node.items[0][0].lineno def visitNot(self, node, func=None): self.bool_test_add(node.expr) newnode = CNode(self.gx, node, parent=func, mv=getmv()) newnode.copymetoo = True self.gx.types[newnode] = set([(def_class(self.gx, 'bool_'), 0)]) # XXX new type? self.visit(node.expr, func) def visitBackquote(self, node, func=None): self.fake_func(node, node.expr, '__repr__', [], func) def visitTuple(self, node, func=None): if len(node.nodes) == 2: self.constructor(node, 'tuple2', func) else: self.constructor(node, 'tuple', func) def visitSubscript(self, node, func=None): # XXX merge __setitem__, __getitem__ if len(node.subs) > 1: subscript = Tuple(node.subs) else: subscript = node.subs[0] if isinstance(subscript, Ellipsis): # XXX also check at setitem error('ellipsis is not supported', self.gx, node, mv=getmv()) if isinstance(subscript, Sliceobj): self.slice(node, node.expr, subscript.nodes, func) else: if node.flags == 'OP_DELETE': self.fake_func(node, node.expr, '__delitem__', [subscript], func) elif len(node.subs) > 1: self.fake_func(node, node.expr, '__getitem__', [subscript], func) else: ident = '__getitem__' self.fake_func(node, node.expr, ident, [subscript], func) def visitSlice(self, node, func=None): self.slice(node, node.expr, [node.lower, node.upper, None], func) def slice(self, node, expr, nodes, func, replace=None): nodes2 = slice_nums(nodes) if replace: self.fake_func(node, expr, '__setslice__', nodes2 + [replace], func) elif node.flags == 'OP_DELETE': self.fake_func(node, expr, '__delete__', nodes2, func) else: self.fake_func(node, expr, '__slice__', nodes2, func) def visitUnarySub(self, node, func=None): self.fake_func(node, node.expr, '__neg__', [], func) def visitUnaryAdd(self, node, func=None): self.fake_func(node, node.expr, '__pos__', [], func) def visitCompare(self, node, func=None): newnode = CNode(self.gx, node, parent=func, mv=getmv()) newnode.copymetoo = True self.gx.types[newnode] = set([(def_class(self.gx, 'bool_'), 0)]) # XXX new type? self.visit(node.expr, func) msgs = {'<': 'lt', '>': 'gt', 'in': 'contains', 'not in': 'contains', '!=': 'ne', '==': 'eq', '<=': 'le', '>=': 'ge'} left = node.expr for op, right in node.ops: self.visit(right, func) msg = msgs.get(op) if msg == 'contains': self.fake_func(node, right, '__' + msg + '__', [left], func) elif msg in ('lt', 'gt', 'le', 'ge'): fakefunc = CallFunc(Name('__%s' % msg), [left, right]) fakefunc.lineno = left.lineno self.visit(fakefunc, func) elif msg: self.fake_func(node, left, '__' + msg + '__', [right], func) left = right # tempvars, e.g. (t1=fun()) for term in node.ops[:-1]: if not isinstance(term[1], (Name, Const)): self.temp_var2(term[1], inode(self.gx, term[1]), func) def visitBitand(self, node, func=None): self.visitBitpair(node, aug_msg(node, 'and'), func) def visitBitor(self, node, func=None): self.visitBitpair(node, aug_msg(node, 'or'), func) def visitBitxor(self, node, func=None): self.visitBitpair(node, aug_msg(node, 'xor'), func) def visitBitpair(self, node, msg, func=None): CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[inode(self.gx, node)] = set() left = node.nodes[0] for i, right in enumerate(node.nodes[1:]): faker = self.fake_func((left, i), left, msg, [right], func) left = faker self.add_constraint((inode(self.gx, faker), inode(self.gx, node)), func) def visitAdd(self, node, func=None): self.fake_func(node, node.left, aug_msg(node, 'add'), [node.right], func) def visitInvert(self, node, func=None): self.fake_func(node, node.expr, '__invert__', [], func) def visitRightShift(self, node, func=None): self.fake_func(node, node.left, aug_msg(node, 'rshift'), [node.right], func) def visitLeftShift(self, node, func=None): self.fake_func(node, node.left, aug_msg(node, 'lshift'), [node.right], func) def visitAugAssign(self, node, func=None): # a[b] += c -> a[b] = a[b]+c, using tempvars to handle sidefx newnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[newnode] = set() clone = copy.deepcopy(node) lnode = node.node if isinstance(node.node, Name): blah = AssName(clone.node.name, 'OP_ASSIGN') elif isinstance(node.node, Getattr): blah = AssAttr(clone.node.expr, clone.node.attrname, 'OP_ASSIGN') elif isinstance(node.node, Subscript): t1 = self.temp_var(node.node.expr, func) a1 = Assign([AssName(t1.name, 'OP_ASSIGN')], node.node.expr) self.visit(a1, func) self.add_constraint((inode(self.gx, node.node.expr), inode(self.gx, t1)), func) if len(node.node.subs) > 1: subs = Tuple(node.node.subs) else: subs = node.node.subs[0] t2 = self.temp_var(subs, func) a2 = Assign([AssName(t2.name, 'OP_ASSIGN')], subs) self.visit(a1, func) self.visit(a2, func) self.add_constraint((inode(self.gx, subs), inode(self.gx, t2)), func) inode(self.gx, node).temp1 = t1.name inode(self.gx, node).temp2 = t2.name inode(self.gx, node).subs = subs blah = Subscript(Name(t1.name), 'OP_APPLY', [Name(t2.name)]) lnode = Subscript(Name(t1.name), 'OP_APPLY', [Name(t2.name)]) else: error('unsupported type of assignment', self.gx, node, mv=getmv()) if node.op == '-=': blah2 = Sub((lnode, node.expr)) if node.op == '+=': blah2 = Add((lnode, node.expr)) if node.op == '|=': blah2 = Bitor((lnode, node.expr)) if node.op == '&=': blah2 = Bitand((lnode, node.expr)) if node.op == '^=': blah2 = Bitxor((lnode, node.expr)) if node.op == '**=': blah2 = Power((lnode, node.expr)) if node.op == '<<=': blah2 = LeftShift((lnode, node.expr)) if node.op == '>>=': blah2 = RightShift((lnode, node.expr)) if node.op == '%=': blah2 = Mod((lnode, node.expr)) if node.op == '*=': blah2 = Mul((lnode, node.expr)) if node.op == '/=': blah2 = Div((lnode, node.expr)) if node.op == '//=': blah2 = FloorDiv((lnode, node.expr)) blah2.augment = True assign = Assign([blah], blah2) register_node(assign, func) inode(self.gx, node).assignhop = assign self.visit(assign, func) def visitSub(self, node, func=None): self.fake_func(node, node.left, aug_msg(node, 'sub'), [node.right], func) def visitMul(self, node, func=None): self.fake_func(node, node.left, aug_msg(node, 'mul'), [node.right], func) def visitDiv(self, node, func=None): self.fake_func(node, node.left, aug_msg(node, 'div'), [node.right], func) def visitFloorDiv(self, node, func=None): self.fake_func(node, node.left, aug_msg(node, 'floordiv'), [node.right], func) def visitPower(self, node, func=None): self.fake_func(node, node.left, '__pow__', [node.right], func) def visitMod(self, node, func=None): if isinstance(node.right, (Tuple, Dict)): self.fake_func(node, node.left, '__mod__', [], func) for child in node.right.getChildNodes(): self.visit(child, func) if isinstance(node.right, Tuple): self.fake_func(inode(self.gx, child), child, '__str__', [], func) else: self.fake_func(node, node.left, '__mod__', [node.right], func) def visitPrintnl(self, node, func=None): self.visitPrint(node, func) def visitPrint(self, node, func=None): pnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[pnode] = set() for child in node.getChildNodes(): self.visit(child, func) self.fake_func(inode(self.gx, child), child, '__str__', [], func) def temp_var(self, node, func=None, looper=None, wopper=None): if node in self.gx.parent_nodes: varname = self.tempcount[self.gx.parent_nodes[node]] elif node in self.tempcount: # XXX investigate why this happens varname = self.tempcount[node] else: varname = '__' + str(len(self.tempcount)) var = default_var(self.gx, varname, func, mv=getmv()) var.looper = looper var.wopper = wopper self.tempcount[node] = varname register_temp_var(var, func) return var def temp_var2(self, node, source, func): tvar = self.temp_var(node, func) self.add_constraint((source, inode(self.gx, tvar)), func) return tvar def temp_var_int(self, node, func): var = self.temp_var(node, func) self.gx.types[inode(self.gx, var)] = set([(def_class(self.gx, 'int_'), 0)]) inode(self.gx, var).copymetoo = True return var def visitRaise(self, node, func=None): if node.expr1 is None or node.expr2 is not None or node.expr3 is not None: error('unsupported raise syntax', self.gx, node, mv=getmv()) for child in node.getChildNodes(): self.visit(child, func) def visitTryExcept(self, node, func=None): self.visit(node.body, func) for handler in node.handlers: self.visit(handler[2], func) if not handler[0]: continue if isinstance(handler[0], Tuple): pairs = [(n, handler[1]) for n in handler[0].nodes] else: pairs = [(handler[0], handler[1])] for (h0, h1) in pairs: if isinstance(h0, Name) and h0.name in ['int', 'float', 'str', 'class']: continue # handle in lookup_class cl = lookup_class(h0, getmv()) if not cl: error("unknown or unsupported exception type", self.gx, h0, mv=getmv()) if isinstance(h1, AssName): var = self.default_var(h1.name, func) else: var = self.temp_var(h0, func) var.invisible = True inode(self.gx, var).copymetoo = True self.gx.types[inode(self.gx, var)] = set([(cl, 1)]) # else if node.else_: self.visit(node.else_, func) self.temp_var_int(node.else_, func) def visitTryFinally(self, node, func=None): error("'try..finally' is not supported", self.gx, node, mv=getmv()) def visitYield(self, node, func): func.isGenerator = True func.yieldNodes.append(node) self.visit(Return(CallFunc(Name('__iter'), [node.value])), func) self.add_constraint((inode(self.gx, node.value), func.yieldnode), func) def visitFor(self, node, func=None): # --- iterable contents -> assign node assnode = CNode(self.gx, node.assign, parent=func, mv=getmv()) self.gx.types[assnode] = set() get_iter = CallFunc(Getattr(node.list, '__iter__'), []) fakefunc = CallFunc(Getattr(get_iter, 'next'), []) self.visit(fakefunc, func) self.add_constraint((inode(self.gx, fakefunc), assnode), func) # --- assign node -> variables XXX merge into assign_pair if isinstance(node.assign, AssName): # for x in.. lvar = self.default_var(node.assign.name, func) self.add_constraint((assnode, inode(self.gx, lvar)), func) elif isinstance(node.assign, AssAttr): # XXX experimental :) # for expr.x in.. CNode(self.gx, node.assign, parent=func, mv=getmv()) self.gx.assign_target[node.assign.expr] = node.assign.expr # XXX multiple targets possible please fakefunc2 = CallFunc(Getattr(node.assign.expr, '__setattr__'), [Const(node.assign.attrname), fakefunc]) self.visit(fakefunc2, func) elif isinstance(node.assign, (AssTuple, AssList)): # for (a,b, ..) in.. self.tuple_flow(node.assign, node.assign, func) else: error('unsupported type of assignment', self.gx, node, mv=getmv()) self.do_for(node, assnode, get_iter, func) # --- for-else if node.else_: self.temp_var_int(node.else_, func) self.visit(node.else_, func) # --- loop body self.gx.loopstack.append(node) self.visit(node.body, func) self.gx.loopstack.pop() self.for_in_iters.append(node.list) def do_for(self, node, assnode, get_iter, func): # --- for i in range(..) XXX i should not be modified.. use tempcounter; two bounds if is_fastfor(node): self.temp_var2(node.assign, assnode, func) self.temp_var2(node.list, inode(self.gx, node.list.args[0]), func) if len(node.list.args) == 3 and not isinstance(node.list.args[2], Name) and not is_literal(node.list.args[2]): # XXX merge with ListComp for arg in node.list.args: if not isinstance(arg, Name) and not is_literal(arg): # XXX create func for better check self.temp_var2(arg, inode(self.gx, arg), func) # --- temp vars for list, iter etc. else: self.temp_var2(node, inode(self.gx, node.list), func) self.temp_var2((node, 1), inode(self.gx, get_iter), func) self.temp_var_int(node.list, func) if is_enum(node) or is_zip2(node): self.temp_var2((node, 2), inode(self.gx, node.list.args[0]), func) if is_zip2(node): self.temp_var2((node, 3), inode(self.gx, node.list.args[1]), func) self.temp_var_int((node, 4), func) self.temp_var((node, 5), func, looper=node.list) if isinstance(node.list, CallFunc) and isinstance(node.list.node, Getattr): self.temp_var((node, 6), func, wopper=node.list.node.expr) self.temp_var2((node, 7), inode(self.gx, node.list.node.expr), func) def bool_test_add(self, node): if isinstance(node, (And, Or, Not)): self.gx.bool_test_only.add(node) def visitWhile(self, node, func=None): self.gx.loopstack.append(node) self.bool_test_add(node.test) for child in node.getChildNodes(): self.visit(child, func) self.gx.loopstack.pop() if node.else_: self.temp_var_int(node.else_, func) self.visit(node.else_, func) def visitWith(self, node, func=None): if node.vars: varnode = CNode(self.gx, node.vars, parent=func, mv=getmv()) self.gx.types[varnode] = set() self.visit(node.expr, func) self.add_constraint((inode(self.gx, node.expr), varnode), func) lvar = self.default_var(node.vars.name, func) self.add_constraint((varnode, inode(self.gx, lvar)), func) else: self.visit(node.expr, func) for child in node.getChildNodes(): self.visit(child, func) def visitListCompIf(self, node, func=None): self.bool_test_add(node.test) for child in node.getChildNodes(): self.visit(child, func) def visitListComp(self, node, func=None): # --- [expr for iter in list for .. if cond ..] lcfunc = Function(self.gx, mv=getmv()) lcfunc.listcomp = True lcfunc.ident = 'l.c.' # XXX lcfunc.parent = func for qual in node.quals: # iter assnode = CNode(self.gx, qual.assign, parent=func, mv=getmv()) self.gx.types[assnode] = set() # list.unit->iter get_iter = CallFunc(Getattr(qual.list, '__iter__'), []) fakefunc = CallFunc(Getattr(get_iter, 'next'), []) self.visit(fakefunc, lcfunc) self.add_constraint((inode(self.gx, fakefunc), inode(self.gx, qual.assign)), lcfunc) if isinstance(qual.assign, AssName): # XXX merge with visitFor lvar = default_var(self.gx, qual.assign.name, lcfunc) # XXX str or Name? self.add_constraint((inode(self.gx, qual.assign), inode(self.gx, lvar)), lcfunc) else: # AssTuple, AssList self.tuple_flow(qual.assign, qual.assign, lcfunc) self.do_for(qual, assnode, get_iter, lcfunc) # cond for child in qual.ifs: self.visit(child, lcfunc) self.for_in_iters.append(qual.list) # node type if node in self.gx.genexp_to_lc.values(): # converted generator expression self.instance(node, def_class(self.gx, '__iter'), func) else: self.instance(node, def_class(self.gx, 'list'), func) # expr->instance.unit self.visit(node.expr, lcfunc) self.add_dynamic_constraint(node, node.expr, 'unit', lcfunc) lcfunc.ident = 'list_comp_' + str(len(self.listcomps)) self.listcomps.append((node, lcfunc, func)) def visitReturn(self, node, func): self.visit(node.value, func) func.returnexpr.append(node.value) if not (isinstance(node.value, Const) and node.value.value is None): newnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[newnode] = set() if isinstance(node.value, Name): func.retvars.append(node.value.name) if func.retnode: self.add_constraint((inode(self.gx, node.value), func.retnode), func) def visitAssign(self, node, func=None): # --- rewrite for struct.unpack XXX rewrite callfunc as tuple if len(node.nodes) == 1: lvalue, rvalue = node.nodes[0], node.expr if self.struct_unpack(rvalue, func) and isinstance(lvalue, (AssList, AssTuple)) and not [n for n in lvalue.nodes if isinstance(n, (AssList, AssTuple))]: self.visit(node.expr, func) sinfo = self.struct_info(rvalue.args[0], func) faketuple = self.struct_faketuple(sinfo) self.visit(Assign(node.nodes, faketuple), func) tvar = self.temp_var2(rvalue.args[1], inode(self.gx, rvalue.args[1]), func) tvar_pos = self.temp_var_int(rvalue.args[0], func) self.gx.struct_unpack[node] = (sinfo, tvar.name, tvar_pos.name) return newnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[newnode] = set() # --- a,b,.. = c,(d,e),.. = .. = expr for target_expr in node.nodes: pairs = assign_rec(target_expr, node.expr) for (lvalue, rvalue) in pairs: # expr[expr] = expr if isinstance(lvalue, Subscript) and not isinstance(lvalue.subs[0], Sliceobj): self.assign_pair(lvalue, rvalue, func) # XXX use here generally, and in tuple_flow # expr.attr = expr elif isinstance(lvalue, AssAttr): self.assign_pair(lvalue, rvalue, func) # name = expr elif isinstance(lvalue, AssName): if (rvalue, 0, 0) not in self.gx.cnode: # XXX generalize self.visit(rvalue, func) self.visit(lvalue, func) lvar = self.default_var(lvalue.name, func) if isinstance(rvalue, Const): lvar.const_assign.append(rvalue) self.add_constraint((inode(self.gx, rvalue), inode(self.gx, lvar)), func) # (a,(b,c), ..) = expr elif isinstance(lvalue, (AssTuple, AssList)): self.visit(rvalue, func) self.tuple_flow(lvalue, rvalue, func) # expr[a:b] = expr # XXX bla()[1:3] = [1] elif isinstance(lvalue, Slice): self.slice(lvalue, lvalue.expr, [lvalue.lower, lvalue.upper, None], func, rvalue) # expr[a:b:c] = expr elif isinstance(lvalue, Subscript) and isinstance(lvalue.subs[0], Sliceobj): self.slice(lvalue, lvalue.expr, lvalue.subs[0].nodes, func, rvalue) # temp vars if len(node.nodes) > 1 or isinstance(node.expr, Tuple): if isinstance(node.expr, Tuple): if [n for n in node.nodes if isinstance(n, AssTuple)]: for child in node.expr.nodes: if (child, 0, 0) not in self.gx.cnode: # (a,b) = (1,2): (1,2) never visited continue if not isinstance(child, Const) and not (isinstance(child, Name) and child.name == 'None'): self.temp_var2(child, inode(self.gx, child), func) elif not isinstance(node.expr, Const) and not (isinstance(node.expr, Name) and node.expr.name == 'None'): self.temp_var2(node.expr, inode(self.gx, node.expr), func) def assign_pair(self, lvalue, rvalue, func): # expr[expr] = expr if isinstance(lvalue, Subscript) and not isinstance(lvalue.subs[0], Sliceobj): if len(lvalue.subs) > 1: subscript = Tuple(lvalue.subs) else: subscript = lvalue.subs[0] fakefunc = CallFunc(Getattr(lvalue.expr, '__setitem__'), [subscript, rvalue]) self.visit(fakefunc, func) inode(self.gx, lvalue.expr).fakefunc = fakefunc if len(lvalue.subs) > 1: inode(self.gx, lvalue.expr).faketuple = subscript if not isinstance(lvalue.expr, Name): self.temp_var2(lvalue.expr, inode(self.gx, lvalue.expr), func) # expr.attr = expr elif isinstance(lvalue, AssAttr): CNode(self.gx, lvalue, parent=func, mv=getmv()) self.gx.assign_target[rvalue] = lvalue.expr fakefunc = CallFunc(Getattr(lvalue.expr, '__setattr__'), [Const(lvalue.attrname), rvalue]) self.visit(fakefunc, func) def default_var(self, name, func): if isinstance(func, Function) and name in func.globals: return default_var(self.gx, name, None, mv=getmv()) else: return default_var(self.gx, name, func, mv=getmv()) def tuple_flow(self, lvalue, rvalue, func=None): self.temp_var2(lvalue, inode(self.gx, rvalue), func) if isinstance(lvalue, (AssTuple, AssList)): lvalue = lvalue.nodes for (i, item) in enumerate(lvalue): fakenode = CNode(self.gx, (item,), parent=func, mv=getmv()) # fake node per item, for multiple callfunc triggers self.gx.types[fakenode] = set() self.add_constraint((inode(self.gx, rvalue), fakenode), func) fakefunc = CallFunc(FakeGetattr3(rvalue, '__getitem__'), [Const(i)]) fakenode.callfuncs.append(fakefunc) self.visit(fakefunc, func) self.gx.item_rvalue[item] = rvalue if isinstance(item, AssName): lvar = self.default_var(item.name, func) self.add_constraint((inode(self.gx, fakefunc), inode(self.gx, lvar)), func) elif isinstance(item, (Subscript, AssAttr)): self.assign_pair(item, fakefunc, func) elif isinstance(item, (AssTuple, AssList)): # recursion self.tuple_flow(item, fakefunc, func) else: error('unsupported type of assignment', self.gx, item, mv=getmv()) def super_call(self, orig, parent): node = orig.node while isinstance(parent, Function): parent = parent.parent if (isinstance(node.expr, CallFunc) and node.attrname not in ('__getattr__', '__setattr__') and isinstance(node.expr.node, Name) and node.expr.node.name == 'super'): if (len(node.expr.args) >= 2 and isinstance(node.expr.args[1], Name) and node.expr.args[1].name == 'self'): cl = lookup_class(node.expr.args[0], getmv()) if cl.node.bases: return cl.node.bases[0] error("unsupported usage of 'super'", self.gx, orig, mv=getmv()) def visitCallFunc(self, node, func=None): # XXX clean up!! newnode = CNode(self.gx, node, parent=func, mv=getmv()) if isinstance(node.node, Getattr): # XXX import math; math.e # rewrite super(..) call base = self.super_call(node, func) if base: node.node = Getattr(copy.deepcopy(base), node.node.attrname) node.args = [Name('self')] + node.args # method call if isinstance(node.node, FakeGetattr): # XXX butt ugly self.visit(node.node, func) elif isinstance(node.node, FakeGetattr2): self.gx.types[newnode] = set() # XXX move above self.callfuncs.append((node, func)) for arg in node.args: inode(self.gx, arg).callfuncs.append(node) # this one too return elif isinstance(node.node, FakeGetattr3): pass else: self.visitGetattr(node.node, func, callfunc=True) inode(self.gx, node.node).callfuncs.append(node) # XXX iterative dataflow analysis: move there? inode(self.gx, node.node).fakert = True ident = node.node.attrname inode(self.gx, node.node.expr).callfuncs.append(node) # XXX iterative dataflow analysis: move there? if isinstance(node.node.expr, Name) and node.node.expr.name in getmv().imports and node.node.attrname == '__getattr__': # XXX analyze_callfunc if node.args[0].value in getmv().imports[node.node.expr.name].mv.globals: # XXX bleh self.add_constraint((inode(self.gx, getmv().imports[node.node.expr.name].mv.globals[node.args[0].value]), newnode), func) elif isinstance(node.node, Name): # direct call ident = node.node.name if ident == 'print': ident = node.node.name = '__print' # XXX if ident in ['hasattr', 'getattr', 'setattr', 'slice', 'type', 'Ellipsis']: error("'%s' function is not supported" % ident, self.gx, node.node, mv=getmv()) if ident == 'dict' and [x for x in node.args if isinstance(x, Keyword)]: error('unsupported method of initializing dictionaries', self.gx, node, mv=getmv()) if ident == 'isinstance': error("'isinstance' is not supported; always returns True", self.gx, node, mv=getmv(), warning=True) if lookup_var(ident, func, mv=getmv()): self.visit(node.node, func) inode(self.gx, node.node).callfuncs.append(node) # XXX iterative dataflow analysis: move there else: self.visit(node.node, func) inode(self.gx, node.node).callfuncs.append(node) # XXX iterative dataflow analysis: move there # --- arguments if not getmv().module.builtin and (node.star_args or node.dstar_args): error('argument (un)packing is not supported', self.gx, node, mv=getmv()) args = node.args[:] if node.star_args: args.append(node.star_args) # partially allowed in builtins if node.dstar_args: args.append(node.dstar_args) for arg in args: if isinstance(arg, Keyword): arg = arg.expr self.visit(arg, func) inode(self.gx, arg).callfuncs.append(node) # this one too # --- handle instantiation or call constructor = lookup_class(node.node, getmv()) if constructor and (not isinstance(node.node, Name) or not lookup_var(node.node.name, func, mv=getmv())): self.instance(node, constructor, func) inode(self.gx, node).callfuncs.append(node) # XXX see above, investigate else: self.gx.types[newnode] = set() self.callfuncs.append((node, func)) def visitClass(self, node, parent=None): if not getmv().module.builtin and not node in getmv().classnodes: error("non-global class '%s'" % node.name, self.gx, node, mv=getmv()) if len(node.bases) > 1: error('multiple inheritance is not supported', self.gx, node, mv=getmv()) if not getmv().module.builtin: for base in node.bases: if not isinstance(base, (Name, Getattr)): error("invalid expression for base class", self.gx, node, mv=getmv()) if isinstance(base, Name): name = base.name else: name = base.attrname cl = lookup_class(base, getmv()) if not cl: error("no such class: '%s'" % name, self.gx, node, mv=getmv()) elif cl.mv.module.builtin and name not in ['object', 'Exception', 'tzinfo']: if def_class(self.gx, 'Exception') not in cl.ancestors(): error("inheritance from builtin class '%s' is not supported" % name, self.gx, node, mv=getmv()) if node.name in getmv().classes: newclass = getmv().classes[node.name] # set in visitModule, for forward references else: check_redef(self.gx, node) # XXX merge with visitModule newclass = Class(self.gx, node, getmv()) self.classes[node.name] = newclass getmv().classes[node.name] = newclass newclass.module = self.module newclass.parent = StaticClass(newclass, getmv()) # --- built-in functions for cl in [newclass, newclass.parent]: for ident in ['__setattr__', '__getattr__']: func = Function(self.gx, mv=getmv()) func.ident = ident func.parent = cl if ident == '__setattr__': func.formals = ['name', 'whatsit'] retexpr = Return(Name('None')) self.visit(retexpr, func) elif ident == '__getattr__': func.formals = ['name'] cl.funcs[ident] = func # --- built-in attributes if 'class_' in getmv().classes or 'class_' in getmv().ext_classes: var = default_var(self.gx, '__class__', newclass) var.invisible = True self.gx.types[inode(self.gx, var)] = set([(def_class(self.gx, 'class_'), def_class(self.gx, 'class_').dcpa)]) def_class(self.gx, 'class_').dcpa += 1 # --- staticmethod, property skip = [] for child in node.code.getChildNodes(): if isinstance(child, Assign) and len(child.nodes) == 1: lvalue, rvalue = child.nodes[0], child.expr if isinstance(lvalue, AssName) and isinstance(rvalue, CallFunc) and isinstance(rvalue.node, Name) and rvalue.node.name in ['staticmethod', 'property']: if rvalue.node.name == 'property': if len(rvalue.args) == 1 and isinstance(rvalue.args[0], Name): newclass.properties[lvalue.name] = rvalue.args[0].name, None elif len(rvalue.args) == 2 and isinstance(rvalue.args[0], Name) and isinstance(rvalue.args[1], Name): newclass.properties[lvalue.name] = rvalue.args[0].name, rvalue.args[1].name else: error("complex properties are not supported", self.gx, rvalue, mv=getmv()) else: newclass.staticmethods.append(lvalue.name) skip.append(child) # --- children for child in node.code.getChildNodes(): if child not in skip: cl = self.classes[node.name] if isinstance(child, FunctionNode): self.visit(child, cl) else: cl.parent.static_nodes.append(child) self.visit(child, cl.parent) # --- __iadd__ etc. if not newclass.mv.module.builtin or newclass.ident in ['int_', 'float_', 'str_', 'tuple', 'complex']: msgs = ['add', 'mul'] # XXX mod, pow if newclass.ident in ['int_', 'float_']: msgs += ['sub', 'div', 'floordiv'] if newclass.ident in ['int_']: msgs += ['lshift', 'rshift', 'and', 'xor', 'or'] for msg in msgs: if not '__i' + msg + '__' in newclass.funcs: self.visit(FunctionNode(None, '__i' + msg + '__', ['self', 'other'], [], 0, None, Stmt([Return(CallFunc(Getattr(Name('self'), '__' + msg + '__'), [Name('other')], None, None))])), newclass) # --- __str__, __hash__ # XXX model in lib/builtin.py, other defaults? if not newclass.mv.module.builtin and not '__str__' in newclass.funcs: self.visit(FunctionNode(None, '__str__', ['self'], [], 0, None, Return(CallFunc(Getattr(Name('self'), '__repr__'), []))), newclass) newclass.funcs['__str__'].invisible = True if not newclass.mv.module.builtin and not '__hash__' in newclass.funcs: self.visit(FunctionNode(None, '__hash__', ['self'], [], 0, None, Return(Const(0)), []), newclass) newclass.funcs['__hash__'].invisible = True def visitGetattr(self, node, func=None, callfunc=False): if node.attrname in ['__doc__']: error('%s attribute is not supported' % node.attrname, self.gx, node, mv=getmv()) newnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[newnode] = set() fakefunc = CallFunc(FakeGetattr(node.expr, '__getattr__'), [Const(node.attrname)]) self.visit(fakefunc, func) self.add_constraint((self.gx.cnode[fakefunc, 0, 0], newnode), func) self.callfuncs.append((fakefunc, func)) if not callfunc: self.fncl_passing(node, newnode, func) def visitConst(self, node, func=None): if type(node.value) == unicode: error('unicode is not supported', self.gx, node, mv=getmv()) map = {int: 'int_', str: 'str_', float: 'float_', type(None): 'none', long: 'int_', complex: 'complex'} # XXX 'return' -> Return(Const(None))? self.instance(node, def_class(self.gx, map[type(node.value)]), func) def fncl_passing(self, node, newnode, func): lfunc, lclass = lookup_func(node, getmv()), lookup_class(node, getmv()) if lfunc: if lfunc.mv.module.builtin: lfunc = self.builtin_wrapper(node, func) elif lfunc.ident not in lfunc.mv.lambdas: lfunc.lambdanr = len(lfunc.mv.lambdas) lfunc.mv.lambdas[lfunc.ident] = lfunc self.gx.types[newnode] = set([(lfunc, 0)]) elif lclass: if lclass.mv.module.builtin: lclass = self.builtin_wrapper(node, func) else: lclass = lclass.parent self.gx.types[newnode] = set([(lclass, 0)]) else: return False newnode.copymetoo = True # XXX merge into some kind of 'seeding' function return True def visitName(self, node, func=None): newnode = CNode(self.gx, node, parent=func, mv=getmv()) self.gx.types[newnode] = set() if node.name == '__doc__': error("'%s' attribute is not supported" % node.name, self.gx, node, mv=getmv()) if node.name in ['None', 'True', 'False']: if node.name == 'None': # XXX also bools, remove def seed_nodes() self.instance(node, def_class(self.gx, 'none'), func) else: self.instance(node, def_class(self.gx, 'bool_'), func) return if isinstance(func, Function) and node.name in func.globals: var = default_var(self.gx, node.name, None, mv=getmv()) else: var = lookup_var(node.name, func, mv=getmv()) if not var: if self.fncl_passing(node, newnode, func): pass elif node.name in ['int', 'float', 'str']: # XXX cl = self.ext_classes[node.name + '_'] self.gx.types[newnode] = set([(cl.parent, 0)]) newnode.copymetoo = True else: var = default_var(self.gx, node.name, None, mv=getmv()) if var: self.add_constraint((inode(self.gx, var), newnode), func) def builtin_wrapper(self, node, func): node2 = CallFunc(copy.deepcopy(node), [Name(x) for x in 'abcde']) l = Lambda(list('abcde'), [], 0, node2) self.visit(l, func) self.lwrapper[node] = self.lambdaname[l] self.gx.lambdawrapper[node2] = self.lambdaname[l] f = self.lambdas[self.lambdaname[l]] f.lambdawrapper = True inode(self.gx, node2).lambdawrapper = f return f def parse_module(name, gx, parent=None, node=None): # --- valid name? if not re.match("^[a-zA-Z0-9_.]+$", name): print ("*ERROR*:%s.py: module names should consist of letters, digits and underscores" % name) sys.exit(1) # --- create module try: if parent and parent.path != os.getcwd(): basepaths = [parent.path, os.getcwd()] else: basepaths = [os.getcwd()] module_paths = basepaths + gx.libdirs absolute_name, filename, relative_filename, builtin = find_module(gx, name, module_paths) module = Module(absolute_name, filename, relative_filename, builtin, node) except ImportError: error('cannot locate module: ' + name, gx, node, mv=getmv()) # --- check cache if module.name in gx.modules: # cached? return gx.modules[module.name] gx.modules[module.name] = module # --- not cached, so parse module.ast = parse_file(module.filename) old_mv = getmv() module.mv = mv = ModuleVisitor(module, gx) setmv(mv) mv.visit = mv.dispatch mv.visitor = mv mv.dispatch(module.ast) module.import_order = gx.import_order gx.import_order += 1 mv = old_mv setmv(mv) return module shedskin-0.9.4/doc/0000775000175000017500000000000012157270661014026 5ustar srepmubsrepmubshedskin-0.9.4/doc/shedskin.rst0000664000175000017500000000317012157270661016371 0ustar srepmubsrepmub======== Shedskin ======== --------------------------------------------------- An experimental (restricted) Python-to-C++ Compiler --------------------------------------------------- :Date: 2013-06-16 :Version: 0.9.4 :Manual section: 1 SYNOPSIS ======== shedskin [OPTION]... FILE DESCRIPTION =========== Shed Skin is an experimental compiler, that can translate pure, but implicitly statically typed Python (2.4-2.6) programs into optimized C++. It can generate stand-alone programs or extension modules that can be imported and used in larger Python programs. Besides the typing restriction, programs cannot freely use the Python standard library (although about 25 common modules, such as random and re, are currently supported). Also, not all Python features, such as nested functions and variable numbers of arguments, are supported. OPTIONS ======= -a --ann Output annotated source code (.ss.py) -b --nobounds Disable bounds checking -e --extmod Generate extension module -f --flags Provide alternate Makefile flags -g --nogcwarns Disable runtime GC warnings -l --long Use long long integers -m --makefile Specify alternate Makefile name -n --silent Silent mode, only show warnings -o --noassert Disable assert statements -r --random Use fast random number generator (rand()) -s --strhash Use fast string hashing algorithm (murmur) -w --nowrap Disable wrap-around checking -x --traceback Print traceback for uncaught exceptions -L --lib Add a library directory shedskin-0.9.4/test.py0000664000175000017500000000002612157270661014610 0ustar srepmubsrepmubprint 'hello, world!' shedskin-0.9.4/setup.py0000775000175000017500000000223212157270661014775 0ustar srepmubsrepmub#!/usr/bin/env python from distutils.core import setup, Command import os class run_tests(Command): description = "run testsuite" user_options = [] def initialize_options(self): pass def finalize_options(self): self.cwd = os.getcwd() ss_dir = os.path.abspath(__file__).split(os.path.sep)[:-1] ss_dir.append('tests') self.tests_dir = os.path.sep.join(ss_dir) def run(self): os.chdir(self.tests_dir) os.system('./run.py') os.chdir(self.cwd) setup(name='shedskin', version='0.9.4', description='Shed Skin is an experimental compiler, that can translate pure, but implicitly statically typed Python programs into optimized C++. It can generate stand-alone programs or extension modules that can be imported and used in larger Python programs.', url='http://code.google.com/p/shedskin/', scripts=['scripts/shedskin'], cmdclass={'test':run_tests}, packages=['shedskin'], package_data={'shedskin': ['lib/*.cpp', 'lib/*.hpp', 'lib/builtin/*.cpp', 'lib/builtin/*.hpp', 'lib/*.py', 'lib/os/*.cpp', 'lib/os/*.hpp', 'lib/os/*.py', 'FLAGS*', 'illegal']}, ) shedskin-0.9.4/tests/0000775000175000017500000000000012157270661014423 5ustar srepmubsrepmubshedskin-0.9.4/tests/136.py0000664000175000017500000001014312157270661015305 0ustar srepmubsrepmub print 'hello, world!' from random import * x = 10*random() from sys import stdin, stdout for line in sorted(file('testdata/words')): print sorted(set(line.strip())) #stdout.write(stdin.read()) print int('12'), int('ff', 16), int('20', 8) print '%d %s %x' % (255, '255', 255), '%o' % 10 char = 2 typedef = 3 typename = 4 std = 6 public = 7 template = 8 virtual = 9 static = 10 namespace = 11 using = 12 private = 13 protected = 14 new = 15 delete = 16 t = (1,2) t2 = (1.0,2) """ "\n\t" """ print """ "\n\t" """ print set({1:2}) aa = set() aa.add(1); aa.add(2) print aa.union(set([1,3])) l = [1,2,3] if l: print l while l: print l.pop() a, b = "ab" print a, b a, b, c, d, e = "abcde" print a, b, c, d, e a, b = tuple("ab") print a, b a, b, c, d, e = tuple("abcde") print a, b, c, d, e print max({1:2, 3:4}) s1 = set([1,2,3]) s2 = set([3,4,5]) print str(s1.copy())+' - '+str(s2.copy())+' =', s1.difference(s2), '=', s1 - s2 s1.difference_update(s2) s2.clear() print s1, s2 s1.remove(1) s1.discard(2) print s1, s1.issubset(s2), set([2,1]).issubset(set([3,1,2,4])) s1.update(set([1,2])) print s1.issuperset(set([1])) af = set([1,2,3]) print af.intersection(s1) af.intersection_update(s1) print af s3 = set([3,2,1]) while s3: print s3.pop() s6 = set(["a", "b", "c"]) assert not s6.isdisjoint(s6) assert not s6.isdisjoint(["a"]) assert s6.isdisjoint(["d"]) import random as rr from random import randint as ri #print rr.randint(0,0), rr.randint(0,2), ri(8, 12) def union(): pass union() for (i,ee) in reversed(list(enumerate(reversed([1,2,3])))): print i, ee print set([1,2,3]).symmetric_difference(set([2,3,4])) ################################################################### sa1 = set([1,2,3]) sa2 = set([3,4,5]) sa3 = set([4,5,6]) assert sa1.difference(sa2) == set([1, 2]) assert sa1.difference(sa3) == sa1 assert sa1.difference([1, 2, 3]) == set([]) assert sa2.difference([4, 5, 6]) == set([3]) assert sa1.intersection([4, 5, 6]) == set([]) assert sa1.intersection([3, 4, 5, 6]) == set([3]) sa4 = set(["a", "b", "d"]) sa5 = set(["d", "e", "f"]) assert sa4.intersection(sa5) == sa4.intersection(["d", "e", "f"]) ################################################################### print {1:2, 2:3}.copy() ff = file('testdata/bla','w') print >>ff, 'niet op scherm' print >>file('testdata/bla2','w'), 'huhuhu' ss = "abcdefghijklmnopqrst"; print ss[2:9:2] mm = [""]; print ["*" for c in mm if c] print "yxxy".split("x") print " ".split(" ") print "hopplop".split("op") print "x\t\nxxx\r".split() class CNF: def CNF(self): print 'CNF' cnf = CNF() cnf.CNF() class Tester: def __init__(self): self.data = None def isempty(self): return self.data is None Tester().isempty() class void: def bla(self): pass vv = void(); vv.bla() def mapp(): allchr = [chr(c) for c in xrange(256)] return allchr print mapp()[-10:] ap = set([1]) bp = set([2]) print ap > bp, ap >= bp, ap == bp, ap != bp, [2,3] <= [1,2,3] print sorted([[3],[2,1],[4,5,6]]) class Test: def __init__(self): self.n = 5 print [i for i in range(self.n)] test = Test() print sum([1,2,3]) print sum([1,2,3],4) print sum([[1],[2],[3,4]], [0]) print sum([[1],[2],[3,4]], []) print dict.fromkeys([1,2,3]) print dict.fromkeys([1,2,3]) print dict.fromkeys([1,2,3], 7) print dict.fromkeys([1,2,3], 4.0) print dict.fromkeys([1,2,3], 'string') if 4 > 2 > 1: print 'hoihoi' print 1 <= 2 == 2 if 1<2: print '1<2' class fred: def __eq__(self, b): return True fr = fred() if fr == fr: print 'fred = fred' # the following line should give a warning if fr == fr == fr: print 'fred = fred = fred' mmt = mma, mmb = 3, 4 print mmt, mma, mmb mmc = mmd = mme = 1 print mmc, mmd, mme mmf = mmg = mmh = 9*mma print mmf, mmg, mmh mma = 9 from math import sqrt mmi = mmj = sqrt(mma) print int(mmi), int(mmj) mma, mmb = mmb, mma print mma, mmb mma, mmb = 1, 2 print mma, mmb mmt = 1, 2 mma, mmb = mmt print mmt, mma, mmb mma,mmb = mmt = mmc,mmd = 1,2 print mma, mmb mma, mmb = mmb, mma = 1, 2 print mma, mmb meuk = [1,2] mma, mmb = meuk print mma, mmb mmx = [1,2] print mmx from sys import exit exit() exit(-1) shedskin-0.9.4/tests/115.py0000664000175000017500000000025412157270661015304 0ustar srepmubsrepmub d = (1, (1.1, 'u')) a, (b, c) = d e, f = d for x,(y,z) in [d]: x y z l = [((v,u),w) for u,(v,w) in [d]] print 'uh', '%.2f %d' % l[0][0], l[0][1], len(l) shedskin-0.9.4/tests/133.py0000664000175000017500000000165412157270661015311 0ustar srepmubsrepmub def mandelbrot(max_iterations=1000): # By Daniel Rosengren, modified # http://www.timestretch.com/FractalBenchmark.html # See also vectorized Python+Numeric+Pygame version: # http://www.pygame.org/pcr/mandelbrot/index.php bailout = 16 for y in xrange(-39, 39): line = [] for x in xrange(-39, 39): cr = y/40.0 - 0.5 ci = x/40.0 zi = 0.0 zr = 0.0 i = 0 while True: i += 1 temp = zr * zi zr2 = zr * zr zi2 = zi * zi zr = zr2 - zi2 + cr zi = temp + temp + ci if zi2 + zr2 > bailout: line.append(" ") break if i > max_iterations: line.append("#") break print "".join(line) for x in range(10): mandelbrot() shedskin-0.9.4/tests/6.py0000664000175000017500000000004412157270661015140 0ustar srepmubsrepmub def bla(): return 8 a = bla() shedskin-0.9.4/tests/185.py0000664000175000017500000000172112157270661015313 0ustar srepmubsrepmub # file.next print file('run.py').next().strip() # re.groups returns tuple import re m = re.match(r"(\d+)\.?(\d+)?", "24") groups = m.groups() print groups # overloading __getitem__ problem class Vector3f: def __getitem__(self, key): return 19 v = Vector3f() print v[0] # more string formatting print '!'+('%06d%6r%6.2f' % (18,'hoi', 1.17))+'!' print '!'+('%0*d%*s%*.*f' % (6,18,6,'hoi',8,2,1.171))+'!' # and/or funtest (already worked) hoppa = (17, 18) a, b = hoppa or (19,20) print a, b hoppa = None a, b = hoppa or (19,20) print a, b x = [1,2] y = [3,4,5] c = x and y or None print c y = None z = None c = x and y or z print c # TI problem (seeding bool) def rungame(strategy, verbose): strategy() def s_bestinfo(): z = [0] print z def s_worstinfo(): z = [0] print z def eval_strategy(strategy): rungame(strategy, False) def main(): eval_strategy(s_bestinfo) eval_strategy(s_worstinfo) main() # test import signal shedskin-0.9.4/tests/38.py0000664000175000017500000000025212157270661015226 0ustar srepmubsrepmub def hu(n, s=-1): # s: [int], n: [int] return [1] # [list(int)] a = [i for i in hu(10)] # [] shedskin-0.9.4/tests/4.py0000664000175000017500000000001212157270661015131 0ustar srepmubsrepmub a = [1] shedskin-0.9.4/tests/191.py0000664000175000017500000001447512157270661015322 0ustar srepmubsrepmub # defaultdict fixes from collections import defaultdict dnone = defaultdict() #dnone = defaultdict(None) dnone = defaultdict(None, [(8, 9)]) dnone = defaultdict(None, dnone) dnone[4] = 5 try: print dnone[5] except KeyError: print 'keyerror' print sorted(dnone.items()) print sorted(defaultdict(str, {1: 'hoi'}).items()) # self variable (TODO: fix analyze_virtuals) self = 4 print self # bin, oct, hex print hex(200), hex(-200), hex(0) print oct(200), oct(-200), oct(0) print bin(200), bin(-200), bin(0) class A(object): def __index__(self): return 42 a = A() print bin(a) print bin(1==2), bin(1!=2) # game of life argv=[0,3,3] r,c=map(int,argv[1:]) p,g=r*c,range z=g(p) w=lambda x:sum((abs(x%r-v%r)<2)&(abs(x/r-v/r)<2)&d[v]for v in z) for m in g(2**p): d=[m>>x&1 for x in z] if all(d[x]&(2 8 print bool_e if bool_e: print 'e' if not bool_e: print '!e' queue = [] augmented = 0 if queue and not augmented: print 'queue' a_bool = bool('hoppa') print a_bool print True & False, True & True, True & 1, 1 & False print True-2, False*3, 2+True, 2-False, 4*True print True*[1,2] har3 = True & True & 1 print har3 niks = None print bool(niks), bool(None) # generator expressions ia = (2*x for x in range(10)) print sum(ia) ib = ((str(x+y) for x in range(10)) for y in range(4)) print [''.join(ur) for ur in ib] ic = [(x+3 for x in range(10)) for y in range(4)] print [sum(ar) for ar in ic] id = ([2.0*(x+y) for x in range(10)] for y in range(4)) print sum(sum(uhh) for uhh in id) ie = ([x,y] for x in range(10) for y in range(4)) print len(list(ie)) class meuk: def layout(self): return (x for x in 'abc') waf = meuk().layout() print list(waf) # random module improvements import random random.seed(1) print random.triangular() print random.triangular(high=1.1, low=0.0) print random.triangular(0.1) print random.triangular(-2, 2) print random.triangular(-2.0, 2.1, 1.5) print random.triangular(mode=1.5) print random.triangular(0, 5, 0) random.seed() random.seed('seed') random.seed(8.0) random.seed(None) random.seed(4) print random.random() # itertools.product fix import itertools print list(itertools.product([0,1,2], [0,1,2])) print list(itertools.product([-1, 0, 1], repeat=2)) print list(itertools.product(iter([1, 2, 3]), iter([4, 5]), repeat = 2)) print list(itertools.product(iter([1, 2, 3]), iter([4, 5]), iter([6, 7, 8]), repeat = 2)) # hashing print hash(-1) print hash(True) print hash(12.345) #and,or mixing 1 or 'hoppa' or [1,2] plb = 1 or 9 plc = not (1 or 9) not (1 or 'hopsa') pld = 1 and 9 ple = not (1 and 9) if 1 or 'hoei': print 'ba' while 1 or 'hoei': print 'uhoh' break while (1 or ('blah' and 1)): break print [plx for plx in range(4) if plx and 'hoei'] print [plx for plx in range(4) if plx and 1] if not (1 and 'hoei'): print 'oh' # copy, deepcopy and None import copy class TreeNode: def __init__(self): self.hoppa = [1] self.hoppa = None tn = TreeNode() tn2 = copy.deepcopy(tn) blar = tn2.hoppa blar2 = copy.copy(blar) # SystemExit import sys try: exit(4) except SystemExit, baratie: print 'jaja deze ook' try: sys.exit(4) sys.exit('hoppa') raise SystemExit() except SystemExit, baratie: print 'exit with', baratie.code, baratie if False: # difference when run from this file sys.exit('aha') sys.exit(baratie.code) # comparison model class Bla: def __eq__(self, o): print o.niks bla1 = Bla() bla1 is None # crash print None in [[None]] # casting to builtins (inference not enough) definiteinnerlist=[1] outerlist=[[1]] #emptyinnerlist=[] outerlist.append(definiteinnerlist) outerlist.append([]) outerlist[0] = [] print outerlist dikkie={1:1} dikkie2 = {1:dikkie} dikkie2[2] = {} dikkie2[3] = dikkie print sorted(dikkie2.items()) # more test cases print reduce(lambda a,b: a+b, '34') print reduce(lambda a,b: a+b, '345', '6') print reduce(lambda a,b: a+b, [3,5,7], 2) print reduce(lambda a,b: a-b, set([3,5,7])) print any([1,2]), all([0,1]), any([]), all([]) print any(set([1,2])), all(set([0,1])), all({}) print any(' '), any(''), all(' '), all('') print []==[[]] print []==None print None==[[]] print None==[1] print [None] == [[1]] print [[1]] == [] print [[]] == [[1]] shedskin-0.9.4/tests/140.py0000664000175000017500000000100312157270661015273 0ustar srepmubsrepmub class father(object): def __init__(self,a): self.a=a b=1 def f(self,x): return x*self.a class son(father): def g(self,x): return x*self.a*self.a myfather=father(3) print myfather.f(4) myson=son(4) print myson.g(5) class mother(object): def __init__(self,a): self.a=a b=1 def f(self,x): return x*self.a class daughter(mother): def g(self,x): return x*self.a*self.a mydaughter = daughter(4) print mydaughter.g(5) shedskin-0.9.4/tests/179.py0000664000175000017500000000542612157270661015324 0ustar srepmubsrepmub #simple fixes print 8+(2 if 1 else 3) print repr('\377ai\37aoi\001123\00hoi\01hoi\0hoi') # add_strs() print 'x'+'x'+'x' #os.path import os.path print os.path.join('heuk') print os.path.join('heuk', 'emeuk') print os.path.join('heuk', 'emeuk', 'meuk') from os.path import * print join('a','b','c') realpath('run.py') commonprefix(['xxx', 'xxxx']) normcase('hoei') splitext('hoei/woei') splitdrive('hoei/woei') basename('hoei/woei') dirname('hoei/woei') exists('testdata') lexists('testdata') isdir('testdata') isfile('testdata') def bleh(arg, top, names): pass def bleh2(arg, top, names): pass walk('testdata', bleh, 77) walk('testdata', bleh2, 'hoei') getsize('run.py') getatime('run.py') getctime('run.py') getmtime('run.py') #locally overloading builtin definition str = '4' t = ('aha', 2) str, x = t def heuk(str): pass heuk('aha') for str in ['hah']: pass [0 for str in ['hah']] for (str,bah) in [('hah', 'bah')]: pass [0 for (str,bah) in [('hah', 'bah')]] #missing string methods print 'ab\ncd\r\nef\rghi\n'.splitlines() print 'ab\ncd\r\nef\rghi\n'.splitlines(1) print int('This Is A Title'.istitle()) print int('This is not a title'.istitle()) print 'a and b and c'.partition('and') print 'a and b and c'.rpartition('and') #default argument problem def msplit(sep=0, spl=-1): return [''] cnf = msplit() #ctype import string print repr(string.lowercase) print repr(string.uppercase) print repr(string.letters) print repr(string.printable) print repr(string.punctuation) print repr(string.whitespace) print repr(string.digits) print repr(string.hexdigits) print repr(string.octdigits) #dict.get problem print {'wah': 2}.get('aap', 3) #finish getopt from getopt import getopt, gnu_getopt args = ['-ahoei', '--alpha=4', 'meuk'] print getopt(args, "a:b", ["alpha=", "beta"]) print getopt(args, "a:b", {"alpha=" : 0, "beta" : 0}) print gnu_getopt(args, "a:b", ["alpha=", "beta"]) print gnu_getopt(args, "a:b", {"alpha=" : 0, "beta" : 0}) print getopt(args, "a:b", "alpha=") print gnu_getopt(args, "a:b", "alpha=") #OSError import os try: os.chdir('ontehunoe') except OSError, e: # print e # print repr(e) print e.errno # print e.strerror print e.filename #int(), float(), str(); test all print int(), float(), list(), dict(), set(), tuple(), frozenset(), # XXX repr(str()) #collections.defaultdict from collections import defaultdict s1 = 'mississippi' d1 = defaultdict(int) for k1 in s1: d1[k1] += 1 print sorted(d1.items()) s2 = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] d2 = defaultdict(list) for k2, v2 in s2: d2[k2].append(v2) print sorted(d2.items()) s3 = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)] d3 = defaultdict(set) for k3, v3 in s3: d3[k3].add(v3) print sorted(d3.items()) shedskin-0.9.4/tests/33.py0000664000175000017500000000046012157270661015222 0ustar srepmubsrepmub #a = 1 # [int] #a = 1 # [int] b = 1 # [int] a = 'hoi' # [str] print a # [int, str] print b # [int] shedskin-0.9.4/tests/71.py0000664000175000017500000000054512157270661015230 0ustar srepmubsrepmub a = (1,) # [tuple(int)] b = [a] # [list(tuple(int))] c = [1] # [list(int)] e = [1.0] # [list(float)] d = [c] # [list(list(int))] f = [e] # [list(list(float))] shedskin-0.9.4/tests/102.py0000664000175000017500000000175412157270661015306 0ustar srepmubsrepmub a = (1,2) # [tuple2(int, int)] a in [(1,2),(3,4)] # [int] (1,2) in [(1,2),(3,4)] # [int] a == (2,3) # [] (1,2) == a # [] b = [1] # [list(int)] b == [] # [int] [] == b # [list(int)] b == [1] # [int] 1 in b # [int] 1 in [1] # [int] [1] == [1] # [int] #b == 'hoi' # [int] #'hoi' == b # [int] 'hoi' == 'hoi' # [int] for c in [(2,3),(3,4)]: # [tuple2(int, int)] if c == (2,3): # [] pass [v for v in [(1,),(2,),(3,)] if v != (1,)] # [list(tuple(int))] e = 1 a in [(1,e)] # [int] shedskin-0.9.4/tests/139.py0000664000175000017500000000662612157270661015323 0ustar srepmubsrepmub def gen(): m = [] l = [1] m.append(l) return m m = gen() print m z=1 and 1 a = range(10) a.__delslice__(1,4) print a import copy b=[[0,1]] c=copy.deepcopy(b) c[0].pop(0) print c, b AH = [1,2] print [[1 for x in AH] for y in AH] for x in xrange(2,1,-1): print x print []*10 a = range(5); del a[0:2]; print a class Individual: def __init__(self, ngenes): self.genome = [True] * ngenes individual = Individual(3) individual.genome[1] = not individual.genome[1] print individual.genome #d,e = -1e500, 1e500; print d, e if not (1==2 or 2==3): print 'jammer' class wahoe: def hoe(self): for self.smurf in range(3): print self.smurf wahoe().hoe() def fun(): global aa aa += 1 return 200 aa = 1 print aa print 97 <= fun() <= 122 print aa print 200 == fun() == 150 print aa def true(): return True print true() == true() in [True] print 1 < 2 < 3 < 4 print 'hash(l)', [hash(i) for i in xrange(3)] #print 'hash(float(l))', [hash(float(i)) for i in xrange(3)] XXX difference [hash(float(i)) for i in xrange(3)] class Toggle: def __init__(self, start_state): self.bool = start_state def value(self): return(self.bool) def activate(self): self.bool = not self.bool return(self) t = Toggle(True) print t.value() t.activate() print t.value() print [None for i in xrange(3)] maxint = 2147483647 minint = -maxint-1 print maxint print minint s1 = set((1,2,3)); s4 = set([1,4]); print s4 > s1, s4 >= s1 #python 2.4 #print set([1,2,3]).issubset((1,2,3)) #print set([1,2,3]).issuperset((1,2,3)) s1 = set((1,2,3)); s2 = set([1,4]); print sorted(set((1,2,3)) & set([1,4])) s1 = set((1,2,3)); s2 = set([1,4]); s3 = set([5]); print sorted(s1 | s2 | s3) # associativity print sorted(set([1,2]).union((2,)).union([3])) s5 = set("abcd"); s6 = set("cdef"); print sorted(s5 ^ s6) # mixing print s1 &= s2 print sorted(s2) s1 -= s2 print sorted(s1) s1 ^= s2 print sorted(s1) s7 = set([(1,2),(1,3),(1,4)]) s7 |= set([(1,6)]) # not (1,6) print sorted(s7) ah = [(2.1, 4), (1.0, 9), (1.0, 7)] ah.sort() print ['%.2f %d' % trt for trt in ah] p, q, r = set('ab'), set('abcde'), set('def') print q <= q, q > r, q >= r y = ("a", "b", "c") zed = "a" print zed is y[0] class Printer: def __str__(self): return "10" def __repr__(self): return "20" pr = Printer(); print Printer(), pr, repr(pr) class Printer2: def __repr__(self): return "20" print Printer2(), repr(Printer2()) class Printer3: pass #print Printer3() def test(): pass print test() l = []; print [l.append(i%2) for i in xrange(5)] def test2(x): if x: return (1,"a") print test2(1) print test2(0) uh = [1] uh = None print uh def fun3(): a = 1 result = fun3() print result s = dict.fromkeys(xrange(2)) for i in xrange(2): if i in s: print "*" b2 = 3; b2 *= 1.5; print '%.1f' % b2 class A: def __init__(self, a): self.a = a def __add__(self, V): return A(self.a+V.a) def __mul__(self, n): return A(self.a*n) v = A(1); v += A(2); print v.a v = A(1) + A(2); print v.a print (v * 2).a v *= 2; print v.a def mut1(aap=[1]): aap.append(2) print aap mut1(); mut1(); mut1() def mut2(a=None): if a is None: a = [] print a a.append("*") mut2(); mut2(); mut2() def fun2(a, b=4, c='1'): return (a+b)*c print fun2(c='2', a=3, b=4), fun2(a=1, b=2, c='8') def fun4(a, b=7, c='1', d=1, e=1.0): print a, b, c, d, int(e) fun4(1,2,e=2.0,c='8') shedskin-0.9.4/tests/147.py0000664000175000017500000000162512157270661015314 0ustar srepmubsrepmub class meuk: pass try: raise meuk() print 'bad!' except meuk: print 'ok!' except int: print 'bad..' try: assert 1 == 0 except AssertionError: print 'crap!' def crapfunction(): a,b,c=1,2,3 assert a > b < c, "the universe won't collapse" try: crapfunction() except AssertionError, msg: print 'more crap!', msg class ueuk: def __init__(self, msg): self.msg = msg def __repr__(self): return 'ueukrepr!' #try: # raise ueuk, 'aha! error.' #except ueuk, x: # print x.msg try: raise ueuk('aha! error.') except ueuk, x: print x try: hum = [1,2,3] print hum.index(4) except ValueError: print 'exceptions are stupid :D' try: raise ValueError('exceptions are stupid :D') except ValueError, y: print y try: {1:2}[3] except KeyError, z: print 'bah!', z try: [1].index(2) except ValueError, v: print 'hah' shedskin-0.9.4/tests/166.py0000664000175000017500000000244412157270661015315 0ustar srepmubsrepmub# optional start/end arguments for str.{count, startswith, endswith} def hop(b): if b: print 1 else: print 0 hop('hoi'.startswith('ho', 0)) hop('hoi'.startswith('ho', 0, 3)) hop('hoi'.startswith('ho', 0, -1)) hop('hoi'.endswith('oi')) hop('hoi'.endswith('oi', 0, 3)) hop('hoi'.endswith('ho', 0, -1)) hop('hoi'.endswith('ho', -3, 2)) hop('hoi'.startswith(':', 3)) hop('hoi:'.startswith(':', 3)) print 'hoooi'.count('o') print 'hoooi'.count('o', 2) print 'hoooi'.count('o', 0, -2) # mother contour (6,5) -> (1,1) instead of (1,5) def getopt(args, longopts): opts = [] opts.append(('',)) do_longs(opts, longopts) def do_longs(opts, longopts): [o for o in longopts] wa = [''] getopt(wa, wa) # cStringIO.StringIO, file.seek import cStringIO, sys s = cStringIO.StringIO(file('testdata/hopsakee').read()) print s.readlines() s = file('testdata/hopsakee') print s.name print s.mode print s.read() f = file('testdata/hopsakee') print f.read() f.seek(0) print f.read() f.close() s = cStringIO.StringIO('blaat') s.seek(-3, 2) print s.read() s = cStringIO.StringIO() print s.tell() s.write('hallo\njoh') print s.tell() s.seek(0, 0) print s.tell() print s.readlines() print s.tell() s.seek(0, 0) print s.tell() s.write('hoi') print s.tell() print s.readlines() print s.tell() blah = set([]) shedskin-0.9.4/tests/errs/0000775000175000017500000000000012157270661015376 5ustar srepmubsrepmubshedskin-0.9.4/tests/errs/6.py0000664000175000017500000000250112157270661016113 0ustar srepmubsrepmubclass AA: def __init__(self, x): if x == 1: self.a = AA(0) self.b = AA(0) def __del__(self): pass def __iter__(self): pass def __call__(self): pass aa = AA(1) gg = {1:2,3:4,5:7} del aa.a, aa.b, gg[1], gg[5] print gg class meuh: attr = 4 def __init__(self): b = self.attr # self.attr = 2 XXX add warning meuh() meuh().attr import struct data = 'data' unpacked = struct.unpack(' 2 and different types of elements #*WARNING* 4.py:10: tuple with length > 2 and different types of elements shedskin-0.9.4/tests/errs/33.py0000664000175000017500000000011012157270661016165 0ustar srepmubsrepmubfrom .. import wa #*ERROR* 33.py:1: relative imports are not supported shedskin-0.9.4/tests/errs/11.py0000664000175000017500000000101512157270661016166 0ustar srepmubsrepmub P = [] [1] if True else P print [1] == P P2 = [[1],P] S = [None] S2 = [['s'],S] def woef(): P = [] return [1.0] return P woef() wa = [1] x = [1.0] x = [1] x = wa r = [[1]] c = [[1.0]] c = [[1]] c = r bp = [True] print [1] == bp #*WARNING* 11.py:3: incompatible types #*WARNING* 11.py:4: incompatible types #*WARNING* 11.py:6: incompatible types #*WARNING* 11.py:14: incompatible types #*WARNING* 11.py:20: incompatible types #*WARNING* 11.py:25: incompatible types #*WARNING* 11.py:28: incompatible types shedskin-0.9.4/tests/errs/14.py0000664000175000017500000000010012157270661016163 0ustar srepmubsrepmubimport baviaan #*ERROR* 14.py:1: cannot locate module: baviaan shedskin-0.9.4/tests/errs/39.py0000664000175000017500000000007412157270661016204 0ustar srepmubsrepmub class A(B): pass #*ERROR* 39.py:2: no such class: 'B' shedskin-0.9.4/tests/errs/28.py0000664000175000017500000000016212157270661016200 0ustar srepmubsrepmub import struct fmt = 'HH' c, d = struct.unpack(fmt+fmt, 'ntaoheu') #*ERROR* 28.py:5: non-constant format string shedskin-0.9.4/tests/errs/30.py0000664000175000017500000000007012157270661016167 0ustar srepmubsrepmub exec('1+1') #*ERROR* 30.py:3: 'exec' is not supported shedskin-0.9.4/tests/errs/2.py0000664000175000017500000000132212157270661016107 0ustar srepmubsrepmubimport camera camera.runpix() a = 1 a = '1' [1.0, []] def hop(): b = [] b.append([1]) b.append(['s']) hop() #*WARNING* 2.py:10: expression has dynamic (sub)type #*WARNING* 2.py:7: expression has dynamic (sub)type #*WARNING* camera.py:9: expression has dynamic (sub)type #*WARNING* 2.py:12: expression has dynamic (sub)type #*WARNING* camera.py:9: expression has dynamic (sub)type #*WARNING* 2.py:11: expression has dynamic (sub)type #*WARNING* 2.py: Variable 'a' has dynamic (sub)type #*WARNING* 2.py: Variable (Function hop, 'b') has dynamic (sub)type #*WARNING* camera.py: Variable (Function runpix, 'x') has dynamic (sub)type #*WARNING* camera.py: Variable (Function runpix, 't') has dynamic (sub)type shedskin-0.9.4/tests/errs/31.py0000664000175000017500000000020112157270661016164 0ustar srepmubsrepmub if __name__ == '__main__': import random #*ERROR* 31.py:3: please place all imports (no 'try:' etc) at the top of the file shedskin-0.9.4/tests/errs/26.py0000664000175000017500000000254012157270661016200 0ustar srepmubsrepmubprint pow(9,-2) a = 1 a = lambda x: x import re re.findall() import socket socket.socket().gettimeout() map(lambda a,b,c:a+b+c, range(2), range(3), range(4)) import itertools itertools.izip_longest(range(2), range(3)) c = 'huhp' import array array.array('x') array.array(c) l = lambda a: a l = lambda a,b: a+b l() import struct aa = 'b' mm, = struct.unpack(aa, 'ntohuntaehu') class meh: @staticmethod def woeh(a,b): pass def hop(self): self.woeh(9,10) m=meh() m.hop() m.woeh(1,2) #*WARNING* function mixed with non-function #*WARNING* 26.py: Variable 'a' has dynamic (sub)type #*WARNING* 26.py:1: pow(int, int) returns int after compilation #*WARNING* 26.py:6: 'findall' does not work with groups (use 'finditer' instead) #*WARNING* 26.py:9: socket.set/gettimeout do not accept/return None #*WARNING* 26.py:11: default fillvalue for 'map' becomes 0 for integers #*WARNING* 26.py:14: default fillvalue for 'izip_longest' becomes 0 for integers #*WARNING* 26.py:18: non-constant or unsupported type code #*WARNING* 26.py:19: non-constant or unsupported type code #*WARNING* 26.py:23: calling functions with different numbers of arguments #*WARNING* 26.py:27: assuming constant format string #*WARNING* 26.py:35: staticmethod 'woeh' called without using class name #*WARNING* 26.py:39: staticmethod 'woeh' called without using class name shedskin-0.9.4/tests/errs/10.py0000664000175000017500000000014012157270661016163 0ustar srepmubsrepmuba = 4 try: pass except a: pass #*ERROR* 10.py:4: unknown or unsupported exception type shedskin-0.9.4/tests/errs/18.py0000664000175000017500000000012412157270661016175 0ustar srepmubsrepmub def func(*aha): pass #*ERROR* 18.py:2: argument (un)packing is not supported shedskin-0.9.4/tests/errs/29.py0000664000175000017500000000016112157270661016200 0ustar srepmubsrepmubimport struct a, = struct.unpack('y', 'tnohu') #*ERROR* 29.py:3: bad or unsupported char in struct format: 'y' shedskin-0.9.4/tests/errs/27.py0000664000175000017500000000017012157270661016176 0ustar srepmubsrepmubimport struct fmt = 'HH' fmt = 'ii' a, b = struct.unpack(fmt, 'tnaoheu') #*ERROR* 27.py:5: non-constant format string shedskin-0.9.4/tests/errs/23.py0000664000175000017500000000013712157270661016175 0ustar srepmubsrepmub class bleh(myset, myset2): pass #*ERROR* 23.py:2: multiple inheritance is not supported shedskin-0.9.4/tests/errs/37.py0000664000175000017500000000010012157270661016170 0ustar srepmubsrepmubx = type(1) #*ERROR* 37.py:1: 'type' function is not supported shedskin-0.9.4/tests/errs/12.py0000664000175000017500000000114712157270661016175 0ustar srepmubsrepmubf = file('meuh', 'w') del f class A: pass a = A() a.b = 'woef' del a.b azo = 1 azo = lambda x: -x def blah(b): pass blah(1) blah(blah) def hoep(a,b,c=4): pass if False: hoep(1) hoep(1,2) hoep(1,2,3) hoep(1,2,3,4) #*WARNING* 12.py:2: 'del' has no effect without refcounting #*WARNING* 12.py:8: 'del' has no effect without refcounting #*WARNING* 12.py: Variable 'azo' has dynamic (sub)type #*WARNING* 12.py: Variable (Function blah, 'b') has dynamic (sub)type #*WARNING* 12.py:23: call with incorrect number of arguments #*WARNING* 12.py:26: call with incorrect number of arguments shedskin-0.9.4/tests/errs/40.py0000664000175000017500000000013412157270661016171 0ustar srepmubsrepmubclass beh: p = property(1, [4]) #*ERROR* 40.py:2: complex properties are not supported shedskin-0.9.4/tests/errs/9.py0000664000175000017500000000012212157270661016113 0ustar srepmubsrepmubtry: pass finally: pass #*ERROR* 9.py:1: 'try..finally' is not supported shedskin-0.9.4/tests/errs/1.py0000664000175000017500000000023512157270661016110 0ustar srepmubsrepmub#{ print 'move along' #} class X: def m(self): pass def m(self): pass #*ERROR* 1.py:8: function/class redefinition is not allowed shedskin-0.9.4/tests/errs/8.py0000664000175000017500000000010412157270661016112 0ustar srepmubsrepmubraise ValueError, 'muhh' #*ERROR* 8.py:1: unsupported raise syntax shedskin-0.9.4/tests/errs/25.py0000664000175000017500000000015212157270661016174 0ustar srepmubsrepmub class U: def V(W): pass #*ERROR* 25.py:3: formal arguments of method must start with 'self' shedskin-0.9.4/tests/errs/36.py0000664000175000017500000000006712157270661016203 0ustar srepmubsrepmub l[...,] #*ERROR* 36.py:2: ellipsis is not supported shedskin-0.9.4/tests/errs/15.py0000664000175000017500000000007412157270661016176 0ustar srepmubsrepmub ahoi = u'ahoi' #*ERROR* 15.py:2: unicode is not supported shedskin-0.9.4/tests/errs/7.py0000664000175000017500000000312312157270661016115 0ustar srepmubsrepmubl = [] l.append(l) r = [] r = l snuif friet() class muf: def __init__(self): a = self.hoppa l = [a] l[0]() def hoppa(self): print 'hoppa' muf() class M(object): pass class P2(M): def handle_key_press(self, n): pass class P1(M): pass p = P1() p = P2() p.handle_key_press("X") class meuh: def __init__(self): moi = self.mmu.hoepa() self.moi = moi meuh() a = [] while a: print a.pop().frut print a.pop().fledder() class woef: hoep = 1 def __init__(self): self.MMU2 = 18 wa = self.MMU.ole_hop(21) wo = self.MMU2.boe ugh = self.MMU2 we = self.MMU3 def roep(self): print 'roep' @property def roep2(self): return 1 woef() flut = 1 flutattr = flut.attr flut2 = woef() flut2attr = flut2.attr #*WARNING* 7.py: Variable 'l' has dynamic (sub)type #*WARNING* 7.py: Variable 'r' has dynamic (sub)type #*WARNING* 7.py:5: variable 'snuif' has no type #*WARNING* 7.py:6: unresolved call to 'friet' #*WARNING* 7.py:12: unresolved call (possibly caused by method passing, which is currently not allowed) #*WARNING* 7.py:28: class 'P1' has no method 'handle_key_press' #*WARNING* 7.py:33: variable 'moi' has no type #*WARNING* 7.py:32: expression has no type #*WARNING* 7.py:39: expression has no type #*WARNING* 7.py:40: expression has no type #*WARNING* 7.py:47: expression has no type #*WARNING* 7.py:48: expression has no type #*WARNING* 7.py:50: expression has no type #*WARNING* 7.py:62: expression has no type #*WARNING* 7.py:65: expression has no type shedskin-0.9.4/tests/errs/16.py0000664000175000017500000000014512157270661016176 0ustar srepmubsrepmubclass myset(set): pass #*ERROR* 16.py:1: inheritance from builtin class 'set' is not supported shedskin-0.9.4/tests/errs/20.py0000664000175000017500000000015712157270661016174 0ustar srepmubsrepmub class X: @property(a,b) def prop(self): pass #*ERROR* 20.py:3: unsupported type of decorator shedskin-0.9.4/tests/errs/camera.py0000664000175000017500000000010712157270661017176 0ustar srepmubsrepmub #{ print 'crap' #} def runpix(): x = 1 x = 's' t = (x,) shedskin-0.9.4/tests/errs/13.py0000664000175000017500000000012412157270661016170 0ustar srepmubsrepmubclass myset2('woef'): pass #*ERROR* 13.py:1: invalid expression for base class shedskin-0.9.4/tests/errs/21.py0000664000175000017500000000015112157270661016167 0ustar srepmubsrepmub class X: @meuuuh def meuh(self): pass #*ERROR* 21.py:3: unsupported type of decorator shedskin-0.9.4/tests/errs/34.py0000664000175000017500000000013412157270661016174 0ustar srepmubsrepmub from __future__ import division #*ERROR* 34.py:2: future 'division' is not yet supported shedskin-0.9.4/tests/errs/22.py0000664000175000017500000000013512157270661016172 0ustar srepmubsrepmub def func2(**kwahah): return 1 #*ERROR* 22.py:2: argument (un)packing is not supported shedskin-0.9.4/tests/errs/43.py0000664000175000017500000000013212157270661016172 0ustar srepmubsrepmub def f(): pass print f.__doc__ #*ERROR* 43.py:5: __doc__ attribute is not supported shedskin-0.9.4/tests/errs/3.py0000664000175000017500000000075312157270661016117 0ustar srepmubsrepmubimport camera def hap(x): print x def hap2(): pass hoplist = [hap2] hoplist = [hop2] class A(object): pass class B(object): pass s = A() s = B() print s #*WARNING* camera.py:6: Function runpix not called! #*WARNING* 3.py:3: Function hap not called! #*WARNING* 3.py:9: 'list' instance containing function reference #*WARNING* 3.py:10: variable 'hop2' has no type #*WARNING* 3.py: Variable 's' has dynamic (sub)type #*WARNING* 3.py:20: expression has dynamic (sub)type shedskin-0.9.4/tests/errs/35.py0000664000175000017500000000013412157270661016175 0ustar srepmubsrepmubfrom random import reutemeut #*ERROR* 35.py:1: no identifier 'reutemeut' in module 'random' shedskin-0.9.4/tests/errs/24.py0000664000175000017500000000015212157270661016173 0ustar srepmubsrepmub a = isinstance(myset2(), myset2) #*WARNING* 24.py:3: 'isinstance' is not supported; always returns True shedskin-0.9.4/tests/errs/42.py0000664000175000017500000000010712157270661016173 0ustar srepmubsrepmub print __doc__ #*ERROR* 42.py:2: '__doc__' attribute is not supported shedskin-0.9.4/tests/errs/17.py0000664000175000017500000000012412157270661016174 0ustar srepmubsrepmubclass X: class nested: pass #*ERROR* 17.py:2: non-global class 'nested' shedskin-0.9.4/tests/errs/41.py0000664000175000017500000000021212157270661016167 0ustar srepmubsrepmub class X: x = 4 def __init__(self): self.x = 5 X() #*WARNING* instance variable 'x' of class 'X' shadows class variable shedskin-0.9.4/tests/errs/32.py0000664000175000017500000000017512157270661016177 0ustar srepmubsrepmub def hop(): from random import random #*ERROR* 32.py:3: please place all imports (no 'try:' etc) at the top of the file shedskin-0.9.4/tests/errs/5.py0000664000175000017500000000015512157270661016115 0ustar srepmubsrepmub class A: def __init__(self): super(A).__init__() #*ERROR* 5.py:4: unsupported usage of 'super' shedskin-0.9.4/tests/errs/19.py0000664000175000017500000000013312157270661016176 0ustar srepmubsrepmub def flunc(): def ahoi(): pass #*ERROR* 19.py:4: non-global function 'ahoi' shedskin-0.9.4/tests/157.py0000664000175000017500000002773012157270661015322 0ustar srepmubsrepmub # This is an extremely simple chess like speed test program written in Python # This program can be distributed under GNU General Public License Version 2. # (C) Jyrki Alakuijala 2005 # # Despite its looks, this program was written in Python, not converted to it. # This program is incomplete, castlings, enpassant situation etc. are not properly implemented # game ending is not recognized. The evaluator as simple as it ever could be. # # The board is an 160-element array of ints, Nones and Booleans, # The board contains the real board in squares indexed in variable 'squares' # The oversized board is to allow for "0x88" chess programming trick for move generation. # Other board data: # 4x castling flags, indices [10-13], queen side white, king side white, queen side black, king side white # turn, enpassant [26, 27] from copy import copy iNone = -999 iTrue = 1 iFalse = 0 setup = (4, 2, 3, 5, 6, 3, 2, 4, iNone, iNone) + (iTrue,)*4 + (iNone, iNone) + (1,) * 8 + (iNone, iNone, iTrue, iNone, iNone, iNone, iNone, iNone,) + ((0, ) * 8 + (iNone,) * 8) * 4 + (-1,) * 8 + (iNone,) * 8 + (-4, -2, -3, -5, -6, -3, -2, -4) + (iNone,) * 40 squares = tuple([i for i in range(128) if not i & 8]) knightMoves = (-33, -31, -18, -14, 14, 18, 31, 33) bishopLines = (tuple(range(17, 120, 17)), tuple(range(-17, -120, -17)), tuple(range(15, 106, 15)), tuple(range(-15, -106, -15))) rookLines = (tuple(range(1, 8)), tuple(range(-1, -8, -1)), tuple(range(16, 128, 16)), tuple(range(-16, -128, -16))) queenLines = bishopLines + rookLines kingMoves = (-17, -16, -15, -1, 1, 15, 16, 17) linePieces = ((), (), (), bishopLines, rookLines, queenLines, (), (), queenLines, rookLines, bishopLines, (), ()) clearCastlingOpportunities = [None] * 0x80 for (i, v) in ((0x0, (10,)), (0x4, (10, 11)), (0x7, (11,)), (0x70, (12,)), (0x74, (12, 13)), (0x77, (13,))): clearCastlingOpportunities[i] = v pieces = ".pnbrqkKQRBNP" def evaluate(board): evals = (0, 100, 300, 330, 510, 950, 100000, -100000, -950, -510, -330, -300, -100) return sum([evals[board[i]] for i in squares]) def printBoard(board): for i in range(7,-1,-1): for j in range(8): ix = i * 16 + j print pieces[board[ix]], print def move(board, mv): ix = (mv >> 8) & 0xff board[mv & 0xff] = board[ix] board[ix] = 0 if clearCastlingOpportunities[ix]: for i in clearCastlingOpportunities[ix]: board[i] = iFalse board[26] = int(not board[26]) # Turn if (mv & 0x7fff0000) == 0: return if (mv & 0x01000000): # double step board[27] = mv & 7 else: board[27] = iNone # no enpassant if (mv & 0x04000000): # castling toix = mv & 0xff if toix == 0x02: board[0x00] = 0 board[0x03] = 4 elif toix == 0x06: board[0x07] = 0 board[0x05] = 4 elif toix == 0x72: board[0x70] = 0 board[0x73] = -4 elif toix == 0x76: board[0x77] = 0 board[0x75] = -4 else: raise "faulty castling" if mv & 0x08000000: # enpassant capture if board[26]: # turn after this move board[mv & 0x07 + 64] = 0 else: board[mv & 0x07 + 48] = 0 if mv & 0x10000000: # promotion a = (mv & 0xff0000) >> 16 if (a >= 0x80): a = a - 0x100 board[mv & 0xff] = a def toString(move): fr = (move >> 8) & 0xff to = move & 0xff letters = "abcdefgh" numbers = "12345678" mid = "-" if (move & 0x04000000): if (move & 0x7) == 0x02: return "O-O-O" else: return "O-O" if move & 0x02000000: mid = "x" retval = letters[fr & 7] + numbers[fr >> 4] + mid + letters[to & 7] + numbers[to >> 4] return retval def moveStr(board, strMove): moves = pseudoLegalMoves(board) for m in moves: if strMove == toString(m): move(board, m) return for m in moves: print toString(m) raise "no move found" #, strMove def rowAttack(board, attackers, ix, dir): own = attackers[0] for k in [i + ix for i in dir]: if k & 0x88: return False if board[k]: return (board[k] * own < 0) and board[k] in attackers def nonpawnAttacks(board, ix, color): return (max([board[ix + i] == color * 2 for i in knightMoves]) or max([rowAttack(board, (color * 3, color * 5), ix, bishopLine) for bishopLine in bishopLines]) or max([rowAttack(board, (color * 4, color * 5), ix, rookLine) for rookLine in rookLines])) nonpawnBlackAttacks = lambda board, ix: nonpawnAttacks(board, ix, -1) nonpawnWhiteAttacks = lambda board, ix: nonpawnAttacks(board, ix, 1) def pseudoLegalMovesWhite(board): retval = pseudoLegalCapturesWhite(board) for sq in squares: b = board[sq] if b >= 1: if b == 1 and not (sq + 16 & 0x88) and board[sq + 16] == 0: if sq >= 16 and sq < 32 and board[sq + 32] == 0: retval.append(sq * 0x101 + 32) retval.append(sq * 0x101 + 16) elif b == 2: for k in knightMoves: if board[k + sq] == 0: retval.append(sq * 0x101 + k) elif b == 3 or b == 5: for line in bishopLines: for k in line: if (k + sq & 0x88) or board[k + sq] != 0: break retval.append(sq * 0x101 + k) if b == 4 or b == 5: for line in rookLines: for k in line: if (k + sq & 0x88) or board[k + sq] != 0: break retval.append(sq * 0x101 + k) elif b == 6: for k in kingMoves: if not (k + sq & 0x88) and board[k + sq] == 0: retval.append(sq * 0x101 + k) if (board[10] and board[1] == 0 and board[2] == 0 and board[3] == 0 and not -1 in board[17:22] and not nonpawnBlackAttacks(board, 2) and not nonpawnBlackAttacks(board, 3) and not nonpawnBlackAttacks(board, 4)): retval.append(0x04000000 + 4 * 0x101 - 2) if (board[11] and board[5] == 0 and board[6] == 0 and not -1 in board[19:24] and not nonpawnBlackAttacks(board, 4) and not nonpawnBlackAttacks(board, 5) and not nonpawnBlackAttacks(board, 6)): retval.append(0x04000000 + 4 * 0x101 + 2) return retval def pseudoLegalMovesBlack(board): retval = pseudoLegalCapturesBlack(board) for sq in squares: b = board[sq] if b < 0: if b == -1 and not (sq + 16 & 0x88) and board[sq - 16] == 0: if sq >= 96 and sq < 112 and board[sq - 32] == 0: retval.append(sq * 0x101 - 32) retval.append(sq * 0x101 - 16) elif b == -2: for k in knightMoves: if board[k + sq] == 0: retval.append(sq * 0x101 + k) elif b == -3 or b == -5: for line in bishopLines: for k in line: if (k + sq & 0x88) or board[k + sq] != 0: break retval.append(sq * 0x101 + k) if b == -4 or b == -5: for line in rookLines: for k in line: if (k + sq & 0x88) or board[k + sq] != 0: break retval.append(sq * 0x101 + k) elif b == -6: for k in kingMoves: if not (k + sq & 0x88) and board[k + sq] == 0: retval.append(sq * 0x101 + k) if (board[12] and board[0x71] == 0 and board[0x72] == 0 and board[0x73] == 0 and not 1 in board[0x61:0x65] and not nonpawnWhiteAttacks(board, 0x72) and not nonpawnWhiteAttacks(board, 0x73) and not nonpawnWhiteAttacks(board, 0x74)): retval.append(0x04000000 + 0x74 * 0x101 - 2) if (board[11] and board[0x75] == 0 and board[0x76] == 0 and not -1 in board[0x63:0x68] and not nonpawnWhiteAttacks(board, 0x74) and not nonpawnWhiteAttacks(board, 0x75) and not nonpawnWhiteAttacks(board, 0x76)): retval.append(0x04000000 + 0x74 * 0x101 + 2) return retval def pseudoLegalMoves(board): if board[26]: return pseudoLegalMovesWhite(board) else: return pseudoLegalMovesBlack(board) def pseudoLegalCapturesWhite(board): retval = [] for sq in squares: b = board[sq] if b >= 1: if b == 1: if not (sq + 17 & 0x88) and board[sq + 17] < 0: retval.append(0x02000000 + sq * 0x101 + 17) if not (sq + 15 & 0x88) and board[sq + 15] < 0: retval.append(0x02000000 + sq * 0x101 + 15) if sq >= 64 and sq < 72 and abs((sq & 7) - board[27]) == 1: # enpassant retval.append(0x02000000 + sq * 0x100 + (sq & 0x70) + 16 + board[27]) elif b == 2: for k in knightMoves: if not (sq + k & 0x88) and board[k + sq] < 0: retval.append(0x02000000 + sq * 0x101 + k) elif b == 6: for k in kingMoves: if not(k + sq & 0x88) and board[k + sq] < 0: retval.append(0x02000000 + sq * 0x101 + k) else: for line in linePieces[b]: for k in line: if (sq + k & 0x88) or board[k + sq] >= 1: break if board[k + sq] < 0: retval.append(0x02000000 + sq * 0x101 + k) break return retval def pseudoLegalCapturesBlack(board): retval = [] for sq in squares: b = board[sq] if b < 0: if b == -1: if board[sq - 17] >= 1: retval.append(0x02000000 + sq * 0x101 - 17) if board[sq - 15] >= 1: retval.append(0x02000000 + sq * 0x101 - 15) if sq >= 48 and sq < 56 and abs((sq & 7) - board[27]) == 1: # enpassant retval.append(0x0a000000 + sq * 0x100 + (sq & 0x70) - 16 + board[27]) elif b == -2: for k in knightMoves: if not (sq + k & 0x88) and board[k + sq] >= 1: retval.append(0x02000000 + sq * 0x101 + k) elif b == -3: for line in bishopLines: for k in line: if board[k + sq] < 0: break if board[k + sq] >= 1: retval.append(0x02000000 + sq * 0x101 + k) break elif b == -4: for line in rookLines: for k in line: if board[k + sq] < 0: break if board[k + sq] >= 1: retval.append(0x02000000 + sq * 0x101 + k) break elif b == -5: for line in queenLines: for k in line: if board[k + sq] < 0: break if board[k + sq] >= 1: retval.append(0x02000000 + sq * 0x101 + k) break elif b == -6: for k in kingMoves: if board[k + sq] >= 1: retval.append(0x02000000 + sq * 0x101 + k) return retval def pseudoLegalCaptures(board): if board[26]: return pseudoLegalCapturesWhite(board) else: return pseudoLegalCapturesBlack(board) def legalMoves(board): allMoves = pseudoLegalMoves(board) retval = [] #from copy import copy kingVal = 6 if board[26]: kingVal = -kingVal for mv in allMoves: board2 = copy(board) move(board2, mv) #print "trying to reduce move", toString(mv) if not [i for i in pseudoLegalCaptures(board2) if board2[i & 0xff] == kingVal]: retval.append(mv) return retval def alphaBetaQui(board, alpha, beta, n): e = evaluate(board) if not board[26]: e = -e if e >= beta: return (beta, iNone) # XXX if (e > alpha): alpha = e bestMove = iNone # XXX if n >= -4: #from copy import copy for mv in pseudoLegalCaptures(board): newboard = copy(board) move(newboard, mv) value = alphaBetaQui(newboard, -beta, -alpha, n - 1) value = (-value[0], value[1]) if value[0] >= beta: return (beta, mv) if (value[0] > alpha): alpha = value[0] bestMove = mv return (alpha, bestMove) def alphaBeta(board, alpha, beta, n): if n == 0: return alphaBetaQui(board, alpha, beta, n) # from copy import copy bestMove = iNone # XXX for mv in legalMoves(board): newboard = copy(board) move(newboard, mv) value = alphaBeta(newboard, -beta, -alpha, n - 1) value = (-value[0], value[1]) if value[0] >= beta: return (beta, mv) if (value[0] > alpha): alpha = value[0] bestMove = mv return (alpha, bestMove) def speedTest(): board = list(setup) moveStr(board, "c2-c4") moveStr(board, "e7-e5") moveStr(board, "d2-d4") res = alphaBeta(board, -99999999, 99999999, 4) print res moveStr(board, "d7-d6") res = alphaBeta(board, -99999999, 99999999, 4) print res speedTest() shedskin-0.9.4/tests/11.py0000664000175000017500000000037612157270661015224 0ustar srepmubsrepmub class fred: # a: [] def huh(self): # [] self.a = 1 # [int] a = fred() # [fred] a.huh() b = a.a # [int] shedskin-0.9.4/tests/184.py0000664000175000017500000000335012157270661015312 0ustar srepmubsrepmub # locally override module name import testdata.bert as game class Game: def __init__(self): self.x = 'xxx' def hup(game): game.__init__() print game.x if game: print 'game' hup(Game()) # class attribute access across module from testdata import bert bert.zeug.purple += 1 blah = bert.zeug.purple print blah # template conflict def opterr(x): pass opterr(1) #opterr('1') # disappearing type def ParseAction(action): return ('',) def ParseRuleLine(line): tmp=line.split() for y in tmp[-1].split(): ParseAction(y) for x in ''.split(): ParseRuleLine(x) # working outside of list a = range(5) a[7:80] = range(5) print a a[10:15] = range(10) print a a[12:20] = range(10) print a a=range(5) a[-8:-5] = [9,9] a[1:1] = [8,8] del a[-7:2] print a a=range(5) a[4:1] = [7,12] print a lll = [1,2] del lll[18:] print lll # split nothing print ''.split() print ' '.split() # casting problem def hoppa(): return ['beh'] return [] return None hop = hoppa() # comment problem def hoezee(): '''kijk een /* C++ comment */''' hoezee() # list comp scoping def knuts(j, var): print 'knuts!', j, var return [7] itjes = [1] globaltje = 'global' def ahoi(): localtje = 'localtje' twitjes = [2] print [1 for i in 3*twitjes for b2 in knuts(i, globaltje)] print [2 for i in 4*itjes if knuts(2*i, localtje)] ahoi() # overloading problem file = open('run.py') print file.read(10) file.close() # xrange reset xrr = xrange(2) print xrr, list(xrr), list(reversed(xrr)) for xr in xrr: for yr in xrr: print xr, yr # tutorial example should work at least.. #class matrix: # def __init__(self, hop): # self.unit = hop # #m1 = matrix([1]) #m2 = matrix([1.0]) shedskin-0.9.4/tests/103.py0000664000175000017500000000154112157270661015301 0ustar srepmubsrepmub class fred: # x: [float, int]* def bla(self): # self: [fred(A)] self.meth_templ(1, 1) # [int] self.meth_templ(1.0, 1) # [float] self.hop(self.x) # [A] def meth_templ(self, x, z): # self: [fred(A)], x: [B]r, z: [int] y = x # [B] return y # [B] def hop(self, x): # self: [fred(A)], x: [A]r return x # [A] a = fred() # [fred(int)] a.x = 1 # [int] a.bla() # [] b = fred() # [fred(float)] b.x = 1.0 # [float] b.bla() # [] shedskin-0.9.4/tests/167.py0000664000175000017500000000103112157270661015305 0ustar srepmubsrepmub print '', print 'hoi', 'huh', print 'hophop' print '', print 'beh' print [1,2,3,1].index(1) print [1,2,3,1].index(1, 1) print [1,2,3,1].index(1, -1) print [1,2,3,1].index(1, -4) print [1,2,3,1].index(1, -3, 4) def RemoveElts(list): newlist=list[:] return newlist print RemoveElts([3]) try: try: {1:2}[3] except KeyError, e: raise e except KeyError, m: print m blah = set([]) blah.add(1) print blah def MergeAndVerify(newModList,finalModHash): if newModList == []: return finalModHash shedskin-0.9.4/tests/39.py0000664000175000017500000000225512157270661015234 0ustar srepmubsrepmub import testdata.bert from testdata.bert import hello, zeug #from sets import Set class jurk: pass testdata.bert.hello(4) # [] hello(4) # [str] s2 = jurk() # [jurk()] s4 = set() # [Set(float)] s4.add(1.0) # [] s3 = set([1,2,3]) # [Set(int)] kn = testdata.bert.zeug() # [zeug()] kn.hallo(4) # [] l1 = lambda x,y: x+y # [lambda0] l2 = lambda x,y: x-y # [lambda0] l5 = l2 # [lambda0] l3 = lambda x,y: 1.0 # [lambda1] def l4(x, y): return x*y # [int] def toepas(l): # l: [lambda0] return l(1,2) # [int] print toepas(l1) # [int] print toepas(l5) # [int] print l3(1.0, 'hoi') # [float] a = l4 # [lambda0] a(3,3) # [int] print toepas(a) # [int] shedskin-0.9.4/tests/197.py0000664000175000017500000002506512157270661015325 0ustar srepmubsrepmub# mmap import mmap import os PAGESIZE = mmap.PAGESIZE TESTFILE_IN = os.path.join('testdata', 'board.py') TESTFILE_OUT = os.path.join('testdata', 'mmap.out') def setUp(): if os.path.exists(TESTFILE_OUT): os.remove(TESTFILE_OUT) def tearDown(map): map.close() try: os.remove(TESTFILE_OUT) except OSError: pass def test_anonymous(): ''' Test mmap module on anonymous files. ''' print '## test_anonymous:' map = mmap.mmap(-1, PAGESIZE) assert len(map) == PAGESIZE print '# write:' ## print map.size() ## TODO: throw mmap.error: [Errno 9] Bad file descriptor? assert map.tell() == 0 map.write_byte('f') assert map.tell() == 1 map.write('oo bar\tbaz\nqux') assert map.tell() == 15 print '# get/set:' assert map[:15] == 'foo bar\tbaz\nqux' print map[0] map[-1]='Z' print map[-1] print map[4:-PAGESIZE+7] print '%r' %map[:15] map[4:7] = "foo" map[PAGESIZE-3:] = "xyz" print map[PAGESIZE-3:] print '# find/seek:' assert map.find("foo") == -1 map.seek(0) assert map.tell() == 0 assert map.find("foo") == 0 assert map.rfind("foo") == 4 map.seek(-1, 2) assert map.tell() == PAGESIZE-1 map.seek(0, 2) assert map.tell() == PAGESIZE map.seek(-PAGESIZE, 1) assert map.tell() == 0 print '# read:' print map.read(3) print '%r' % map.read_byte() print '%r' % map.readline() print '%r' % map.read(3) print '# move:' map.move(8, 4, 3) print '# iter:' assert "f" in map assert "a" not in map map.flush() print '# Result:' print '%r' % map[:15] ## re not supported (yet) ## m = re.search('z',map) ## assert m.start() == PAGESIZE -1 h = 0 for c in map: h += ord(c) * 31 print h try: map.resize(0x2000) assert len(map) == 0x2000 except: pass map.close() def test_basic(): ''' Taken from python 2.7 ''' print '## test_basic:' setUp() # Test mmap module on Unix systems and Windows # Create a file to be mmap'ed. f = open(TESTFILE_OUT, 'w+') # Write 2 pages worth of data to the file f.write('\0'* PAGESIZE) f.write('foo') f.write('\0'* (PAGESIZE-3) ) f.flush() m = mmap.mmap(f.fileno(), 2 * PAGESIZE) f.close() # Simple sanity checks assert m.find('foo') == PAGESIZE assert len(m) == 2*PAGESIZE print repr(m[0]) print repr(m[0:3]) try: m[len(m)] except IndexError: print "ok" # Modify the file's content m[0] = '3' m[PAGESIZE +3: PAGESIZE +3+3] = 'bar' # Check that the modification worked print repr(m[0]) print repr(m[0:3]) print repr(m[PAGESIZE-1 : PAGESIZE + 7]) m.flush() # Try to seek to negative position... try: m.seek(-1) except ValueError: print "ok" # Try to seek beyond end of mmap... try: m.seek(1, 2) except ValueError: print "ok" try: m.seek(-len(m)-1, 2) except ValueError: print "ok" tearDown(m) def test_readonly(): ''' Reads a file. ''' print '## test_readonly:' f = open(TESTFILE_IN, 'r+') mapsize = os.path.getsize(TESTFILE_IN) map = mmap.mmap(f.fileno(), 0) assert map.size() == mapsize print repr(map.read(mapsize)) map.close() def test_rfind(): # test the new 'end' parameter works as expected print '## test_rfind:' setUp() f = open(TESTFILE_OUT, 'w+') data = 'one two ones' n = len(data) f.write(data) f.flush() m = mmap.mmap(f.fileno(), n) f.close() assert m.rfind('one') == 8 assert m.rfind('one ') == 0 assert m.rfind('one', 0, -1) == 8 assert m.rfind('one', 0, -2) == 0 assert m.rfind('one', 1, -1) == 8 assert m.rfind('one', 1, -2) ==-1 tearDown(m) def test_tougher_find(): ''' Taken from python 2.7 ''' print '## test_tougher_find:' setUp() # Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2, # searching for data with embedded \0 bytes didn't work. f = open(TESTFILE_OUT, 'w+') data = 'aabaac\x00deef\x00\x00aa\x00' n = len(data) f.write(data) f.flush() m = mmap.mmap(f.fileno(), n) f.close() for start in range(n+1): for finish in range(start, n+1): slice = data[start : finish] print m.find(slice) , data.find(slice) print m.find(slice + 'x') == -1 tearDown(m) test_anonymous() test_basic() test_readonly() test_rfind() test_tougher_find() # re search empty string, match_object.span import re r = re.compile('^a?$') print r.search('').start() print r.search('').end() print r.search('').span() print r.search('a').start() print r.search('a').end() print r.search('a').span() # id foo_a="foo";foo_b="foo";foo_c="foo"; print id(foo_a)==id(foo_b)==id(foo_c) # reduce fixes, more tests from math import fabs print reduce(lambda x,y: x + fabs(y), xrange(10)) print reduce(lambda x,y: x + fabs(y), xrange(10), 1) print reduce(lambda x,y: x + fabs(y), xrange(10), 1.0) print reduce(lambda x,y: x + fabs(y), map(float, xrange(10))) print reduce(lambda x,y: x + fabs(y), map(float, xrange(10)), 2) print reduce(lambda x,y: x + fabs(y), map(float, xrange(10)), 2.0) class Aap: def __init__(self, value): self.value = value def __add__(self, other): return Aap(self.value+other.value) def __str__(self): return 'Aap(%s)' % self.value aaplist = [Aap(3), Aap(4), Aap(5)] print sum(aaplist, Aap(6)) print reduce(lambda a,b:a+b, aaplist), reduce(lambda a,b:a+b, aaplist, Aap(6)) # set methods now often take multiple args sett = set(range(3)) sett.update(range(2,5), range(12,14)) print sorted(sett) sett.update(range(2,5), range(12,14), range(18, 20)) print sorted(sett) sett = set(range(4)) print sorted(sett.union(set(range(6)), (6,7))) print sorted(sett.union([5], [3, 4], range(3))) print sorted(sett.intersection(range(1, 4), range(2, 5))) print sorted(sett.intersection(range(3), [2], range(4))) print sorted(sett.difference(range(2), range(3))) print sorted(sett.difference(range(2), range(3), [3, 6])) sett = set(range(4)) sett.intersection_update(range(2), range(3)) print sorted(sett) sett = set(range(3)) sett.intersection_update(range(2), range(3), range(4)) print sorted(sett) sett = set(range(4)) sett.difference_update(range(2), range(3)) print sorted(sett) sett = set(range(5)) sett.difference_update(range(2), range(3), [3, 6]) print sorted(sett) #cannot hurt to test this print [].__class__.__name__ print 'hoi'.__class__.__name__ #string formatting asterisk print "%d * %d" % (1,2) print "%d* %% %d" % (1,2) print "%d%% *%d" % (1,2) #rich comparison fallbacks class inst(object): def __init__(self, num, opcode='add', pc='1'): self.opcode = opcode self.pc = pc self.num = num def __lt__( self, other): return self.num < other.num def __repr__(self): return "%d" % self.num Seq = [inst(3),inst(1),inst(4),inst(2)] print Seq print sorted(Seq) class LT: def __gt__(self, o): print 'gt!' return False def __le__(self, o): print 'le!' return True print LT() < LT() print LT() >= LT() class LT2: def __lt__(self, o): print 'lt!' return False def __ge__(self, o): print 'ge!' return True print LT2() > LT2() print LT2() <= LT2() #complex a = 4j + 3j print a b = a.real print sum([1j, 2j, 3j]) print '%s' % (1+3j) print 1==0j, 0.0==0j, 1.0==0j, 0j==0.0 #colorsys import colorsys print '%.2f' % colorsys.ONE_THIRD print '%.2f' % colorsys.ONE_SIXTH print '%.2f' % colorsys.TWO_THIRD def pr(t): print [('%.2f'%x) for x in t] pr(colorsys.hls_to_rgb(1.0, 0.5, 0.7)) pr(colorsys.rgb_to_hls(1.0, 0.5, 0.7)) pr(colorsys.yiq_to_rgb(1.0, 0.5, 0.7)) pr(colorsys.rgb_to_yiq(1.0, 0.5, 0.7)) pr(colorsys.hsv_to_rgb(1.0, 0.5, 0.7)) pr(colorsys.rgb_to_hsv(1.0, 0.5, 0.7)) #equality t1 = ('rc', (0, 0)) t2 =('rc', (0, 0) ) print t1!=t2 print t1==t2 print {(3,2): 0} == {(3,2): 1} #generator and arg unpacking def genpack((i,j),a,b): yield i yield j yield a yield b ttt = (1,2) for aaa in genpack(ttt,3,4): print aaa #fill in virtual variable types class CCache: def Probe(self): self.VictimLineAddr = [1] self.VictimLineAddr = None class CCache1(CCache): pass class CCache2(CCache): pass c = CCache1() c = CCache2() c.Probe() # forward referencing vars in inherited method class TraceParser: def parseProgramCode(self): self.basicBlockList = [] # basicblock = 1 for x in range(2): if x == 1: self.basicBlockList.append(basicblock) else: basicblock = 2 print self.basicBlockList class CUnifiedTraceParser(TraceParser): pass CUnifiedTraceParser().parseProgramCode() # rewrite incompatible types if possible C1 = {1: 'een'} C2 = (1.0, 'woef') D = (C1, C2) if True else ({}, None) print D print [1] if True else None print [] if True else [1] print [[]] == [[1]], [[1]] == [[]] print dict([(1,2.0)]) == dict() print dict([(1,2.0)]) == {} print set() == set([1,2]) print (set(['a']), set([1.0])) == (set(), set()) print (set(['a']), set([1.0])) == (set(), None) def slicing(): a = range(10) a[2:] = range(4) a[2:] = [] print a b = map(str, a) b[2:] = [] b[2:] = ['woef'] b[2:] = [None] print b slicing() print [1] or [] print [] or ['uhm'] print None or 'waf' print [1]+[] print [[]]+[[1]] print [None]+[['uh']] print set([]) == set([1]) print set([1]) == set([1.0]) print 1==0j print [1j]==[1.0] print 0 == True, 1 == True, 2 == True print 0 == False, 1 == False, 2 == False print [x == True for x in range(3)] print [1] == [True] # for .., .. in somedict.iteritems() def fastdictiteritems(): d = {3: 4} for a,b in d.iteritems(): print a, b for c in d.iteritems(): print c print [(a, b) for a,b in d.iteritems()] print [c for c in d.iteritems()] d2 = {(3,4): (4,5)} for (e,f), (g,h) in d2.iteritems(): print e,f,g,h d3 = {1.0: 'hallo'} print [(x, y) for x,y in d3.iteritems()] fastdictiteritems() # deepcopy improvement import copy class A: pass class B: pass def copytest(): a = A() a.b = B() c = copy.deepcopy(a) a.b.x = 18 c.b.x = 19 print a.b.x, c.b.x copytest() # return 'nothing' in generator def hoppagen(): yield True yield False print 'hoppa' return for hoppax in hoppagen(): print hoppax # sys.exit case import sys sys.exit('woef') shedskin-0.9.4/tests/186.py0000664000175000017500000000541012157270661015313 0ustar srepmubsrepmub # print space or not print 'aha ', print 'hum' # minus in front print hex(200), hex(-200) # import as from sys import maxint as MAXINT from sys import maxsize as MAXSIZE import random from random import random as randum from string import * random.seed(1) a = MAXINT bb = MAXSIZE #print a print randum() print a == bb #default argument problem import getopt flats = getopt.getopt(['a'],'a') print flats #more casting problems.. def hop(): yield () yield (1,2) h = hop() print list(h) print list(h) def hap(): return () return (1,2) print hap() #FOR_IN_T2 for listcomp #class animal: # def sound(self): # print 'oink' #class dog(animal): pass #class cat(animal): pass #print [a.sound() for a in dog(), cat()] #tests used for optimizing enumerate/zip l = [(7,8),(9,10)] class D: pass y = D() for a, b in enumerate(l): print a,b for a, y.b in enumerate(l): print a,y.b class C: pass x = C() for x.a, (c,d) in enumerate(l): print x.a,c,d for t in enumerate(l): print t print [(a, b) for a, b in enumerate(l)] print [(a, y.b) for a, y.b in enumerate(l)] print [(x.a, (c,d)) for x.a, (c,d) in enumerate(l)] print [t for t in enumerate(l)] l2 = [(7,8), (9,10)] for a2, b2 in zip(l2, l2[::-1]+l2): print a2, b2 for t2 in zip(l2, l2): print t2 class C2: pass c2 = C2() for c2.x, (d2,e2) in zip(2*l2, l2): print c2.x, d2, e2 for (d2,e2), c2.x in zip(2*l2, l2): print d2, e2, c2.x for (d2,e2), (f2,g2) in zip(l2, l2): print d2, e2, f2, g2 print zip('hoi','hap'), zip('ah', 'bh', 'ch') for u2,v2 in zip('hoi','hap'): print u2+v2 print zip('ahoi', range(5)) print [((d2, e2), c2.x) for (d2,e2), c2.x in zip(2*l2, l2)] #pyseq:str special cases print 'a'.join('hap') fi = file('testdata/humba', 'w') fi.writelines('hoei') fi.close() print file('testdata/humba').read() print tuple('hap') up = ['a'] up.extend('hoi') print up up += 'wap' print up print min('gehakt'), max('gehakt') print list(reversed('gehakt')) #variable naming #def sentences(): # next = 12 # yield next #print list(sentences()) #defdict problem import collections hoppa = collections.defaultdict(int) hoppa[4] = 5 for xxx in hoppa: print xxx, hoppa[xxx] #backticks, scalar ahh = 19 hoi = 'hoi' print `18`, `ahh+1`, `hoi` #bisect should model __cmp__, fix sorting problem from bisect import insort class A(object): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "A(%s, %s)" % (self.x, self.y) def __cmp__(self, other): return cmp(self.x + self.y, other.x + other.y) pairs = [[18, 6], [28, 5], [35, 26], [31, 28], [3, 3], [32, 37], [11, 17], [28, 29]] items = [] for pair in pairs: insort(items, A(pair[0], pair[1])) print items #sum ints with double items2 = range(5) ork = sum(items2, 0.3) print ork shedskin-0.9.4/tests/28.py0000664000175000017500000000032212157270661015223 0ustar srepmubsrepmub def propagate(la): # la: [list(int)] print la, la # [str], [str] propagate([1]) # [] propagate([2]) # [] shedskin-0.9.4/tests/187.py0000664000175000017500000001173012157270661015316 0ustar srepmubsrepmub # __ss_result print [result for result in range(4)] # void *constant caused by unused function def parse150(): a = '150' def ftpcp(): 'ah' in ('150',) parse150() # forward func refs class FTP: def retrbinary(self, callback): callback('hoi') def retrlines(self): callback = print_line callback('hoi2') def print_line(line): print line ftp = FTP() ftp.retrbinary(print_line) ftp.retrlines() # re.subn, re.re_object.subn (douglas mcneil) import re res = re.subn('a', 'ama', 'amadeus') print res res = re.compile('a').subn('ama', 'amadeus') print res # str.replace corner cases (douglas mcneil) print 'faaaaaredfredbrrr'.replace('r', 'rr') print 'aha'.replace('','men') # strip whitespace print int(' \t\n0x18 \t ', 16) # check add_strs optimization print 'hoi' print 'hoi' + 'mams' print 'hoi' + 'mams' + 'ik' print 'hoi' + 'mams' + 'ik' + 'ben' print 'hoi' + 'mams' + 'ik' + 'ben' + 'er' print 'hoi' + 'mams' + 'ik' + 'ben' + 'er' + 'weer' print 'h' print 'h' + 'm' print 'h' + 'm' + 'i' print 'h' + 'm' + 'i' + 'b' print 'h' + 'm' + 'i' + 'b' + 'e' print 'h' + 'm' + 'i' + 'b' + 'e' + 'w' # optimize addition of 1-length lists print [1]+[2,3,4], [1,2,3]+[4] # known problem (issue 8) def quicksort(L): x = y = 0 if L == []: return [] pivot = L[0] return quicksort([x for x in L[1:] if x < pivot]) + [pivot] + quicksort([y for y in L[1:] if y >= pivot]) data = [1, 200, 50, 485, 22, 22, 3534, 22112] print "quickdata: %s " % quicksort(data) # test list.__setslice__ optimization a = [1, 2, 3] a[-2:] = (1, 7, 9, 10) print a a[-2:] = set((10,)) print a # map, filter, reduce def hoppa(x, y, z): return str(x+y+z) def uhppa(a, b): return a+int(b) amap = map(hoppa, [1,2,3], [3,4,5], [5,6,7]) print amap bmap = map(uhppa, [1,2,3], ['3','4','5']) print bmap def hoppa2(a, b): if b: return a+b return a+'X' print map(hoppa2, 'banaan', 'aap') def hoppa3(a, b): if b: return a+b return a print map(hoppa3, range(8), range(4)) print sorted(map(lambda u: len(u), ['aaaaa', 'aaa', 'a'])) print reduce(lambda a,b: a+b, [3,5,7]) print reduce(lambda a,b: a-b, set([3,5,7]), 1) print filter(lambda a: 2<=a<5, range(10)) print filter(lambda c: c>'a', 'abaaac') print filter(lambda c: c>'a', tuple('abaaac')) print filter(None, range(3)), filter(None, 'abc'), filter(None, tuple(range(3))) options = filter(lambda option: option != 'fake', ['duh']) print options # next it1 = iter(range(4)) for i in range(10): print next(it1, -1) it2 = iter('abcd') try: for i in range(10): print next(it2) except StopIteration: print 'stop' it3 = iter('aha') for i in range(10): print next(it3, None) # sort(ed) key argument a = range(4) print sorted(a) print sorted(a, cmp=lambda a,b:cmp(a,b)) print sorted(a, cmp=lambda a,b:cmp(b,a)) print sorted(a, key=lambda a:a) print sorted(a, key=lambda a:-a) print sorted(a, cmp=lambda a,b:cmp(a,b), key=lambda a:-a) l = range(4) print l l.sort(cmp=lambda a,b:cmp(a,b)) print l l.sort(cmp=lambda a,b:cmp(b,a)) print l l.sort(key=lambda a:a) print l l.sort(key=lambda a:-a) print l l.sort(cmp=lambda a,b:cmp(a,b), key=lambda a:-a) print l print sorted('dcba', key=lambda c: c) # missing cast class hop: def __init__(self, a): print 'oh', a class hop2(hop): def __init__(self): bla = None hop.__init__(self, bla) class hop3(hop): def __init__(self): hop.__init__(self, 'hoi') hop2() hop3() # improve local variable overloading def bleh(a, b): return 1 def snrted(it, bleh): bleh(1, 1) snrted(range(4), lambda a,b: a+b) # forward var refs def hoep(): for x in range(10): if x == 8: print happa elif x == 9: print hoepa else: happa = x hoepa, banaan = x, 2 [n for n in range(4)] class wa: def wh(self): if False: y = 3 y = 2 print y if False: u.strip() u = 'hoi' if False: z += 1 z = 2 print z hoep() wa().wh() # passing builtins around print sorted([[2,3,4], [5,6], [7]], key=len) print map(len, ['a','bc']) print map(max, ['a','bc'], ['d'], ['e']) print map(max, ['a'], ['d'], ['e']) print map(str, range(12)) print map(list, 'abc') print map(int, ['18', '19']) print map(set, [[1]]) def two(f, a, b): return f(a, b) def three(f, a, b, c): return f(a, b, c) print two(max, 'a', 'b') print three(max, 'a', 'b', 'c') # __cmp and None lamp = ['a', 'b', None] lamp.sort() print lamp # bool and func pointers, misc fixes def wopper(x): print 'wopper', x wopper('wopper') DEBUG = wopper DEBUG = None if DEBUG: DEBUG('wopper') if not DEBUG: print 'no debug' if not 18: DEBUG('wopper') print 'debug' if () else 'no debug' print int(bool(DEBUG)) print int(bool(1)) # with statement with file('testdata/lop', 'w') as fp: fp.write('neh') with file('testdata/lop') as ap: print repr(ap.read()) f = file('testdata/lop') with f: print f.read() shedskin-0.9.4/tests/30.py0000664000175000017500000001137212157270661015223 0ustar srepmubsrepmub # (c) Mark Dufour # --- mark.dufour@gmail.com def _reduce(f, l, i=-1): # f: [lambda0], i: [int], l: [list(int)], r: [int] if not l: # [list(int)] if i != -1: return i # [int] print '*** ERROR! *** reduce() called with empty sequence and no initial value' # [str] if i != -1: # [int] r = f(i, l[0]) # [int] else: r = l[0] # [int] for i in range(len(l)-1): # [int] r = f(r, l[i+1]) # [int] return r # [int] # prelims argv = ['','testdata/uuf250-010.cnf'] # [list(str)] cnf = [l.strip().split() for l in file(argv[1]) if l[0] not in 'c%0\n'] # [list(list(str))] clauses = [[int(x) for x in m[:-1]] for m in cnf if m[0] != 'p'] # [list(list(int))] nrofvars = [int(n[2]) for n in cnf if n[0] == 'p'][0] # [int] vars = range(nrofvars+1) # [list(int)] occurrence = [[] for l in vars+range(-nrofvars,0)] # [list(list(list(int)))] for clause in clauses: # [list(int)] for lit in clause: occurrence[lit].append(clause) # [int] fixedt = [-1 for var in vars] # [list(int)] def solve_rec(): # la_mods: [list(int)], var: [int], prop_mods: [list(int)], choice: [int] global nodecount nodecount += 1 # [] if nodecount == 100: return 1 if not -1 in fixedt[1:]: # [int] print 'v', ' '.join([str((2*fixedt[i]-1)*i) for i in vars[1:]]) # [str], [str] return 1 # [int] la_mods = [] # [list(int)] var = lookahead(la_mods) # [int] #print 'select', var # [str], [int] if not var: return backtrack(la_mods) # [int] for choice in [var, -var]: # [int] prop_mods = [] # [list(int)] if propagate(choice, prop_mods) and solve_rec(): return 1 # [int] backtrack(prop_mods) # [int] return backtrack(la_mods) # [int] def propagate(lit, mods): # current: [int], unfixed: [int], mods: [list(int)], clause: [list(int)], lit: [int], length: [int] global bincount current = len(mods) # [int] mods.append(lit) # [] while 1: # [int] if fixedt[abs(lit)] == -1: # [int] fixedt[abs(lit)] = int(lit>0) # [int] for clause in occurrence[-lit]: # [list(int)] length, unfixed = info(clause) # [tuple(int)] if length == 0: return 0 # [int] elif length == 1: mods.append(unfixed) # [] elif length == 2: bincount += 1 # [] elif fixedt[abs(lit)] != int(lit>0): return 0 # [int] current += 1 # [] if current == len(mods): break # [int] lit = mods[current] # [int] return 1 # [int] def lookahead(mods): # mods: [list(int)], dif: [list(int)], choice: [int], score: [list(int)], prop_mods: [list(int)], var: [int], prop: [int] global bincount dif = [-1 for var in vars] # [list(int)] for var in unfixed_vars(): # [int] score = [] # [list(int)] for choice in [var, -var]: # [int] prop_mods = [] # [list(int)] bincount = 0 # [int] prop = propagate(choice, prop_mods) # [int] backtrack(prop_mods) # [int] if not prop: # [int] if not propagate(-choice, mods): return 0 # [int] break score.append(bincount) # [] dif[var] = _reduce(lambda x, y: 1024*x*y+x+y, score, 0) # [int] return dif.index(max(dif)) # [int] def backtrack(mods): # lit: [int], mods: [list(int)] for lit in mods: fixedt[abs(lit)] = -1 # [int] return 0 # [int] def info(clause): # lit: [int], clause: [list(int)], unfixed: [int], len: [int] len, unfixed = 0, 0 # [int], [int] for lit in clause: # [int] if fixedt[abs(lit)] == -1: unfixed, len = lit, len+1 # [int], [int] elif fixedt[abs(lit)] == int(lit>0): return -1, 0 # [tuple(int)] return len, unfixed # [tuple(int)] def unfixed_vars(): return [var for var in vars[1:] if fixedt[var] == -1] # [list(int)] nodecount = 0 # [int] if not solve_rec(): # [int] print 'unsatisfiable', nodecount # [str], [int] shedskin-0.9.4/tests/2.py0000664000175000017500000000003612157270661015135 0ustar srepmubsrepmub class fred: pass x = fred() shedskin-0.9.4/tests/46.py0000664000175000017500000000042212157270661015224 0ustar srepmubsrepmub def hoi(a, b): # a: [int, str], b: [int] a # [int, str] a = 'hoi' # [str] print a # [int, str] hoi('1', 1) # [] shedskin-0.9.4/tests/201.py0000664000175000017500000000452412157270661015304 0ustar srepmubsrepmub# unpacking negative integers import struct s = struct.pack('h', -12) xx, = struct.unpack('h', s) print xx s = struct.pack('i', -13) xx, = struct.unpack('i', s) print xx s = struct.pack('l', -14) xx, = struct.unpack('l', s) # block comment fix print xx print "hi there" #{ comment here ok # nothing #} not ok print "hi there 3" # virtual function: merge parent as well class X: def __str__(self): return 'X' class Y(X): def __str__(self): return 'Y' class A: def woef(self, l): print l class B(A): def woef(self, l): print l x = A() x.woef(X()) y = A() y = B() y.woef(Y()) # using builtin names class meuh: def set(self, x): self.x = x def filter(self, x): self.x = x def array(self, x): self.x = x mmm = meuh() mmm.set(8) mmm.filter(8) mmm.array(8) print mmm.x # class passing is not supported, but at least we shouldn't crash on this class wowf: pass x = wowf x # type inference bug uncovered by C64 SVN (list type is left unsplit) class BRKHandler: pass class Tape(BRKHandler): pass class IECMember(BRKHandler): pass class ComputerDevice(IECMember): pass def wop(a, b): t = Tape() c = ComputerDevice() hooks = [t, c] x, y = 1, 1.0 y = 1.0 blah = [x, y] bla = Tape() bla = IECMember() bla = ComputerDevice() wop(bla, bla) # default args and inheritance class Alpha: def func(self, value=True): print("value is:" + str(value)) class Beta(Alpha): pass beta = Beta() beta.func(); # changing list while iterating over it.. testdellastelem = [] testdellastelem.append("test1") testdellastelem.append("test2") testdellastelem.append("test3") for v in testdellastelem: if (v == "test3"): testdellastelem.remove("test3") print testdellastelem testdellastelem = [] testdellastelem.append("test1") testdellastelem.append("test2") testdellastelem.append("test3") for v in testdellastelem: if (v == "test2"): testdellastelem.remove("test3") print testdellastelem testdellastelem = [] testdellastelem.append("test1") testdellastelem.append("test2") testdellastelem.append("test3") for v in testdellastelem: if (v == "test2"): testdellastelem.remove("test1") print testdellastelem # global declaration ignored day = 0 def wopp(): global day for day in range(3): print day wopp() print day shedskin-0.9.4/tests/118.py0000664000175000017500000000103312157270661015303 0ustar srepmubsrepmub class node: # activation: [int]* def __init__(self, euh, input): # self: [node], euh: [int], input: [list(int)] pass d = [11] # [list(int)] e = [12] # [list(int)] x = [] # [list(int)] y = x # [list(int)] #a = node(1, y) # [node] b = node(2, d) # [node] c = node(3, e) # [node] shedskin-0.9.4/tests/109.py0000664000175000017500000000056712157270661015316 0ustar srepmubsrepmub board = 1 # [int] def best_move(board): # board: [int] max_move = (1,2) # [tuple2(int, int)] max_mobility = 1 # [int] return max_move, max_mobility # [tuple2(tuple2(int, int), int)] move, mob = best_move(board) # [tuple2(tuple2(int, int), int)] shedskin-0.9.4/tests/104.py0000664000175000017500000000100612157270661015276 0ustar srepmubsrepmub class fred: # y: [int]* def __eq__(self, x): # self: [fred], x: [fred] return self.y == x.y # [int] a = fred() # [fred] a.y = 1 # [int] b = fred() # [fred] b.y = 2 # [int] print a == b # [int] print a == a # [int] print b == b # [int] shedskin-0.9.4/tests/183.py0000664000175000017500000000613312157270661015313 0ustar srepmubsrepmub # casting class Foo: def __init__(self): a = None self.v = [1] self.v = a print self.v w = self.v w = a print w x = [1,2,3] x[1:2] = [] x[1:2] = [4,5] print x self.x = [1,2,3] self.x[1:2] = [] print self.x f1 = Foo() # print None a = [None] print a b = None print b # ugliness ss = set([frozenset([1,2,3])]) ss.discard(set([1,2,3])) print ss # complex c = complex(7.1, 4.7) print c, c.real, c.imag d = complex(7) print d, d.real, d.imag e = c+d print e, e.real, e.imag f = 1.2+complex() print f g = complex()+1.3 print g d = 7+1.1j c += d c += 8.4 c += 2 c += 9j print c print (7+4j)*(9+5j), 3*(7+4j) c,d = 2+2j, -3+4j print c.conjugate() print abs(c) print 7-c, c-7, c-d print 2/c, c/2, c/d print +c, ++c, -d, --d print 1-1j print int(1j == 1j), int(1j != 1j) print hash(12+10j) print int(bool(0j)), int(bool(1+1j)) print divmod((5+5j), (1+2j)) print (5+5j)//(1+2j), (5+5j)%(1+2j) print divmod((5+5j), 2) print (5+5j)//2, (5+5j)%2 #print divmod((5+5j), 2.2) print (5+5j)//2.2, (5+5j)%2.2 print divmod((5.5+5.5j), 2) print (5.5+5.5j)//2, (5.5+5.5j)%2 #print divmod((5.5+5.5j), 2.8) print (5.5+5.5j)//2.8, (5.5+5.5j)%2.8 # complex(str) import re def parsevalue(s): if not s: return 0+0j mult = 1+0j if s[-1] == 'j': s = s[:-1] mult = 0+1j if s in ['+', '-']: s += '1' return float(s)*mult def hak(s): pat = '(?P<%s>[+-]?([\d\.]+e[+-]?\d+|[\d\.]*)j?)' imag = re.compile(pat % 'one' + pat % 'two' + '?$') m = imag.match(s.strip()) if m: return parsevalue(m.group('one')) + parsevalue(m.group('two')) else: raise ValueError('complex() arg is a malformed string') print hak(' 2.4+0j' ), hak('2.4'), hak(' .4j'), hak('1-j') print hak('-10-j'), hak('+10.1+2.4j'), hak('+j') print hak('2e02'), hak('2e-02-2e+01j'), hak('-1.3e-3.1j') print complex(' 2.4+0j' ), complex('2.4'), complex(' .4j'), complex('1-j') print complex('-10-j'), complex('+10.1+2.4j'), complex('+j') #print complex('2e02'), complex('2e-02-2e+01j'), complex('-1.3e-3.1j') XXX 2.7? class PI: def __float__(self): return 3.14 print complex(PI()) # %% woes print "%%(%s)s" % 'ole' print '%%(bert)s %(bert)s' % {'bert': 18} ddd = {'bert': 19.9} print '%%(bert)s %(bert)s' % ddd # re.group multiple int/str arguments imag = re.compile('(a)(b)') m = imag.match('ab') print m.group(), m.group(0), m.group(1), m.group(2) print m.group(0, 2), m.group(2, 1, 1, 2) imag = re.compile('(?Pa)(?Pb)') m = imag.match('ab') print m.group(), m.group('one'), m.group('two') print m.group('two', 'one') wap = m.group('one') print wap hop = m.group('one', 'two', 'one') print hop # join empty list el = ['hap'][7:8] print ' '.join(el) # this works now #def p(msg): # print msg #p(15) #p("hello") # hash(None) dwek = {('a', 'b', None): 18} print dwek[('a', 'b', None)] # merge_simple_types lrp = [] print [lrp.append(0)] # inheritance from Exception descendant class X(RuntimeError): def __init__(self, msg=None): RuntimeError.__init__(self, msg) print 'ole', msg X() shedskin-0.9.4/tests/31.py0000664000175000017500000000070012157270661015215 0ustar srepmubsrepmub #class list: # unit: [int]* # def maonetuhcount(self): # self: [] # return 1 # [int] # # def __len__(self): # self: [list(int)] # return 1 # [int] # #class int_: # def __repr__(self): # self: [int] # return '' # [str] puzzlecolumns = [1] print puzzlecolumns.__len__() shedskin-0.9.4/tests/26.py0000664000175000017500000000016512157270661015226 0ustar srepmubsrepmub cube = [(1,2),(3,4)] # [list_tuple_int] cube[0] = (1,2) # [tuple_int] shedskin-0.9.4/tests/10.py0000664000175000017500000000056412157270661015222 0ustar srepmubsrepmub class fred: # hallo: [int] pass a = fred() # [fred_int] a.hallo = 1 # [int] b = a.hallo # [int] c = fred() # [fred_str] c.a = 'god' # [str] d = c.a # [str] shedskin-0.9.4/tests/18.py0000664000175000017500000000024312157270661015224 0ustar srepmubsrepmub cube = [] # [list_float_int] #cube.append(1) # [None] cube.append(1.0) # [None] shedskin-0.9.4/tests/29.py0000664000175000017500000000062712157270661015234 0ustar srepmubsrepmub argv = ['','testdata/uuf250-010.cnf'] # [list(str)] cnf = [l.strip().split() for l in file(argv[1]) if l[0] not in 'c%0\n'] clauses = [[int(x) for x in m[:-1]] for m in cnf if m[0] != 'p'] nrofvars = [int(n[2]) for n in cnf if n[0] == 'p'][0] vars = range(nrofvars+1) occurrence = [[[c for c in clauses if -v in c],[c for c in clauses if v in c]] for v in vars] fixedt = [-1 for var in vars] shedskin-0.9.4/tests/23.py0000664000175000017500000000061112157270661015217 0ustar srepmubsrepmub def hoei(cube): # x: [tuple_int], cube: [list_tuple_int], pos: [tuple_int] for pos in cube: # [list_tuple_int] x = pos # [tuple_int] return x # [tuple_int] cube = [(1,2),(3,4),(5,6)] # [list_tuple_int] b = hoei(cube) # [tuple_int] shedskin-0.9.4/tests/88.py0000664000175000017500000000100012157270661015223 0ustar srepmubsrepmub a = [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]] c = a[0] # [list(int)] c.append(1) # [] b = [['']] # [list(list(str))] d = b[0] # [list(str)] d.append('') # [] shedskin-0.9.4/tests/170.py0000664000175000017500000000235512157270661015311 0ustar srepmubsrepmub # --- model list.__str__ call to elem.__repr__ class Vertex(object): def __repr__(self): return 'rrrepr' print [Vertex()] # --- always true/false, but should work print [0,1][isinstance(7.0, float)] print [0,1][isinstance(7, int)] # --- initialize class attrs in .cpp file class blah: blah = 'blah' blah2 = ('blah', 'blah') blah3 = abs(-1) print blah.blah, blah.blah2, blah.blah3 # --- inf a,b = -1e500, 1e500; print a,b # --- argh print sorted('hopla') # --- dict * d = {} print d # --- cl attr problem class FilebasedMazeGame: hop = 18 def __init__(self): a = FilebasedMazeGame.hop print a FilebasedMazeGame() # --- define cl before use def print_aap(): aap = Aap() print aap class Aap: def __repr__(self): return 'hrngh!' print_aap() # --- virtual case class hop: def hop(self): self.hop2() class hop2(hop): def hop2(self): print 'hop2' hop2().hop() # --- str.split s = " foo zbr bar " print "default separator:" print s.split(None) print s.split(None, 0) print s.split(None, 1) print "space separator:" print s.split(' ') print s.split(' ', 0) # --- comparison class g: pass e, f = g(), g() print (e < f) + (e > f), [0,1][e == f] shedskin-0.9.4/tests/192.py0000664000175000017500000000530012157270661015306 0ustar srepmubsrepmub from __future__ import print_function # unbound ident import testdata.subsub print(list(testdata.subsub.blah(4))) # exception hierarchy try: try: raise SystemExit('hoei') except Exception, msg: print('foute boel') except BaseException as ork: print('base exc', ork) # map external func import math som=sum(map(math.factorial, [1,2,3])) print(som) from testdata.subsub import fact som=sum(map(fact, [1,2,3])) print(som) mf = math.factorial print(map(mf, range(10))) import testdata.subsub som=sum(map(testdata.subsub.fact, [1,2,3])) print(som) tsf = testdata.subsub.fact print(tsf(10)) # set problems collector = set() collector.add(frozenset([1,2])) collector.add(frozenset([1,2,3])) print(sorted(collector)) low_hits = set([19460, 19877, 20294, 20711, 21128, 21545, 21962, 19599, 20016, 20433, 20850, 21267, 21684, 22101, 19738, 20155, 20572, 20989, 21406, 21823]) high_hits = set([22052, 21605, 21158, 20711, 20264, 19817, 19370, 21903, 21456, 21009, 20562, 20115, 19668, 21754, 21307, 20860, 20413, 19966, 19519]) hits = low_hits.symmetric_difference(high_hits) print(sorted(hits)) # generator methods class GM: def loop(self): yield self.loop2(4) yield def loop2(self, x): return x*'patattie' g = GM() for xn in g.loop(): print(xn) class GenMeth2: def __init__(self, y): self.y = y def loop(self, x): z = self.y for i in x: yield i+z gm2 = GenMeth2(2) print(list(gm2.loop([4,1,5]))) # __future__ print_function class B: def __init__(self, value): self.value = value def __repr__(self): return 'B(%d)' % self.value b = B(14) print(b, b, b, sep='hoi', end='\n\n') print(min(1,2,3, key=lambda x:-x)) # problem with inheritance across files from testdata.subsub import aa class baa(aa): def __init__(self): aa.__init__(self) print("init b") self.hoppa() baa() # @x.setter syntax class bert(object): @property def patat(self): print('get') return self._x @patat.setter def patat(self, y): print('set') self._x = y b1 = bert() b1.patat = 12 print(b1.patat) # class-level constructors class aazz: class_dict_var={} class_dict_var2={} class_dict_var3={(1,2): 7} kwek = [] kwad = (1,2) wof = 'wof' s = set() t = s z = t | s wa = [2*x for x in range(10)] def __init__(self): self.y = 10 aazz.class_dict_var[4] = 5 aazz.class_dict_var2['4'] = 5 aazz.kwek.append('1') aazz.s.update(aazz.kwad) print(aazz().y) print(aazz.class_dict_var) print(aazz.class_dict_var2) print(aazz.class_dict_var3) print(aazz.kwek) print(aazz.kwad) print(aazz.wof) print(aazz.s, aazz.t, aazz.z) print(aazz.wa) shedskin-0.9.4/tests/114.py0000664000175000017500000000056712157270661015312 0ustar srepmubsrepmub def bla(): a = [] # [list(int)] a.append(1) # [] b = a # [list(int)] a = [] # [list(int)] a.append(2) # [] print b # [list(int)] bla() # [] shedskin-0.9.4/tests/100.py0000664000175000017500000000104112157270661015271 0ustar srepmubsrepmub print [1,2] == [1,2] # [int] print [(1,2),(2,3)] == [(1,2),(2,3)] # [int] print [(1,4),(2,3)] == [(1,2),(2,3)] # [int] print 1 in (1,2,3) # [int] print 1 in (1,2) # [int] print 3 in (1,2) # [int] print (1,2) in [(1,2),(2,3)] # [int] print (1,4) in [(1,2),(2,3)] # [int] print ((1,)) in [((2,)),((1,))] # [int] print ((3,)) in [((2,)),((1,))] # [int] print [1] in ([2],[1]) # [int] shedskin-0.9.4/tests/171.py0000664000175000017500000000562512157270661015315 0ustar srepmubsrepmub # --- math.pow import math print int(math.pow(2,3)) print pow(2.0,3.0) print pow(2,3.0) print pow(2.0,3) print pow(2,3) print pow(1000,1000,1234) # --- end-of-file problem print [l for l in file('testdata/scene.txt') if l.startswith('material')] # --- append '.0' when printing 'integer' floats (but not in case of %g!) print 8.0, '%g' % 8.0 # --- iterators b = [1,2,3] for a in b: print a, print print [a for a in b] g = iter(b) for x in range(3): print g.next(), print print [n for n in iter(b)] h = iter(b) e = iter(h) for f in e: print f, print i = [1,2,3] i = iter(i) i = [1,2,3] for j in i: print j, print print [j for j in i] print [y for y in 'stroop'] print [n for n in {1: '1', 2: '2', 3: '3'}] print [z for z in [[1],[2],[3]]] print sorted([m for m in set([1.0,2.0,3.0])]) print [l for l in file('testdata/hoppa')] # --- generators def blah(a): while a > 0: yield a yield 17 a -= 1 hop = blah(3) for x in range(4): print hop.next(), print hop = blah(1) try: for x in range(4): print hop.next(), print except StopIteration: print 'klaar.' # --- verify some things still work import os.path print os.path.split('hoempa/nohu') import math print '%g' % math.log(10) # --- % revisited print -2 % 3 print 2 % 3 print math.fmod(-2.0, 3) print -2.0 % 3 print 4 % 3 print 4 % 3.0 print math.fmod(2.0, -3) print -2.0 % -3 print -2.0 % -3.0 print 2.0 % -3.0 print '%g' % 3.0 # --- and/or revisited print 0 or 5 or 4 print 0 or 0 print 1 > 2 or 3 < 4 ax = [1] ax = [] bx = [2] print ax or bx print [0,1][(ax or None) is None] print bx or None print None or bx print 1 and 4, 4 and 1 print bx and [] def ef(x): print 'hah', x return 0 ef(5) and ef(6) # --- allow mixing when result is not used n = 1 n < 0 or bx bx and n > 1 # --- make this compile (XXX we shouldn't implicitly call parent constructors though) class smurf: def __init__(self, a=-1): print 'hallo', a class baviaan(smurf): def __init__(self, a=-1): print 'oehoehoe', a smurf() baviaan() # --- simple itertools functions #import itertools #gg = itertools.count() #print [gg.next() for i in range(10)] # #cycle = itertools.cycle(range(3)) #print [cycle.next() for i in range(10)] # #repeat = itertools.repeat([1,2,3], 10) #print [repeat.next() for i in range(3)] # --- xrange, enumerate, reversed as iterators ah = xrange(10) for x in ah: print x, print ah = xrange(0,10,3) for x in ah: print x, print ah = xrange(10,0,-3) for x in ah: print x, print bh = enumerate(xrange(10,0,-3)) print [y for y in bh] ch = enumerate([(1.0, 's') for x in range(4)]) print [z for z in ch] print [zz for zz in reversed(range(10))] print [zzz for zzz in reversed(xrange(10))] # --- dict.{iterkeys, itervalues, iteritems} waa = {1: '2', 2: '4'} for wax in waa.iterkeys(): print wax, print for way in waa.itervalues(): print way, print for wat in waa.iteritems(): print wat, print shedskin-0.9.4/tests/37.py0000664000175000017500000001672312157270661015237 0ustar srepmubsrepmub # (c) Mark Dufour # --- mark.dufour@gmail.com def _reduce(f, l, i=-1): # f: [lambda0], i: [int], l: [list(int)], r: [int] if not l: # [list(int)] if i != -1: return i # [int] print '*** ERROR! *** reduce() called with empty sequence and no initial value' # [str] if i != -1: # [int] r = f(i, l[0]) # [int] else: r = l[0] # [int] for i in range(len(l)-1): # [int] r = f(r, l[i+1]) # [int] return r # [int] argv = ['','testdata/uuf250-010.cnf'] # [list(str)] # solver cnf = [l.strip().split() for l in file(argv[1]) if l[0] not in 'c0%\n'] # [list(list(str))] clauses = [[int(x) for x in l[:-1] if x != ''] for l in cnf if l[0] != 'p'] # [list(list(int))] nrofvars = [int(l[2]) for l in cnf if l[0] == 'p'][0] # [int] vars = range(nrofvars+1) # [list(int)] occurrence = [[] for l in 2*vars] for clause in clauses: # [list(int)] for lit in clause: occurrence[lit].append(clause) # [int] fixedt = [-1 for var in vars] # [list(int)] nodecount, propcount = 0, 0 # [int], [int] def solve_rec(): # la_mods: [list(int)], var: [int], prop_mods: [list(int)], choice: [int]* global nodecount nodecount += 1 # [] if nodecount == 100: return 1 if not -1 in fixedt[1:]: # [int] print 'v', ' '.join([str((2*fixedt[i]-1)*i) for i in range(1,nrofvars+1)]) # [list(str)] return 1 # [int] la_mods = [] # [list(int)] var = lookahead(la_mods) # [int] #print 'select', var # [str], [int] if not var: return backtrack(la_mods) # [int] for choice in [var,-var]: # [int] prop_mods = [] # [list(int)] if propagate(choice, prop_mods) and solve_rec(): return 1 # [int] backtrack(prop_mods) # [int] return backtrack(la_mods) # [int] def propagate(lit, mods, failed_literal=0): # lit_truth: [int], current: [int], unfixed: [int]*, mods: [list(int)], clause: [list(int)], lit: [int]*, length: [int], failed_literal: [int] global bincount, propcount current = len(mods) # [int] mods.append(lit) # [None] #print 'prop', lit # [str], [int] while 1: # [int] if fixedt[abs(lit)] == -1: # [int] fixedt[abs(lit)] = int(lit>0) # [int] propcount += 1 # [] mask_propagate(lit) # [] for clause in occurrence[-lit]: # [list(int)] length, unfixed = info(clause) # [tuple(int)] if length == 0: # [int] #print 'dead', lit # [str], [int] return 0 # [int] elif length == 1: mods.append(unfixed) # [None] elif length == 2: # [int] bincount += 1 # [] if failed_literal: mask_binclause(unfixed_lits(clause)) # [] elif fixedt[abs(lit)] != int(lit>0): return 0 # [int] current += 1 # [] if current == len(mods): break # [int] lit = mods[current] # [int] return 1 # [int] def mask_propagate(lit): # lit: [int] global lit_mask, part_mask # XXX lit_mask[lit] |= part_mask # [] def mask_binclause(lits): # lit: [int], lits: [list(int)] global global_mask, lit_mask # XXX for lit in lits: global_mask |= lit_mask[-lit] # [int] hoppa = 0xffffffff def lookahead(mods): # mods: [list(int)], i: [int], u: [list(int)], var: [int], part: [list(int)] global global_mask, lit_mask, part_mask, some_failure global_mask = hoppa # [int] lit_mask = [0 for var in range(2*(nrofvars+1))] # [list(int)] u = unfixed_vars() # [list(int)] parts = [u[(i*len(u))>>5:((i+1)*len(u))>>5] for i in range(32)] # [list(list(int))] masks = [1<0): return -1, 0 # [tuple(int)] return len, unfixed # [tuple(int)] def unfixed_vars(): return [var for var in range(1,nrofvars+1) if fixedt[var] == -1] # [list(int)] def unfixed_lits(clause): # lit: [int]*, clause: [list(int)], result: [list(int)]r result = [] # [list(int)] for lit in clause: # [int] if fixedt[abs(lit)] == -1: result.append(lit) # [None] return result # [list(int)] if not solve_rec(): # [int] print 'unsatisfiable' # [str] print 'nodes', nodecount, 'propagations', propcount # [str], [int], [str], [int] shedskin-0.9.4/tests/52.py0000664000175000017500000000145612157270661015231 0ustar srepmubsrepmub #def hoi(a, b, c=1, d=1): # a: [int], b: [int], c: [int, float]r, d: [int] # print a, b, c, d # [int], [int], [int, float], [int] # return c # [int, float] # # #hoi(1,2) # [int] #hoi(1,2,3) # [int] #hoi(1,2,3,4) # [int] # #hoi(1,2,3.1) # [int, float] def hoi(a, b, c=1, d=1): # a: [int], b: [int], c: [int, float]r, d: [int] print a, b, c, d # [int], [int], [int, float], [int] return c # [int, float] hoi(1,2) # [int] hoi(1,2,3) # [int] hoi(1,2,3,4) # [int] shedskin-0.9.4/tests/12.py0000664000175000017500000000106212157270661015216 0ustar srepmubsrepmub class integer: def __gt__(self, b): return True def __repr__(self): return 'integer!' def maxi(a, b): # [integer], [integer] if a > b: # [bool] return a # [integer] return b # [integer] a = integer() # [integer] b = integer() # [integer] c = maxi(a, b) # [integer] d = a > b # [bool] print a, b, c, d shedskin-0.9.4/tests/129.py0000664000175000017500000000066312157270661015315 0ustar srepmubsrepmub import sys a = [(2,3),(3,2),(),(2,),(3,4,5),(2,2),(1,4),(4,1),(4,2),(4,3),(3,4),(4,4),(4,5),(1,5),(1,20),(20,1),(20,2)] print sorted(a) b = [[3,2],[1,3]] print sorted(b) c = ['b','c','aa'] print sorted(c) print sys.argv[1:] if (0 or 0 or 1) and 1: print 'yay' ah = 'hatsie flatsie pots' print ah.split(), ah == ' '.join(ah.split()) print 'hoei hoei'.split() print 'hoei hoei\\n'.split() print ['hoei\\n'] print 'hoei\\n' shedskin-0.9.4/tests/198.py0000664000175000017500000000146512157270661015324 0ustar srepmubsrepmub# __NOT macro missing parentheses x=10 if not 1 0: # [int] count += 1 # [int] nums.append((count, f)) # [] temp = f # [int] f = s # [int] s = temp + s # [int] n -= 1 # [int] print nums # [list(tuple2(int, int))] shedskin-0.9.4/tests/8.py0000664000175000017500000000011412157270661015140 0ustar srepmubsrepmub def aap(y): return y def hap(y): return y x = aap(1) y = hap(1.0) shedskin-0.9.4/tests/25.py0000664000175000017500000000002712157270661015222 0ustar srepmubsrepmub cube = [[1,2],(3,4)] shedskin-0.9.4/tests/124.py0000664000175000017500000000267412157270661015314 0ustar srepmubsrepmub # A simple program to find the prime factors of a given number. # (c) Rohit Krishna Kumar # --- http://www.geocities.com/rohitkkumar import math def prime(n): # n: [int] if(n==1): # [int] return False # [int] if(n==2): # [int] return True # [int] if(not n%2): # [int] return False # [int] for i in range(3,int(math.sqrt(n))+1,2): # [list(int)] if(not n%i): # [int] return False # [int] return True # [int] def factorize(n,l): # n: [int], l: [list(int)] for i in range(2,int(math.sqrt(n))+1): # [list(int)] if(not n%i): # [int] if(prime(i)): # [int] l.append(i) # [] else: factorize(i,l) # [] if(prime(n/i)): # [int] l.append(n/i) # [] else: factorize(n/i,l) # [] break factors=[] # [list(int)] n='2079283419' # [int] #raw_input("Number to factorize:") # [str] factorize(int(n),factors) # [] print factors # [list(int)] shedskin-0.9.4/tests/36.py0000664000175000017500000000043412157270661015226 0ustar srepmubsrepmub argv = ['','testdata/uuf250-010.cnf'] # [list(str)] def ffile(name): # name: [str] return [1] # [list(int)] x = argv[0] # [str] cnf = [y for y in ffile(x)] # [list(int)] shedskin-0.9.4/tests/153.py0000664000175000017500000000623512157270661015313 0ustar srepmubsrepmub # (c) (the sister of) Peter Goodspeed # --- coriolinus@gmail.com #plife.py - conway's game of life, with no object-orientation, # a 20x20 non-wrapping grid, and no exceptions #functions def rawBoard(): return [200 * [False] for i in xrange(200)] #def fromKb(): # eventLoop(lambda arg: raw_input(arg)) def nextI(qstr): global source if source == 1: #from keyboard return raw_input(qstr) elif source == 2: #from file global flines global fcur if fcur < len(flines): ret = flines[fcur] fcur += 1 return ret def pb(board): #print board print "-" * 20 for row in board: ro = '' for i in xrange(len(row)): if row[i]: ro += "X" else: ro += " " print ro print "-" * 20 def eventLoop(nextInput): cont = 'p' while cont.lower()[0] == 'p': board = rawBoard() #how many inputs should we expect? numcells = int(nextInput("how many cells? ")) #get that many cells for i in xrange(numcells): xy = str(nextInput("x,y: ")).split(',') x,y = int(xy[0]),int(xy[1]) #set those cells board[x][y] = True #pb(board) runSim(board) cont = nextInput("play again? (p for yes; anything else for no): ") def runSim(board): #main loop for simulating life turns = 0 ob = None # old board while turns < 10 and board != ob: turns += 1 ob = board board = nextgen(board) #pb(board) #print if turns >= 10000: print "10 turns exhausted" else: print "stabilized on turn %s" % str(turns + 1) def nextgen(board): #transform the old board into a new one nb = rawBoard() for rown in xrange(len(board)): for coln in xrange(len(board[rown])): nn = 0 for r,c in neighbors(rown, coln): if board[r][c]: nn += 1 if nn == 3: nb[rown][coln] = True elif nn >= 4 or nn < 2: nb[rown][coln] = False else: nb[rown][coln] = board[rown][coln] return nb def neighbors(x,y): rl = [] for mx in [-1,0,1]: for my in [-1,0,1]: if not (mx == 0 and my == 0): r = (x + mx, y + my) if r[0] >= 0 and r[0] < 20 and r[1] >= 0 and r[1] < 20: rl.append(r) return rl #main source = 0 while source not in [1,2]: source = 2 #int(raw_input("1 for input from keyboard; 2 for input from file: ")) if source==2: fp = open('testdata/life.txt') flines = [line for line in fp] fp.close() fcur = 0 eventLoop(nextI) shedskin-0.9.4/tests/95.py0000664000175000017500000000021612157270661015231 0ustar srepmubsrepmub l = [(1,2.0),(2,4.0)] # [list(tuple2(int, float))] m = [(bh, ah) for (ah, bh) in l] # [list(tuple2(float, int))] shedskin-0.9.4/tests/168.py0000664000175000017500000000076312157270661015321 0ustar srepmubsrepmub a = frozenset([1]) d = a & a d = a | a d = a - a d = a ^ a print a, d c = set([1,2]) e = set([]) f = set() print c, e, f g = frozenset([1]) h = {} h[g] = 4 print h h[frozenset([3,2,1])] = 5 del h[frozenset([1])] for x in h: print sorted(x), h[x] try: {set([1]): 1} except TypeError, m: print m z,y = [(1,2),(3,), (4,5,6)], [(3,),(4,5,6),(1,2)] v, w = frozenset(z), frozenset(y) print 'eq', [0, 1][v == w] print 'hash', [0, 1][hash(v) == hash(w)] k = set([1]) k = frozenset([2]) shedskin-0.9.4/tests/120.py0000664000175000017500000000072712157270661015305 0ustar srepmubsrepmub l = [(b,a) for a,b in enumerate([1,2,3])] # [list(tuple(int))] print l # [list(tuple(int))] for a,b in enumerate([1.1,2.2,3.3]): # [tuple2(int, float)] print 'huhu', a, '%.1f' % b # [str], [int], [float] def bla(): return ('1',2.2) # [tuple2(str, float)] x,y = bla() # [tuple2(str, float)] z,v = bla() # [tuple2(str, float)] shedskin-0.9.4/tests/15.py0000664000175000017500000000203412157270661015221 0ustar srepmubsrepmub class xevious: # y: [int]*, z: [str]* def solvalou(self, x): # x: [int]*, self: [xevious_str] return x # [int] def pacman(a): # a: [int] return 1 # [int] def qbert(): # a: [int], c: [int], b: [int]*, e: [int]*, d: [int]*, x: [xevious_str] c = 1 # [int] a = 1 # [int] pacman(a) # [int] b = 1 # [int] a = c # [int] d = 1 # [int] e = 1 # [int] x = xevious() # [xevious_str] x.y = d # [int] x.z = 'hoi' # [str] x.solvalou(e) # [int] return b # [int] qbert() # [int] shedskin-0.9.4/tests/106.py0000664000175000017500000000250112157270661015301 0ustar srepmubsrepmub def escapement(): a = vars1() # [list(int)] return a # [list(int)] def vars1(): return [1,2] # [list(int)] x = escapement() # [list(int)] y = escapement() # [list(int)] y.append(3) # [] print x # [list(int)] def escapement2(): bla(vars3()) # [] def bla(x): # x: [list(int)]* global bye bye = x # [list(int)] def vars3(): return [1] # [list(int)] def joink(): x = vars3() # [list(int)] escapement2() # [] bye.append(2) # [] joink() # [] print bye # [list(int)] def transitive(): a = vars2() # [list(int)] hoi() # [] print a # [list(int)] def vars2(): return [1,2] # [list(int)] def hoi(): a = vars2() # [list(int)] a.append(3) # [] transitive() # [] shedskin-0.9.4/tests/113.py0000664000175000017500000000035712157270661015306 0ustar srepmubsrepmub a = [1,2,3] # [list(int)] print a[0], a[1], a[-2], a[-1] # [int], [int], [int], [int] d = {-1: 2} # [dict(int, int)] print d[-1] # [int] shedskin-0.9.4/tests/169.py0000664000175000017500000000261712157270661015322 0ustar srepmubsrepmub # --- assignment expressions bweh = (2,[4,6]) [a, (b,c)] = bweh print a,b,c (a,b), (c,d) = (6,9), (8,7) print a,b,c,d [(a,b), (c,d)] = (9,8), (7,6) print a,b,c,d [(a,b), (c,d)] = [(1,8), (7,2)] print a,b,c,d [[a,b],c] = (5,6),3 print a,b,c [[a,b],c] = [[4,5],6] print a,b,c a, [b,c] = [1, (2,3)] print a,b,c a, (b,c,d) = 1, (1,2,3) print a,b,c,d [(a,b), [c,d]] = [[1,2], (3,4)] print a,b,c,d njeh = [[8,7,6],[5,4,3],[2,1,0]] [[a,b,c],[d,e,f],[g,h,i]] = njeh print a,b,c,d,e,f,g,h,i [dx,[a,b,c],ex] = njeh print dx,a,b,c,ex blah = (1,2,3,4,5,6) a,b,c,d,e,f = blah print a,b,c,d,e,f # --- underscore in assignment _ = 4 print _, _ #a, _ = 1, '2' #huh = 1, 2 #_, b = huh #mtx = [[1,2,3],[4,5,6],[6,7,8]] #[du, [x, y, _], _] = mtx #print du, x, y #hop = [(1,(2,3))] #for _ in hop: print 'hop' #for _, (a,b) in hop: print 'hop', a, b #for a, (_,b) in hop: print 'hop', a, b #for a, _ in hop: print 'hop', a #print ['hop' for _ in hop] #print ['hop %d %d' % (a,b) for _, [a,b] in hop] #print ['hop %d %d' % (a,b) for a, [_,b] in hop] #print ['hop %d' % a for a, _ in hop] # --- except 'tuple' for a in range(2): try: if not a: assert 1 > 2, 'parasmurf' else: {1:2}[3] except (AssertionError, KeyError), m: print 'foutje3 of 4', m # --- getopt.GetoptError test import getopt try: opts, args = getopt.getopt(['-x'], 'nf:', ['nowrap', 'flags=']) except getopt.GetoptError: print 'fout' shedskin-0.9.4/tests/155.py0000664000175000017500000001547712157270661015325 0ustar srepmubsrepmub # (c) Peter Cock # --- http://www2.warwick.ac.uk/fac/sci/moac/currentstudents/peter_cock/python/sudoku/ TRIPLETS = [[0,1,2],[3,4,5],[6,7,8]] ROW_ITER = [[(row,col) for col in range(0,9)] for row in range(0,9)] COL_ITER = [[(row,col) for row in range(0,9)] for col in range(0,9)] TxT_ITER = [[(row,col) for row in rows for col in cols] for rows in TRIPLETS for cols in TRIPLETS] class soduko: def __init__(self, start_grid=None) : self.squares =[ [range(1,10) for col in range(0,9)] for row in range(0,9)] if start_grid is not None: assert len(start_grid)==9, "Bad input!" for row in range(0,9) : self.set_row(row, start_grid[row]) self._changed=False def copy(self) : soduko_copy = soduko(None) for row in range(0,9) : for col in range(0,9) : soduko_copy.squares[row][col] = self.squares[row][col][:] soduko_copy._changed=False return soduko_copy def set_row(self,row, x_list) : assert len(x_list)==9, 'not 9' for col in range(0,9) : try : x = int(x_list[col]) except : x = 0 self.set_cell(row,col,x) def set_cell(self,row,col,x): if self.squares[row][col] == [x] : pass elif x not in range(1,9+1) : pass else: assert x in self.squares[row][col], "bugger2" self.squares[row][col] = [x] self.update_neighbours(row,col,x) self._changed=True def cell_exclude(self, row,col,x) : assert x in range(1,9+1), 'inra' if x in self.squares[row][col] : self.squares[row][col].remove(x) assert len(self.squares[row][col]) > 0, "bugger" if len(self.squares[row][col]) == 1 : self._changed=True self.update_neighbours(row,col,self.squares[row][col][0]) else : pass return def update_neighbours(self,set_row,set_col,x) : for row in range(0,9) : if row <> set_row : self.cell_exclude(row,set_col,x) for col in range(0,9) : if col <> set_col : self.cell_exclude(set_row,col,x) for triplet in TRIPLETS : if set_row in triplet : rows = triplet[:] if set_col in triplet : cols = triplet[:] rows.remove(set_row) cols.remove(set_col) for row in rows : for col in cols : assert row <> set_row or col <> set_col , 'meuh' self.cell_exclude(row,col,x) def get_cell_digit_str(self,row,col) : if len(self.squares[row][col])==1 : return str(self.squares[row][col][0]) else : return "0" def __str__(self): answer = " 123 456 789\n" for row in range(0,9) : answer = answer + str(row+1) + " [" + "".join([self.get_cell_digit_str(row,col).replace("0","?") for col in range(0,3)]) + "] [" + "".join([self.get_cell_digit_str(row,col).replace("0","?") for col in range(3,6)]) + "] [" + "".join([self.get_cell_digit_str(row,col).replace("0","?") for col in range(6,9)]) + "]\n" if row+1 in [3,6] : answer = answer + " --- --- ---\n" return answer def check(self) : self._changed=True while self._changed: self._changed=False self.check_for_single_occurances() self.check_for_last_in_row_col_3x3() return def check_for_single_occurances(self): for check_type in [ROW_ITER, COL_ITER, TxT_ITER]: for check_list in check_type : for x in range(1,9+1) : #1 to 9 inclusive x_in_list = [] for (row,col) in check_list : if x in self.squares[row][col] : x_in_list.append((row,col)) if len(x_in_list)==1 : (row,col) = x_in_list[0] if len(self.squares[row][col]) > 1 : self.set_cell(row,col,x) def check_for_last_in_row_col_3x3(self): for (type_name, check_type) in [("Row",ROW_ITER),("Col",COL_ITER),("3x3",TxT_ITER)]: for check_list in check_type : unknown_entries = [] unassigned_values = range(1,9+1) #1-9 inclusive known_values = [] for (row,col) in check_list : if len(self.squares[row][col]) == 1 : assert self.squares[row][col][0] not in known_values, "bugger3" known_values.append(self.squares[row][col][0]) assert self.squares[row][col][0] in unassigned_values, "bugger4" unassigned_values.remove(self.squares[row][col][0]) else : unknown_entries.append((row,col)) assert len(unknown_entries) + len(known_values) == 9, 'bugger5' assert len(unknown_entries) == len(unassigned_values), 'bugger6' if len(unknown_entries) == 1 : x = unassigned_values[0] (row,col) = unknown_entries[0] self.set_cell(row,col,x) return def one_level_supposition(self): progress=True while progress : progress=False for row in range(0,9) : for col in range(0,9): if len(self.squares[row][col]) > 1 : bad_x = [] for x in self.squares[row][col] : soduko_copy = self.copy() try: soduko_copy.set_cell(row,col,x) soduko_copy.check() except AssertionError, e : bad_x.append(x) del soduko_copy if len(bad_x) == 0 : pass elif len(bad_x) < len(self.squares[row][col]) : for x in bad_x : self.cell_exclude(row,col,x) self.check() progress=True else : assert False, "bugger7" for x in range(50): t = soduko(["800000600", "040500100", "070090000", "030020007", "600008004", "500000090", "000030020", "001006050", "004000003"]) t.check() t.one_level_supposition() t.check() print t shedskin-0.9.4/tests/194.py0000664000175000017500000000706012157270661015315 0ustar srepmubsrepmub#new casting/conversion approach digit_dict = { "1":{1:(1,2,3,4,5),2:(1,3,5) ,3:()} ,"2":{1:(2,) ,2:() ,3:(4,)} ,"3":{1:(2,4) ,2:() ,3:()} ,"4":{1:(4,5) ,2:(1,5) ,3:()} ,"5":{1:(4,) ,2:() ,3:(2,)} ,"6":{1:() ,2:() ,3:(2,)} ,"7":{1:(2,3,4,5) ,2:(3,5) ,3:()} ,"8":{1:() ,2:() ,3:()} ,"9":{1:(4,) ,2:() ,3:()} ,"0":{1:() ,2:(3,) ,3:()} } for d in sorted(digit_dict): d2 = digit_dict[d] for g in sorted(d2): print d, g, d2[g] l = [[7,8,9], [7.7,8.8,9.9]] for ll in l: for lll in ll: print '%.2f' % lll #circular includes from testdata import bert class Here: def __str__(self): return 'here' bert.hello(Here()) #partial support for 'super' class A(object): def __init__(self, x): print 'a', x class C(A): def __init__(self, x): print 'c', x class B(C): def __init__(self, x): super(B, self).__init__(x) super(C, self).__init__(3*x) A.__init__(self, 2*x) C.__init__(self, 3*x) print 'b', x B(7) #update with genexpr _hextochr = dict(('%02x' % i, chr(i)) for i in range(256)) _hextochr.update(('%02X' % i, chr(i)) for i in range(256)) print(sorted(_hextochr)) #C++ looks in class namespace first kwek = 18 class Test1(object) : def __init__(self, lenin) : self.len = lenin self.buf = "x" * lenin self.kwek = 17 def getlen(self) : print kwek return(len(self.buf)) f = Test1(100) n = f.getlen() print(n) # {IOError, OSError}.{errno, strerror} try : print("Try block") fd = open("nosuchfile") # open will fail print("File opened") except IOError as e: print e, repr(e) print e.errno, e.strerror, e.filename #import os XXX fix under windows #try: # os.chdir('meuheuheu') #except OSError as e2: # print e2, repr(e2) # print e2.errno, e2.strerror, e2.filename # del crash class AA: def __init__(self, x): if x == 1: self.a = AA(0) self.b = AA(0) def __del__(self): pass aa = AA(1) gg = {1:2,3:4,5:7} del aa.a, aa.b, gg[1], gg[5] print gg lx = [1,2] del aa, gg, lx # char_cache out of bounds for nnn in '"\xd8\xc3A~s': print repr(nnn) # partition model (ar,br,cr) = 'allo ballo'.partition(' ') print ar print br print cr # tuple_flow and globals def bwa(): return (11,12) def bwb(): print bwg def bwmain(): global bwg bwg=30 bwg,bwh=bwa() bwb() bwmain() # dict.update model dikkie = {} dikkie.update((a,-a) for a in range(-5,5,2)) print sorted(dikkie.keys()), sorted(dikkie.values()) import collections dikkie2 = collections.defaultdict(int) dikkie2.update((a,-a) for a in range(-5,5,2)) print sorted(dikkie2.keys()), sorted(dikkie2.values()) # unused import from testdata.bert2 import hello from testdata.bert2 import * # late binding from testdata import board from testdata import piece piece.latebinding() # missing forward class declaration from testdata.Shape import Shape class HitResult(object): def update(self, s): print 'update' self.s = s hitresult = HitResult() shape = Shape() shape.woef(hitresult) # almost closure d3 = {1: 3, 2: 2, 3: 1} l3 = [1,2,3] print sorted(l3, key = lambda x: d3[x]) # float(..) print float(' \n iNf'), float('INF'), float(' -inf'), float('NaN'), float('-nan'), float('infinity'), float('-infinITY') shedskin-0.9.4/tests/105.py0000664000175000017500000000046312157270661015305 0ustar srepmubsrepmub def bla(): return x, y # [tuple2(int, int)] def blu(): global x x = 2 # [int] y = 2 # [int] blu() # [] print bla() # [tuple2(int, int)] shedskin-0.9.4/tests/77.py0000664000175000017500000000031012157270661015224 0ustar srepmubsrepmub u = [' p o', 'c o '] # [list(str)] cnf = [x.strip().split() for x in u if not x.startswith('x')] # [list(list(str))] cnf2 = [[3] for x in u] # [list(list(int))] shedskin-0.9.4/tests/7.py0000664000175000017500000000006212157270661015141 0ustar srepmubsrepmub def bwa(): d = 'hoi' return d a = bwa() shedskin-0.9.4/tests/122.py0000664000175000017500000001161212157270661015302 0ustar srepmubsrepmub # (c) Mark Dufour # --- mark.dufour@gmail.com import random random.seed(42) from math import sqrt, e sigmoid = lambda x: pow((1+pow(e,-x)),-1) # [lambda0] deriv = lambda x: pow(e,-x) * pow((1+pow(e,-x)),-2) # [lambda0] class link: # in_node: [node], weight: [float], activation: [], out_node: [node], delta: [], input: [], output: [], unit: [] def __init__(self, in_node, out_node): # self: [nlink], in_node: [node]*, out_node: [node]* self.in_node = in_node; self.out_node = out_node # [node] self.weight = (random.random()-0.5)/2 # [float] class node: # in_node: [], weight: [], activation: [float], out_node: [], delta: [float], output: [list(nlink)], input: [list(nlink)], unit: [] def __init__(self, input_nodes): # self: [node], input_nodes: [list(node)] self.input, self.output = [], [] # [list(nlink)], [list(nlink)] for node in input_nodes: # [list(node)] l = link(node,self) # [nlink] self.input.append(l) # [] node.output.append(l) # [] def incoming(node): return sum([link.in_node.activation * link.weight for link in node.input]) # [float] def neural_network_output(network, input): # network: [list(list(node))], input: [list(int)] # set input layer activations for index, node in enumerate(network[0]): # [tuple(int, node)] node.activation = input[index] # [int] # forward propagate output for layer in network[1:]: # [list(list(node))] for node in layer: # [list(node)] node.activation = sigmoid(incoming(node)) # [float] return [node.activation for node in network[-1]] # [list(float)] def back_propagate_error(network, answer): # network: [list(list(node))], answer: [list(int)] #output = [node.activation for node in network[-1]] # [list(float)] # output layer deltas for index, node in enumerate(network[-1]): # [tuple(int, node)] node.delta = deriv(incoming(node)) * (answer[index] - node.activation) # [float] # backward propagate error for layer in network[-2::-1]: # [list(list(node))] for node in layer: # [list(node)] node.delta = deriv(incoming(node)) * sum([link.out_node.delta * link.weight for link in node.output]) # [float] for link in node.output: # [list(nlink)] link.weight += alpha * node.activation * link.out_node.delta # [float] def append_error(network, examples): # network: [list(list(node))], examples: [list(tuple(list(int)))] compare = [(neural_network_output(network, example)[0], answer[0]) for example, answer in examples] # [list(tuple(float, int))] errors.append(sqrt((1.0/len(examples))*sum([pow(answer-output,2) for output, answer in compare]))) # [tuple(float, int)] def train_network(network, examples, epochs): # network: [list(list(node))], examples: [list(tuple(list(int)))], epochs: [int] global errors errors = [] # [list(float)] append_error(network, examples) # [] for epoch in range(epochs): # [list(int)] for example, answer in examples: # [tuple(list(int))] output = neural_network_output(network, example) # [list(float)] back_propagate_error(network, answer) # [] #print_weights(network) append_error(network, examples) # [] #def print_weights(network): # for number, layer in enumerate(network[-2::-1]): # print 'layer', number # for node in layer: # print [link.weight for link in node.output] alpha = 0.5 # [float] input_layer = [node([]) for n in range(10)] # [list(node)] hidden_layer = [node(input_layer) for n in range(4)] # [list(node)] output_layer = [node(hidden_layer) for n in range(1)] # [list(node)] network = [input_layer, hidden_layer, output_layer] # [list(list(node))] examples = [ ([1,0,0,1,1,2,0,1,0,0], [1]), # [list(tuple(list(int)))] ([1,0,0,1,2,0,0,0,2,2], [0]), # [tuple(list(int))] ([0,1,0,0,1,0,0,0,3,0], [1]), # [list(int)] ([1,0,1,1,2,0,1,0,2,1], [1]), # [tuple(list(int))] ([1,0,1,0,2,2,0,1,0,3], [0]), # [tuple(list(int))] ([0,1,0,1,1,1,1,1,1,0], [1]), # [tuple(list(int))] ([0,1,0,0,0,0,1,0,3,0], [0]), # [list(int)] ([0,0,0,1,1,1,1,1,2,0], [1]), # [list(int)] ([0,1,1,0,2,0,1,0,3,3], [0]), # [list(int)] ([1,1,1,1,2,2,0,1,1,1], [0]), # [list(int)] ([0,0,0,0,0,0,0,0,2,0], [0]), # [list(int)] ([1,1,1,1,2,0,0,0,3,2], [1]) ] # [list(int)] epochs = 1000 # [int] train_network(network, examples, epochs) # [] for hup in [neural_network_output(network, example) for example, answer in examples]: print [('%.2f'%hap) for hap in hup] shedskin-0.9.4/tests/45.py0000664000175000017500000000100012157270661015214 0ustar srepmubsrepmub class bert: def __repr__(self): # self: [] return 'bert' # [str] y = None # [None] y = bert() # [bert()] if y: # [int] print y # [str] z = None # [None] z = [1] # [int] if z: # [int] print z # [str] shedskin-0.9.4/tests/162.py0000664000175000017500000000141512157270661015306 0ustar srepmubsrepmub class City(object): def __init__(self): self.latitude = 1 class SortedTree(object): def __init__(self, compareKey): self.compareKey = compareKey class Map(object): def __init__(self): st = SortedTree(lambda x: x.latitude) st.compareKey(c) c = City() m = Map() print "1, 3, 5".replace(",", "") print "1, 3, 5".replace(",", "", -1) print "1, 3, 5".replace(",", "", 0) print "1, 3, 5".replace(",", "", 1) a = [] a = [[]] a = [[1]] b = [] b = [1] d = () d = (5,) print a, b, d def bla(t): print t bla(()) bla((1,)) def oink(): return [[1]] return [[]] oink() def test(t=()): if t: print t else: test(t + (5,)) test() e = {} e[2,3] = 4 f = {} f[5] = 6 print e, f import os x = os.listdir('.') shedskin-0.9.4/tests/151.py0000664000175000017500000002546012157270661015312 0ustar srepmubsrepmub # (c) Dave Griffiths # --- http://www.pawfal.org/index.php?page=PyGmy from math import sin, cos, sqrt import random, sys def sq(a): return a*a def conv_value(col): if col >= 1.0: return "255" elif col <= 0.0: return "0" else: return str(int(col*255.0)) class Shaderinfo: pass class vec: def __init__(self, x, y, z): self.x = float(x) self.y = float(y) self.z = float(z) def __add__(self,other): return vec(self.x+other.x, self.y+other.y, self.z+other.z) def __sub__(self,other): return vec(self.x-other.x, self.y-other.y, self.z-other.z) def __mul__(self,amount): return vec(self.x*amount, self.y*amount, self.z*amount) def __div__(self,amount): return vec(self.x/amount, self.y/amount, self.z/amount) def __neg__(self): return vec(-self.x, -self.y, -self.z) def dot(self,other): return self.x*other.x + self.y*other.y + self.z*other.z def dist(self,other): return sqrt((other.x-self.x)*(other.x-self.x)+ (other.y-self.y)*(other.y-self.y)+ (other.z-self.z)*(other.z-self.z)) def sq(self): return sq(self.x) + sq(self.y) + sq(self.z) def mag(self): return self.dist(vec(0.0, 0.0, 0.0)) def norm(self): mag = self.mag() if mag != 0: self.x = self.x/mag self.y = self.y/mag self.z = self.z/mag def reflect(self,normal): vdn = self.dot(normal)*2 return self - normal*vdn class line: def __init__(self, start, end): self.start = start self.end = end def vec(self): return self.end - self.start class renderobject: def __init__(self, shader): self.shader = shader class plane(renderobject): def __init__(self, plane, dist, shader): renderobject.__init__(self, shader) self.plane = plane self.dist = dist def intersect(self,l): vd = self.plane.dot(l.vec()) if vd == 0: return "none",(vec(0.0, 0.0, 0.0),vec(0.0, 0.0, 0.0)) v0 = -(self.plane.dot(l.start)+self.dist) t = v0/vd if t<0 or t>1: return "none",(vec(0.0, 0.0, 0.0),vec(0.0, 0.0, 0.0)) return "one", (l.start+(l.vec()*t), self.plane) class sphere(renderobject): def __init__(self, pos, radius, shader): renderobject.__init__(self, shader) self.pos = pos self.radius = radius def intersect(self,l): lvec = l.vec() a = sq(lvec.x) + sq(lvec.y) + sq(lvec.z) b = 2*(lvec.x*(l.start.x-self.pos.x)+ lvec.y*(l.start.y-self.pos.y)+ lvec.z*(l.start.z-self.pos.z)) c = self.pos.sq()+l.start.sq() - 2*(self.pos.x*l.start.x+self.pos.y*l.start.y+self.pos.z*l.start.z)-sq(self.radius) i = b*b - 4*a*c intersectiontype = "none" pos = vec(0.0, 0.0, 0.0) norm = vec(0.0, 0.0, 0.0) t = 0.0 if i > 0: if i == 0: intersectiontype="one" t = -b/(2*a) else: intersectiontype="two" t = (-b - sqrt( b*b - 4*a*c )) / (2*a) if t>0 and t<1: pos = l.start + lvec*t norm = pos - self.pos norm.norm() else: intersectiontype="none" return intersectiontype, (pos, norm) class light: def checkshadow(self, obj, objects,l): for ob in objects: if ob is not obj: intersects,(pos, norm) = ob.intersect(l) if intersects is not "none": return 1 return 0 class parallellight(light): def __init__(self, direction, col): direction.norm() self.direction = direction self.col= col def inshadow(self, obj, objects, pos): l = line(pos, pos+self.direction*1000.0) return self.checkshadow(obj, objects,l) def light(self, shaderinfo): if self.inshadow(shaderinfo.thisobj, shaderinfo.objects, shaderinfo.position): return vec(0.0, 0.0, 0.0) return self.col*self.direction.dot(shaderinfo.normal) class pointlight(light): def __init__(self, position, col): self.position = position self.col = col def inshadow(self, obj, objects, pos): l = line(pos, self.position) return self.checkshadow(obj, objects,l) def light(self, shaderinfo): if self.inshadow(shaderinfo.thisobj, shaderinfo.objects, shaderinfo.position): return vec(0.0, 0.0, 0.0) direction = shaderinfo.position - self.position direction.norm() direction = -direction return self.col*direction.dot(shaderinfo.normal) class shader: def getreflected(self, shaderinfo): depth = shaderinfo.depth col = vec(0.0, 0.0, 0.0) if depth > 0: lray = line(shaderinfo.ray.start, shaderinfo.ray.end) #copy.copy(shaderinfo.ray) ray = lray.vec() normal = vec(shaderinfo.normal.x, shaderinfo.normal.y, shaderinfo.normal.z) #copy.copy(shaderinfo.normal) ray = ray.reflect(normal) reflected = line(shaderinfo.position,shaderinfo.position+ray) obj = shaderinfo.thisobj objects = shaderinfo.objects newshaderinfo = Shaderinfo() #copy.copy(shaderinfo) # XXX newshaderinfo.thisobj = shaderinfo.thisobj newshaderinfo.objects = shaderinfo.objects newshaderinfo.lights = shaderinfo.lights newshaderinfo.position = shaderinfo.position newshaderinfo.normal = shaderinfo.normal newshaderinfo.ray = reflected newshaderinfo.depth = depth - 1 # todo - depth test for ob in objects: if ob is not obj: intersects,(position,normal) = ob.intersect(reflected) if intersects is not "none": newshaderinfo.thisobj = ob newshaderinfo.position = position newshaderinfo.normal = normal col = col + ob.shader.shade(newshaderinfo) return col def isoccluded(self, ray, shaderinfo): dist = ray.mag() test = line(shaderinfo.position, shaderinfo.position+ray) obj = shaderinfo.thisobj objects = shaderinfo.objects # todo - depth test for ob in objects: if ob is not obj: intersects,(position,normal) = ob.intersect(test) if intersects is not "none": return 1 return 0 def doocclusion(self, samples, shaderinfo): # not really very scientific, or good in any way... oc = 0.0 for i in xrange(samples): ray = vec(float(random.randrange(-100,100)),float(random.randrange(-100,100)),float(random.randrange(-100,100))) ray.norm() ray = ray * 2.5 if self.isoccluded(ray, shaderinfo): oc = oc + 1 oc = oc / float(samples) return 1-oc def shade(self,shaderinfo): col = vec(0.0, 0.0, 0.0) for lite in shaderinfo.lights: col = col + lite.light(shaderinfo) return col class world: def __init__(self,width,height): self.lights = [] self.objects = [] self.cameratype = "persp" self.width = width self.height = height self.backplane = 2000.0 self.imageplane = 5.0 self.aspect = self.width/float(self.height) def render(self, filename): out_file = file(filename, 'w') # PPM header print >>out_file, "P3" print >>out_file, self.width, self.height print >>out_file, "256" total = self.width * self.height count = 0 for sy in xrange(self.height): pixel_line = [] for sx in xrange(self.width): x = 2 * (0.5-sx/float(self.width)) * self.aspect y = 2 * (0.5-sy/float(self.height)) if self.cameratype=="ortho": ray = line(vec(x, y, 0.0),vec(x, y, self.backplane)) else: ray = line(vec(0.0, 0.0, 0.0),vec(x, y, self.imageplane)) ray.end=ray.end*self.backplane col = vec(0.0, 0.0, 0.0) depth = self.backplane shaderinfo = Shaderinfo() #{"ray":ray,"lights":self.lights,"objects":self.objects,"depth":2} shaderinfo.ray = ray shaderinfo.lights = self.lights shaderinfo.objects = self.objects shaderinfo.depth = 2 for obj in self.objects: intersects,(position,normal) = obj.intersect(ray) if intersects is not "none": if position.z0: depth = position.z shaderinfo.thisobj = obj shaderinfo.position = position shaderinfo.normal = normal col = obj.shader.shade(shaderinfo) pixel_line.append( conv_value(col.x) ) pixel_line.append( conv_value(col.y) ) pixel_line.append( conv_value(col.z) ) count = count + 1 print >>out_file, " ".join(pixel_line) percentstr = str(int((count/float(total))*100))+"%" print "" + percentstr out_file.close() class everythingshader(shader): def shade(self,shaderinfo): col = shader.shade(self,shaderinfo) ref = self.getreflected(shaderinfo) col = col*0.5+ref*0.5 return col*self.doocclusion(10,shaderinfo) class spotshader(shader): def shade(self,shaderinfo): col = shader.shade(self, shaderinfo) position = shaderinfo.position jitter = sin(position.x) + cos(position.z) if jitter > 0.5: col = col / 2 ref = self.getreflected(shaderinfo) return ref*0.5 + col*0.5*self.doocclusion(10,shaderinfo) # Main # Give sixe x and y of the image if len(sys.argv) == 3: nx, ny = int(sys.argv[1]), int(sys.argv[2]) else: nx, ny = 160, 120 w = world(nx, ny) numballs = 10.0 offset = vec(0.0,-5.0,55.0) rad = 12.0 radperball = (2 * 3.141592) / numballs for i in xrange(int(numballs)): x = sin(0.3+radperball*float(i))*rad y = cos(0.3+radperball*float(i))*rad w.objects.append(sphere(vec(x,0.0,y)+offset,2.0,everythingshader())) w.objects.append(sphere(vec(3.0,3.0,0.0)+offset,5.0,everythingshader())) w.objects.append(plane(vec(0.0,1.0,0.0),7.0, spotshader())) w.lights.append(parallellight(vec(1.0,1.0,-1.0), vec(0.3,0.9,0.1))) w.lights.append(pointlight(vec(5.0,100.0,-5.0), vec(0.5,0.5,1.0))) w.render('test.ppm') shedskin-0.9.4/tests/134.py0000664000175000017500000000035112157270661015303 0ustar srepmubsrepmub def pascal(n): """pascal(n): print n first lines of Pascal's triangle (shortest version).""" r = [[1]] for i in xrange(1, n): r += [[1] + [sum(r[-1][j:j+2]) for j in range(i)]] return r print pascal(9) shedskin-0.9.4/tests/152.py0000664000175000017500000000253012157270661015304 0ustar srepmubsrepmub x = '0,0' b = str(x) print b a = [[1]] c = [None, [2]] print a == c d = [3]; d = None e = [4]; e = None print d == e, None == d, e == None, a == None, c[0] == None, c[1] == None class board(object): def mergemask(self): print 'mergemask' def solve(self, board): global bekos bekos += 1 #[board.mergemask() for x in range(1)] # XXX list(none) .. board.mergemask() board.mergemask() bekos = 0 bo = board() bo.solve(bo) class heuk: aha = 4 def bla(self): heuk.aha += 1 self.ahah = 2 print self.ahah, heuk.aha h = heuk() h.lala = 1 h.bla() heuk.aha heuk.aha += 1 print heuk.aha heuk.noinit = 3 print heuk.noinit, h.ahah class myiter: def __init__(self, container): self.container = container self.count = -1 def next(self): self.count +=1 if self.count < len(self.container): return self.container[self.count] raise StopIteration class container: def __init__(self): self.unit = range(3) def __getitem__(self, i): return self.unit[i] def __iter__(self): return myiter(self) def __len__(self): return len(self.unit) def iter_(x): return x.__iter__() i = iter_(container()) try: while 1: y = i.next() print y except StopIteration: pass shedskin-0.9.4/tests/16.py0000664000175000017500000000126012157270661015222 0ustar srepmubsrepmub class integer: def __gt__(self, b): # self: [integer], b: [integer] return True def maxi(a, b): # a: [integer]r, b: [integer]r if a > b: # [bool] return a # [integer] return b # [integer] def qbert(): # a: [integer], c: [integer]r, b: [integer] a = integer() # [integer] b = integer() # [integer] c = maxi(a, b) # [integer] return c # [integer] qbert() # [integer] shedskin-0.9.4/tests/163.py0000664000175000017500000000153012157270661015305 0ustar srepmubsrepmub import time time.sleep(1.01) import sys #print 'Python version:', sys.version sys.stdout.flush() a = '\001\00\0boink' print repr('hello, world') print repr('hello\0, world2') print 'hello, world' print repr('hello\0, world2') # XXX no repr! print repr(a), len(a) print repr(chr(0)), len(chr(0)+chr(0)) print repr('\0') print repr(''.join([chr(i) for i in range(256)])) class behh: def __init__(self, a, b, c): pass behh(1,2,c=3) # sudoku solver!! see: http://markbyers.com/moinmoin/moin.cgi/ShortestSudokuSolver def r(a): i=a.find('0') if not ~i: print a; sys.exit() [m in [a[j] for j in range(81) if not (i-j)%9*(i/9^j/9)*(i/27^j/27|i%9/3^j%9/3)] or r(a[:i]+m+a[i+1:]) for m in '3814697265625'] return True # because the type of an 'or' clause is the superset of its terms, we cannot (implicitly) return None here r(81*'0') shedskin-0.9.4/tests/94.py0000664000175000017500000000067212157270661015236 0ustar srepmubsrepmub t = (1, 2.0) # [tuple2(int, float)] a = t[0] # [int] b = t[1] # [float] l = [(1, 2.0)] # [list(tuple2(int, float))] aha = l[0] # [tuple2(int, float)] c = aha[0] # [int] d = aha[1] # [float] for e,f in l: pass shedskin-0.9.4/tests/20.py0000664000175000017500000000005112157270661015212 0ustar srepmubsrepmub cube = [] cube.append(1.0) y = cube[0] shedskin-0.9.4/tests/158.py0000664000175000017500000000204612157270661015314 0ustar srepmubsrepmub def appl(predicate, x): return predicate(x) print [0,1][appl(lambda n: n>5, 10)], [0,1][appl(lambda n: n>10, 8)] def split(seq, predicate): pair = [], [] for el in seq: pair[not predicate(el)].append(el) return pair print split(range(-5,6), lambda n: n%2==0) #class Obj: # def __init__(self, n): self.n = n # def __gt__(self, other): return self.n > other.n # def __str__(self): return str(self.n) #def mymax(seq): # maxval = seq[0] # for el in seq: # if el > maxval: # gives error # maxval = el # return maxval #l = [Obj(i) for i in xrange(100)] #print mymax(l), mymax(range(100)) class Num: def __init__(self, n): self.n = float(n) def __str__(self): return str(self.n) def __add__(self, other): return Num(self.n + other.n) print sum([Num(i) for i in range(5)], Num(0)) print sum(range(5)) for a in 1,2: print a for a in [1,2]: print a for a in 1,2,3: print a print 'aaaa'.replace('a','b', 2) print 'aaaa'.replace('a','b', -1) print 'aaaa'.split('a', 2) print 'aaaa'.split('a', -1) shedskin-0.9.4/tests/123.py0000664000175000017500000002234512157270661015310 0ustar srepmubsrepmub # (c) Jack Ha # --- jack.ha@gmail.com def validMove(puzzle, x, y, number): # puzzle: [list(list(int))], x: [int], y: [int], number: [int] #see if the number is in any row, column or his own 3x3 square blnOK = True # [int] px = x / 3 # [int] py = y / 3 # [int] if puzzle[x][y] != 0: # [int] blnOK = False # [int] if blnOK: # [] for i in range(9): # [list(int)] if puzzle[i][y] == number: # [int] blnOK = False # [int] if blnOK: # [] for j in range(9): # [list(int)] if puzzle[x][j] == number: # [int] blnOK = False # [int] if blnOK: # [] for i in range(3): # [list(int)] for j in range(3): # [list(int)] if puzzle[px*3+i][py*3+j] == number: # [int] blnOK = False # [int] return blnOK # [int] def findallMoves(puzzle,x,y): # puzzle: [list(list(int))], x: [int], y: [int] returnList = [] # [list(int)] for n in range(1,10): # [list(int)] if validMove(puzzle, x, y, n): # [int] returnList.append(n) # [] return returnList # [list(int)] def solvePuzzleStep(puzzle): # puzzle: [list(list(int))] isChanged = False # [int] for y in range(9): # [list(int)] for x in range(9): # [list(int)] if puzzle[x][y] == 0: # [int] allMoves = findallMoves(puzzle, x, y) # [list(int)] if len(allMoves) == 1: # [int] puzzle[x][y] = allMoves[0] # [int] isChanged = True # [int] return isChanged # [int] #try to solve as much as possible without lookahead def solvePuzzleSimple(puzzle): # puzzle: [list(list(int))] iterationCount = 0 # [int] while solvePuzzleStep(puzzle) == True: # [int] iterationCount += 1 # [int] hashtable = {} # [dict(int, int)] def calc_hash(puzzle): # puzzle: [list(list(int))] hashcode = 0 # [int] for c in range(9): # [list(int)] hashcode = hashcode * 17 + hash(tuple(puzzle[c])) # [int] return hashcode # [int] def hash_add(puzzle): # puzzle: [list(list(int))] hashtable[calc_hash(puzzle)] = 1 # [int] def hash_lookup(puzzle): # puzzle: [list(list(int))] return hashtable.has_key(calc_hash(puzzle)) # [int] #solve with lookahead #unit is 3x3, (i,j) is coords of unit. l is the list of all todo's def perm(puzzle, i, j, l, u): # puzzle: [list(list(int))], i: [int], j: [int], l: [list(int)], u: [list(tuple(int))] global iterations iterations += 1 # [int] if (u == []) and (l == []): # [int] print "Solved!" # [str] #printpuzzle(puzzle) # [] print "iterations: ", iterations # [str], [int] return True # [int] else: if l == []: # [int] #here we have all permutations for one unit #some simple moves puzzlebackup = [] # [list(tuple(int))] for c in range(9): # [list(int)] puzzlebackup.append(tuple(puzzle[c])) # [] solvePuzzleSimple(puzzle) # [] #next unit to fill for c in range(len(u)): # [list(int)] if not hash_lookup(puzzle): # [int] inew, jnew = u.pop(c) # [tuple(int)] l = genMoveList(puzzle, inew, jnew) # [list(int)] #only print new situations #print "inew, jnew, l, u:", inew, jnew, l, u # [str], [int], [int], [list(int)], [list(tuple(int))] #printpuzzle(puzzle) # [] #print "iterations: ", iterations # [str], [int] if perm (puzzle, inew, jnew, l, u): # [int] return True # [int] else: hash_add(puzzle) # [] u.insert(c, (inew, jnew)) # [] #undo simple moves for y in range(9): # [list(int)] for x in range(9): # [list(int)] puzzle[x][y] = puzzlebackup[x][y] # [int] hash_add(puzzle) # [] return False # [int] else: #try all possibilities of one unit ii = i * 3 # [int] jj = j * 3 # [int] for m in range(len(l)): # [list(int)] #find first empty for y in range(3): # [list(int)] for x in range(3): # [list(int)] if validMove(puzzle, x+ii, y+jj, l[m]): # [int] puzzle[x+ii][y+jj] = l[m] # [int] backup = l.pop(m) # [int] if (perm(puzzle, i, j, l, u)): # [int] return True # [int] else: hash_add(puzzle) # [] l.insert(m, backup) # [] puzzle[x+ii][y+jj] = 0 # [int] return False # [int] #gen move list for unit (i,j) def genMoveList(puzzle, i, j): # puzzle: [list(list(int))], i: [int], j: [int] l = range(1,10) # [list(int)] for y in range(3): # [list(int)] for x in range(3): # [list(int)] p = puzzle[i*3+x][j*3+y] # [int] if p != 0: # [int] l.remove(p) # [] return l # [list(int)] def printpuzzle(puzzle): # puzzle: [list(list(int))] for x in range(9): # [list(int)] s = ' ' # [str] for y in range(9): # [list(int)] p = puzzle[x][y] # [int] if p == 0: # [int] s += '.' # [str] else: s += str(puzzle[x][y]) # [str] s += ' ' # [str] print s # [str] def main(): puzzle = [[0, 9, 3, 0, 8, 0, 4, 0, 0], # [list(list(int))] [0, 4, 0, 0, 3, 0, 0, 0, 0], # [list(int)] [6, 0, 0, 0, 0, 9, 2, 0, 5], # [list(int)] [3, 0, 0, 0, 0, 0, 0, 9, 0], # [list(int)] [0, 2, 7, 0, 0, 0, 5, 1, 0], # [list(int)] [0, 8, 0, 0, 0, 0, 0, 0, 4], # [list(int)] [7, 0, 1, 6, 0, 0, 0, 0, 2], # [list(int)] [0, 0, 0, 0, 7, 0, 0, 6, 0], # [list(int)] [0, 0, 4, 0, 1, 0, 8, 5, 0]] # [list(int)] #create todo unit(each 3x3) list (this is also the order that they will be tried!) u = [] # [list(tuple(int))] lcount = [] # [list(int)] for y in range(3): # [list(int)] for x in range(3): # [list(int)] u.append((x,y)) # [] lcount.append(len(genMoveList(puzzle, x, y))) # [] #sort for j in range(0,9): # [list(int)] for i in range(j,9): # [list(int)] if i != j: # [int] if lcount[i] < lcount[j]: # [int] u[i], u[j] = u[j], u[i] lcount[i], lcount[j] = lcount[j], lcount[i] l = genMoveList(puzzle, 0, 0) # [list(int)] perm (puzzle, 0, 0, l, u) # [int] iterations = 0 # [int] for x in range(30): main() # [] shedskin-0.9.4/tests/112.py0000664000175000017500000000074312157270661015304 0ustar srepmubsrepmub def row_perm_rec(numbers): # numbers: [list(int)] hoppa_row = [] # [] hoppa_row = ['.'] # [list(str)] def solve_row(numbers, old_row): # numbers: [list(int)] old_row.append('u') row_perm_rec(8) # [] puzzlerows = [[2]] # [list(list(int))] puzzleboard = [['']] # [list(list(str))] solve_row(17, puzzleboard[0]) # [] shedskin-0.9.4/tests/69.py0000664000175000017500000000041712157270661015235 0ustar srepmubsrepmub def ident(x): # x: [list(pyobj)]r return x # [list(pyobj)] ah = [] # [list(pyobj)] #ident(ah).append(1) # [] ident(ah).append(1.0) # [] shedskin-0.9.4/tests/141.py0000664000175000017500000000114712157270661015305 0ustar srepmubsrepmub class renderobject: def __init__(self, shader): self.shader=shader class plane(renderobject): def __init__(self,plane,dist,shader): renderobject.__init__(self,shader) self.plane=plane self.dist=dist class sphere(renderobject): def __init__(self, pos, radius, shader): renderobject.__init__(self,shader) self.pos=pos self.radius=radius class world: def __init__(self): self.objects = [] w = world() w.objects.append(plane(6,7,8)) w.objects.append(sphere(6,7,8)) shedskin-0.9.4/tests/60.py0000664000175000017500000000132412157270661015222 0ustar srepmubsrepmub #class evert: # __class__: [class evert]* # pass # #bla = evert() # [evert()] # #cl = bla.__class__ # [class evert] # #print cl # [class evert] #print cl.__name__ # [str] # #print type(bla) # [class evert] #print type(bla).__name__ # [str] # #if cl == type(evert()): # [int] # print 'equal!' # [str] # #if type('2') == type('3'): # [int] # print 'equal str!' # [str] # #if type(1) == type('1'): # [int] # print 'equal non-equal!' # [str] shedskin-0.9.4/tests/176.py0000664000175000017500000000213212157270661015310 0ustar srepmubsrepmub # --- string formatting problem print '%i%%-%i%%' % (1,2) numbers = (1,2) print '%i%%-%i%%' % numbers print '%i%%-%s%%' % (12, '21') t2 = (12, '21') print '%i%%-%s%%' % t2 # --- aug assign problem (or: the value of testing) a = [1,2,3,4,5] c = a b = [6,7,8,9,10] a += b print a, c ah = '12345' ch = ah bh = '67890' ah += bh print ah, ch # --- __iadd__ etc. class C: def __init__(self, value): self.value = value def __iadd__(self, other): self.value += other.value return self def __floordiv__(self, b): return C(self.value // b.value) def __ifloordiv__(self, b): self.value //= b.value return self def __str__(self): return str(self.value) x = C(4) x += x x.__iadd__(x) print x print [1,2].__iadd__([2,3]) y = [1,2,3] y += set([4,5]) print y v = 3 v += 1.5 print v hm = [] hm += set([1]) print hm d = C(8) print d // C(3) d //= C(3) print d # --- inheritance problem class Maze(object): def __init__(self): self.maze = [[0]] self.maze[0][0] |= 1 class ASCIIMaze(Maze): pass maze = ASCIIMaze() shedskin-0.9.4/tests/13.py0000664000175000017500000000052212157270661015217 0ustar srepmubsrepmub class fred: # thingy: [int] def hottum(self, x): # [fred], [str] b = 4 # [int] return b # [int] def hottum(): pass h = fred() # [fred] c = h.hottum('jo') # [int] shedskin-0.9.4/tests/190.py0000664000175000017500000001610312157270661015307 0ustar srepmubsrepmub import itertools print '### Count ###' #for a in itertools.count(4, 3): # if a > 16: # break # print a for ca in itertools.count(3): if ca > 16: break print ca #for cb in itertools.count(4, -3): # if cb < -16: # break # print cb print '### Cycle ###' ctt = 0 for b in itertools.cycle([1, 2, 3]): ctt += 1 if ctt > 10: break print b print '-' woo = itertools.cycle(set([1, 2, 3])) print woo.next() print woo.next() print woo.next() print '### Repeat ###' ctt2 = 0 for c in itertools.repeat(42): ctt2 += 1 if ctt2 > 5: break print c for c in itertools.repeat(42, 3): print c print '### Chain ###' for al in itertools.chain([1, 2]): print al print '-' for am in itertools.chain([1, 2], [3, 4]): print am print '-' for an in itertools.chain([1, 2], [3, 4], [5, 6]): print an print '-' for ao in itertools.chain([1, 2], [3, 4], [5, 6], [7, 8]): print ao print '-' for ap in itertools.chain([1, 2], [3, 4], [5, 6], [7, 8], [9, 10]): print ap print '-' for aq in itertools.chain([1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12, 13]): print aq print '### Compress ###' #for d in itertools.compress([42, 32, 21, 55, 303], [True, False, True, False, True]): # print c print '### Dropwhile ###' pred = lambda x: x < 5 for e in itertools.dropwhile(pred, [1, 4, 6, 4, 1]): print e print '### Groupby ###' def key(x): if x > 5: return 1 else: return 0 for k, g in itertools.groupby([1, 4, 6, 4, 1], key): for f in g: print f, print ':', k print '### Ifilter ###' pred1 = lambda x: x % 2 for h in itertools.ifilter(pred1, range(10)): print h for i in itertools.ifilter(None, range(10)): print i print '### Ifilterfalse ###' pred1 = lambda x: x % 2 for j in itertools.ifilterfalse(pred1, range(10)): print j for j in itertools.ifilterfalse(None, range(10)): print j print '### Islice ###' print '--1' for l in itertools.islice('ABCDEFG', 2): print l print '--2' for m in itertools.islice('ABCDEFG', 2, None): print m print '--3' for n in itertools.islice('ABCDEFG', 2, 4, 1): print n print '--4' for o in itertools.islice('ABCDEFG', 2, 4, 2): print o print '--5' for p in itertools.islice('ABCDEFG', 2, 4): print p print '--6' for r in itertools.islice('ABCDEFG', 0, 4, 2): print r print '--7' for s in itertools.islice('ABCDEFG', None, 4, 2): print s print '--8' for t in itertools.islice('ABCDEFG', None, 4): print t print '--9' for u in itertools.islice('ABCDEFG', 2, None, 2): print u print '--10' for v in itertools.islice('ABCDEFG', None): print v print '--11' for w in itertools.islice('ABCDEFG', None, None, 2): print w print '--12' for y in itertools.islice('ABCDEFG', None, None): print y print '--13' for z in itertools.islice('ABCDEFG', 2, None, None): print z print '--14' for aa in itertools.islice('ABCDEFG', None, None, None): print aa print '--15' for ab in itertools.islice('ABCDEFG', 2, 0): print ab print '--16' for ac in itertools.islice('ABCDEFG', 2, 0, 2): print ac print '--17' for ad in itertools.islice('ABCDEFG', 0, 0, 2): print ad print '### Imap ###' def foo(a): return '{%i}' % a def foo2(a, b): return '{%i//%f}' % (a, b) def foo3(a, b, c): return '{%i//%f//%s}' % (a, b, c) def foo4(a, b, c, d): return '{%i//%f//%s//%i}' % (a, b, c, d) def foo5(a, b, c, d, e): return '{%i//%f//%s//%i//%s}' % (a, b, c, d, str(e)) for iia in itertools.imap(foo, (21, 12, 42)): print iia print '-' for iib in itertools.imap(foo2, (21, 12, 42), (.21, .12)): print iib print '-' for iic in itertools.imap(foo3, (21, 12, 42), (.21, .12), ('a', 'b', 'c')): print iic print '-' for iid in itertools.imap(foo4, (21, 12, 42), (.21, .12), ('a', 'b', 'c'), (42, 12, 14, 6)): print iid print '-' for iie in itertools.imap(foo5, (21, 12, 42), (.21, .12), ('a', 'b', 'c'), (42, 12, 14, 6), ([5, 4], [8, 9])): print iie print '### Starmap ###' # TODO print '### Tee ###' def gen(): for ae in [1, 2, 3, 4, 5]: yield ae it1, it2 = itertools.tee(gen()) for af in it1: print af for ag in it2: print ag it3, it4, it5 = itertools.tee(gen(), 3) for ah in it3: print ah for ai in it4: print ai for aj in it5: print aj print '### Takewhile ###' pred2 = lambda x: x < 5 for ak in itertools.takewhile(pred2, [1,4,6,4,1]): print ak print '### Izip ###' for ar in itertools.izip(): print ar print '-' for at in itertools.izip([1, 3, 4]): print at print '-' for au in itertools.izip([1, 3, 4], [42, 21], [12, 21, 33, 55]): print au print '-' for au2 in itertools.izip([1, 3, 4], ['a', 'b']): print au2 print '### Izip_longest ###' for av1 in itertools.izip_longest(): print av1 print '-' for av in itertools.izip_longest(fillvalue = 42): print av print '-' for aw1 in itertools.izip_longest(['a', 'b', 'c']): print aw1 print '-' for aw2 in itertools.izip_longest([1, 3, 4], fillvalue = 42): print aw2 print '-' for ax in itertools.izip_longest([1, 3, 4], [42, 21], [12, 21, 33, 55], fillvalue = 42): print ax print '-' for aw3 in itertools.izip_longest([[1, 2], [3, 4], [5, 6]], ['a', 'b']): print aw3 print '### Product ###' for ay in itertools.product(): print ay print '-' for az in itertools.product('A'): print az print '-' for ba in itertools.product('AB'): print ba print '-' for bb in itertools.product('A', ''): print bb print '-' for bc in itertools.product('A', 'B'): print bc print '-' for bd in itertools.product('AB', repeat = 2): print bd print '-' for be in itertools.product('A', 'B', repeat = 2): print be print '-' for bf in itertools.product('AB', 'CD'): print bf print '-' for bg in itertools.product('AB', 'CD', repeat = 2): print bg print '-' for bhy in itertools.product([.4, .42], [1, 2, 3]): print '%.2f %d' % bhy print '-' for bhz in itertools.product('AB', [1, 2, 3]): print bhz print '### Permutations ###' for bh in itertools.permutations('ABDC'): print bh print '-' for bi in itertools.permutations('ABDC', 0): print bi print '-' for bj in itertools.permutations('ABDC', 1): print bj print '-' for bk in itertools.permutations('ABDC', 2): print bk print '-' for bl in itertools.permutations('ABDC', 3): print bl print '-' for bm in itertools.permutations('ABDC', 5): print bm print '### Combinations ###' for bn in itertools.combinations('ABDC', 0): print bn print '-' for bo in itertools.combinations('ABDC', 1): print bo print '-' for bp in itertools.combinations('ABDC', 2): print bp print '-' for bq in itertools.combinations('ABDC', 3): print bq print '-' for br in itertools.combinations('ABDC', 5): print br print '### Combinations_with_replacement ###' #for bs in itertools.combinations_with_replacement('ABDC', 0): # print bs #print '-' #for bt in itertools.combinations_with_replacement('ABDC', 1): # print bt #print '-' #for bu in itertools.combinations_with_replacement('ABDC', 2): # print bu #print '-' #for bv in itertools.combinations_with_replacement('ABDC', 3): # print bv #print '-' #for bw in itertools.combinations_with_replacement('ABDC', 5): # print bw shedskin-0.9.4/tests/21.py0000664000175000017500000000014712157270661015221 0ustar srepmubsrepmub def gettuple(): return (5,6) a = gettuple() cube = [(1,2),(3,4),a,gettuple()] #cube.append(1.0) shedskin-0.9.4/tests/22.py0000664000175000017500000000010712157270661015216 0ustar srepmubsrepmub for a in [(1,2),(3,4)]: # [list_tuple_int] pass shedskin-0.9.4/tests/135.py0000664000175000017500000000263012157270661015306 0ustar srepmubsrepmub # Textual Voronoi code modified from: # http://www.ocf.berkeley.edu/~Eabhishek/ import random # for generateRandomPoints random.seed(42) from math import sqrt def generateRandomPoints(npoints=6): """Generate a few random points v1...vn""" print npoints, "points x,y:" points = [] for i in xrange(npoints): xrand, yrand = random.random(), random.random() print '%.2f' % xrand, '%.2f' % yrand for xoff in range(-1, 2): for yoff in range(-1, 2): points.append( (xrand + xoff, yrand + yoff) ) return points def closest(x,y,points): """Function to find the closest of the vi.""" best,good = 99.0*99.0, 99.0*99.0 for px, py in points: dist = (x-px)*(x-px) + (y-py)*(y-py) if dist < best: best, good = dist, best elif dist < good: good = dist return sqrt(best) / sqrt(good) def generateScreen(points, rows=40, cols=80): yfact = 1.0 / cols xfact = 1.0 / rows screen = [] chars = " -.,+*$&#~~" for i in xrange(rows): x = i*xfact line = [ chars[int(10*closest(x, j*yfact, points))] for j in xrange(cols) ] screen.extend( line ) screen.append("\n") return "".join(screen) from time import clock points = generateRandomPoints(10) print t1 = clock() print generateScreen(points, 40, 80) t2 = clock() round(t2-t1, 3) shedskin-0.9.4/tests/196.py0000664000175000017500000002577612157270661015335 0ustar srepmubsrepmub# template not removed after iteration class BadError(Exception): pass if __name__=='__main__': BadError() BadError("AOE") # crash in assign_needs_cast XXX try self.method() instead of self.msg! class MyBaseException: def __init__(self, msg=None): self.msg = msg class MyException(MyBaseException): pass class MyStandardError(MyException): pass class MyBadError(MyException): pass if __name__=='__main__': MyStandardError() MyBadError() # default hash method class waf(object): pass w = waf() print hash(w) - hash(w) # struct import struct from struct import unpack b1, b2, h1, h2, h3, i1, i2, s1 = unpack("0i0p3p2x' ,'g\xb0\x0c\x02\x89\xd3\xaf' ,'\xfdK\xb4\xb1\xf6l' ) print repr(packer) print struct.calcsize( '>0i0p3p2x' ) packer = struct.pack( '>b0p2s' ,110 ,'\x1d' ,'' ) print repr(packer) print struct.calcsize( '>b0p2s' ) packer = struct.pack( '@2f0p3p1L' ,531.0 ,469.0 ,'1k\x1a\x8c\xfd\xc0u\xdf' ,'' ,60 ) print repr(packer) print struct.calcsize( '@2f0p3p1L' ) try: s = struct.pack('h', 18.18) print repr(s) dwah, = struct.unpack('h', s) print dwah s = struct.pack('h', '19') print repr(s) dwah, = struct.unpack('h', s) print dwah s = struct.pack('h', True) print repr(s) dwah, = struct.unpack('h', s) print dwah except Exception, messg: print 'baaah' try: s = struct.pack('fd', 17, 18) print repr(s) dwad, ewad = struct.unpack('fd', s) print dwad, ewad s = struct.pack('fd', 'oi', 18) except Exception: print 'oopsie' try: raise struct.error("butkus") except struct.error, e: print e booll, = struct.unpack('?', '\x02') print booll print repr(struct.pack('p', 300*'x')) # array import array arr = array.array('i', [3,2,1]) print arr print arr.tolist(), repr(arr.tostring()) print arr[0], arr[1], arr[2] arr.fromlist([4,5]) print sorted(arr) arr2 = array.array('c') arr2.extend('hoei') print arr2.count('h'), arr2.index('h') arr2[-1] = 'X' arr2.insert(0, '-') arr2.fromlist(['a', 'b']) print arr2, arr2.tolist(), arr2.tostring() print arr2[0] fla = array.array('f', [3.141]) fla = array.array('d', (142344, 2384234)) fla.fromlist([1234,]) fla[0] = 28000 print fla.typecode, fla.itemsize print repr(fla.tostring()), ['%.2f' % flah for flah in fla.tolist()] print '%.2f' % fla[1] print repr(fla) arr3 = array.array('i') arr3.fromstring(arr.tostring()) print arr, arr3 areq = (arr==arr3) print areq arradd = arr+arr print arradd beh = arr arr += arr print arr print beh mul1 = arr * 4 mul2 = 3 * arr print mul1, mul2 wah = mul1 mul1 *= 1 print mul1 print wah boolt = wah.__contains__(5), 6 in wah print boolt print len(wah), wah.count(5), wah.index(5) print wah.pop(4) print wah.pop() print wah.pop(-2) print wah wah.remove(5) print wah print wah[-2] wah.reverse() print wah wah.byteswap() print wah wah[3] = 99 print wah wah.insert(7, 98) print wah arr4 = array.array('i', [3,2,1]) print arr4 f = open('testdata/blabla', 'w') arr4.tofile(f) f.close() arr5 = array.array('i') f = open('testdata/blabla') arr5.fromfile(f, 2) try: arr5.fromfile(f, 2) except EOFError, e: print e f.close() print arr5 import copy arr = array.array('i', [3,2,1]) c1 = copy.copy(arr) c1.append(4) c2 = copy.deepcopy(arr) c2.append(5) print c1, c2, arr arra = array.array('i', [1,2]) arrb = array.array('i', [1,2,3]) print arra == arrb, arra > arrb, arra < arrb, cmp(arra, arrb) # XXX compare with non-arrays del arrb[1] del arrb[-1] print arrb allr = array.array('H', range(10)) print allr print allr[2:8:2] allr[1:3] = array.array('H', range(5)) print allr del allr[1:7:2] print allr aahaa = array.array('i', range(5)) aahaa.extend(aahaa) print aahaa arghx = array.array('i', range(5)) arghy = array.array('h', range(5)) print arghx == arghy arghy.append(7) print arghx == arghy # binascii import binascii str = 'my guitar wants to strum all night long' b2a = binascii.b2a_qp(str) print repr(b2a) a2b = binascii.a2b_qp(b2a) print repr(a2b) b2a = binascii.b2a_uu(str) print repr(b2a) a2b = binascii.a2b_uu(b2a) print repr(a2b) b2a = binascii.b2a_hex(str) print repr(b2a) a2b = binascii.a2b_hex(b2a) print repr(a2b) b2a = binascii.b2a_hqx(str) print repr(b2a) a2b,done = binascii.a2b_hqx(b2a) # returns a string instead of a tuple print repr(a2b),done b2a = binascii.b2a_base64(str) print repr(b2a) a2b = binascii.a2b_base64(b2a) print repr(a2b) b2a = binascii.rlecode_hqx(str) print repr(b2a) a2b = binascii.rledecode_hqx(b2a) print repr(a2b) b2a = binascii.hexlify(str) print repr(b2a) a2b = binascii.unhexlify(b2a) print repr(a2b) # delslice bug all = range(10) print all[2:8:2] all[1:3] = range(5) print all del all[1:7:2] print all # OMG omg, = (17,) print omg # sys vars import sys print sys.platform print sys.byteorder copyright = sys.copyright assert (sys.version_info[0], sys.version_info[1]) >= (2, 4) # str.title print '8RMgvsFN51QrM0sJeXU11yXodq1drv'.title() # forward referencing base class doesn't work import testdata.timer class smurf (testdata.timer.TimingOut): def __init__(self): testdata.timer.TimingOut.fire_timer(self) smurfje = smurf() testdata.timer.timeout_add(42, smurfje) # file.xreadlines (deprecated but still) lines = open('testdata/crap.py','r').xreadlines() for line in lines: print line.strip() # bin() limited to 12 digits print bin(123456789) # exception printing valeur = ValueError('valeur') print valeur print repr(valeur) print valeur.message print valeur.__class__.__name__ # sys.exit case import sys sys.exit() shedskin-0.9.4/tests/138.py0000664000175000017500000000115712157270661015314 0ustar srepmubsrepmub x = [0,1] i, x[i] = 1, 2 print x from math import * sin(pi) print repr(' '.strip()) print 'a\vb'.split() s1={1:"a",2:"b"}; s2={1:"a",2:"b"}; print s1 == s2, s1 != s2 print "ab cd\tef\ngh\ril\vmn\fop".split() a=2; b=3; print bool(a==b) def test(): s1, s2 = "ab", "AB" alist = ["_"] * 2 for pos in range(2): alist[ord(s1[pos])-ord('a')] = s2[pos] return alist print test() def f(s): return [s[0] for j in xrange(1)] print [(i,j+k) for (k,j) in enumerate(xrange(3)) for i in xrange(j) for z in range(2)] print "a".join(["a","b"]) "".join(set(["he", "oh"])) "**".join({" oh":1, "he":0}) shedskin-0.9.4/tests/43.py0000664000175000017500000000144312157270661015225 0ustar srepmubsrepmub def hoi(): # dinges: [list(int)], bla: [list(int)] bla = [1,2] # [list(int)] dinges = [1,2] # [list(int)] jada = [1,2] # [list(int)] u = [x for x in bla] # [list(int)] v = [[a for a in bla] for c in dinges] # [list(list(int))] w = [[[a for a in jada] for c in bla] for d in dinges] # [list(list(list(int)))] print u # [list(int)] print v # [list(list(int))] print w # [list(list(list(int)))] return bla # [list(int)] return dinges # [list(int)] print hoi() # [list(int)] shedskin-0.9.4/tests/99.py0000664000175000017500000001415512157270661015244 0ustar srepmubsrepmub # (c) Mark Dufour, Haifang Ni # --- mark.dufour@gmail.com empty, black, white = 0, 1, -1 # [int], [int], [int] board = [[empty for x in range(8)] for y in range(8)] # [list(list(int))] board[3][3] = board[4][4] = white # [int] board[3][4] = board[4][3] = black # [int] player, depth = {white: 'human', black: 'lalaoth'}, 3 # [dict(int, str)], [int] def possible_move(board, x, y, color): # board: [list(list(int))], x: [int], y: [int], color: [int] if board[x][y] != empty: # [int] return False # [int] for direction in [(1, 1), (-1, 1), (0, 1), (1, -1), (-1, -1), (0, -1), (1, 0), (-1, 0)]: # [list(tuple2(int, int))] if flip_in_direction(board, x, y, direction, color): # [int] return True # [int] return False # [int] def flip_in_direction(board, x, y, direction, color): # board: [list(list(int))], x: [int], y: [int], direction: [tuple2(int, int)], color: [int] other_color = False # [int] while True: # [int] x, y = x+direction[0], y+direction[1] # [int], [int] if x not in range(8) or y not in range(8): # [int] return False # [int] square = board[x][y] # [int] if square == empty: return False # [int] if square != color: other_color = True # [int] else: return other_color # [int] def flip_stones(board, move, color): # board: [list(list(int))], move: [tuple2(int, int)], color: [int]* global flips flips += 1 # [int] for direction in [(1, 1), (-1, 1), (0, 1), (1, -1), (-1, -1), (0, -1), (1, 0), (-1, 0)]: # [list(tuple2(int, int))] if flip_in_direction(board, move[0], move[1], direction, color): # [int] x, y = move[0]+direction[0], move[1]+direction[1] # [int], [int] while board[x][y] != color: # [int] board[x][y] = color # [int] x, y = x+direction[0], y+direction[1] # [int], [int] board[move[0]][move[1]] = color # [int] #def print_board(board, turn): # board: [], turn: [] # for line in board: # [] # print ' '.join([{white: 'O', black: 'X', empty: '.'}[square] for square in line]) # [] # print 'turn:', player[turn] # [], [] # print 'black:', stone_count(board, black), 'white:', stone_count(board, white) # [], [], [], [] def possible_moves(board, color): # board: [list(list(int))], color: [int] return [(x,y) for x in range(8) for y in range(8) if possible_move(board, x, y, color)] # [list(tuple2(int, int))] #def coordinates(move): # move: [] # return (int(move[1])-1, 'abcdefgh'.index(move[0])) # [] def stone_count(board, color): # board: [list(list(int))], color: [int] return sum([len([square for square in line if square == color]) for line in board]) # [list(int)] #def human_move(move): # move: [] # return 'abcdefgh'[move[0]]+str(move[1]+1) # [] def best_move(board, color, first, step=1): # board: [list(list(int))], color: [int]*, first: [int], step: [int] max_move, max_mobility, max_score = None, 0, 0 # [none], [int], [int] #print 'possible', possible_moves(board, color) # [str], [list(tuple2(int, int))] for move in possible_moves(board, color): # [list(tuple2(int, int))] #print 'board before' # [str] #print_board(board, color) # [] #print 'move', move # [str], [tuple2(int, int)] if move in [(0,0),(0,7),(7,0),(7,7)]: # [list(tuple2(int, int))] mobility, score = 64, 64 # [int], [int] if color != first: # [int] mobility = 64-mobility # [int] else: testboard = [[square for square in line] for line in board] # [list(list(int))] flip_stones(testboard, move, color) # [] #print_board(testboard, color) # [] if step < depth: # [int] #print 'deeper' # [str] next_move, mobility = best_move(testboard, -color, first, step+1) # [tuple2(tuple2(int, int), int)] else: #print 'mobility' # [str] mobility = len(possible_moves(testboard, first)) # [int] score = mobility # [int] if color != first: # [int] score = 64-score # [int] if score >= max_score: # [] max_move, max_mobility, max_score = move, mobility, score # [tuple2(int, int)], [int], [int] #print 'done' # [str] return max_move, max_mobility # [tuple2(tuple2(int, int), int)] flips = 0 # [int] steps = 0 # [int] turn = black # [int] while possible_moves(board, black) or possible_moves(board, white): # [list(tuple2(int, int))] if possible_moves(board, turn): # [list(tuple2(int, int))] #print_board(board, turn) # [] #print 'flips', flips # [str], [int] # steps += 1 # [int] # if steps > 5: # [int] # break #if turn == black: # [int] move, mobility = best_move(board, turn, turn) # [tuple2(tuple2(int, int), int)] #else: # move = coordinates(raw_input()) # [tuple2(int, int)] if not possible_move(board, move[0], move[1], turn): # [int] print 'impossible!' # [str] turn = -turn # [int] else: flip_stones(board, move, turn) # [] turn = -turn # [int] #print_board(board, turn) print 'flips', flips # [str], [int] if stone_count(board, black) == stone_count(board, white): # [int] print 'draw!' # [str] else: if stone_count(board, black) > stone_count(board, white): print player[black], 'wins!' # [str], [str] else: print player[white], 'wins!' # [str], [str] shedskin-0.9.4/tests/160.py0000664000175000017500000002177512157270661015317 0ustar srepmubsrepmub # Based on original version written in BCPL by Dr Martin Richards # in 1981 at Cambridge University Computer Laboratory, England # and a C++ version derived from a Smalltalk version written by # L Peter Deutsch. # Translation from C++, Mario Wolczko # Outer loop added by Alex Jacoby # Task IDs I_IDLE = 1 I_WORK = 2 I_HANDLERA = 3 I_HANDLERB = 4 I_DEVA = 5 I_DEVB = 6 # Packet types K_DEV = 1000 K_WORK = 1001 # Packet BUFSIZE = 4 BUFSIZE_RANGE = range(BUFSIZE) class Packet(object): def __init__(self,l,i,k): self.link = l self.ident = i self.kind = k self.datum = 0 self.data = [0] * BUFSIZE def append_to(self,lst): self.link = None if lst is None: return self else: p = lst next = p.link while next is not None: p = next next = p.link p.link = self return lst # Task Records class TaskRec(object): pass class DeviceTaskRec(TaskRec): def __init__(self): self.pending = None class IdleTaskRec(TaskRec): def __init__(self): self.control = 1 self.count = 10000 class HandlerTaskRec(TaskRec): def __init__(self): self.work_in = None self.device_in = None def workInAdd(self,p): self.work_in = p.append_to(self.work_in) return self.work_in def deviceInAdd(self,p): self.device_in = p.append_to(self.device_in) return self.device_in class WorkerTaskRec(TaskRec): def __init__(self): self.destination = I_HANDLERA self.count = 0 # Task class TaskState(object): def __init__(self): self.packet_pending = True self.task_waiting = False self.task_holding = False def packetPending(self): self.packet_pending = True self.task_waiting = False self.task_holding = False return self def waiting(self): self.packet_pending = False self.task_waiting = True self.task_holding = False return self def running(self): self.packet_pending = False self.task_waiting = False self.task_holding = False return self def waitingWithPacket(self): self.packet_pending = True self.task_waiting = True self.task_holding = False return self def isPacketPending(self): return self.packet_pending def isTaskWaiting(self): return self.task_waiting def isTaskHolding(self): return self.task_holding def isTaskHoldingOrWaiting(self): return self.task_holding or (not self.packet_pending and self.task_waiting) def isWaitingWithPacket(self): return self.packet_pending and self.task_waiting and not self.task_holding tracing = False layout = 0 def trace(a): global layout layout -= 1 if layout <= 0: print layout = 50 print a print a, TASKTABSIZE = 10 class TaskWorkArea(object): def __init__(self): self.taskTab = [None] * TASKTABSIZE self.taskList = None self.holdCount = 0 self.qpktCount = 0 taskWorkArea = TaskWorkArea() class Task(TaskState): def __init__(self,i,p,w,initialState,r): self.link = taskWorkArea.taskList self.ident = i self.priority = p self.input = w self.packet_pending = initialState.isPacketPending() self.task_waiting = initialState.isTaskWaiting() self.task_holding = initialState.isTaskHolding() self.handle = r taskWorkArea.taskList = self taskWorkArea.taskTab[i] = self def fn(self,pkt,r): raise NotImplementedError def addPacket(self,p,old): if self.input is None: self.input = p self.packet_pending = True if self.priority > old.priority: return self else: p.append_to(self.input) return old def runTask(self): if self.isWaitingWithPacket(): msg = self.input self.input = msg.link if self.input is None: self.running() else: self.packetPending() else: msg = None self return self.fn(msg,self.handle) def waitTask(self): self.task_waiting = True return self def hold(self): taskWorkArea.holdCount += 1 self.task_holding = True return self.link def release(self,i): t = self.findtcb(i) t.task_holding = False if t.priority > self.priority: return t else: return self def qpkt(self,pkt): t = self.findtcb(pkt.ident) taskWorkArea.qpktCount += 1 pkt.link = None pkt.ident = self.ident return t.addPacket(pkt,self) def findtcb(self,id): t = taskWorkArea.taskTab[id] if t is None: raise Exception("Bad task id %d" % id) return t # DeviceTask class DeviceTask(Task): def __init__(self,i,p,w,s,r): Task.__init__(self,i,p,w,s,r) def fn(self,pkt,r): d = r assert isinstance(d, DeviceTaskRec) if pkt is None: pkt = d.pending if pkt is None: return self.waitTask() else: d.pending = None return self.qpkt(pkt) else: d.pending = pkt if tracing: trace(str(pkt.datum)) return self.hold() class HandlerTask(Task): def __init__(self,i,p,w,s,r): Task.__init__(self,i,p,w,s,r) def fn(self,pkt,r): h = r assert isinstance(h, HandlerTaskRec) if pkt is not None: if pkt.kind == K_WORK: h.workInAdd(pkt) else: h.deviceInAdd(pkt) work = h.work_in if work is None: return self.waitTask() count = work.datum if count >= BUFSIZE: h.work_in = work.link return self.qpkt(work) dev = h.device_in if dev is None: return self.waitTask() h.device_in = dev.link dev.datum = work.data[count] work.datum = count + 1 return self.qpkt(dev) # IdleTask class IdleTask(Task): def __init__(self,i,p,w,s,r): Task.__init__(self,i,0,None,s,r) def fn(self,pkt,r): i = r assert isinstance(i, IdleTaskRec) i.count -= 1 if i.count == 0: return self.hold() elif i.control & 1 == 0: i.control /= 2 return self.release(I_DEVA) else: i.control = i.control/2 ^ 0xd008 return self.release(I_DEVB) # WorkTask A = ord('A') class WorkTask(Task): def __init__(self,i,p,w,s,r): Task.__init__(self,i,p,w,s,r) def fn(self,pkt,r): w = r assert isinstance(w, WorkerTaskRec) if pkt is None: return self.waitTask() if w.destination == I_HANDLERA: dest = I_HANDLERB else: dest = I_HANDLERA w.destination = dest pkt.ident = dest pkt.datum = 0 for i in BUFSIZE_RANGE: # xrange(BUFSIZE) w.count += 1 if w.count > 26: w.count = 1 pkt.data[i] = A + w.count - 1 return self.qpkt(pkt) import time def schedule(): t = taskWorkArea.taskList while t is not None: pkt = None if tracing: print "tcb =",t.ident #print '*', t.__class__ if t.isTaskHoldingOrWaiting(): t = t.link else: if tracing: trace(chr(ord("0")+t.ident)) t = t.runTask() class Richards(object): def run(self, iterations): for i in xrange(iterations): taskWorkArea.holdCount = 0 taskWorkArea.qpktCount = 0 IdleTask(I_IDLE, 1, 10000, TaskState().running(), IdleTaskRec()) wkq = Packet(None, 0, K_WORK) wkq = Packet(wkq , 0, K_WORK) WorkTask(I_WORK, 1000, wkq, TaskState().waitingWithPacket(), WorkerTaskRec()) wkq = Packet(None, I_DEVA, K_DEV) wkq = Packet(wkq , I_DEVA, K_DEV) wkq = Packet(wkq , I_DEVA, K_DEV) HandlerTask(I_HANDLERA, 2000, wkq, TaskState().waitingWithPacket(), HandlerTaskRec()) wkq = Packet(None, I_DEVB, K_DEV) wkq = Packet(wkq , I_DEVB, K_DEV) wkq = Packet(wkq , I_DEVB, K_DEV) HandlerTask(I_HANDLERB, 3000, wkq, TaskState().waitingWithPacket(), HandlerTaskRec()) wkq = None; DeviceTask(I_DEVA, 4000, wkq, TaskState().waiting(), DeviceTaskRec()); DeviceTask(I_DEVB, 5000, wkq, TaskState().waiting(), DeviceTaskRec()); schedule() if taskWorkArea.holdCount == 9297 and taskWorkArea.qpktCount == 23246: pass else: return False return True r = Richards() iterations = 10 result = r.run(iterations) print result shedskin-0.9.4/tests/193.py0000664000175000017500000000166212157270661015316 0ustar srepmubsrepmub # __init__ not called from testdata import Material # ugly imports from testdata.bert import * print os.getcwd(), len(sys.argv) # isinstance problem from testdata.bert import zeug print isinstance(zeug(), zeug) # dict corruption x = 62 S = {} t2 = (-25,9) for i in range(-x, x+1): for j in range(-x, x+1): S[i, j] = 'hi' if t2 in S: print "we got 'em" # cast subtype in container class Bla: pass class Sub(Bla): pass blas = [Bla()] blas = [Sub()] # generator and FAST_FOR_NEG def gen(s): for i in range(1,10,s): yield i for i in gen(2): print i # argument unpacking def blah((a,(b,c)), d): print a, b, c, d t1 = (4, (3,2)) t2 = (7,8) blah(t1, 1) blah((1, (2,3)), 4) blah((6,t2), 9) class Oink: def __getitem__(self, (x,y)): print 'get', x, y return x*y def __setitem__(self, (x,y), z): print 'set', x, y, z oink = Oink() oink[4,5] = oink[2,3] oink[t2] = oink[t2] shedskin-0.9.4/tests/195.py0000664000175000017500000000167212157270661015321 0ustar srepmubsrepmub# !$!#$ try: [1].pop(-2) except IndexError,e: print e try: [].pop(0) except IndexError,e: print e try: [].remove(0) except ValueError,e: print e l = [] l.insert(4, 1) l.insert(-1, 2) l.insert(-10, 3) print l # basic __call__ overloading class meuk: def __call__(self, x, y): print 'called with:', x, y return 'return' m = meuk() x = m(7,1) print x class mycall: def __call__(self, x): return -x print max(range(17,80), key=mycall()) # basic __iter__ overloading class itermeuk: def __iter__(self): return iter('itermeuk') i = itermeuk() for x in i: print x # __contains__ fallback to __iter__ import string print [x for x in string.ascii_lowercase if x in i] # dict.__init__ takes iterable iterable da = iter([1,2]) db = iter([3,4]) dd = dict([da,db]) print sorted(dd) # writelines takes iterable a = file('testdata/blah','w') a.writelines(set(['hoi\n', 'mama\n'])) a.close() shedskin-0.9.4/tests/164.py0000664000175000017500000000325312157270661015312 0ustar srepmubsrepmub # basic string module support import string print string.join(['hello', 'world!']), string.join(['hello', 'world!'], '_') # add random.shuffle import random l = [1,2,3,4,5] random.shuffle(l) print set(l) # add __or__ to builtin.int.. class c: # grr def a(self): return 1|1 def b(self): return 1&1 def c(self): return 1^1 def d(self): return ~1 a_c = c() print a_c.a(), a_c.b(), a_c.c(), a_c.d() # fake child nodes conflicting for binary tuples (e.g. one for unit and one for first) class LowLevel: def comRxHeader(self): ('a', 'h') (7, 8) bsl = LowLevel() bsl.comRxHeader() # self.mergeinh instead of self.merge XXX fix others class LowLevel2: def bslTxRx(self, addr): addr % 2 class BootStrapLoader2(LowLevel2): pass bsl2 = BootStrapLoader2() bsl2.bslTxRx(0) # improve parent constructor calls class L: def __init__(self): pass class BSL(L): def __init__(self, a, b): L.__init__(self) BSL(1, 2) # for/while-else construction bla = True while bla: for a in range(10): for b in range(10): pass else: print 'bah1' while bla: bla = False break else: print 'bah4' break else: print 'bah2' else: print 'bah3' # user-defined exception class problems class MyException(Exception): pass try: raise MyException('hoepa') except MyException, m: print m # parent constructor call and default arguments class LowLevel3: def __init__(self, a=1): pass class BootStrapLoader3(LowLevel3): def __init__(self): LowLevel3.__init__(self) BootStrapLoader3() shedskin-0.9.4/tests/180.py0000664000175000017500000001137012157270661015307 0ustar srepmubsrepmub #re import re try: a = re.compile(r'\b(?P[\w.-]+?)@(?P[a-z.-]{3,})\b', re.IGNORECASE) b = 'bob (BoB@gmaiL.com) said to sally (sally123_43.d@hOtmail.co.uk) that no-name (not_a-real@em_ail.dres) was annoying...' print a.search(b, 20).group(0) print a.match(b, 5).expand(r'the found name: \g\nthe domain: \g') print a.subn(r'\1 AT \g', b) print a.sub(r'\1', b) # print a.findall(b) c = re.compile(r''' \b (?Phttps?|(ftp)|(?Pmailto)) :(?(mailto)|//) ( (?P[\w._-]+?) (?(mailto) | :(?P[\w._-]*?) ) @ )? (?P[\w.-]+) (?(mailto) | (?P/[^\s]*) ) \b ''', re.X) d = 'fasdf mailto:bob@gmail.com, dasdfed ftp://haha:hoho@bla.com/files, http://fsesdf@asd.com orRLY!!?!L!? \ https://example.com/OMG.html' allm = c.finditer(d) i = 1 for mo in allm: s = str(i) + ': \n' s += '\tfull: ' + mo.group(0) s += '\n\tnamed: ' gd = mo.groupdict() for k in sorted(gd): if gd[k] == None: continue s += '\n\t\t' + k + ': ' + gd[k] print s i += 1 print re.split(r'\W+', b) print re.split(r'(\W+)', b, 2) except re.error, msg: print msg #time import time try: print time.mktime(time.struct_time((1970, 2, 17, 23, 33, 34, 1, 48, -1))) print time.mktime((1970, 2, 17, 23, 33, 34, 3, 17, -1)) print time.localtime(4142014) # print time.localtime() # print time.localtime(time.mktime(time.localtime())) # print time.gmtime(time.mktime(time.gmtime())) # print time.asctime() print time.asctime(time.struct_time((2008, 6, 24, 12, 50, 00, 0, 120, -1))) # print time.ctime() print time.ctime(1000000) y = (2008, 6, 24, 12, 50, 00, 0, 120, -1) x = time.struct_time(y) print x print x.tm_mon print x[6] # print time.strftime("%R",time.localtime()) # print time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) print time.strftime("%a, %d %b %Y %H:%M:%S", (2008, 6, 24, 12, 50, 00, 0, 120, -1)) # print time.strftime("%d %b %Y %H:%M:%S", time.strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S")) # XXX %a # print time.strftime("%Y", time.strptime("2001", "%Y")) # XXX %a # print time.timezone print len(time.tzname) except TypeError, e: print e #corner cases print int(''.isdigit()) print int(''.isalpha()) print int(''.isalnum()) print int(''.islower()) print int(''.isupper()) print int(''.istitle()) #glob, fnmatch import glob print glob.glob('run.py') import fnmatch print int(fnmatch.fnmatch('run.py', 'run.[py]y')) #staticmethod, property class woef(object): def x(a): print a def y(self, b): print b def getz(self): return 15+self._x def setz(self, x): self._x = x x = staticmethod(x) z = property(getz, setz) w = woef() w.y(4321) woef.x(1234) woef.k = 1 woef.k w.z = 14 print w.z class base: def x(): return 12 def gety(self): return self.value def sety(self, val): self.value = val x = staticmethod(x) y = property(gety, sety) z = 21 class der(base): pass print der.x() derder = der() derder.y = 99 print derder.y #print der.z # XXX #unaryadd class V: def __init__(self, x): self.x = x def __pos__(self): return V(self.x+1) def __neg__(self): return V(self.x-1) def __repr__(self): return 'V(%d)' % self.x v = V(1) print ++v, +-+-v #multidir fixes from testdata import crap print crap.incrap() import testdata.bert2 as bert print bert.hello(1) from testdata import crap2 crap2.incrap2() import testdata.crap2 tc2c2 = testdata.crap2.crap2() #int/double crap def to_ints(l): return [int(x) for x in l] print to_ints([4.0, 4.0, 61]), to_ints((4.0, 4.0, 61)) print int(min(4.0, 4.0, 2)) print int(max(4.0, 4.0, 6)) print int(min(4.0, 4.0, 4.0, 2)) print int(max(4.0, 4.0, 4, 0, 6)) l = [6] l.append(1.0) print to_ints(l) #assorted fixes [1] != [] from collections import defaultdict print sorted(defaultdict.fromkeys(range(7,10), 'a').items()) import collections print sorted(collections.defaultdict.fromkeys(range(7,10), 'a').items()) from string import * class string: pass string.x = 4 shedskin-0.9.4/tests/51.py0000664000175000017500000000116212157270661015222 0ustar srepmubsrepmub import testdata.bert class zeug: def meuk(self): # self: [zeug()] return '2' # [str] def hoi(): return 1 # [float] print hoi() # [float] a = zeug() # [zeug()] print testdata.bert.hello(1) # [str] z = testdata.bert.zeug() # [bert::zeug()] z.hallo(1) # [int] print a.meuk() # [str] l1 = lambda x,y: x+y # [lambda0] print l1(1,2) # [int] shedskin-0.9.4/tests/3.py0000664000175000017500000000015512157270661015140 0ustar srepmubsrepmub def ident(x): return x def boing(c, d): return ident(c) a = 1 h = boing(boing(a,1.0),boing(3.0,a)) shedskin-0.9.4/tests/leak/0000775000175000017500000000000012157270661015337 5ustar srepmubsrepmubshedskin-0.9.4/tests/leak/leak_main.py0000664000175000017500000000123112157270661017626 0ustar srepmubsrepmubimport leak print leak.__file__ p = leak.Point() while True: p.x p.y p.s p.n leak.test_float(4.4) leak.test_int(4) leak.test_str('beh') leak.test_str(None) for q in leak.points: q.x leak.test_list(range(100)) leak.test_list(None) leak.test_list2([1.1, 2.2, 3.3]) leak.test_list2(None) leak.test_list_nested([[1,2],None,[]]) leak.test_list_nested(None) leak.test_dict({'hoi': 8.8, 'wa': 9.1}) leak.test_dict(None) leak.test_set(set([1,2,3])) leak.test_set(None) leak.test_tuple((1,2,3)) leak.test_tuple(None) leak.test_tuple2(('hoi', 8.9)) leak.test_tuple2(None) shedskin-0.9.4/tests/leak/leak.py0000664000175000017500000000147512157270661016634 0ustar srepmubsrepmub class Point: def __init__(self): self.x = 4.4 self.y = 7 self.s = 'wah' self.n = None points = [Point() for x in range(10)] def test_float(x): return x def test_int(x): return x def test_str(s): return s def test_none(n): return n def test_list(l): return l def test_list2(l): return l def test_list_nested(l): return l def test_dict(d): return d def test_set(d): return d def test_tuple(d): return d def test_tuple2(d): return d if __name__ == '__main__': p = Point() test_float(4.4) test_int(4) test_str('hoi') test_none(None) test_list([1,2]) test_list2([1.0,2.0]) test_list_nested([[1,2]]) test_dict({'hoi': 8.8}) test_set(set([1,2,3])) test_tuple((1,2,3)) test_tuple2(('hoi', 8.9)) shedskin-0.9.4/tests/144.py0000664000175000017500000000066312157270661015312 0ustar srepmubsrepmub class huhuhu: pass obj = huhuhu() if obj is obj: print 'ok1' if obj is not None: print 'ok2' obj = None if obj is None: print 'ok3' if obj == None: print 'ok4' if obj is not None: print 'bad' if obj != None: print 'bad' if not obj: print 'ok5' bla = "hoei" if bla == 'hoei': print 'ok6' if bla is 'hoei': print 'ok7' if bla != 'meuk': print 'ok8' if bla is not 'meuk': print 'ok9' shedskin-0.9.4/tests/175.py0000664000175000017500000000272212157270661015314 0ustar srepmubsrepmub # --- out of bounds can be okay a = range(5) print a[:10], a[:10:2] print a[-10:], a[-10::2] # --- abs class C: def __abs__(self): return self def __neg__(self): return self def __repr__(self): return 'C' print abs(C()), abs(23), abs(-1.3), -abs(C()) # --- str.translate problem import string atable = string.maketrans("bc", "ef") print 'abcdeg'.translate(atable, "cde") gtable = string.maketrans("", "") word = 'aachen\n' key = word.translate(gtable, "a\n") print 'word', repr(word) # --- string.{capitalize, capwords, swapcase, center, atoi, atol, atof} print string.capitalize('hoi'), ' hoi'.capitalize() print string.capwords('yo momma')+'!'+string.capwords(' yo momma ')+'!'+string.capwords(' yo momma ', 'mm')+'!' allchars = ''.join([chr(x) for x in range(256)]) print repr(allchars.swapcase()), repr(string.swapcase(allchars)) print string.center('hoi', 10), string.center('hoi', 10, 'u') print 'hoi'.center(10, 'u') for i in range(10): print '!'+'hoi'.center(i)+'!' print string.atoi('+0x10', 0), string.atol('-100l', 0), string.atof('-1.234') # --- improve overloading class D: def __int__(self): return 7 def __float__(self): return 7.0 def __str__(self): return '__str__' def __repr__(self): return '__repr__' def __cmp__(self, b): return 1 def __nonzero__(self): return True def __len__(self): return 1 d = D() print [0,1][bool(d)], str(d), int(d), float(d), max([d,d]), min([d,d]) if 5: print 5 if d: print 6 shedskin-0.9.4/tests/testdata/0000775000175000017500000000000012157270661016234 5ustar srepmubsrepmubshedskin-0.9.4/tests/testdata/woef.csv0000664000175000017500000000004712157270661017712 0ustar srepmubsrepmubaap| noot| 18| ole aap| noot| 19| ole2 shedskin-0.9.4/tests/testdata/d1541.py0000664000175000017500000000025612157270661017347 0ustar srepmubsrepmub#!/usr/bin/env python import iec2 class D1541(iec2.IECMember): pass #def __init__(self, IEC, bus_address): # iec2.IECMember.__init__(self, IEC, bus_address) shedskin-0.9.4/tests/testdata/bleh.py0000664000175000017500000000000012157270661017506 0ustar srepmubsrepmubshedskin-0.9.4/tests/testdata/iec2.py0000664000175000017500000000073212157270661017432 0ustar srepmubsrepmub#!/usr/bin/env python class IECMember(object): def __init__(self, IEC, bus_address): self.B_clock = False self.B_data = True self.B_ATN = False self.bus_address = bus_address self.IEC = IEC self.B_talker = False # effect not immediate self.B_switch_talker = False self.received_count = 0 self.received_value = 0 def get_data(self): return(self.B_data) class IECBus(object): pass shedskin-0.9.4/tests/testdata/scene.txt0000664000175000017500000000750312157270661020077 0ustar srepmubsrepmub# scene descripting for yopyraSS.py # (optional, default 320 240) size = size of the final image size 320 240 nbounces 5 # (optional, default 1) oversampling=1 implica que no hay oversampling oversampling 2 # (optional, default 60) vision = size of the visual field vision 60 # (optional, default all span) renderslice: start_rendering_line end_rendering_line # renderslice note: if you use oversampling>1 it may give problems #renderslice 10 40 posCamara 6.0 6.0 6.0 lookCamara 1.0 0.8 -0.1 upCamara 0.0 0.0 1.0 # sphere: material_number x y z radius sphere 0 -8 2.26163 0.993114 0.35 sphere 2 -8 0.73837 3.00689 0.35 sphere 0 -7.6 1.60521 0.803393 0.35 sphere 2 -7.6 1.39479 3.19661 0.35 sphere 0 -7.2 0.923034 0.906644 0.35 sphere 2 -7.2 2.07697 3.09336 0.35 sphere 0 -6.8 0.382118 1.27759 0.35 sphere 2 -6.8 2.61788 2.72241 0.35 sphere 0 -6.4 0.114898 1.8254 0.35 sphere 2 -6.4 2.8851 2.1746 0.35 sphere 0 -6 0.1868 2.41596 0.35 sphere 2 -6 2.8132 1.58404 0.35 sphere 0 -5.6 0.580219 2.90468 0.35 sphere 2 -5.6 2.41978 1.09532 0.35 sphere 0 -5.2 1.19883 3.17191 0.35 sphere 2 -5.2 1.80117 0.828095 0.35 sphere 0 -4.8 1.89118 3.1522 0.35 sphere 2 -4.8 1.10882 0.847796 0.35 sphere 0 -4.4 2.48776 2.8504 0.35 sphere 2 -4.4 0.512244 1.1496 0.35 sphere 0 -4 2.84249 2.34039 0.35 sphere 2 -4 0.157506 1.65961 0.35 sphere 0 -3.6 2.86854 1.74705 0.35 sphere 2 -3.6 0.131458 2.25295 0.35 sphere 0 -3.2 2.55952 1.21563 0.35 sphere 2 -3.2 0.440477 2.78437 0.35 sphere 0 -2.8 1.9911 0.876252 0.35 sphere 2 -2.8 1.0089 3.12375 0.35 sphere 0 -2.4 1.30243 0.812009 0.35 sphere 2 -2.4 1.69757 3.18799 0.35 sphere 0 -2 0.662139 1.03863 0.35 sphere 2 -2 2.33786 2.96137 0.35 sphere 0 -1.6 0.226984 1.50062 0.35 sphere 2 -1.6 2.77302 2.49938 0.35 sphere 0 -1.2 0.103507 2.08488 0.35 sphere 2 -1.2 2.89649 1.91512 0.35 sphere 0 -0.8 0.321941 2.64836 0.35 sphere 2 -0.8 2.67806 1.35164 0.35 sphere 0 -0.4 0.828804 3.0531 0.35 sphere 2 -0.4 2.1712 0.946901 0.35 sphere 0 0 1.5 3.2 0.35 sphere 2 0 1.5 0.8 0.35 sphere 0 0.4 2.1712 3.0531 0.35 sphere 2 0.4 0.828804 0.946901 0.35 sphere 0 0.8 2.67806 2.64836 0.35 sphere 2 0.8 0.321941 1.35164 0.35 sphere 0 1.2 2.89649 2.08488 0.35 sphere 2 1.2 0.103507 1.91512 0.35 sphere 0 1.6 2.77302 1.50062 0.35 sphere 2 1.6 0.226984 2.49938 0.35 sphere 0 2 2.33786 1.03863 0.35 sphere 2 2 0.662139 2.96137 0.35 sphere 0 2.4 1.69757 0.812009 0.35 sphere 2 2.4 1.30243 3.18799 0.35 sphere 0 2.8 1.0089 0.876252 0.35 sphere 2 2.8 1.9911 3.12375 0.35 sphere 0 3.2 0.440477 1.21563 0.35 sphere 2 3.2 2.55952 2.78437 0.35 sphere 0 3.6 0.131458 1.74705 0.35 sphere 2 3.6 2.86854 2.25295 0.35 sphere 0 4 0.157506 2.34039 0.35 sphere 2 4 2.84249 1.65961 0.35 sphere 0 4.4 0.512244 2.8504 0.35 sphere 2 4.4 2.48776 1.1496 0.35 sphere 0 4.8 1.10882 3.1522 0.35 sphere 2 4.8 1.89118 0.847796 0.35 sphere 0 5.2 1.80117 3.17191 0.35 sphere 2 5.2 1.19883 0.828095 0.35 sphere 0 5.6 2.41978 2.90468 0.35 sphere 2 5.6 0.580219 1.09532 0.35 sphere 0 6 2.8132 2.41596 0.35 sphere 2 6 0.1868 1.58404 0.35 sphere 0 6.4 2.8851 1.8254 0.35 sphere 2 6.4 0.114898 2.1746 0.35 sphere 0 6.8 2.61788 1.27759 0.35 sphere 2 6.8 0.382118 2.72241 0.35 sphere 0 7.2 2.07697 0.906644 0.35 sphere 2 7.2 0.923034 3.09336 0.35 sphere 0 7.6 1.39479 0.803393 0.35 sphere 2 7.6 1.60521 3.19661 0.35 # plano: material normal_x normal_y normal_z distancia: plano 1 0.0 0.0 1.0 0.0 # light: x y z r g b puntual/ambiental (rgb are in [0,1]) light -2.0 1.0 4.0 1.0 1.0 1.0 puntual light -2.0 1.0 4.0 0.1 0.1 0.1 ambiental # material: r g b cDifuso cEspecular dEspecular cReflexion cTransmitividad iRefraccion material 0.3 0.3 1.0 0.7 0.4 0.8 0.3 0.0 0.0 material 0.4 1.0 1.0 0.2 0.0 0.0 0.2 0.0 0.0 material 1.0 0.2 0.2 0.9 0.7 1.0 0.2 0.0 0.0 material 0.2 1.0 0.2 0.4 0.4 0.9 0.7 0.3 0.9 shedskin-0.9.4/tests/testdata/life.txt0000664000175000017500000000010112157270661017704 0ustar srepmubsrepmub6 0,0 0,2 1,1 2,1 3,0 3,2 p 3 0,0 1,0 1,1 q shedskin-0.9.4/tests/testdata/binf0000664000175000017500000000003112157270661017067 0ustar srepmubsrepmubik haat feda smurven.. shedskin-0.9.4/tests/testdata/hopsakee0000664000175000017500000000003212157270661017751 0ustar srepmubsrepmubhoei hoeiii feda hop hap shedskin-0.9.4/tests/testdata/piece.py0000664000175000017500000000020512157270661017670 0ustar srepmubsrepmubimport board def latebinding(): a = board.WHITE print a b = board.white() print b c = board.Board() print c shedskin-0.9.4/tests/testdata/b6.pz0000664000175000017500000000013212157270661017112 0ustar srepmubsrepmub45......6 ..3..1..7 ....23... ....4.25. ..93.21.. .81.7.... ...58.... 9..7..8.. 7......64 shedskin-0.9.4/tests/testdata/binas0000664000175000017500000000253612157270661017261 0ustar srepmubsrepmub!LV@# How a lz4 compressed file (*.lz4) looks like? # -> Official implementation F7 total byte-size: 4 + ) !4 s  Magic number (0x02 0x21 0x4C 0x18575Streams (1 or more) % (x of a single s3yT`length< sequences (Little Endiany,--> Addi"#alr rules: * Last 5tof each-are alwayeterals: - ALWAYS: lDp has noset nor Part B;umatch must st"Pat leK#12before endL$ Q1+Token, divided in$ =P4 bitBHigh)7Low3[ ofTbe copi`/=_ BAmin-,f(where-B = 4* Copyingbcurrent (pos # -vXs41) inclup- cycl@if rB *  *'fformat#- 1]65535N-*$exW "in2CReal8=/*8-(- +is referredu`data shedskin-0.9.4/tests/testdata/symbols.INI0000664000175000017500000000002712157270661020264 0ustar srepmubsrepmub [symbols] a = 4 b = 5 shedskin-0.9.4/tests/testdata/bert.py0000664000175000017500000000067012157270661017545 0ustar srepmubsrepmubimport os, sys class zeug: purple = 3 def hallo(self, x): # self: [], x: [] a = 1 # [] return 1 # [] def hello(x): # x: [] a = x # [] print a # [] return 'hello' # [] shedskin-0.9.4/tests/testdata/crap2/0000775000175000017500000000000012157270661017243 5ustar srepmubsrepmubshedskin-0.9.4/tests/testdata/crap2/bert.py0000664000175000017500000000004712157270661020552 0ustar srepmubsrepmub def hello(x): print 'andere bert' shedskin-0.9.4/tests/testdata/crap2/__init__.py0000664000175000017500000000010712157270661021352 0ustar srepmubsrepmubimport bert class crap2: pass def incrap2(): bert.hello(144) shedskin-0.9.4/tests/testdata/timer.py0000664000175000017500000000021612157270661017725 0ustar srepmubsrepmubclass TimingOut(object): def fire_timer(self): print 'timer' return True def timeout_add(timeout_ms, obj): return 42 shedskin-0.9.4/tests/testdata/board.py0000664000175000017500000000017512157270661017700 0ustar srepmubsrepmubimport piece WHITE = 4 def white(): return 'white' class Board(object): def __str__(self): return 'board' shedskin-0.9.4/tests/testdata/Material.py0000664000175000017500000000012612157270661020343 0ustar srepmubsrepmubclass Material: def __init__(self, name): print 'hoi' EMPTY=Material("Empty") shedskin-0.9.4/tests/testdata/test.conf0000664000175000017500000000025212157270661020061 0ustar srepmubsrepmub[book] title: The Python Standard Library author: Fredrik Lundh email: fredrik@pythonware.com version: 2.0-001115 [ematter] pages: 250 hop: True [hardcopy] pages: 350 shedskin-0.9.4/tests/testdata/Shape.py0000664000175000017500000000007512157270661017650 0ustar srepmubsrepmub class Shape: def woef(self, h): h.update(self) shedskin-0.9.4/tests/testdata/words0000664000175000017500000000003612157270661017314 0ustar srepmubsrepmubaaaaaha cccde bbbbbbbbbbbc shedskin-0.9.4/tests/testdata/__init__.py0000664000175000017500000000000012157270661020333 0ustar srepmubsrepmubshedskin-0.9.4/tests/testdata/subsub.py0000664000175000017500000000110712157270661020110 0ustar srepmubsrepmub def fact(n): """Return n factorial""" product = 1 for ii in range(2, n + 1): product *= ii return product l = lambda x: -x a = [1 for x in range(10)] b = (x for x in range(10)) def blah(x): yield x yield x+1 c = list(blah(4)) print 'subsub:' print l(3) GLOBAL = 'global' class helper(): flag=0 @staticmethod def enable(flag): helper.flag=flag print 'enabled' class aa(object): def __init__(self): print "init a" aa=[1,2] for i in aa: print i def hoppa(self): x = GLOBAL print x helper.enable(1) shedskin-0.9.4/tests/testdata/bert2.py0000664000175000017500000000035712157270661017631 0ustar srepmubsrepmub def hello(x): return 'rootbert' def def1(a=-14): return a a = 15 def def2(a=a): return a def huh(): return a a = 17 def def3(a=2*a): return a b = None def def4(a=b): pass def1() def2() def3() huh() def4([1]) shedskin-0.9.4/tests/testdata/CCMView.py0000664000175000017500000000014612157270661020044 0ustar srepmubsrepmubclass Line: pass class CCMView: def wa(self): self.line = Line() shedskin-0.9.4/tests/testdata/bla0000664000175000017500000000001712157270661016713 0ustar srepmubsrepmubniet op scherm shedskin-0.9.4/tests/testdata/hoppa0000664000175000017500000000001712157270661017264 0ustar srepmubsrepmubhop hop hoppa! shedskin-0.9.4/tests/testdata/crap.py0000664000175000017500000000005212157270661017530 0ustar srepmubsrepmub#import blup def incrap(): return 14 shedskin-0.9.4/tests/testdata/uuf250-010.cnf0000775000175000017500000003560512157270661020264 0ustar srepmubsrepmubc This Formular is generated by mcnf c c horn? no c forced? no c mixed sat? no c clause length = 3 c p cnf 250 1065 -144 244 131 0 -117 -19 84 0 -188 137 -38 0 176 130 43 0 107 197 -240 0 197 165 -155 0 -106 211 180 0 232 -54 -206 0 6 -143 -90 0 -171 -39 -126 0 -71 -96 -63 0 136 -28 -117 0 133 -198 36 0 -49 222 146 0 -67 190 116 0 -90 -170 110 0 -58 30 60 0 -157 227 -222 0 194 114 -160 0 166 68 -119 0 -46 100 138 0 -188 -60 -62 0 -22 140 -132 0 182 25 60 0 -21 29 -128 0 -108 105 -237 0 27 -197 47 0 -244 -215 -170 0 -72 -140 -25 0 192 -1 -166 0 -207 -247 80 0 -180 19 -37 0 -6 -100 -195 0 -176 -47 43 0 87 -121 -24 0 228 146 -160 0 -147 -18 170 0 163 -249 126 0 10 100 147 0 75 -135 -195 0 -11 -115 30 0 -72 -68 -134 0 -17 163 126 0 -242 29 -150 0 -214 155 -83 0 43 -14 84 0 189 -5 73 0 200 -12 235 0 126 145 32 0 222 -248 16 0 24 198 -31 0 -112 209 -220 0 -6 232 -226 0 244 -182 24 0 -136 -178 -55 0 -168 5 -158 0 -164 -223 -137 0 97 -163 -149 0 -202 -146 138 0 33 -56 21 0 -48 124 8 0 133 236 -92 0 187 215 160 0 -42 -240 -208 0 22 248 -183 0 45 133 13 0 171 -221 -70 0 -9 177 -152 0 -230 -54 -178 0 -65 3 -141 0 -137 -90 -108 0 233 62 -165 0 -240 -82 81 0 80 -229 -146 0 151 -231 -130 0 16 115 -14 0 -68 246 216 0 -137 -38 120 0 -59 9 98 0 -81 -166 -243 0 -135 222 86 0 53 -162 -94 0 -57 -31 179 0 -45 57 -116 0 -155 91 47 0 -118 185 224 0 -51 -135 230 0 -36 -209 -155 0 -72 180 -209 0 69 226 60 0 118 -241 -46 0 -159 -245 -13 0 -13 -175 124 0 70 -212 189 0 247 217 235 0 -167 199 99 0 90 198 -38 0 -17 98 -101 0 149 84 201 0 96 -109 -66 0 189 82 -96 0 80 -133 -83 0 -43 -250 16 0 133 38 -114 0 -182 220 -85 0 -145 238 81 0 -125 -104 -6 0 -125 -12 -239 0 170 37 166 0 10 52 8 0 -94 -194 45 0 49 58 -174 0 44 139 156 0 161 -198 -97 0 115 241 18 0 -180 178 -10 0 -44 -150 1 0 110 248 -153 0 -75 190 128 0 -137 130 -99 0 -221 -181 -110 0 -173 -198 112 0 187 25 63 0 -19 113 32 0 5 170 -177 0 -35 45 110 0 -106 198 -212 0 32 -58 -151 0 -186 166 52 0 -15 -66 6 0 -121 203 -124 0 -243 -202 -97 0 -81 201 -25 0 -170 -230 -86 0 58 -212 -213 0 142 25 -82 0 164 -247 131 0 227 32 249 0 172 -183 -23 0 154 212 182 0 246 -165 110 0 -6 94 110 0 -185 80 92 0 -227 1 -37 0 -218 162 -153 0 235 23 91 0 207 -106 -115 0 -126 176 -47 0 73 86 -127 0 -50 -52 47 0 -128 185 -97 0 240 235 52 0 71 -176 219 0 243 216 -163 0 5 125 -48 0 -125 -205 -183 0 -167 163 -195 0 -10 -87 171 0 -56 196 30 0 215 70 229 0 -10 -245 -193 0 131 95 98 0 109 -197 14 0 142 -95 -108 0 75 231 -191 0 97 96 -6 0 192 -33 -39 0 -148 -90 -145 0 -127 -84 81 0 -61 225 -234 0 244 -171 -235 0 223 -172 205 0 143 110 -187 0 10 -220 9 0 133 -16 228 0 95 -156 -223 0 -1 -160 126 0 -53 64 196 0 -165 -111 -8 0 -177 -84 -230 0 -206 44 -48 0 132 124 145 0 94 132 -130 0 78 -36 92 0 49 135 126 0 -170 -244 -119 0 203 53 -115 0 163 -153 -76 0 23 19 -232 0 157 -152 76 0 -107 99 108 0 216 18 -82 0 25 28 -44 0 47 -93 3 0 219 -114 -9 0 158 -2 37 0 -44 193 -120 0 17 -48 12 0 44 -194 -175 0 -153 -190 -245 0 -98 142 -134 0 200 60 106 0 -91 130 -102 0 -194 -221 -108 0 143 132 235 0 151 -7 -16 0 -20 -112 193 0 215 -28 -155 0 250 122 152 0 98 4 225 0 -206 96 222 0 -10 120 48 0 -100 83 -228 0 166 -132 -61 0 -121 246 -128 0 -159 -8 63 0 235 -7 -190 0 105 -241 79 0 240 72 -220 0 204 -216 208 0 48 -76 17 0 181 -190 -19 0 -115 -76 150 0 32 -147 -129 0 -213 -151 190 0 -180 -29 -183 0 -209 -70 79 0 138 -55 126 0 -94 29 -180 0 -195 -242 27 0 -241 93 -36 0 -62 -247 -245 0 190 -112 -238 0 97 -243 13 0 -153 159 -34 0 -186 -33 39 0 -147 -18 75 0 74 -98 161 0 46 9 52 0 -101 187 -174 0 81 51 -249 0 197 169 -223 0 -70 -133 -228 0 39 240 -149 0 106 149 -121 0 -245 -114 37 0 -198 -180 227 0 51 -19 168 0 167 -37 -101 0 249 233 47 0 -105 -6 -193 0 -199 197 104 0 -210 -167 72 0 239 -111 -107 0 237 175 -216 0 113 148 -233 0 230 -237 178 0 -137 -121 -211 0 -28 236 -15 0 118 61 -116 0 60 207 57 0 -24 241 -81 0 -245 -210 112 0 110 195 -94 0 -65 196 229 0 -90 -88 29 0 241 154 30 0 62 -100 173 0 164 174 125 0 103 -198 21 0 123 -64 -121 0 9 -97 189 0 141 51 102 0 -94 19 4 0 11 -159 -203 0 -123 64 -243 0 153 -91 -32 0 -132 61 -33 0 64 -217 -157 0 73 -197 121 0 -204 100 -126 0 -131 246 -49 0 -145 -219 -28 0 -66 15 118 0 184 -154 -250 0 -116 132 -77 0 -199 208 166 0 -215 -15 -221 0 144 -9 186 0 65 -164 171 0 41 84 230 0 -38 24 -14 0 -206 -235 149 0 98 -120 99 0 -51 196 -243 0 69 -50 45 0 105 -205 -144 0 -2 113 148 0 241 -137 244 0 -169 -221 70 0 -122 10 129 0 -37 163 -250 0 -93 -32 21 0 115 173 196 0 232 180 86 0 130 -213 37 0 53 166 7 0 -210 -188 70 0 -39 -42 -173 0 -100 -132 -115 0 -212 62 168 0 92 -123 213 0 -137 135 -245 0 -207 -153 215 0 -64 -236 -246 0 133 -132 241 0 99 221 182 0 119 -206 241 0 36 -117 -8 0 115 -60 131 0 40 -120 182 0 90 19 32 0 127 198 -57 0 -222 97 85 0 124 212 -140 0 -54 -111 -246 0 47 230 185 0 -180 -128 211 0 204 -217 -118 0 119 69 -176 0 -134 126 -109 0 -46 -159 -228 0 -244 -167 105 0 226 -106 66 0 -58 12 -80 0 230 154 -28 0 86 80 49 0 -42 54 201 0 246 235 209 0 -73 135 -115 0 55 -97 38 0 222 21 -151 0 55 126 14 0 109 17 7 0 171 151 219 0 -169 -107 182 0 -28 -161 -137 0 -138 -236 96 0 -173 -100 -134 0 -13 88 -112 0 -67 -42 -21 0 115 -26 121 0 221 -91 37 0 183 164 127 0 -27 106 132 0 52 -153 45 0 -33 15 68 0 -206 237 110 0 -20 235 15 0 190 -76 213 0 74 16 202 0 -161 122 -117 0 248 -231 -119 0 -208 -242 -155 0 80 -78 -31 0 92 222 10 0 144 -1 -220 0 142 -225 -10 0 40 188 85 0 -92 153 70 0 226 23 -73 0 -162 113 -140 0 63 -65 108 0 3 -187 243 0 -127 -41 221 0 9 -151 -43 0 -3 -79 17 0 -26 -63 171 0 -187 -139 -4 0 -247 103 -244 0 237 -58 -69 0 -184 142 66 0 -57 -58 128 0 46 128 -152 0 -2 53 230 0 -163 6 -86 0 218 -219 -116 0 -98 -226 -206 0 -239 249 24 0 -18 50 -133 0 25 -207 -158 0 -85 -156 162 0 99 37 57 0 -201 -1 8 0 -55 -158 -105 0 -90 180 105 0 190 125 -199 0 -183 -173 -174 0 248 -179 187 0 -61 -208 -235 0 -64 -235 69 0 -212 172 233 0 -48 -206 -230 0 157 -192 136 0 -170 -8 -246 0 43 -236 126 0 -218 116 29 0 -8 74 -7 0 195 202 142 0 117 214 96 0 -3 159 -5 0 235 -237 24 0 66 -195 -123 0 -23 -126 -118 0 2 -71 -125 0 116 186 -150 0 29 49 156 0 -102 -128 -37 0 141 -222 93 0 169 -138 118 0 -35 -100 237 0 104 248 -100 0 -102 151 -39 0 -106 14 211 0 245 -191 -234 0 -164 -19 68 0 201 58 34 0 -211 168 234 0 -238 161 -122 0 45 -104 84 0 245 129 87 0 -99 229 -97 0 -216 -239 -81 0 -57 -226 -47 0 -71 -219 -9 0 221 241 -103 0 -98 151 -109 0 -108 -248 125 0 74 -3 88 0 25 -1 -230 0 7 -125 216 0 239 -29 -169 0 234 81 -199 0 220 -91 147 0 69 235 -77 0 -25 -24 105 0 -8 -121 66 0 127 201 -119 0 220 29 -227 0 193 -155 -171 0 249 -228 211 0 -141 -226 -213 0 245 29 -131 0 -250 -73 218 0 163 -132 -208 0 -21 149 139 0 75 143 -82 0 217 -94 12 0 36 -206 -188 0 56 -94 -8 0 175 -198 56 0 169 4 202 0 -233 7 144 0 109 -139 -56 0 -174 -197 82 0 -88 -134 -206 0 -203 116 -140 0 204 98 186 0 197 -202 177 0 240 171 -14 0 79 189 107 0 171 -35 168 0 -239 -142 -131 0 -214 -184 -175 0 -208 -176 -93 0 194 75 -133 0 103 30 -1 0 -54 79 80 0 158 217 -2 0 4 64 113 0 -40 229 184 0 -139 -132 -73 0 -78 -206 -241 0 -225 -48 211 0 164 -133 -131 0 135 -19 -144 0 -111 -81 -54 0 96 -47 162 0 80 -179 -99 0 -107 69 111 0 152 229 109 0 217 158 130 0 -226 187 146 0 189 -103 -28 0 -16 -249 1 0 216 -138 74 0 124 -149 27 0 -103 145 54 0 97 220 179 0 56 -22 70 0 -151 -114 193 0 94 61 -24 0 238 -166 87 0 117 -9 88 0 -116 -189 117 0 -241 145 128 0 58 212 -125 0 23 69 104 0 228 227 -25 0 160 137 96 0 -40 -63 234 0 231 -52 -63 0 -43 155 213 0 -163 241 49 0 -95 -215 206 0 29 -31 90 0 -115 179 137 0 72 -77 -174 0 -106 135 -177 0 -224 166 -198 0 207 -135 -218 0 132 -11 -94 0 -37 9 -203 0 147 149 -60 0 56 -26 -46 0 -191 -161 -36 0 -74 89 -193 0 -44 143 -140 0 -172 249 -81 0 -111 -127 159 0 58 -70 -24 0 -25 -52 -70 0 -99 12 -69 0 171 -179 -170 0 135 -20 -118 0 -156 234 67 0 -162 101 96 0 -157 -167 75 0 146 76 -12 0 -56 212 190 0 -170 161 -99 0 -31 -2 27 0 190 139 -12 0 98 10 243 0 -29 131 31 0 -85 -224 196 0 -109 241 37 0 -229 57 157 0 -158 170 31 0 -220 57 236 0 -35 110 -13 0 15 151 213 0 -70 14 -137 0 247 162 -127 0 133 30 -212 0 -158 65 -97 0 142 60 39 0 184 -180 87 0 -28 -153 2 0 59 -62 93 0 8 88 -187 0 242 149 -40 0 -105 187 -80 0 33 -34 137 0 -97 247 128 0 -61 -25 -145 0 228 -120 51 0 -167 -5 -240 0 231 228 -30 0 184 109 -215 0 119 108 -7 0 -166 82 -4 0 19 16 159 0 5 243 83 0 -2 43 -202 0 -199 -223 -106 0 89 151 -42 0 -246 -135 -156 0 9 -147 160 0 -184 -183 -62 0 -134 98 138 0 -200 -141 -23 0 131 -229 -23 0 203 -141 -62 0 -131 165 176 0 -108 -184 -174 0 117 123 140 0 245 -78 -51 0 234 101 28 0 -227 246 197 0 27 -229 -196 0 138 -26 -39 0 56 -134 -216 0 72 -148 27 0 120 -220 -103 0 89 -166 127 0 -206 75 -55 0 132 158 -77 0 23 -86 -140 0 157 55 -103 0 -157 210 34 0 -199 167 117 0 -74 -177 -137 0 -193 -26 -17 0 -148 -15 -2 0 64 95 -150 0 159 -37 -160 0 -85 -52 105 0 99 -165 70 0 15 -53 -44 0 10 23 -160 0 28 -148 -62 0 137 -179 -95 0 206 -65 -209 0 86 -23 -31 0 113 5 233 0 -175 -151 241 0 227 -89 -46 0 -211 -152 -10 0 -157 -60 -1 0 -147 24 237 0 -222 -220 -11 0 80 183 -53 0 70 -215 151 0 57 -40 183 0 40 124 -118 0 -137 -224 134 0 -111 -15 22 0 161 208 240 0 185 -93 -163 0 211 -190 13 0 -242 -103 -237 0 -215 111 187 0 12 -184 33 0 124 179 161 0 -175 6 -1 0 -201 -76 70 0 110 -166 -180 0 -122 -163 -136 0 -222 37 59 0 -194 -248 -111 0 -162 136 -21 0 -51 -184 137 0 183 101 201 0 198 226 -152 0 -111 -53 -221 0 -165 -159 41 0 157 -191 47 0 81 -104 -127 0 147 -120 -107 0 -21 -237 19 0 -147 122 175 0 51 213 -6 0 -123 87 -31 0 -130 15 -72 0 -202 83 -184 0 -236 -221 12 0 -225 209 32 0 -158 -179 -124 0 -141 58 7 0 159 -247 -151 0 -233 203 197 0 63 88 -94 0 -226 -47 -5 0 54 33 3 0 174 73 -222 0 -249 242 -143 0 -55 193 -60 0 29 -237 -224 0 30 -205 230 0 103 -143 -130 0 -22 -157 161 0 -162 204 -208 0 148 -131 206 0 70 -193 60 0 -106 -77 173 0 154 173 -238 0 80 -248 -91 0 -7 -116 -73 0 -68 57 -180 0 157 -211 -232 0 -160 -99 -55 0 35 243 139 0 -67 -36 -222 0 127 -66 15 0 -211 -100 81 0 -74 -89 126 0 -20 31 159 0 211 97 -237 0 227 160 210 0 -243 -202 -161 0 -8 -219 62 0 157 225 -131 0 -79 -38 65 0 -156 -41 219 0 181 -95 -209 0 123 -193 207 0 -165 -121 -187 0 58 -69 164 0 133 153 -86 0 150 -22 72 0 -3 8 -239 0 -16 -182 -7 0 -79 219 65 0 37 223 215 0 -100 224 -77 0 -218 85 -220 0 93 59 168 0 -96 89 -47 0 -222 -118 -111 0 241 89 -158 0 -29 -229 74 0 -236 -28 -176 0 232 -223 25 0 167 103 -223 0 212 29 50 0 -109 125 -38 0 -49 109 -250 0 -161 244 -99 0 41 -189 83 0 224 177 -150 0 83 154 -120 0 93 134 101 0 220 -19 -106 0 -49 245 66 0 -2 231 207 0 146 -86 -157 0 8 -49 -125 0 220 -111 -2 0 203 227 -201 0 49 109 -187 0 -9 -156 -26 0 156 177 -30 0 14 5 -32 0 -221 33 187 0 128 101 -153 0 5 115 -67 0 11 -185 -164 0 -212 -228 203 0 -203 -214 66 0 183 11 218 0 247 80 -188 0 -51 -120 152 0 163 121 94 0 -148 -67 -50 0 81 -139 194 0 122 221 -99 0 -27 85 -78 0 -44 211 -108 0 -224 -39 -23 0 18 133 -150 0 -188 77 -18 0 -24 -196 53 0 -244 205 147 0 -104 -4 -157 0 -82 208 -33 0 -150 -211 216 0 -143 -231 22 0 -234 176 221 0 -249 160 -4 0 56 118 -153 0 -227 -249 137 0 18 58 76 0 55 236 -182 0 26 114 -147 0 -224 -75 -51 0 -52 140 148 0 33 -100 122 0 114 -194 -109 0 -33 245 88 0 -113 247 182 0 -63 174 235 0 -24 145 166 0 -19 -116 -248 0 -101 194 -211 0 -45 -236 231 0 71 -211 -27 0 121 195 33 0 91 -109 150 0 55 86 -5 0 -249 68 114 0 83 -113 -224 0 247 38 -89 0 -63 165 38 0 23 58 -108 0 155 72 -198 0 30 -134 104 0 -148 238 -58 0 -165 79 47 0 224 63 -107 0 -212 244 2 0 150 41 -186 0 23 -191 -165 0 213 92 10 0 -59 18 -113 0 96 -61 -196 0 150 120 248 0 -12 -145 -232 0 107 -189 1 0 167 -7 -62 0 -232 18 -221 0 -135 14 -60 0 -173 110 77 0 64 143 -242 0 -108 -176 196 0 80 187 -135 0 241 -248 12 0 232 -53 219 0 112 21 -147 0 -41 193 82 0 -104 -192 223 0 -236 232 -84 0 107 14 -16 0 -80 183 244 0 -133 76 -21 0 -1 -166 74 0 -59 -139 47 0 106 243 -91 0 95 -149 -96 0 -167 -8 207 0 -70 -223 184 0 209 -4 225 0 -83 3 -104 0 -86 218 -70 0 63 -222 -203 0 -38 229 147 0 57 80 -96 0 -142 167 244 0 -167 176 -86 0 -181 -170 113 0 -58 -226 250 0 -46 3 202 0 -133 -176 203 0 244 86 218 0 221 -3 -246 0 105 -193 160 0 -226 193 -109 0 -213 216 -68 0 -207 -85 -74 0 -250 -134 147 0 179 -72 225 0 194 151 -64 0 195 -40 -112 0 249 161 -38 0 217 86 -114 0 9 124 -120 0 41 200 -55 0 37 67 64 0 47 182 55 0 -86 -103 -91 0 -94 192 17 0 204 -79 16 0 210 207 -110 0 39 -52 63 0 -99 -6 182 0 83 48 56 0 -165 -214 172 0 222 -181 49 0 212 242 105 0 104 183 -30 0 -150 204 32 0 -50 -217 -5 0 63 -118 157 0 164 -160 -101 0 43 37 -117 0 56 -222 -192 0 -128 -226 210 0 109 9 -223 0 -124 34 81 0 159 -94 51 0 -14 222 94 0 -199 186 -246 0 -14 -176 201 0 -157 64 -104 0 1 -196 -249 0 173 -234 -135 0 36 137 -128 0 -223 58 122 0 243 -199 -125 0 -1 -186 107 0 163 -159 -61 0 240 -209 -135 0 -64 -212 236 0 246 -127 -227 0 -32 -224 98 0 215 -136 -242 0 -23 174 125 0 86 105 -75 0 99 -34 86 0 237 185 32 0 -168 37 140 0 -74 -84 246 0 -62 -76 -141 0 -96 245 213 0 239 -58 51 0 -24 -59 -160 0 -237 -41 54 0 -19 -50 120 0 89 -31 12 0 -168 -199 139 0 92 -242 89 0 221 142 96 0 -69 143 133 0 -205 -141 100 0 -48 110 -9 0 216 -170 220 0 -109 -140 -17 0 -38 -233 32 0 239 126 -225 0 -148 139 27 0 135 40 -25 0 -229 -202 -84 0 -134 193 -230 0 -249 -101 26 0 -140 -86 5 0 21 -127 -119 0 239 -208 164 0 71 86 11 0 -201 -250 -224 0 94 113 -249 0 177 50 118 0 -7 -84 56 0 -239 227 93 0 122 -228 94 0 8 -48 -179 0 247 73 7 0 211 125 184 0 23 33 66 0 209 -154 70 0 118 7 -71 0 4 9 -39 0 111 82 31 0 -133 -120 -109 0 41 -194 44 0 -134 -113 50 0 -171 -163 -70 0 -116 32 23 0 222 -209 -239 0 220 198 -167 0 57 -192 -24 0 -167 68 95 0 -176 142 48 0 68 10 -81 0 -29 59 -185 0 -106 -129 184 0 -62 -151 -148 0 -172 62 66 0 -92 -77 -113 0 160 162 11 0 88 213 -212 0 -102 153 -95 0 -173 -230 43 0 -92 -40 129 0 -157 -123 172 0 -250 54 240 0 156 -176 235 0 232 97 -23 0 -75 -177 -11 0 -214 -38 95 0 -163 -204 -116 0 -234 70 116 0 11 52 -137 0 4 150 149 0 238 -66 68 0 144 113 -226 0 199 -249 210 0 -105 228 -82 0 213 15 -56 0 -142 17 -223 0 -230 243 36 0 95 201 50 0 -84 120 245 0 70 -74 20 0 32 76 -66 0 -178 -14 -157 0 14 64 110 0 81 200 244 0 80 184 178 0 -149 91 -187 0 129 -23 -235 0 195 249 -17 0 -178 -142 -84 0 -26 -47 -131 0 64 45 29 0 -222 -138 -146 0 -159 -118 -130 0 -59 99 83 0 -89 -45 -96 0 -159 61 37 0 -61 -33 184 0 -87 147 -225 0 -157 131 244 0 -39 112 59 0 -127 -240 -117 0 -209 -237 -211 0 -249 -29 -95 0 119 -152 48 0 -127 180 183 0 241 191 -3 0 -36 -62 166 0 194 -202 -137 0 206 -44 -33 0 75 -33 -104 0 21 208 -109 0 203 100 -23 0 226 39 177 0 245 33 146 0 201 111 84 0 223 -139 9 0 43 -115 173 0 -40 209 -57 0 -69 66 -167 0 15 44 -64 0 -150 -71 -70 0 -231 194 96 0 -171 49 37 0 111 83 174 0 231 -61 -60 0 227 -58 -229 0 28 196 74 0 -202 -19 -131 0 96 -106 228 0 35 -115 -85 0 -2 -136 51 0 -110 209 210 0 -16 33 77 0 42 -117 177 0 -43 170 175 0 -120 -16 -219 0 -185 106 -109 0 203 -56 -223 0 100 243 -110 0 86 -55 -165 0 94 -51 -195 0 211 154 47 0 -10 1 141 0 226 -128 85 0 -81 -240 150 0 174 -93 131 0 58 -115 5 0 191 -194 53 0 174 -151 209 0 104 66 101 0 -84 234 193 0 66 160 -212 0 -122 204 89 0 -6 -208 -164 0 -75 69 216 0 -26 156 10 0 65 186 20 0 172 127 237 0 -24 56 -229 0 88 94 12 0 79 45 240 0 -199 77 46 0 -1 25 -177 0 -8 -58 34 0 -179 -83 -146 0 24 214 -209 0 -123 -250 -48 0 -218 -219 -175 0 -122 -213 17 0 207 128 -96 0 -17 92 -195 0 -198 -228 -247 0 % 0 shedskin-0.9.4/tests/testdata/bla20000664000175000017500000000000712157270661016774 0ustar srepmubsrepmubhuhuhu shedskin-0.9.4/tests/128.py0000664000175000017500000000077612157270661015321 0ustar srepmubsrepmub def main(): ws = open("testdata/hoppa","r").read().split() d = {} for i, w in enumerate(ws): s = "".join(sorted(list(w.lower()))) d.setdefault(s, []).append(i) for l in d.values(): if len(l) > 1: print [ws[i] for i in l] main() def subsets(sequence): result = [[]] * (2**len(sequence)) for i,e in enumerate(sequence): i2, el = 2**i, [e] for j in xrange(i2): result[j+i2] = result[j] + el return result print subsets(range(4)) shedskin-0.9.4/tests/run.py0000775000175000017500000001510212157270661015603 0ustar srepmubsrepmub#!/usr/bin/env python import traceback import sys import os import time import subprocess import glob import os.path import functools import itertools from difflib import unified_diff from multiprocessing import Pool from multiprocessing.pool import IMapIterator #Fix for multiprocessing. Without this, Ctrl+C will not kill the process immediately set_timeout_decorator = lambda func: lambda self: func(self, timeout=1e100) IMapIterator.next = set_timeout_decorator(IMapIterator.next) if os.path.exists('../shedskin/__init__.py'): SS = '../shedskin/__init__.py' else: SS = '../../shedskin/__init__.py' def usage(): print "'-l': give individual test numbers" print "'-r': reverse test order" print "'-f': break after first failure" print "'-e': run extension module tests" print "'-n': normal tests as extension modules" print "'-x': run error/warning message tests" print "'-p': run the tests in parallel" sys.exit() def parse_options(): args, options = [], set() for arg in sys.argv[1:]: if arg.startswith('-'): options.update(arg[1:]) else: args.append(int(arg)) return args, options def test_numbers(args, options): if 'l' in options: tests = args elif len(args) == 1: tests = [args[0]] elif len(args) == 2: if args[0] > args[1]: args[0], args[1] = args[1], args[0] options.add('r') tests = range(args[0], args[1]) else: tests = sorted([int(os.path.splitext(f)[0]) for f in glob.glob('*.py') if f != 'run.py']) if 'r' in options: tests.reverse() return tests def tests(args, options): parallel = 'p' in options msvc = 'v' in options partial_run_test = functools.partial(run_test, msvc=msvc, options=options) tests = [test for test in test_numbers(args, options) if os.path.exists('%d.py' % test)] failures = [] imap = Pool().imap if parallel else itertools.imap for result in imap(partial_run_test, tests): if result is not None: failures.append(result) if 'f' in options: break return failures def main(): args, options = parse_options() if 'h' in options: usage() if 'e' in options: failures = extmod_tests(args, options) elif 'x'in options: failures = error_tests(args, options) else: failures = tests(args, options) if not failures: print '*** no failures, yay!' else: print '*** tests failed:', len(failures) print failures def run_test(test, msvc, options): parallel = 'p' in options show_output = not parallel print '*** test:', test with open(os.devnull, "w") as fnull: if show_output: fnull = None execute = functools.partial(subprocess.call, stdout=fnull, stderr=fnull, shell=True) t0 = time.time() try: if msvc: assert execute('python %s -v %d' % (SS, test)) == 0 elif 'n' in options: assert execute('python %s -e -m Makefile.%d %d' % (SS, test, test)) == 0 else: assert execute('python %s -m Makefile.%d %d' % (SS, test, test)) == 0 if msvc: assert execute('nmake /C /S clean') == 0 assert execute('nmake /C /S') == 0 command = '.\\%d' % test else: assert execute('make clean -f Makefile.%d' % test) == 0 assert execute('make -f Makefile.%d' % test) == 0 if sys.platform == 'win32': command = '%d' % test else: command = './%d' % test if 'n' in options: if test not in [136, 154, 163, 191, 196, 197, 198]: # sys.exit assert execute('python -c "__import__(str(%d))"' % test) == 0 else: check_output(command, 'python %d.py' % test) print '*** success: %d (%.2f)' % (test, time.time() - t0) except AssertionError: print '*** failure:', test traceback.print_exc() return test def extmod_tests(args, options): failures = [] tests = sorted([int(t[1:]) for t in glob.glob('e*') if t[1:].isdigit()]) for test in tests: print '*** test:', test os.chdir('e%d' % test) try: extmod = file('main.py').next()[1:].strip() assert os.system('python ../%s -e %s' % (SS, extmod)) == 0 assert os.system('make') == 0 native_output = get_output('python main.py') if sys.platform == 'win32': ext = '.pyd' else: ext = '.so' assert os.system('rm %s' % (extmod + ext)) == 0 cpython_output = get_output('python main.py') if native_output != cpython_output: print 'diff:' print generate_diff(native_output, cpython_output) raise AssertionError print '*** success:', test except AssertionError: print '*** failure:', test failures.append(test) os.chdir('..') return failures def error_tests(args, options): failures = [] os.chdir('errs') tests = sorted([int(t[:-3]) for t in glob.glob('*.py') if t[:-3].isdigit()]) for test in tests: print '*** test:', test try: checks = [] for line in file('%d.py' % test): if line.startswith('#*'): checks.append(line[1:].strip()) output = get_output('python ../%s %d 2>&1' % (SS, test)) assert not [l for l in output if 'Traceback' in l] for check in checks: print check assert [l for l in output if l.startswith(check)] print '*** success:', test except AssertionError: print '*** failure:', test failures.append(test) os.chdir('..') return failures def get_output(command): com = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE).stdout output = com.readlines() com.close() return output def check_output(command, command2): native_output = get_output(command) cpython_output = get_output(command2) if native_output != cpython_output: print 'diff:' print generate_diff(native_output, cpython_output) raise AssertionError def generate_diff(native_output, cpython_output): return ''.join(unified_diff(native_output, cpython_output, lineterm="\n")) if __name__ == '__main__': main() shedskin-0.9.4/tests/78.py0000664000175000017500000000024712157270661015236 0ustar srepmubsrepmub cnf = ['8'] # [list(str)] for x in cnf: # [list(str)] d = int(x) # [int] shedskin-0.9.4/tests/119.py0000664000175000017500000000051512157270661015310 0ustar srepmubsrepmub a = [1,2,3,4,5] # [list(int)] print a[:-1] # [list(int)] print a[1:3] # [list(int)] print a[::] # [list(int)] print a[:3:] # [list(int)] print a[::-1] # [list(int)] shedskin-0.9.4/tests/24.py0000664000175000017500000000023312157270661015220 0ustar srepmubsrepmub class integer: pass class fred: def __add__(self, x): i = integer() return i def hoei(): a = fred() return a+a a = hoei() shedskin-0.9.4/tests/154.py0000664000175000017500000002116312157270661015311 0ustar srepmubsrepmub # (c) Peter Goodspeed # --- coriolinus@gmail.com from math import ceil from time import time import sys class bmp(object): def __init__(self, vals=9*[True], n=-1): self.v = vals[0:9] if n>=0: self.v[n] = not self.v[n] def __and__(self, other): return bmp([self.v[i] and other.v[i] for i in xrange(9)]) def cnt(self): return len([i for i in self.v if i]) class boardRep(object): def __init__(self, board): self.__fields = list(board.final) def fields(self): return self.__fields def __eq__(self, other): return self.__fields==other.fields() def __ne__(self, other): return self.__fields!=other.fields() def __hash__(self): rep="" for i in xrange(9): for j in xrange(9): rep += str(self.__fields[i][j]) return hash(rep) class board(object): notifyOnCompletion = True #let the user know when you're done computing a game completeSearch = False #search past the first solution def __init__(self): #final numbers: a 9 by 9 grid self.final = [9 * [0] for i in xrange(9)] self.rows = 9 * [bmp()] self.cols = 9 * [bmp()] self.cels = [3 * [bmp()] for i in xrange(3)] #statistics self.__turns = 0 self.__backtracks = 0 self.__starttime = 0 self.__endtime = 0 self.__status = 0 self.__maxdepth = 0 self.__openspaces = 81 #a set of all solved boards discovered so far self.solutions = set() #a set of all boards examined--should help reduce the amount of search duplication self.examined = set() def fread(self,fn=''): #self.__init__() if fn=='': fn = raw_input("filename: ") f = file(fn, 'r') lines = f.readlines() for row in xrange(9): for digit in xrange(1,10): try: self.setval(row,lines[row].index(str(digit)),digit) except ValueError: pass f.close() def setval(self, row, col, val): #add the number to the grid self.final[row][col] = val self.__openspaces -= 1 #remove the number from the potential masks mask = bmp(n = val - 1) #rows and cols self.rows[row] = self.rows[row] & mask self.cols[col] = self.cols[col] & mask #cels cr = self.cell(row) cc = self.cell(col) self.cels[cr][cc] = self.cels[cr][cc] & mask def cell(self, num): return int(ceil((num + 1) / 3.0)) - 1 def __str__(self): ret = "" for row in xrange(9): if row == 3 or row == 6: ret += (((3 * "---") + "+") * 3)[:-1] + "\n" for col in xrange(9): if col == 3 or col == 6: ret += "|" if self.final[row][col]: c = str(self.final[row][col]) else: c = " " ret += " "+c+" " ret += "\n" return ret def solve(self, notify=True, completeSearch=False): if self.__status == 0: self.__status = 1 self.__starttime = time() board.notifyOnCompletion = notify board.completeSearch = completeSearch self.__solve(self, 0) def openspaces(self): return self.__openspaces def __solve(self, _board, depth): global bekos bekos += 1 if bekos == 5000: self.onexit() sys.exit() if boardRep(_board) not in self.examined: self.examined.add(boardRep(_board)) #check for solution condition: if _board.openspaces() <= 0: self.solutions.add(boardRep(_board)) print 'sol', _board if depth == 0: self.onexit() if not board.completeSearch: self.onexit() else: #update the statistics self.__turns += 1 if depth > self.__maxdepth: self.__maxdepth = depth #figure out the mincount mincnt, coords = _board.findmincounts() if mincnt <= 0: self.__backtracks += 1 if depth == 0: self.onexit() else: #coords is a list of tuples of coordinates with equal, minimal counts # of possible values. Try each of them in turn. for row, col in coords: #now we iterate through possible values to put in there broken = False for val in [i for i in xrange(9) if _board.mergemask(row, col).v[i] == True]: if not board.completeSearch and self.__status == 2: broken = True break val += 1 t = _board.clone() t.setval(row, col, val) self.__solve(t, depth + 1) #if we broke out of the previous loop, we also want to break out of # this one. unfortunately, "break 2" seems to be invalid syntax. if broken: break #else: didntBreak = True #if not didntBreak: break def clone(self): ret = board() for row in xrange(9): for col in xrange(9): if self.final[row][col]: ret.setval(row, col, self.final[row][col]) return ret def mergemask(self, row, col): return self.rows[row] & self.cols[col] & self.cels[self.cell(row)][self.cell(col)] def findmincounts(self): #compute the list of lenghths of merged masks masks = [] for row in xrange(9): for col in xrange(9): if self.final[row][col] == 0: numallowed = self.mergemask(row, col).cnt() masks.append((numallowed, row, col)) #return the minimum number of allowed moves, and a list of cells which are # not currently occupied and which have that number of allowed moves return min(masks)[0], [(i[1],i[2]) for i in masks if i[0] == min(masks)[0]] def onexit(self): self.__endtime = time() self.__status = 2 if board.notifyOnCompletion: print self.stats()['turns'] def stats(self): if self.__status == 1: t = time() - self.__starttime else: t = self.__endtime - self.__starttime return {'max depth' : self.__maxdepth, 'turns' : self.__turns, 'backtracks' : self.__backtracks, 'elapsed time' : int(t), 'boards examined': len(self.examined), 'number of solutions' : len(self.solutions)} bekos = 0 puzzle = board() puzzle.fread('testdata/b6.pz') #print puzzle puzzle.solve() shedskin-0.9.4/tests/174.py0000664000175000017500000000554412157270661015320 0ustar srepmubsrepmub # iter.__len__ print len(xrange(10)) # try.. else try: print 'probeer' except Exception: pass else: print 'geen exceptie..' # collections from collections import deque d = deque([3,2,1]) d.append(4) d.appendleft(0) print len(d) for i in range(len(d)): print d[i], print print d.pop(), d.popleft() print d while d: print d d.pop() d = deque([3,2,1]) e = iter(d) print [x for x in e] d.extend(set([4,5])) print d d.extendleft(set([6,7])) print d print sorted(d), [e for e in reversed(d)] d[2] = d[-2] = 4 print d print [0,1][4 in d], [0,1][9 in d] #d.remove(1) # python 2.5 #print d d.rotate(3) print d d.rotate(-2) print d d = deque() print d d.rotate(1) # no error print d d.clear() print d d.extend(xrange(10)) del d[-4] print d print [e for e in reversed(deque(xrange(10)))] # bisect from bisect import * def blah(s, e): print bisect_left(s, e) print bisect_left(s, e, 0) print bisect_left(s, e, 0, len(s)) print bisect_right(s, e) print bisect(s, e) insort_left(s, e) insort_right(s, e) insort(s, e) print s blah([1,2,3,4,5,6,6,7], 4) #blah(['1','2','3','4','5','6','7'], '4') # copy import copy kb = [1,2] ka = copy.copy(kb) ka.append(3) print ka, kb, copy.copy(178) print copy.copy((1, 2)), copy.deepcopy((2, 3)) print copy.copy('1234'), copy.deepcopy('1234') print copy.copy((1, '1')), copy.deepcopy((1, '1')) print sorted(copy.copy(set([1, 2]))), sorted(copy.deepcopy(set([1, 2]))) print copy.copy({1 : 1.0}), copy.deepcopy({1.0 : 1}) print copy.copy(deque(range(10))), copy.deepcopy(deque(xrange(10))) kc = [1,2] kd = (kc,) ke = copy.deepcopy(kd) ke[0][0] = 3 print kd, ke rll = [1, 2] bll = [rll, rll] cll = copy.deepcopy(bll) cll[0].append(3) print cll class bert: pass abert = bert() abert.a = 7.0 cbert = bert() cbert.a = 1.0 print abert.a, cbert.a copy.copy(abert) copy.deepcopy(abert) class dert: pass adert = dert() adert.a = [1,2] bdert = copy.copy(adert) bdert = copy.deepcopy(adert) bdert.a.append(3) # reversed(xrange) import random random.seed(1) for z in range(1000): l,u,s = random.randrange(-5,5), random.randrange(-5,5), random.randrange(-5,5) print l, u, s try: x = xrange(l,u,s) y = reversed(xrange(l,u,s)) xl = [e for e in x] yl = [e for e in y] print xl, yl, [0, 1][xl == list(reversed(yl))] except ValueError, v: print v # for _ in (x)range total = 0 for _ in range(10): for _ in range(10): total += 1 print total print [0 for _ in range(10)] # remove ifa_empty_constructors fromage = [] def non_internal(ptree): noni = [c for c in ptree] ptree = [1,2] row_pointers = [None, ptree] non_internal(row_pointers[1]) # string.maketrans import string si = 'abcde' t1 = string.maketrans('abc', 'xyz') print si.translate(t1) # optimize dict[..] += .. dd = {} dd['hoi'] = 0 dd['hoi'] += 10 print dd['hoi'] shedskin-0.9.4/tests/188.py0000664000175000017500000000322512157270661015317 0ustar srepmubsrepmub import csv, collections d = collections.defaultdict(list) for (a,b,n,l) in csv.reader(open('testdata/woef.csv'), delimiter='|'): d[a,b].append((int(n),l)) for a,b in sorted(d, key=lambda t: t[1]): hoppa = ' '.join([l for (n,l) in sorted(d[a,b], key=lambda t: t[0])]) hoppa = hoppa.replace(' ', ' ') print '

%s

%s

' % (b,a), hoppa output = open('testdata/bla.csv', 'w') wr = csv.writer(output, delimiter='|') wr.writerow(['aa', 'bb', 'cc']) wr.writerows(2*[['a', 'c', 'b']]) output.close() print open('testdata/bla.csv').read() print csv.field_size_limit() print csv.field_size_limit(1000) print csv.field_size_limit() print sorted(csv.list_dialects()) csv.reader(open('testdata/woef.csv'), dialect = 'excel', delimiter = ',', quotechar = '"', lineterminator = '\r\n', escapechar = '') csv.writer(file('testdata/bla.csv', 'w'), dialect = 'excel', delimiter = ',', quotechar = '"', lineterminator = '\r\n', escapechar = '') bla = file('testdata/bla.csv', 'w') fieldnames = ['hop', 'hap', 'ole', 'aap'] wr2 = csv.DictWriter(bla, fieldnames, restval='ah', quoting=csv.QUOTE_ALL) rd = csv.DictReader(open('testdata/woef.csv'), fieldnames, restval='uh', restkey='oh', delimiter='|') for d2 in rd: print sorted(d2.values()) wr2.writerow(d2) wr2.writerows([d2]) bla.close() rd.fieldnames = fieldnames print rd.fieldnames print open('testdata/bla.csv').read() csv.DictReader(open('testdata/woef.csv'), None, dialect = 'excel', delimiter = ',', quotechar = '"', lineterminator = '', escapechar = '') csv.DictWriter(open('testdata/woef.csv'), None, dialect = 'excel', delimiter = ',', quotechar = '"', lineterminator = '', escapechar = '') shedskin-0.9.4/tests/e3/0000775000175000017500000000000012157270661014732 5ustar srepmubsrepmubshedskin-0.9.4/tests/e3/blah.py0000664000175000017500000000010212157270661016203 0ustar srepmubsrepmubdef woef(i): return i if __name__ == '__main__': woef(7) shedskin-0.9.4/tests/e3/main.py0000664000175000017500000000007412157270661016231 0ustar srepmubsrepmub#blah import blah print blah.woef(18) print blah.woef(18L) shedskin-0.9.4/tests/82.py0000664000175000017500000000025512157270661015230 0ustar srepmubsrepmub vars = [1] # [list(int)] def bla(): return [var for var in vars] # [] a = bla() # [] shedskin-0.9.4/tests/127.py0000664000175000017500000000264512157270661015315 0ustar srepmubsrepmub print 'hello, world!' # [str] l = 'luis gonzales' # [str] print l[3:7] # [str] print l[1::2] # [str] t = (1,2,3,4,5) # [tuple(int)] print t[1:4] # [tuple(int)] s = 'we are testing shedskin on windows' # [str] d = {} # [dict(str, int)] for i in s: # [str] if not i in d: # [] d[i]= 1 # [int] else: d[i]= d[i] + 1 # [int] for k,v in d.items(): # [tuple(str, int)] if k == ' ': print k, ':', v # [str], [str], [int] x=[] # [list(dude)] class dude: # age: [int], last: [str], name: [str] def __init__(self, name, last , age): # self: [dude], name: [str]*, last: [str]*, age: [int]* self.name = name # [str] self.last = last # [str] self.age = age # [int] x.append(self) # [] def __repr__(self): # self: [dude] return '%s %s is %s years old' %(self.name, self.last, str(self.age)) # [str] dude('luis','gonzalez',35) # [dude] print x[0] # [dude] shedskin-0.9.4/tests/200.py0000664000175000017500000000310612157270661015276 0ustar srepmubsrepmub#select.select import os import select f = os.popen("ls", "r") rFDs, wFDs, xFDs = select.select((), [], set(), 0) rFDs, wFDs, xFDs = select.select([f.fileno()], set(), [], 20) print len(rFDs), len(wFDs), len(xFDs) #time crashes without args import time print time.asctime()[:10] print str(time.localtime())[:50] #null char print 'hello\0world' print repr('hello\0world') print repr('woef%swaf' % 'waaa\0wa') print repr('woef%swaf%s!' % ('waaa\0wa\0wa', '\0haaap')) #inheritance, generator expression import array class Common(object): CostScale = 7 #StateTable = FsmGenerator().stateTable def __init__(self, inputStream, outputStream, options): lzpLowCount = 10 self.lzpLow = array.array("H", (0xffb5 for _ in xrange(lzpLowCount))) class Decoder(Common): def __init__(self, inputStream, outputStream, options): Common.__init__(self, inputStream, outputStream, options) d = Decoder(None, None, None) print d.lzpLow #static code class moeha: x = 4 print 'joeh' l = [] for i in range(10): l.append(x) l.append(i) y = [[1.0]] def mwa(self): pass @property def ole(self): return 7 print moeha.x print moeha.l print moeha.y #add case from issue 125, plus a few more tests class MyClass(object): A, B, C = range(3) x, y = 9, (10, 11) z = zz = B*x t, t2 = v, v2 = y print MyClass.A, MyClass.B, MyClass.C, MyClass.x, MyClass.y, MyClass.z, MyClass.zz, MyClass.t, MyClass.t2, MyClass.v, MyClass.v2 #unused default func def bla(hop=cmp): print 'ole', hop(9,10) bla(lambda a,b:a+b) shedskin-0.9.4/tests/165.py0000664000175000017500000000175712157270661015322 0ustar srepmubsrepmub print 1, 2, '3', '%.2f' % 4.1 print '%04x' % 0xfeda # '..' % (..) print '%d %x %d' % (10, 11, 12) print '%d %s' % (1, 'een') print '%d %s %.2f' % (1, 'een', 8.1) # '..' % tuple t = (10, 11, 12) print '%x %d %x' % t t2 = ('twee', 2) print '%s %04x' % t2 # mod a = '%04x' % 0xdefa print a, a, '%02x' % 0x1234 # all chars print '%o' % 10 print "%.4s %.4r\n" % ("abcdefg", "\0hoplakee") # print to file f = file('testdata/binf', 'w') print >>f, 'ik haat %04x\n' % 0xfeda, 'smurven..\n' f.close() # conversions print repr('?%% %c?' % 70), repr('?%c?%%' % 0), '%c' % 'X' print '!%s!' % [1,2,3] print '%.2f %d %.2f %d' % (4, 4.4, 5.5, 5) print '%s.' % 1, '%s.' % (1,) # %s, %r print repr(18), repr('x') print 'aha %s %r' % (18, 19) # class file f = file('testdata/hopsakee') print 1, f.readline(), print f.readline(5) print f.readline(), f.close() print 2, file('testdata/hopsakee').read() print 3, file('testdata/hopsakee').readlines() for line in file('testdata/hopsakee'): print 'aha', line, shedskin-0.9.4/tests/111.py0000664000175000017500000000143012157270661015275 0ustar srepmubsrepmub a = (1,2) # [tuple(int)] b = (1,2,3) # [tuple(int)] c = a # [tuple(int)] c = b # [tuple(int)] d = a+b # [tuple(int)] print d # [tuple(int)] def bla(x): # x: [A] pass bla(a) # [] bla(b) # [] bla([1,2,3]) # [] dc = {} # [dict(tuple(int), int)] dc[a] = 2 # [int] dc[b] = 3 # [int] print a, dc[a], b, dc[b] # [tuple(int)], [int], [tuple(int)], [int] shedskin-0.9.4/tests/89.py0000664000175000017500000000052612157270661015240 0ustar srepmubsrepmub f = {} # [dict(float, int)] f[1.0] = 1 # [int] g = {} # [dict(int, float)] g[1] = 1.0 # [float] e = {} # [dict(int, float)] e[4] = 1.0 # [float] shedskin-0.9.4/tests/59.py0000664000175000017500000000012312157270661015226 0ustar srepmubsrepmub print 'hoi %d %s' % (2, '3') # [str] print 'foo\0bar' != 'foo\0baz' shedskin-0.9.4/tests/0.py0000664000175000017500000000001012157270661015123 0ustar srepmubsrepmub a = 1 shedskin-0.9.4/tests/64.py0000664000175000017500000000055612157270661015234 0ustar srepmubsrepmub def dupl(y): # y: [list(int)] k = [] # [list(float)] k.append(1.0) # [] a = [] # [list(int)] a = [] # [list(int)] a.append(1) # [] dupl(a) # [] shedskin-0.9.4/tests/173.py0000664000175000017500000000702012157270661015306 0ustar srepmubsrepmub # --- division revisited print -496 // 3, 496 // 3, -496 // -3, 496 // -3 print -496.0 // 3.0, 496.0 // 3.0, -496.0 // -3.0, 496.0 // -3.0 print -496.0 // 3, 496 // 3.0, -496.0 // -3, 496 // -3.0 print -496 / 3, 496 / 3, -496 / -3, 496 / -3 print '%g' % (-496.0 / 3.0), '%g' % (496.0 / 3.0), '%g' % (-496.0 / -3.0), '%g' % (496.0 / -3.0) # XXX no '%g' print '%g' % (-496.0 / 3), '%g' % (496 / 3.0), '%g' % (-496.0 / -3), '%g' % (496 / -3.0) xx, yy, zz = divmod(-496, 3), divmod(-496.0, 3), divmod(-496, 3.0) print xx, yy, zz print divmod(-496, 3), divmod(496, 3), divmod(-496, -3), divmod(496,-3) print divmod(-496.0, 3.0), divmod(496.0, 3.0), divmod(-496.0, -3.0), divmod(496.0,-3.0) print divmod(-496.0, 3), divmod(496, 3.0), divmod(-496.0, -3), divmod(496,-3.0) # --- don't crash print [0]*-4, (0,)*-4, repr('0'*-4) # --- list.extend takes iterable w = [1,2] w.extend(set([3])) print w # --- use %.12g to print floats print 1/3.0, 1.1234123412341234, 1.1, 8.0 #print 9.12341234e20 # XXX difference on win, e020? # --- slice assignment (random test) import random random.seed(10) for x in range(1000): l,u,s = random.randrange(-5,5), random.randrange(-5,5), random.randrange(-5,5) a = range(5) print a, 'lower', l, 'upper', u, 'step', s try: z = range(random.randrange(0,5)) print 'xrange', z a[l:u:s] = z print 'done', a except ValueError, v: print v ax = range(10) ax[-2:-3] = [0,1] print ax # --- do not print space after 14 print 14, print print 'boe' # --- aug assignment revisited class hoepa: def __init__(self): self.elems = [1,2,3] self.smurf = 1 def __getitem__(self, index): print 'get', index return self.elems[index] def __setitem__(self, index, elem): print 'set', index, elem self.elems[index] = elem uh = hoepa() uh[2] = 3 print uh[2] uh[2] += 4 print uh.elems ux = 1 ux += 1 print ux uy = [1] uy += [2] print uy uh.smurf += 1 print uh.smurf blah = [1,2,4] blah[2] += 5 print blah ud = {'7': 7} print ud['7'] ud['7'] = 8 ud['7'] += 1 print ud class hoepa2: def __init__(self): self.hop = {} def __getitem__(self, index): print 'get', index return self.hop[index] def __setitem__(self, index, elem): print 'set', index, elem self.hop[index] = elem def __delitem__(self, index): del self.hop[index] yh = hoepa2() yh[1,2] = 10 yh[1,2] += 10 print yh[1,2] # --- __delitem__ print yh.hop del yh[1,2] print yh.hop yx = [1,2,3] del yx[1] print yx # --- some string tests import string print string.join(['a','b']) print string.join(['a','b'], '_') print string.find('abc', 'b') print string.find('abc', 'b', 0) print string.find('abc', 'b', 0, 3) print string.split('a b c') print string.split('a b c', ' ') print string.split('a b c', ' ', 1) print string.replace('abc', 'c', 'd') print string.replace('abc', 'c', 'd', 1) print string.count('abc', 'b') print string.count('abc', 'b', 0) print string.count('abc', 'b', 0, 3) print string.expandtabs('abc') print string.expandtabs('abc', 4) print string.strip(' abc ') print string.strip('xabcx', 'x') print string.ljust('abc', 8) print string.ljust('abc', 8, '_') print string.rsplit('a b c', ' ', 1) # --- recursive generator test def A003714(): yield 1 for x in A003714(): yield 2*x if not (x & 1): yield 2*x+1 hop = A003714() for x in range(20): print hop.next(), print # --- allow 'self' as formal argument in non-method function def blahx(self, x): print self, x blahx(18, 19) shedskin-0.9.4/tests/199.py0000664000175000017500000000404412157270661015321 0ustar srepmubsrepmub# skipped Discard node inside generator def streams(archive): archive.seek(4) # Skip magic number print 'zoek', archive.tell() stream = '' yield stream def decompressor(archive): for stream in streams(archive): pass decompressor(open('testdata/binas', 'rb')) # set.__ior__ etc. model class waf: def __init__(self, value): self.value = value def __iand__(self, b): return waf(self.value + b.value) def __isub__(self, b): return waf(self.value - b.value) wa = waf(4) wa &= waf(9) wa -= waf(2) print wa.value set1 = set() set1 |= set([2,3]) print set1 set2 = set() set2 &= set([2,3]) print set2 set3 = set() set3 ^= set([2,3]) print set3 set4 = set() set4 -= set([2,3]) print set4 # overflow in pow, use long long internally print pow(290797,2,50515093) # float.is_integer print 7.7.is_integer() print 7.0.is_integer() a = 3.14 print a.is_integer(), (a+a).is_integer() print 2*a.is_integer() # model __eq__ from __contains__ class Point: def __init__(self, x, y): self.x, self.y = x, y def __eq__(self, other): return (self.x, self.y) == (other.x, other.y) def __str__(self): return 'Point(%s, %s)' % (self.x, self.y) def wof(): a = b = c = d = Point(7,8) p = Point(7,8) return p if p not in (a, b, c, d) else None print wof() # context of inherited method import testdata.CCMView class GameView(testdata.CCMView.CCMView): pass gv = GameView() gv.wa() # optimized array slicing, test import array arr = array.array('B') arr.extend(range(20)) print arr print arr[:] print arr[-7:] print arr[-7::2] print arr[:8:3] print arr[15:1:-2] #os.popen2 improvement import os child_stdin, child_stdout = os.popen2(["echo", "a text"], "r") print repr(child_stdout.read()) child_stdin, child_stdout = os.popen2(iter(["echo", "a text"]), "r") print repr(child_stdout.read()) child_stdin, child_stdout = os.popen2(("echo", "a text"), "r") print repr(child_stdout.read()) child_stdin, child_stdout = os.popen2("echo a text", "r") print repr(child_stdout.read()) shedskin-0.9.4/tests/159.py0000664000175000017500000001311212157270661015311 0ustar srepmubsrepmub # (c) Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013. # --- Translated from ADA to C by Rick Richardson. # --- Translated from C to Python by Guido van Rossum. from time import clock LOOPS = 50000 Ident1, Ident2, Ident3, Ident4, Ident5 = range(1,6) class Record: def __init__(self, PtrComp = None, Discr = 0, EnumComp = 0, IntComp = 0, StringComp = ''): # XXX '' should be None self.PtrComp = PtrComp self.Discr = Discr self.EnumComp = EnumComp self.IntComp = IntComp self.StringComp = StringComp def copy(self): return Record(self.PtrComp, self.Discr, self.EnumComp, self.IntComp, self.StringComp) def main(loops=LOOPS): benchtime, stones = pystones(loops) # print "Pystone(%s) time for %d passes = %g" % # (__version__, loops, benchtime) # print "This machine benchmarks at %g pystones/second" % stones def pystones(loops=LOOPS): return Proc0(loops) IntGlob = 0 BoolGlob = False Char1Glob = ' ' # ! Char2Glob = ' ' Array1Glob = [0]*51 #Array2Glob = map(lambda x: x[:], [Array1Glob]*51) Array2Glob = [x[:] for x in [Array1Glob]*51] PtrGlb = None PtrGlbNext = None def Proc0(loops=LOOPS): global IntGlob global BoolGlob global Char1Glob global Char2Glob global Array1Glob global Array2Glob global PtrGlb global PtrGlbNext starttime = clock() for i in range(loops): pass nulltime = clock() - starttime PtrGlbNext = Record() PtrGlb = Record() PtrGlb.PtrComp = PtrGlbNext PtrGlb.Discr = Ident1 PtrGlb.EnumComp = Ident3 PtrGlb.IntComp = 40 PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING" String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING" Array2Glob[8][7] = 10 starttime = clock() for i in range(loops): Proc5() Proc4() IntLoc1 = 2 IntLoc2 = 3 String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING" EnumLoc = Ident2 BoolGlob = not Func2(String1Loc, String2Loc) while IntLoc1 < IntLoc2: IntLoc3 = 5 * IntLoc1 - IntLoc2 IntLoc3 = Proc7(IntLoc1, IntLoc2) IntLoc1 = IntLoc1 + 1 Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3) PtrGlb = Proc1(PtrGlb) CharIndex = 'A' while CharIndex <= Char2Glob: if EnumLoc == Func1(CharIndex, 'C'): EnumLoc = Proc6(Ident1) CharIndex = chr(ord(CharIndex)+1) IntLoc3 = IntLoc2 * IntLoc1 IntLoc2 = IntLoc3 / IntLoc1 IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1 IntLoc1 = Proc2(IntLoc1) benchtime = clock() - starttime - nulltime return benchtime, (loops / benchtime) def Proc1(PtrParIn): PtrParIn.PtrComp = NextRecord = PtrGlb.copy() PtrParIn.IntComp = 5 NextRecord.IntComp = PtrParIn.IntComp NextRecord.PtrComp = PtrParIn.PtrComp NextRecord.PtrComp = Proc3(NextRecord.PtrComp) if NextRecord.Discr == Ident1: NextRecord.IntComp = 6 NextRecord.EnumComp = Proc6(PtrParIn.EnumComp) NextRecord.PtrComp = PtrGlb.PtrComp NextRecord.IntComp = Proc7(NextRecord.IntComp, 10) else: PtrParIn = NextRecord.copy() NextRecord.PtrComp = None return PtrParIn def Proc2(IntParIO): IntLoc = IntParIO + 10 while 1: if Char1Glob == 'A': IntLoc = IntLoc - 1 IntParIO = IntLoc - IntGlob EnumLoc = Ident1 if EnumLoc == Ident1: break return IntParIO def Proc3(PtrParOut): global IntGlob if PtrGlb is not None: PtrParOut = PtrGlb.PtrComp else: IntGlob = 100 PtrGlb.IntComp = Proc7(10, IntGlob) return PtrParOut def Proc4(): global Char2Glob BoolLoc = Char1Glob == 'A' BoolLoc = BoolLoc or BoolGlob Char2Glob = 'B' def Proc5(): global Char1Glob global BoolGlob Char1Glob = 'A' BoolGlob = False def Proc6(EnumParIn): EnumParOut = EnumParIn if not Func3(EnumParIn): EnumParOut = Ident4 if EnumParIn == Ident1: EnumParOut = Ident1 elif EnumParIn == Ident2: if IntGlob > 100: EnumParOut = Ident1 else: EnumParOut = Ident4 elif EnumParIn == Ident3: EnumParOut = Ident2 elif EnumParIn == Ident4: pass elif EnumParIn == Ident5: EnumParOut = Ident3 return EnumParOut def Proc7(IntParI1, IntParI2): IntLoc = IntParI1 + 2 IntParOut = IntParI2 + IntLoc return IntParOut def Proc8(Array1Par, Array2Par, IntParI1, IntParI2): global IntGlob IntLoc = IntParI1 + 5 Array1Par[IntLoc] = IntParI2 Array1Par[IntLoc+1] = Array1Par[IntLoc] Array1Par[IntLoc+30] = IntLoc for IntIndex in range(IntLoc, IntLoc+2): Array2Par[IntLoc][IntIndex] = IntLoc Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1 Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc] IntGlob = 5 def Func1(CharPar1, CharPar2): CharLoc1 = CharPar1 CharLoc2 = CharLoc1 if CharLoc2 != CharPar2: return Ident1 else: return Ident2 def Func2(StrParI1, StrParI2): IntLoc = 1 while IntLoc <= 1: if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1: CharLoc = 'A' IntLoc = IntLoc + 1 if CharLoc >= 'W' and CharLoc <= 'Z': IntLoc = 7 if CharLoc == 'X': return True else: if StrParI1 > StrParI2: IntLoc = IntLoc + 7 return True else: return False def Func3(EnumParIn): EnumLoc = EnumParIn if EnumLoc == Ident3: return True return False main(LOOPS) shedskin-0.9.4/tests/150.py0000664000175000017500000001010212157270661015274 0ustar srepmubsrepmub # (c) Bearophile from random import random, randint, choice from math import sin, pi from copy import copy infiniteNeg = -1e302 class Individual: def __init__(self, ngenes): self.ngenes = ngenes self.genome = [random()<0.5 for i in xrange(ngenes)] self.fitness = infiniteNeg def bin2dec(self, inf=0, sup=0): # Sup has to be None, SS workaround ************* if sup == 0: sup = self.ngenes - 1 # if sup is None: ... result = 0 for i in xrange(inf, sup+1): if self.genome[i]: result += 1 << (i-inf) return result def computeFitness(self): self.fitness = self.fitnessFun(self.computeValuesGenome()) def __repr__(self): return "".join([str(int(gene)) for gene in self.genome]) def fitnessFun(self, x): return x + abs(sin(32*x)) def computeValuesGenome(self, xMin=0, xMax=pi): scaleFactor = (xMax-xMin) / (1< individual2.fitness: pop2.append(individual1) else: pop2.append(individual2) else: if individual1.fitness > individual2.fitness: pop2.append(individual2) else: pop2.append(individual1) return pop2 # fixed def crossingOverPop(self): nCrossingOver = int(round(self.popSize * self.crossingOverProb)) for i in xrange(nCrossingOver): ind1 = choice(self.population) ind2 = choice(self.population) crossPosition = randint(0, self.genomeSize-1) for j in xrange(crossPosition+1): ind1.genome[j], ind2.genome[j] = ind2.genome[j], ind1.genome[j] def showGeneration_bestIndFind(self): fitnessTot = 0.0 bestIndividualGeneration = self.population[0] for individual in self.population: fitnessTot += individual.fitness if individual.fitness > bestIndividualGeneration.fitness: bestIndividualGeneration = individual if self.bestIndividual.fitness < bestIndividualGeneration.fitness: self.bestIndividual = copy(bestIndividualGeneration) # shallow copies should work now.. def run(self): self.generateRandomPop() self.bestIndividual = Individual(self.genomeSize) for self.generation in xrange(1, self.generationsMax+1): # works now self.computeFitnessPop() self.showGeneration_bestIndFind() self.population = self.tounamentSelectionPop() self.mutatePop() self.crossingOverPop() sga = SGA() sga.generationsMax = 3000 sga.genomeSize = 20 sga.popSize = 30 sga.geneMutationProb = 0.01 sga.run() shedskin-0.9.4/tests/142.py0000664000175000017500000000054512157270661015307 0ustar srepmubsrepmub class renderobject: pass class plane(renderobject): pass class sphere(renderobject): pass class light: def hoei(self): print 'hoei!' class parallellight(light): pass class pointlight(light): pass objects = [] objects.append(plane()) objects.append(sphere()) lights = [] lights.append(parallellight()) lights.append(pointlight()) lights[0].hoei() shedskin-0.9.4/tests/53.py0000664000175000017500000000056212157270661015227 0ustar srepmubsrepmub def yoyo(a): # a: [int] print 'yoyo', a # [str], [int] def yoyoyo(b): # b: [int] print 'yoyoyo', b # [str], [int] x = yoyo # [yoyo] x = yoyoyo # [lambda0] x(1) # [] shedskin-0.9.4/tests/126.py0000664000175000017500000000234312157270661015307 0ustar srepmubsrepmub # (c) Wensheng Wang from math import sqrt def primes(n): # n: [int] "primes(n): return a list of prime numbers <=n." if n == 2: # [int] return [2] # [list(int)] elif n<2: # [int] return [] # [list(int)] s = range(3, n+2, 2) # [list(int)] mroot = n ** 0.5 # [float] #mroot = sqrt(n) half = len(s) # [int] i = 0 # [int] m = 3 # [int] while m <= mroot: # [int] if s[i]: # [int] j = (m*m - 3) / 2 # [int] s[j] = 0 # [int] while j < half: # [int] s[j] = 0 # [int] j += m # [int] i += 1 # [int] m = 2 * i + 3 # [int] if s[-1] > n: s[-1] = 0 #return [2] + filter(None, s) return [2] + [x for x in s if x] # [list(int)] print primes(100) # [list(int)] shedskin-0.9.4/tests/148.py0000664000175000017500000001455012157270661015316 0ustar srepmubsrepmub # (c) Peter Goodspeed # --- coriolinus@gmail.com #import random from math import exp #from sets import Set #set = Set #functions def sigmoid(x): return float(1)/(1 + exp(-x)) def sig(x, xshift=0, xcompress=1): return 0 + (1 * sigmoid(xcompress * (x - xshift))) #exceptions class SpaceNotEmpty(Exception): pass class MultiVictory(Exception): def __init__(self, victorslist): self.victors = victorslist #classes class rectBoard(object): def __init__(self, edge=3): self.edge = edge self.__board = [edge * [0] for i in xrange(edge)] self.__empty = edge**2 def assign(self, row, col, value): if(self.__board[row][col] == 0): self.__board[row][col] = value self.__empty -= 1 else: raise SpaceNotEmpty() def isfull(self): return self.__empty == 0 #def valueof(self, row, col): # return self.__board[row][col] def isvictory(self): victors = [] #examine rows for row in self.__board: if len(set(row)) == 1: if row[0] != 0: victors.append(row[0]) #examine cols for i in xrange(self.edge): col = [row[i] for row in self.__board] if len(set(col)) == 1: if col[0] != 0: victors.append(col[0]) #examine diagonals #left diagonal ld = [] for i in xrange(self.edge): ld.append(self.__board[i][i]) if len(set(ld)) == 1: if ld[0] != 0: victors.append(ld[0]) #right diagonal rd = [] for i in xrange(self.edge): rd.append(self.__board[i][self.edge-(1+i)]) if len(set(rd)) == 1: if rd[0] != 0: victors.append(rd[0]) #return if len(victors) == 0: return 0 if len(set(victors)) > 1: raise MultiVictory(set(victors)) return victors[0] def __str__(self): ret = "" for row in xrange(self.edge): if row != 0: ret += "\n" for i in xrange(self.edge): if i != 0: ret += '+' ret += "---" ret += "\n" ret += " " for col in xrange(self.edge): if col != 0: ret += " | " if self.__board[row][col] == 0: ret += ' ' else: ret += str(self.__board[row][col]) return ret def doRow(self, fields, indices, player, scores): players = set(fields).difference(set([0])) if(len(players) == 1): if list(players)[0] == player: for rown, coln in indices: scores[rown][coln] += 15 * sig(fields.count(player) / float(self.edge), .5, 10) else: for rown, coln in indices: scores[rown][coln] += 15 * fields.count(list(players)[0]) / float(self.edge) def makeAImove(self, player): scores = [self.edge * [0] for i in xrange(self.edge)] for rown in xrange(self.edge): row = self.__board[rown] self.doRow(row, [(rown, i) for i in xrange(self.edge)], player, scores) for coln in xrange(self.edge): col = [row[coln] for row in self.__board] self.doRow(col, [(i, coln) for i in xrange(self.edge)], player, scores) indices = [(i, i) for i in xrange(self.edge)] ld = [self.__board[i][i] for i in xrange(self.edge)] self.doRow(ld, indices, player, scores) #also, because diagonals are just more useful for rown, coln in indices: scores[rown][coln] += 1 #now, we do the same for right diagonals indices = [(i, (self.edge - 1) - i) for i in xrange(self.edge)] rd = [self.__board[i][(self.edge - 1) - i] for i in xrange(self.edge)] self.doRow(rd, indices, player, scores) #also, because diagonals are just more useful for rown, coln in indices: scores[rown][coln] += 1 scorelist = [] for rown in xrange(self.edge): for coln in xrange(self.edge): if(self.__board[rown][coln] == 0): scorelist.append((scores[rown][coln],(rown,coln))) scorelist.sort() scorelist.reverse() #print scorelist scorelist = [x for x in scorelist if x[0] == scorelist[0][0]] #return random.choice([(x[1], x[2]) for x in scorelist]) #scorelist = [(random.random(), x[1],x[2]) for x in scorelist] #scorelist.sort() return (scorelist[0][1][0], scorelist[0][1][1]) def aigame(size=30, turn=1, players=2): b = rectBoard(size) while((not b.isfull()) and (b.isvictory() == 0)): if(turn==1): #player turn #print #print b r, c = b.makeAImove(turn) b.assign(r,c,1) turn = 2 else: #computer turn r, c = b.makeAImove(turn) b.assign(r,c,turn) if(turn == players): turn = 1 else: turn += 1 #print #print b.__str__() #print if(b.isvictory() == 0): print "Board is full! Draw!" else: print "Victory for player "+str(b.isvictory())+"!" aigame() shedskin-0.9.4/tests/93.py0000664000175000017500000000073712157270661015237 0ustar srepmubsrepmub range(4) # [list(int)] a = {} # [dict(int, float)] a[4] = 1.0 # [float] x = a.items() # [list(tuple(int))] bert = (1,2,3) # [tuple(int)] hans = (1.0,2.0,3.0) # [tuple(float)] bert_list = [bert] # [list(tuple(int))] hans_list = [hans] # [list(tuple(float))] shedskin-0.9.4/tests/76.py0000664000175000017500000000006612157270661015233 0ustar srepmubsrepmub cnf = [''.split()] # [list()] shedskin-0.9.4/tests/181.py0000664000175000017500000000560212157270661015311 0ustar srepmubsrepmub #time.strptime import time print time.strftime("%d %b %Y %H:%M:%S", time.strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S")) print time.strftime("%Y", time.strptime("2001", "%Y")) #improve default arguments import testdata.bert2 as bert print bert.def1() print bert.def2() bert.a = 16 print bert.huh() print bert.def2() print bert.def3() def bleh(l=[1,2]): return l print bleh() bert.def4() #C++ bool type def h(x): if x in ['False', '0']: return 0 elif x in ['True', '1']: return 1 else: return 2 print hex(1==2), hex(1!=2) print oct(1==2), oct(1!=2) print abs(1==2), abs(1!=2) print h(str(1==2)), h(str(1!=2)) print h(repr(1==2)), h(repr(1!=2)) print int(1==2), int(1!=2) print float(1==2), float(1!=2) print ord(chr(1==2)), ord(chr(1!=2)) #random.sample/choice import random print random.sample(xrange(1), 1) print random.sample(set([1]), 1) #fast_for_neg in listcomp_for print [(i, i) for i in range(29, -1, -1)] #works, but add as test def ah(): pass def bh(func=ah): func() bh() # sorted, list.sort: cmp and reverse args def mut(a,b): return -cmp(a,b) def cmut(a,b): return -cmp(a,b) print sorted([5,1,3,2,4]) print sorted([5,1,3,2,4], reverse=True) print sorted([5,1,3,2,4], cmp=mut) print sorted([5,1,3,2,4], cmp=mut, reverse=True) print sorted(set([5,1,3,2,4])) print sorted(set([5,1,3,2,4]), reverse=True) print sorted(set([5,1,3,2,4]), cmp=mut) print sorted(set([5,1,3,2,4]), cmp=mut, reverse=True) print sorted('abcde') print sorted('abcde', reverse=True) print sorted('abcde', cmp=cmut) print sorted('abcde', cmp=cmut, reverse=True) l = [1,4,5,2,3] l.sort(); print l l.sort(cmp=mut); print l l.sort(reverse=True); print l l.sort(cmp=mut, reverse=True); print l # tempvars/new nodes and inheritance (XXX add more here) class network: def shortestpath(self): for node in set([1]): print node print [node for node in [1]] class smallworld(network): pass s = smallworld() s.shortestpath() # ss-progs regression class LowLevel: def bslTxRx(self, blkout=None): pass class BootStrapLoader(LowLevel): def actionRun(self): self.bslTxRx() bsl = BootStrapLoader() bsl.actionRun() # test compilation import socket import stat # test all cases a = 1 print [x for x in range(1,10,1)] print [x for x in range(10,1,-1)] print [x for x in range(1,10,+1)] print [x for x in range(1,10,a)] print [x for x in range(10,1,-a)] print [x for x in range(1,10,+a)] print [x for x in range(1,10,a*1)] print [x for x in range(1,10,-(-1))] print [x for x in range(1,10,+(+a))] for x in range(1,10,1): print x, print for x in range(1,10,+1): print x, print for x in range(1,10,a): print x, print for x in range(1,10,+a): print x, print for x in range(1,10,-(-1)): print x, print for x in range(1,10,+(+1)): print x, print for x in range(1,10,+(+a)): print x, print for x in range(10,1,-1): print x, print for x in range(10,1,-a): print x, print shedskin-0.9.4/tests/143.py0000664000175000017500000000062112157270661015303 0ustar srepmubsrepmub class renderobject: def intersect(self,l): return "none", (l, l) class plane(renderobject): def intersect(self,l): return "one", (l, l) class sphere(renderobject): def intersect(self,l): return "none", (l, l) p = plane() s = sphere() print p.intersect(1) print s.intersect(2) meuk = [p, s] for obj in meuk: print obj.intersect(1) shedskin-0.9.4/tests/e2/0000775000175000017500000000000012157270661014731 5ustar srepmubsrepmubshedskin-0.9.4/tests/e2/memory.py0000664000175000017500000000000012157270661016601 0ustar srepmubsrepmubshedskin-0.9.4/tests/e2/blah.py0000664000175000017500000000105412157270661016211 0ustar srepmubsrepmubimport memory class NotExported(Exception): pass class meuk: def __init__(self): self.memory = 4 class CPU: def AND(self): return 'AND' c = CPU() def press(keys): return sorted(list(keys)) def press2(keys): return ('h' in keys, 'x' in keys, 'u' in keys, 'r' in keys) def hoppa(d): return d if __name__ == '__main__': c.AND() press(set('a')) press2(set('a')) print meuk().memory hoppa({'acht': 8.8}) NotExported() initblah = 4 print initblah addblah = 5 print addblah shedskin-0.9.4/tests/e2/main.py0000664000175000017500000000022012157270661016221 0ustar srepmubsrepmub#blah import blah as test print test.press(set('harka')) print test.press2(set('harka')) print test.hoppa({'acht': 17.0}) print test.c.AND() shedskin-0.9.4/tests/178.py0000664000175000017500000000221112157270661015310 0ustar srepmubsrepmub # --- more aug assignment f = -112 print f f /= -3 print f, f / -3 f %= -3 print f f //= -1 print f d={} somme = 9.0 i=4 j=5 d[i,j] = 3.0 d[i,j] += somme d[i,j] *= somme d[i,j] /= somme print d e = {} e[i,j] = -7 e[i,j] /= -2 e[i,j] *= -2 e[i,j] %= -2 e[i,j] //= -2 print e # --- tests these for once print max([1]) print max(1, 2) print max(7.7, 7) print max(7, 7.7) print max(1, 2, 3) print max(1, 2, 3, 4, 5) print min([1]) print min(1, 2) print min(6.7, 7) print min(7, 6.7) print min(1, 2, 3) print min(1, 2, 3, 4, 5) # --- virtual test case 1 class Z: def boink(self, a): pass def beh(self): print self.boink(9) class Y(Z): def boink(self, a): return a y = Y() y.beh() # --- virtual test case 2 class C: def boink(self): print 'C' class D(C): pass class A(C): def boink(self): print 'A' class B(C): pass c = D() c.boink() b = B() b = A() b.boink() # --- virtual case 3 class CC: pass class AA(CC): def __init__(self): self.a = 4 class BB(CC): def __init__(self): self.a = 5 cc = AA() cc = BB() print cc.a # --- just in case this = 1 shedskin-0.9.4/tests/130.py0000664000175000017500000000017712157270661015305 0ustar srepmubsrepmub a = range(10) b = a[1::3] print b del a[9] print a del a[1:3] print a del a[::2] print a d = {1: 4, 2: 5} del d[1] print d shedskin-0.9.4/tests/50.py0000664000175000017500000000053712157270661015226 0ustar srepmubsrepmub a = [] # [list(int)] a.append(1) # [] b = [] # [list(str)] b.append('1') # [] c = [] # [list(list(int))] c.append([1]) # [] #d = [] #d.append(1) #d.append('1') shedskin-0.9.4/tests/79.py0000664000175000017500000000005512157270661015234 0ustar srepmubsrepmub nrofvars = [1][0] vars = range(nrofvars+1) shedskin-0.9.4/tests/110.py0000664000175000017500000000052012157270661015273 0ustar srepmubsrepmub def row_perm_rec(numbers): # numbers: [list(int)] range(numbers[0]) # [list(int)] puzzlecolumns = [[7]] # [list(list(int))] puzzlerows = [[8]] # [list(int)] ['u'] # [list(str)] row_perm_rec(puzzlerows[0]) # [] shedskin-0.9.4/tests/172.py0000664000175000017500000000555012157270661015313 0ustar srepmubsrepmub import random # --- module-level functions random.seed(37) rstate = random.getstate() # (state is not cross-compatible with CPython) random.setstate(rstate) for i in range(25): print "%.8f" % random.random() print random.randrange(-30,15) print random.randrange(-15,15,3) print random.randint(50,100) fibs = [0,1,1,2,3,5,8,13,21] print fibs print random.choice(fibs) print random.sample(fibs,3) random.shuffle(fibs) print fibs nums = [3.141, 2.71828, 1.41421, 1.0] print nums print random.choice(nums) print random.sample(nums,3) random.shuffle(nums) print nums print "%.8f" % random.uniform(-0.5,0.5) print "%.8f" % random.normalvariate(0.0, 1.0) print "%.8f" % random.lognormvariate(0.0, 1.0) print "%.8f" % random.expovariate(1.0) print "%.8f" % random.vonmisesvariate(0.0, 1.0) print "%.8f" % random.gammavariate(20.0, 1.0) print "%.8f" % random.gauss(0.0, 1.0) print "%.8f" % random.betavariate(3.0, 3.0) print "%.8f" % random.paretovariate(1.0) print "%.8f" % random.weibullvariate(1.0, 1.0) #print "%.8f" % random.stdgamma(1.0,1.0,1.0,1.0) # deprecated in CPython #print "%.8f" % random.cunifvariate(0.0,1.0) # deprecated in CPython print random.getrandbits(8) print random.getrandbits(16) print random.getrandbits(30) print '' # --- (test set for RNGs) def runrng(r): print "%.8f" % r.random() print r.randrange(0,10) print r.randrange(-10,10,2) print r.randint(-5,5) fibs = [0,1,1,2,3,5,8,13,21] print fibs print r.choice(fibs) print r.sample(fibs,4) r.shuffle(fibs) print fibs nums = [3.141, 2.71828, 1.41421, 1.0] print nums print random.choice(nums) print random.sample(nums,1) random.shuffle(nums) print nums print "%.8f" % r.uniform(-0.5,0.5) print "%.8f" % r.normalvariate(0.0, 1.0) print "%.8f" % r.lognormvariate(0.0, 1.0) print "%.8f" % r.expovariate(1.0) print "%.8f" % r.vonmisesvariate(0.0, 1.0) print "%.8f" % r.gammavariate(20.0, 1.0) print "%.8f" % r.gauss(0.0, 1.0) print "%.8f" % r.betavariate(3.0, 3.0) print "%.8f" % r.paretovariate(1.0) print "%.8f" % r.weibullvariate(1.0, 1.0) #print "%.8f" % r.stdgamma(1.0, 1.0, 1.0, 1.0) # deprecated in CPython #print "%.8f" % r.cunifvariate(0.0, 1.0) # deprecated in CPython print '' # --- random.Random (Mersenne Twister) mt = random.Random() mt.seed() mt.seed(79) mtstate = mt.getstate() # (state is not cross-compatible with CPython) mt.setstate(mtstate) #mt.jumpahead(1000000) # (not yet supported) for i in range(25): runrng(mt) # --- random.WichmannHill wh = random.WichmannHill() wh.seed() wh.seed(86) wh.whseed() wh.whseed(41) whstate = wh.getstate() # (state is not cross-compatible with CPython) wh.setstate(whstate) wh.jumpahead(1000000) for i in range(25): runrng(wh) shedskin-0.9.4/tests/146.py0000664000175000017500000000030412157270661015304 0ustar srepmubsrepmub class C1: def m1(self): self.a1 = 1 def m2(self): self.a2 = 2 class C2(C1): def m2(self): self.a = 3 class C3(C2): def m2(self): self.a2 = 4 c3 = C3() c3.m1() c3.m2() print c3.a1, c3.a2 shedskin-0.9.4/tests/161.py0000664000175000017500000000047112157270661015306 0ustar srepmubsrepmub bla = {} meuk = (12, 13) z = (5,(3,4)) bla[1], (c, d) = z print bla class X: pass x = X() for x.z in [1,2,3]: print x.z x.y, (c, d) = z print x.y, x.z s = ['a', 'b', 'c'] s = 'abc' for y in s: print y print print s, str(s), repr(s) t2 = 1, 'een' print '%d %s' % t2 f = dict([(1,'1'), (2, '2')]) print f shedskin-0.9.4/tests/156.py0000664000175000017500000000235612157270661015316 0ustar srepmubsrepmub print 'he\\"' class A: def __init__(self): pass a = A() a.__init__() class B: def __init__(self, n): print 'b init with', n def huhu(self): self.__init__(4) b = B(5) b.huhu() class C: def __init__(self): pass c = C() # Probably simpler OOP problems #class Pet: # def speak(self): pass #class Cat(Pet): # def speak(self): print "meow!" #class Dog(Pet): # def speak(self): print "woof!" #def command(pet): pet.speak() #pets = Cat(), Dog() #for pet in pets: command(pet) #for pet in (pets[1], pets[0]): command(pet) clearCastlingOpportunities = [None] clearCastlingOpportunities[0] = (10,) board = [1,2,3] board[0] = 0 print clearCastlingOpportunities, board print range(-17, -120, -17) v = -1 w = 4 for x in range(w,-2,v): print x for x in range(w+1,-2,2*v): print x for x in range(0,w+1,1): print x d = [i for i in xrange(10)] print d d[::2] = [1,2,3,4,5] print d d[::-2] = range(5) print d e = ["X" for i in xrange(10)] e[::2] = "abcde" print e f = ["Y" for i in xrange(10)] f[1::2] = tuple("abcde") print f def sgn(x): if x < 0: return -1 else: return 1 for j in [-2, -1]: print [i for i in xrange(-10*sgn(j), -1*sgn(j), j) if True for k in range(2) if k] shedskin-0.9.4/tests/32.py0000664000175000017500000000060212157270661015217 0ustar srepmubsrepmub def qbert(a): # a: [int] print a # [int] a=1 # [int] b=2 # [int] qbert(1) # [] qbert(2) # [] qbert(a) # [] qbert(b) # [] shedskin-0.9.4/tests/5.py0000664000175000017500000000013112157270661015134 0ustar srepmubsrepmub class fred: def speak(self, x): return x b = fred() c = b.speak('goedzo!') shedskin-0.9.4/tests/86.py0000664000175000017500000000074112157270661015234 0ustar srepmubsrepmub def row_perm_rec(): hoppa_row = [] # [list(str)] new_row = [''] # [list(str)] a = hoppa_row # [list(str)] new_row.extend(a) # [] hoppa_row = new_row[:] hoppa_row.append('u') # [] return hoppa_row numbers = [1] # [list(int)] numberscopy = numbers[:] # [list(int)] s = row_perm_rec() # [] shedskin-0.9.4/tests/83.py0000664000175000017500000000037612157270661015235 0ustar srepmubsrepmub def propagate(lit): # lit: [int] global lit_mask # XXX lit_mask[lit] |= 1 # [int] def lookahead(): # mods: [list(int)] global lit_mask lit_mask = [1] lookahead() propagate(0) shedskin-0.9.4/tests/84.py0000664000175000017500000000075012157270661015232 0ustar srepmubsrepmub def solve_rec(): la_mods = [1] # [list(int)] for var in la_mods: # [list(int)] lookahead_variable(var, la_mods) # [] propagate(var, la_mods) # [] def propagate(lit, mods, bla=0): # lit: [int], mods: [list(int)], bla: [int] pass def lookahead_variable(var, mods): # var: [int], mods: [list(int)] propagate(10, mods) # [] solve_rec() # [] shedskin-0.9.4/tests/44.py0000664000175000017500000000026312157270661015225 0ustar srepmubsrepmub #from sets import Set a = set([1,2]) # [Set(int)] a.add(3) # [] print a # [Set(int)] shedskin-0.9.4/tests/97.py0000664000175000017500000000013412157270661015232 0ustar srepmubsrepmub print [(2*a, b) for a in range(4) if a > 0 for b in ['1','2']] # [list(tuple2(int, str))] shedskin-0.9.4/tests/131.py0000664000175000017500000000117212157270661015302 0ustar srepmubsrepmub # (c) Bearophile import random random.seed(42) #from sets import Set points = [ (random.random(), random.random()) for i in xrange(200) ] def isntRightTurn(e): p0, p1 = e[-3] q0, q1 = e[-2] r0, r1 = e[-1] return q0*r1 + p0*q1 + r0*p1 >= q0*p1 + r0*q1 + p0*r1 def half(points): extrema = points[0:2] for p in points[2:]: extrema.append(p) while len(extrema)>2 and isntRightTurn(extrema): del extrema[-2] return extrema points = sorted(set(points)) upper = half(points) points.reverse() lower = half(points) print [('%.2f' % x, '%.2f' % y) for x, y in upper + lower[1:-1]] shedskin-0.9.4/tests/98.py0000664000175000017500000000031612157270661015235 0ustar srepmubsrepmub def best_move(xx): # xx: [int] return (0, 0), 0 # [tuple2(tuple2(int, int), int)] a, b = best_move(1) # [tuple2(tuple2(int, int), int)] shedskin-0.9.4/tests/108.py0000664000175000017500000000070212157270661015304 0ustar srepmubsrepmub t = (1,2,3) # [tuple(int)] v = (1,) # [tuple(int)] w = (1,2,3) # [tuple(int)] e = {} # [dict(tuple(int), int)] e[t] = 1 # [int] e[v] = 2 # [int] e[w] = 3 # [int] print e[t], e[v], e[w] # [int], [int], [int] shedskin-0.9.4/tests/202.py0000664000175000017500000000020212157270661015272 0ustar srepmubsrepmub# name clash debug = True class Debug: def debug(self, msg): if debug: print msg Debug().debug('debug') shedskin-0.9.4/tests/125.py0000664000175000017500000000176212157270661015312 0ustar srepmubsrepmub print 'hello, world!' for x in range(10,14): # [list(int)] print x**3 # [int] y = 'luis' # [str] for i in y: # [str] print i # [str] print [i*2 for i in 'luis'] # [list(str)] f = open('testdata/hoppa') # [file] print 'lc', [l for l in f] # [str], [list(str)] f.close() # [] f = open('testdata/hoppa') # [file] print 'read', f.read() # [str], [str] f.close() # [] f = file('testdata/hoppa') # [file] print 'lines', f.readlines() # [str], [list(str)] f.close() # [] conv = {"A": 0, "B": 1} # [dict(str, int)] print conv["A"], conv["B"] # [int], [int] print [{"A": 0, "B": 1}[c] for c in "ABABABA"] # [list(int)] shedskin-0.9.4/tests/56.py0000664000175000017500000000105212157270661015225 0ustar srepmubsrepmub def hoi(a, b, e): # a: [int, float], b: [int, float], e: [int] c = a # [int, float] d = b # [int, float] f = 1 # [int] g = 1 # [int] h = f+g # [int] s = 'ho'+'i' # [str] return c+d # [int, float] hoi(1, 2, 3) # [int] hoi(1.0, 2.0, 4) # [float] shedskin-0.9.4/tests/75.py0000664000175000017500000000055212157270661015232 0ustar srepmubsrepmub a = [1,2,3,4] # [list(int)] b = [1.0] # [list(float)] #c = [1,2,2.0] # [list(pyobj)] d = [(1,)] # [list(tuple(int))] e = [[1, 2],[2, 3, 4]] # [list(list(int))] #f = [[1, 2.0],[2, 3, 4]] # [list(list(pyobj))] shedskin-0.9.4/tests/81.py0000664000175000017500000000136712157270661015234 0ustar srepmubsrepmub def _reduce(f, l, i=-1): # f: [lambda0], l: [list(int)], i: [int]r if not l: # [list(int)] if i != -1: return i # [int] print '*** ERROR! *** reduce() called with empty sequence and no initial value' # [str] if i != -1: # [int] r = f(i, l[0]) # [int] else: r = l[0] # [int] for i in range(len(l)-1): # [list(int)] r = f(r, l[i+1]) # [int] return r # [int] acc = lambda x,y: x+y # [lambda0] score = [1,2,3,4] # [list(int)] print _reduce(acc, score, 0) # [int] shedskin-0.9.4/tests/19.py0000664000175000017500000000103212157270661015222 0ustar srepmubsrepmub #class list: # unit: [float]* # def append(self, x): # x: [float], self: [list_float] # self.unit = x # [float] # # def __getitem__(self, i): # i: [int], a: [float], self: [list_float] # a = self.unit # [float] # return a # [float] cube = [] # [list_float] cube.append(1.0) # [] y = cube[0] # [float] shedskin-0.9.4/tests/55.py0000664000175000017500000000025612157270661015231 0ustar srepmubsrepmub a={} # [dict(int->list(float))] a.setdefault(1,[]).append(1.0) # [] b= a[1] # [list(float)] shedskin-0.9.4/tests/177.py0000664000175000017500000000030412157270661015310 0ustar srepmubsrepmub # --- import problem from testdata.bert import * z = zeug() # --- '_' renaming mangle import testdata.bert class hello: def hello(self): testdata.bert.hello(1) s=hello().hello() shedskin-0.9.4/tests/189.py0000664000175000017500000000265012157270661015321 0ustar srepmubsrepmub import heapq heap = [21] print heap heapq.heappush(heap, 42) print heap heapq.heappush(heap, 12) print heap print heapq.heappop(heap) print heap print heapq.heappushpop(heap, 63) print heap print heapq.heappop(heap) print heap print heapq.heappop(heap) print heap heapq.heappush(heap, 12) print heap heapq.heappush(heap, 52) print heap heapq.heappush(heap, 112) print heap heapq.heappush(heap, 1) print heap heapq.heappush(heap, 12) print heap print heapq.heappop(heap) print heap print heapq.heappushpop(heap, 63) print heap print heapq.heappop(heap) print heap print heapq.heappop(heap) print heap print '--------------' l = [42, 45, 35, 3] print l heapq.heapify(l) print l print heapq.heapreplace(l, 36) print l print heapq.heappop(l) print l print heapq.heappop(l) print l print heapq.heappop(l) print l print heapq.heappop(l) print l print '--------------' for i in heapq.merge(): print i print list(heapq.merge()) print '-' for j in heapq.merge([3, 7, 18]): print j print '-' for k in heapq.merge([3, 7, 18], [5, 21, 44]): print k print '-' for m in heapq.merge([3, 7, 18], [5, 21, 44], [2, 33]): print m print '------' for n in heapq.nlargest(5, [3, 15, 56, 38, 49, 12, 41]): print n print '-' for np in heapq.nlargest(5, [3, 15]): print np print '---' for o in heapq.nsmallest(5, [3, 15, 56, 38, 49, 12, 41]): print o print '-' for op in heapq.nsmallest(5, [3, 15]): print op shedskin-0.9.4/README0000664000175000017500000000022212157270661014135 0ustar srepmubsrepmubsudo python setup.py install shedskin test.py make ./test see the online documentation for details: http://code.google.com/p/shedskin/wiki/docs