Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find out what stdlib modules lack a pure Python implementation #60855

Closed
brettcannon opened this issue Dec 9, 2012 · 28 comments
Closed

Find out what stdlib modules lack a pure Python implementation #60855

brettcannon opened this issue Dec 9, 2012 · 28 comments
Labels
easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@brettcannon
Copy link
Member

BPO 16651
Nosy @rhettinger, @terryjreedy, @amauryfa, @pitrou, @tiran, @pjenvey, @ezio-melotti, @merwok, @alex, @briancurtin, @asvetlov, @meadori, @cjerdonek, @ericsnowcurrently, @zware, @serhiy-storchaka
Dependencies
  • bpo-16659: Pure Python implementation of random
  • bpo-16694: Add pure Python operator module
  • bpo-16991: Add OrderedDict written in C
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-01-31.22:02:44.851>
    created_at = <Date 2012-12-09.18:39:53.870>
    labels = ['easy', 'type-feature', 'library']
    title = 'Find out what stdlib modules lack a pure Python implementation'
    updated_at = <Date 2020-01-31.22:02:44.850>
    user = 'https://github.com/brettcannon'

    bugs.python.org fields:

    activity = <Date 2020-01-31.22:02:44.850>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-01-31.22:02:44.851>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2012-12-09.18:39:53.870>
    creator = 'brett.cannon'
    dependencies = ['16659', '16694', '16991']
    files = []
    hgrepos = []
    issue_num = 16651
    keywords = ['easy']
    message_count = 28.0
    messages = ['177230', '177231', '177232', '177247', '177291', '177293', '177294', '177299', '177300', '177302', '177303', '177309', '177310', '177316', '177332', '177338', '177419', '177431', '177520', '177521', '177887', '177891', '177892', '177898', '178674', '178688', '180625', '183914']
    nosy_count = 19.0
    nosy_names = ['rhettinger', 'terry.reedy', 'amaury.forgeotdarc', 'pitrou', 'christian.heimes', 'pjenvey', 'ezio.melotti', 'eric.araujo', 'Arfrever', 'alex', 'brian.curtin', 'asvetlov', 'meador.inge', 'chris.jerdonek', 'tshepang', 'eric.snow', 'zach.ware', 'serhiy.storchaka', 'superluser']
    pr_nums = []
    priority = 'low'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue16651'
    versions = ['Python 3.6']

    @brettcannon
    Copy link
    Member Author

    I'm wondering which modules in the stdlib lack pure Python implementations (and could legitimately have one, e.g. sqlite can't really have a pure Python implementation). Once we know how big/small the list is a decision could be made as to whether to take on the effort to bring over some pure Python version from another VM or something for any of these modules.

    To start the list:

    csv
    itertools

    @brettcannon brettcannon added stdlib Python modules in the Lib dir easy labels Dec 9, 2012
    @serhiy-storchaka
    Copy link
    Member

    array
    audioop
    binascii
    random
    re
    struct
    xml.parsers.expat
    cjkcodecs

    zlib, bz2, lzma and crypts also can be implemented in pure Python.

    @tiran
    Copy link
    Member

    tiran commented Dec 9, 2012

    hashlib backends (md5, sha1, sha256 / 384 / 512) either through openssl wrappers or implementations based on libtomcrypt.

    @cjerdonek
    Copy link
    Member

    Would it make sense for this list to be somehow reflected in or be reconstructible from the documentation?

    @brettcannon
    Copy link
    Member Author

    So expat doesn't count as that literally wraps the expat library. Random also requires accessing the system randomization libraries to work properly so I don't think that is a candidate either. As for the compression libraries, those could be re-implemented, but I view those as wrappers around the libraries (same as the crypto stuff). I mean it doesn't have to be that way, but I'm trying to keep this framed in a tractable problem to start.

    So to summarize the non-contentious modules (including adding functools) in alphabetical order, that puts us at:

    array
    audioop
    binascii
    cjkcodecs
    csv
    functools
    itertools
    re
    struct

    I would be curious to see what frequency these modules are used to know what might be higher priority so that the least used modules could eventually be marked as CPython-specific.

    And to answer Chris' question, there is no need to be able to generate this from the docs until there is some PEP listing what modules must be implemented by a VM in order to considers its stdlib complete.

    And thanks for the help so far, everyone!

    @amauryfa
    Copy link
    Member

    PyPy has a pure Python implementation of sqlite (using ctypes):
    https://bitbucket.org/pypy/pypy/src/default/lib_pypy/_sqlite3.py

    It most probably works on CPython as well.
    Does it belong to this list?

    @tiran
    Copy link
    Member

    tiran commented Dec 10, 2012

    All cryptographic hash function are available as pure Python implementations. Although the Python based variants are super slow (except maybe on PyPy), they still serve as an academic show case.

    IIRC PyPy has pure Python implementations of MD5, SHA-1 and SHA-2 family.

    @brettcannon
    Copy link
    Member Author

    No, sqlite does not belong on this list as it wraps a C library. Plus there is an explicit block for ctypes-based modules in the stdlib which would prevent moving over the PyPy implementation.

    And I'm sure pure Python versions of all the crypto libraries are out there, but I would be worried about them being implemented wrong, too slow to care, etc. Plus short of PyPy all of the other VMs have access to those crypto libraries in their own standard library so there is no need to provide it in Python's in pure Python.

    @pitrou
    Copy link
    Member

    pitrou commented Dec 10, 2012

    Agreed with Brett.

    @pjenvey
    Copy link
    Member

    pjenvey commented Dec 10, 2012

    zipimport

    @briancurtin
    Copy link
    Member

    winreg does not have a pure equivalent, nor could it

    @serhiy-storchaka
    Copy link
    Member

    So expat doesn't count as that literally wraps the expat library.

    XML parser can be implemented in pure Python. There are a lot of XML parsers
    implemented in different programming languages.

    Random
    also requires accessing the system randomization libraries to work
    properly so I don't think that is a candidate either.

    No, random have C implemented some basic methods only for speed. It uses
    time() system function for initial seeding, but time() is accessible from pure
    Python too.

    Some OS-specific modules (pwd, grp, spwd, resource, ossaudiodev, etc) can be
    implemented in pure Python on some platforms (using I/O and ioctl on special
    files in /etc, /dev or /proc).

    @brettcannon
    Copy link
    Member Author

    Sure, a general XML parsing library could be written in Python, but they wouldn't be named expat. =) The expat module wraps a specific XML parser (expat) so I still do not consider it applicable for this list.

    As for random, you are right that it doesn't use platform-specific random code.

    @alex
    Copy link
    Member

    alex commented Dec 10, 2012

    A lot of builtins :)

    @amauryfa
    Copy link
    Member

    What's the purpose of these alternate implementations? For education, experiments? As a replacement for other VMs?

    Other modules that could be considered:
    marshal
    operator
    unicodedata

    @brettcannon
    Copy link
    Member Author

    Alex: yes, the builtins could almost all be re-implemented in pure Python, but there is a performance consideration vs. benefit (e.g. if we re-implemented map() in Python which VMs would use it vs. implement it in some native way for performance?).

    But one place I think there could be a benefit w/o performance issues is the built-in exceptions as you should be able to store references to the classes to help minimize the added cost of startup creating the exceptions from Python code.

    Amaury: other VMs and easier extensibility in the future. We might as well all share the burden of maintaining pure Python versions instead of re-implementing the same thing over and over in little VM silos. And if some new thing needs to be added for a module it's easier to do it in Python than C (which then also continues to benefit other VMs).

    @merwok
    Copy link
    Member

    merwok commented Dec 13, 2012

    Should itertools be in the list, as its stated purpose is to provide highly efficient functions?

    @brettcannon
    Copy link
    Member Author

    Eric, I know you didn't just mean to suggest Python != efficient. =)

    Yes, it should be in the list because other VMs might not want to re-implement that code in some native language to the VM. And I assume all code in the stdlib tries to be efficient, so that label holds no sway with me.

    @terryjreedy
    Copy link
    Member

    itertools should be fairly easy as the docs already contain Python equivalents (or near equivalents).

    @pjenvey
    Copy link
    Member

    pjenvey commented Dec 14, 2012

    PyPy had a pure python itertools until recently (it's been deleted):

    https://bitbucket.org/pypy/pypy/src/c1aa74c06e86/lib_pypy/itertools.py?at=py3k

    @merwok
    Copy link
    Member

    merwok commented Dec 21, 2012

    Oops, sorry I slipped :)

    @meadori
    Copy link
    Member

    meadori commented Dec 21, 2012

    I noticed functools in the list. bpo-14373 was opened somewhat recently to reimplement functools.lru_cache in C. This issue seems to be promoting have more things implemented in pure Python. Someone involved in this issue might want to weigh in on bpo-14373 if that is the case.

    @pjenvey
    Copy link
    Member

    pjenvey commented Dec 21, 2012

    The guidelines for this are in PEP-399. Basically, adding 'accelerated' implementations when necessary isn't a bad thing as long as there are pure Python equivalents (unless it's a special case) and both are tested.

    bpo-14373's latest patch seems to be following the PEP

    @serhiy-storchaka
    Copy link
    Member

    I'm involved in both issues and I think it's good to have so much simple Python implementations as possible and to have C accelerators for any performance critical code.

    @brettcannon
    Copy link
    Member Author

    One thing I should say about this list of modules is please don't go nuts porting every single module blindly. There is always a possibility that another VM has already ported the code and has simply not contributed it back and so there is no need to write it from scratch and more just political wrangling to get contributions pushed upstream from other VMs. There might also be reasons to not worry about porting something. Always start a conversation first before starting a port; last thing I want is someone putting in the time to port some code that no one will necessarily use for a while.

    @serhiy-storchaka
    Copy link
    Member

    There is one additional benefit. I have already implemented audioop module in Python, and due to this it has found many bugs in the current C implementation (bpo-16686).

    @ericsnowcurrently
    Copy link
    Member

    Also missing a pure-Python implementation:

    collections.defaultdict (relatively trivial)
    collections.deque

    In the spirit of what Brett said, I found that PyPy has an implementation already:

    https://bitbucket.org/pypy/pypy/src/default/lib_pypy/_collections.py

    Jython and IronPython do not have pure Python implentations:
    http://hg.python.org/jython/file/default/src/org/python/modules/_collections
    http://ironpython.codeplex.com/SourceControl/changeset/view/99885#1091023

    @ezio-melotti ezio-melotti added the type-feature A feature request or enhancement label Jan 26, 2013
    @rhettinger
    Copy link
    Contributor

    I don't think itertools or collections.deque should go on this list. The other implementations already have these and a pure python versions miss the point of these being high performance tools. AFAICT, there is zero benefit to adding a bunch of extra code that will never get used. It will only complicate my maintenance of those modules.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests