Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: Looking for ideas to improve library API Date: Thu, 26 Nov 2015 12:36:21 +0000 Lines: 43 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de +dKt4KFuCFji9T1t9TRZtwzWAfqBuCxfUkEGwBvZ7dQA== 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; "'w')": 0.09; 'descriptor': 0.09; 'open()': 0.09; 'subject:library': 0.09; 'url:github': 0.09; "'r')": 0.16; 'appreciated!': 0.16; 'complains': 0.16; 'descriptor.': 0.16; 'files)': 0.16; 'fp.close()': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'missed.': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:ideas': 0.16; 'wrote:': 0.16; 'library,': 0.18; 'passes': 0.18; 'typical': 0.18; 'input': 0.18; 'library': 0.20; 'fine,': 0.22; 'ok.': 0.22; 'parse': 0.22; 'file.': 0.22; 'pass': 0.22; 'leave': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'disk': 0.27; 'developing': 0.28; 'i/o': 0.29; 'objects': 0.29; "i'm": 0.30; 'operations': 0.31; 'another': 0.32; 'especially': 0.32; 'received:84': 0.32; 'point': 0.33; 'problem': 0.33; 'open': 0.33; 'file': 0.34; 'add': 0.34; 'something': 0.35; 'but': 0.36; 'there': 0.36; '(and': 0.36; 'to:addr:python-list': 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; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'hello,': 0.40; 'close': 0.61; 'skip:u 10': 0.61; 'entire': 0.61; 'subject:improve': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MbeRwMLf c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=IkcTkHD0fZMA:10 a=NEAV23lmAAAA:8 a=Vo_CO2_6H24PZnPcNbYA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 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:99567 On 2015-11-25 20:52, 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! > Why pass a file descriptor? Why not a filename?