0
>>> import cStringIO
>>> a = cStringIO.StringIO()
>>> type(a)
<type 'cStringIO.StringO'>
>>> isinstance(a, cStringIO.StringO)

Traceback (most recent call last):
  File "<pyshell#223>", line 1, in <module>
    isinstance(a, cStringIO.StringO)
AttributeError: 'module' object has no attribute 'StringO'

I need to return some data (a file-like object) to wsgi app. That data would be loaded to an cStringIO object if it is not already a cStringIO object(because I dont want to re-read memory again) but isinstance(a, cStringIO.StringO) or isinstance(a, cStringIO.StringIO) both throw an exception. How can I do check whether an instance is cStringIO object?

thkang
  • 11,215
  • 14
  • 67
  • 83

1 Answers1

8

Use cStringIO.InputType and cStringIO.OutputType instead:

>>> import cStringIO
>>> s = cStringIO.StringIO('output')
>>> isinstance(s, cStringIO.InputType)
True
>>> s = cStringIO.StringIO()
>>> isinstance(s, cStringIO.OutputType)
True
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    what is the difference between them? – thkang Feb 06 '13 at 17:38
  • 1
    @thkang: The `cStringIO` module optimizes for input *or* output. The first example can be read from (it has data), the other can be written to (it is initialized as empty). – Martijn Pieters Feb 06 '13 at 17:39
  • There's also a bit of a typo? `AttributeError: 'module' object has no attribute 'StringO'` - which could be the actual problem... – Jon Clements Feb 06 '13 at 17:39
  • @JonClements type(a) said it is `StringO`, and it seems like pure python cannot access them. – thkang Feb 06 '13 at 17:41
  • @thkang I stand suitably chastised for not paying attention :) – Jon Clements Feb 06 '13 at 17:43
  • @MartijnPieters Now everything works after `if isinstance(wsgi_input_obj, (StringIO.InputType, StringIO.OutputType)):`. I have to do this because PyISAPIe has no support for `readline` method in `environ` variable (the author of PyISAPIe did not implement this, since it is not mandatory in wsgi PEP ... ). My hack is reading them into memory file and serving it to WSGI as if nothing was happened. Would this affect my server's performance? – thkang Feb 06 '13 at 17:47
  • @JonClements: No, the types are named `StringO` and `StringI` actually; input and output are treated separately in the `cStringIO` optimized versions. – Martijn Pieters Feb 06 '13 at 17:48
  • @thkang: I'd build a wrapper around it that adds a `.readline()` implementation, I guess; unless there is some sort of protection in IIS against overly large request bodies, that could affect performance as memory get's filled up with a large request. – Martijn Pieters Feb 06 '13 at 17:50
  • @MartijnPieters How are large request bodies handled in IIS? shouldn't they be loaded into the memory of my server after user uploads anyway? – thkang Feb 06 '13 at 17:52
  • @thkang: A proper implementation would be using a memory or disk-based buffer in-between the network socket and your WSGI handler, not read it *all* into memory before sending it on. – Martijn Pieters Feb 06 '13 at 17:55