Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Devin Jeanpierre Newsgroups: comp.lang.python Subject: Re: Looking for ideas to improve library API Date: Thu, 26 Nov 2015 04:46:34 -0800 Lines: 51 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de xd0k5hrjNfHaB44+X0xzgw/ol0vAdQ7Arub0TAumP4Wg== 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; 'context': 0.05; 'memory.': 0.05; 'modified': 0.05; 'f.close()': 0.07; 'method,': 0.07; 'api': 0.09; 'cc:addr:python-list': 0.09; "'w')": 0.09; 'descriptor': 0.09; 'open()': 0.09; 'subject:library': 0.09; 'url:github': 0.09; 'wed,': 0.15; "'r')": 0.16; 'appreciated!': 0.16; 'complains': 0.16; 'descriptor.': 0.16; 'files)': 0.16; 'fp.close()': 0.16; 'missed.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:ideas': 0.16; 'wrote:': 0.16; 'library,': 0.18; 'ownership': 0.18; 'passes': 0.18; 'typical': 0.18; 'input': 0.18; 'library': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fine,': 0.22; 'ok.': 0.22; 'parse': 0.22; 'file.': 0.22; 'leave': 0.23; 'this:': 0.23; 'thanks,': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'disk': 0.27; 'object,': 0.27; 'developing': 0.28; 'i/o': 0.29; 'objects': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'operations': 0.31; 'another': 0.32; 'especially': 0.32; 'point': 0.33; 'problem': 0.33; 'url:python': 0.33; 'open': 0.33; 'url:listinfo': 0.34; 'file': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'nov': 0.35; 'requiring': 0.35; 'something': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; '(and': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'done.': 0.37; 'thought': 0.37; 'skip:o 20': 0.38; 'means': 0.39; 'why': 0.39; 'along': 0.39; 'url:mail': 0.40; 'called': 0.40; 'hello,': 0.40; 'close': 0.61; 'skip:u 10': 0.61; 'entire': 0.61; 'subject:improve': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=x6PWfUhwOuWPYrxE9mOwqd5iVHEgTqeP8Nuho/ti57g=; b=nj04ebNVR0FaUIePO6tM09f3IjtnazXqOZYGf2XP4mYX0DDiBtIkrmrFH1kItNHDcm Ovmgqr2mU5vM/w/l27Onul9XqTb/mWnAgi1mkw+q4rCGEKlNYZ8Hnu3KPg/Q1z6/a04A HHBdNHWurP1h+4Q4XEXtdS/vpRcKL6KrODZNMZvQToNFysqDM1Z/Ws1mCkKoaObgjXMe g9l28njQWSf9/YnmrtgPyClOwO42IPEwz3vRTpZ2cfDhRL4PKx6OScM6NCHJU7TrDXP1 p75WeJV29HyWZaaZsXe7hxWXHlXP1hXqSdghfLBhBvpnxNVHZNKWD8mRqm92jufRSvof QRGg== X-Received: by 10.194.134.72 with SMTP id pi8mr47542718wjb.34.1448542034296; Thu, 26 Nov 2015 04:47:14 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99568 Why not take ownership of the file object, instead of requiring users to manage lifetimes? -- Devin On Wed, Nov 25, 2015 at 12:52 PM, Chris Lalancette wrote: > Hello, > I'm currently developing a library called pyiso ( > https://github.com/clalancette/pyiso), used for manipulating ISO disk > images. I'm pretty far along with it, but there is one part of the API > that I really don't like. > Typical usage of the library is something like: > > import pyiso > > p = pyiso.PyIso() // create the object > f = open('/path/to/original.iso', 'r') > p.open(f) // parse all of the metadata from the input ISO > fp = open('/path/to/file/to/add/to/iso', 'r') > p.add_fp(fp) // add a new file to the ISO > out = open('/path/to/modified.iso', 'w') > p.write(out) // write out the modified ISO to another file > out.close() > fp.close() > f.close() > > This currently works OK. The problem ends up being the file descriptor > lifetimes. I want the user to be able to do multiple operations to the > ISO, and I also don't want to read the entire ISO (and new files) into > memory. That means that internal to the library, I take a reference to the > file object that the user passes in during open() and add_fp(). This is > fine, unless the user decides to close the file object before calling the > write method, at which point the write complains of I/O to a closed file. > This is especially problematic when it comes to using context managers, > since the user needs to leave the context open until they call write(). > I've thought of a couple ways to deal with this: > > 1. Make a copy of the file object internal to the library, using os.dup() > to copy the file descriptor. This is kind of nasty, especially since I > want to support other kinds of file objects (think StringIO). > 2. Just document the fact that the user needs to leave the file objects > open until they are done. This is simple, but not super user-friendly. > > I'm looking for any ideas of how to do this better, or something I missed. > Any input is appreciated! > > Thanks, > Chris Lalancette > -- > https://mail.python.org/mailman/listinfo/python-list