为Python 2编写一个模块。倍和3.倍

时间:2021-08-20 02:43:07

I've written a pure-Python module for Python 3.0/3.1 which I'd also like to make it compatible with 2.x (probably just 2.6/2.7) in order to make it available to the widest possible audience.

我已经为Python 3.0/3.1编写了一个纯Python模块,我也希望使它与2兼容。x(可能只有2.6/2.7),以便让最广泛的受众可以使用它。

The module is concerned with reading and writing a set of related file formats, so the differences between 2.x and 3.x versions would be slight — e.g. io.BytesIO instead of StringIO.StringIO — but not all of them are easily handled via try/except blocks, such as setting metaclasses.

该模块关注的是读取和编写一组相关的文件格式,因此两者之间的区别在于。x和3。x个版本比较轻,例如io。BytesIO代替StringIO。StringIO -但是不是所有的这些都可以通过try/except块来处理,比如设置元类。

What's the correct way to handle this? Two nearly-identical codebases which must be kept in sync or one codebase sprinkled with feature detection? A single, clean codebase plus 2to3 or 3to2?

正确的处理方法是什么?两个几乎相同的码基必须保持同步,还是一个代码基点缀着特征检测?一个干净的代码基加上2到3还是3到2?

1 个解决方案

#1


11  

Write your code entirely against 2.x, targeting the most recent version in the 2.x series. In this case, it's probably going to remain 2.7. Run it through 2to3, and if it doesn't pass all of its unit tests, fix the 2.x version until the generated 3.x version works.

完全按照2编写代码。x,针对2中最新的版本。x系列。在这种情况下,它可能仍然是2。7。运行它到2to3,如果它没有通过所有的单元测试,修复2。x版本直到生成3。x版本有效。

Eventually, when you want to drop 2.x support, you can take the generated 3.x version and start modifying it directly. Until then, only modify the 2.x version.

最终,当你想放弃2。x支持,可以取生成的3。x版本并开始直接修改。在此之前,只修改2。x版本。

This is the workflow highly recommended by the people who worked on 2to3. Unfortunately I don't remember offhand the link to the document I gleaned all of this from.

这是2to3工作人员极力推荐的工作流程。不幸的是,我不记得我从文件中获取的链接。

#1


11  

Write your code entirely against 2.x, targeting the most recent version in the 2.x series. In this case, it's probably going to remain 2.7. Run it through 2to3, and if it doesn't pass all of its unit tests, fix the 2.x version until the generated 3.x version works.

完全按照2编写代码。x,针对2中最新的版本。x系列。在这种情况下,它可能仍然是2。7。运行它到2to3,如果它没有通过所有的单元测试,修复2。x版本直到生成3。x版本有效。

Eventually, when you want to drop 2.x support, you can take the generated 3.x version and start modifying it directly. Until then, only modify the 2.x version.

最终,当你想放弃2。x支持,可以取生成的3。x版本并开始直接修改。在此之前,只修改2。x版本。

This is the workflow highly recommended by the people who worked on 2to3. Unfortunately I don't remember offhand the link to the document I gleaned all of this from.

这是2to3工作人员极力推荐的工作流程。不幸的是,我不记得我从文件中获取的链接。