classification
Title: Importing ctypes.wintypes on Linux gives a ValueError instead of an ImportError
Type: behavior Stage: patch review
Components: ctypes Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: benjamin.peterson Nosy List: Mike Place, amaury.forgeotdarc, belopolsky, benjamin.peterson, christian.heimes, dmi.baranov, ezio.melotti, jason.coombs, meador.inge, techtonik
Priority: normal Keywords: patch

Created on 2012-11-03 15:22 by techtonik, last changed 2017-01-01 18:15 by ppperry.

Files
File name Uploaded Description Edit
16396_vbool.patch christian.heimes, 2013-06-24 12:17 review
Messages (17)
msg174634 - (view) Author: anatoly techtonik (techtonik) Date: 2012-11-03 15:22
>>> import ctypes.wintypes
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.2/ctypes/wintypes.py", line 20, in <module>
    class VARIANT_BOOL(ctypes._SimpleCData):
ValueError: _type_ 'v' not supported
>>> 


Shouldn't it just import silently without failing? Or if it's destined to fail, explain how to make a cross-platform import?
msg174635 - (view) Author: anatoly techtonik (techtonik) Date: 2012-11-03 15:24
Perhaps the patch already there - see http://www.themacaque.com/?p=826
msg188245 - (view) Author: Dmi Baranov (dmi.baranov) * Date: 2013-05-01 22:16
Found only this "patch" [1] :) I think is possible to change VARIANT_BOOL._type_ to any of short types [2] for non-"nt" platforms?

[1] https://code.launchpad.net/~mandel/python-distutils-extra/import_issues/+merge/53519
[2] http://msdn.microsoft.com/en-us/library/cc237864.aspx
msg188707 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-05-08 08:05
That patch is more a workaround than an actual fix.  Lib/ctypes/wintypes.py should either fail with an ImportError or be importable.  For the former it's possible to catch the ValueError and turn it into an ImportError, or perhaps raise it if some precondition is missing; for the latter, either the creation of that signle class is skipped if _type_ 'v' is not supported, or a way to define it that works on other platforms too should be found instead.
msg191756 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-06-24 12:17
The fix is trivial: 

- define VARIANT_FALSE and VARIANT_BOOL according to specs
- enable 'v' on non-Windows systems
- enable tests for vbool

Done
msg191763 - (view) Author: Jason R. Coombs (jason.coombs) * (Python committer) Date: 2013-06-24 13:56
Looks good to me. Ben, any objections to applying this to 2.7?
msg192324 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-04 23:48
RM, please decide. :)
msg194927 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-08-12 05:50
Smells like a new feature to me.
msg194948 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-08-12 12:11
Even if the patch is applied only on 3.4, I would still like to see the ValueError turned into ImportError for 2.7/3.3.
msg194985 - (view) Author: Jason R. Coombs (jason.coombs) * (Python committer) Date: 2013-08-12 18:58
My sense on the issue is that wintypes was added to the library and was never intended to raise a ValueError on import. By that logic, the behavior is a bug, not a new feature. I agree with Ezio that raising a ValueError on import is a bug. And since the patch not only addresses the ValueError on import, but simply addresses the underlying cause, it seems to me the most obvious solution.
msg195652 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2013-08-19 17:48
I think it's the opposite: when Unix support was added to ctypes, 'import ctypes.wintypes' was not considered. By that logic, the patch is a new feature.
IMO "historical" arguments are moot :-)

I agree with the conclusion tough: the patch will not break code that carefully catches ValueError, and so it suitable for 2.7.
msg195675 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2013-08-20 01:42
On Mon, Aug 12, 2013 at 7:11 AM, Ezio Melotti <report@bugs.python.org>wrote:

> Even if the patch is applied only on 3.4, I would still like to see the
> ValueError turned into ImportError for 2.7/3.3.
>

Why not raise an ImportError for all versions?  I don't see how
'ctypes.wintypes' is ever actually useful on anything but Windows.
 Allowing it to successfully import on non-Windows systems seems misleading.
msg195825 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-08-21 21:36
We can argue about whether it's a bugfix or not. But I don't see how having this patch in 2.7 helps anyone, since ctypes.wintypes is useless on non-Windows platforms.
msg195924 - (view) Author: Jason R. Coombs (jason.coombs) * (Python committer) Date: 2013-08-22 21:39
The first thing it helps is that it eliminates a ValueError on import. Without it, code must catch both ValueError and ImportError to run portably:

import ctypes

try:
  import ctypes.wintypes
except ImportError, ValueError:
  # catch ImportError and ValueError due to issue16396
  pass

...

def code_that_runs_only_on_win():
  ctypes.wintypes.foo

One _could_ cause ctypes.wintypes to always raise an ImportError on non-Windows systems, allowing the import routine to be simpler:

try:
  import ctypes.wintypes
except ImportError:
  pass

But it would be even nicer if ctypes.wintypes always imported on any platform, such that the statement could be simply:

import ctypes.wintypes

But it's conceivable that certain functionality in wintypes might be useful on other systems. Consider, for example, a routine that works with pickles produced on a Windows system, or simply a comparison of some object in ctypes.wintypes against a value produced on a Windows system.

The argument for that need (esp. on Python 2.7) is not strong, but since the patch to address the ValueError is simple, straightforward, and perhaps even simpler than something that would address the ValueError specifically, it seems worthwhile to accept the patch.
msg195926 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2013-08-22 21:54
Using 'ctypes.wintypes' on non-Windows systems is most likely a bad idea.  Most of the types are defined in terms of the types for the target that the interpreter is built for.  Comparing serializations thereof in a cross platform manner doesn't make sense for a lot of cases.  

I really think it is only useful for Windows platforms and allowing it to silently import is misleading.
msg195940 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-08-23 04:24
This is not a regression, though, so most people wouldn't be able to simplify their code because they have to support older Python versions.
msg284191 - (view) Author: Mike Place (Mike Place) Date: 2016-12-28 18:41
+1 to getting this patch in. The fact that this raises a ValueError and not an ImportError is really annoying and we definitely see it as a bug.
History
Date User Action Args
2017-01-01 18:15:16ppperrysettitle: Importing ctypes.wintypes on Linux gives a traceback -> Importing ctypes.wintypes on Linux gives a ValueError instead of an ImportError
2016-12-28 18:41:07Mike Placesetnosy: + Mike Place
messages: + msg284191
2013-08-23 04:24:01benjamin.petersonsetmessages: + msg195940
2013-08-22 21:54:45meador.ingesetmessages: + msg195926
2013-08-22 21:39:03jason.coombssetmessages: + msg195924
2013-08-21 21:36:07benjamin.petersonsetmessages: + msg195825
2013-08-20 01:42:30meador.ingesetmessages: + msg195675
2013-08-19 17:48:54amaury.forgeotdarcsetmessages: + msg195652
2013-08-12 18:58:20jason.coombssetmessages: + msg194985
2013-08-12 12:11:20ezio.melottisetmessages: + msg194948
2013-08-12 05:50:01benjamin.petersonsetmessages: + msg194927
2013-07-04 23:48:47christian.heimessetassignee: benjamin.peterson
messages: + msg192324
stage: needs patch -> patch review
2013-06-24 13:56:57jason.coombssetnosy: + benjamin.peterson
messages: + msg191763
2013-06-24 12:17:08christian.heimessetfiles: + 16396_vbool.patch

nosy: + christian.heimes
messages: + msg191756

keywords: + patch
2013-06-24 08:32:53jason.coombssetnosy: + jason.coombs
2013-05-08 08:05:48ezio.melottisetmessages: + msg188707
2013-05-01 22:16:42dmi.baranovsetnosy: + dmi.baranov
messages: + msg188245
2013-05-01 12:23:50ezio.melottisetnosy: + ezio.melotti
stage: needs patch
type: behavior

versions: + Python 3.4, - Python 3.2
2012-11-11 17:20:18terry.reedysetnosy: + amaury.forgeotdarc, belopolsky, meador.inge

versions: - Python 3.1
2012-11-03 15:24:30techtoniksetmessages: + msg174635
2012-11-03 15:22:51techtonikcreate