Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.04; 'anyway.': 0.05; 'subject:Python': 0.06; 'matches': 0.07; 'skip:` 10': 0.07; 'versions.': 0.07; 'apis': 0.09; 'builtin': 0.09; 'password)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'strings.': 0.09; 'subject:design': 0.09; 'subject:skip:c 10': 0.09; 'runs': 0.10; 'api': 0.11; 'python': 0.11; '"w")': 0.16; 'api,': 0.16; 'bit)': 0.16; 'cleaner': 0.16; 'expects': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'schwarzer': 0.16; 'subclass': 0.16; 'subject:API': 0.16; 'underlying': 0.16; 'world!")': 0.16; 'wrote:': 0.18; 'library': 0.18; 'skip:f 30': 0.19; 'stefan': 0.19; 'version.': 0.19; 'header :User-Agent:1': 0.23; 'byte': 0.24; 'bytes': 0.24; 'unicode': 0.24; 'regardless': 0.24; 'server.': 0.24; 'versions': 0.24; 'file.': 0.24; '(or': 0.24; "i've": 0.25; 'developers': 0.25; '(see': 0.26; 'this:': 0.26; 'least': 0.26; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; '[1]': 0.29; 'character': 0.29; 'respective': 0.29; "doesn't": 0.30; 'returned': 0.30; "i'm": 0.30; 'code': 0.31; 'anyone': 0.31; 'run': 0.32; 'text': 0.33; 'open': 0.33; 'running': 0.33; 'moment': 0.34; 'classes': 0.35; 'possible.': 0.35; 'but': 0.35; 'there': 0.35; 'returning': 0.36; 'should': 0.36; 'changing': 0.37; 'example,': 0.37; 'two': 0.37; 'clear': 0.37; 'remote': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'issue': 0.38; 'pm,': 0.38; 'rather': 0.38; 'anything': 0.39; 'supporting': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'users': 0.40; 'subject: / ': 0.60; 'most': 0.60; 'break': 0.61; 'received:173': 0.61; 'simply': 0.61; 'offer': 0.62; 'became': 0.64; 'more': 0.64; 'different': 0.65; 'here': 0.66; 'approaches': 0.68; 'difficulty': 0.68; 'user,': 0.69; 'information:': 0.72; 'behavior': 0.77; 'reading,': 0.84; 'received:fios.verizon.net': 0.84; 'opens': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Jan Reedy Subject: Re: API design for Python 2 / 3 compatibility Date: Sat, 13 Apr 2013 13:32:51 -0400 References: <51698989.2070805@sschwarzer.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 In-Reply-To: <51698989.2070805@sschwarzer.net> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 92 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365874394 news.xs4all.nl 2686 [2001:888:2000:d::a6]:60160 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43532 On 4/13/2013 12:36 PM, Stefan Schwarzer wrote: > Hello, > > I'm currently changing the FTP client library ftputil [1] > so that the same code of the library works with Python > 2 (2.6 and up) and Python 3. (At the moment the code is for > Python 2 only.) I've run into a API design issue where I > don't know which API I should offer ftputil users under > Python 2 and Python 3. > > [1] http://ftputil.sschwarzer.net/ > > Some important background information: A key idea in ftputil > is that it uses the same APIs as the Python standard library > where possible. For example, with ftputil you can write code > like this: > > with ftputil.FTPHost(host, user, password) as ftp_host: > # Like `os.path.isdir`, but works on the FTP server. > if ftp_host.path.isdir("hello_dir"): > # Like `os.chdir`, but works on the FTP server. > ftp_host.chdir("hello_dir") > # Like the `open` builtin, but opens a remote file. > with ftp_host.open("new_file", "w") as fobj: > # Like `file.write` and `file.close` > fobj.write("Hello world!") > fobj.close() > > Since most of Python 2's and Python 3's filesystem-related > APIs accept either bytes and character strings (and return > the same type if they return anything string-like at all), > the design here is rather clear to me. > > However, I have some difficulty with ftputil's counterpart > of the `open` builtin function when files are opened for > reading in text mode. Here are the approaches I've been > thinking of so far: > > * Approach 1 > > When opening remote text files for reading, ftputil will > return byte strings from `read(line/s)` when run under > Python 2 and unicode strings when run under Python 3. > > Pro: Each of the Python versions has ftputil behavior > which matches the Python standard library behavior of > the respective Python version. > > Con: Developers who want to use ftputil under Python 2 > _and_ 3 have to program against two different APIs since > their code "inherits" ftputil's duality. > > Con: Support for two different APIs will make the > ftputil code (at least a bit) more complicated than just > returning unicode strings under both Python versions. > > * Approach 2 > > When opening remote text files for reading, ftputil will > always return unicode strings from `read(line/s)`, > regardless of whether it runs under Python 2 or Python 3. > > Pro: Uniform API, independent on underlying Python > version. > > Pro: Supporting a single API will result in cleaner code > in ftputil than when supporting different APIs (see > above). > > Con: This approach might break some code which expects > the returned strings under Python 2 to be byte strings. > > Con: Developers who only use Python 2 might be confused > if ftputil returns unicode strings from `read(line/s)` > since this behavior doesn't match files opened with > `open` in Python 2. > > Which approach do you recommend and why do you prefer that > approach? Are there other approaches I have overlooked? Do > you have other suggestions? Approach 2 matches (or should match) io.open, which became builtin open in Python 3. I would simply document that ftp_host.open mimics io.open in the same way that ftp_host.chdir, etcetera, match os.chdir, etc. Your principle will remain intact. Anyone writing *new* Py 2 code with any idea of ever running on Py 3 should be using io.open anyway. That is why it was backported. You might be able to reuse some io code or subclass some io classes for your implementation.