Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31164 > unrolled thread

Checking for dlls in ctypes

Started byWanderer <wanderer@dialup4less.com>
First post2012-10-12 08:36 -0700
Last post2012-10-13 06:40 -0700
Articles 9 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Checking for dlls in ctypes Wanderer <wanderer@dialup4less.com> - 2012-10-12 08:36 -0700
    Re: Checking for dlls in ctypes Dave Angel <d@davea.name> - 2012-10-12 12:28 -0400
      Re: Checking for dlls in ctypes Wanderer <wanderer@dialup4less.com> - 2012-10-12 10:57 -0700
      Re: Checking for dlls in ctypes Wanderer <wanderer@dialup4less.com> - 2012-10-12 10:57 -0700
      Re: Checking for dlls in ctypes Nobody <nobody@nowhere.com> - 2012-10-13 12:32 +0100
    Re: Checking for dlls in ctypes MRAB <python@mrabarnett.plus.com> - 2012-10-12 17:57 +0100
      Re: Checking for dlls in ctypes Wanderer <wanderer@dialup4less.com> - 2012-10-12 10:52 -0700
      Re: Checking for dlls in ctypes Wanderer <wanderer@dialup4less.com> - 2012-10-12 10:52 -0700
    Re: Checking for dlls in ctypes 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-13 06:40 -0700

#31164 — Checking for dlls in ctypes

FromWanderer <wanderer@dialup4less.com>
Date2012-10-12 08:36 -0700
SubjectChecking for dlls in ctypes
Message-ID<975da919-c70a-492a-9d49-62254a76eb6c@googlegroups.com>
I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?

        try:
            self.dll = windll.pvcam64
        except:
            print "No pvcam64"
            try:
                self.dll = windll.pvcam32
            except:
                print "No pvcam32"
                try:
                    self.dll = windll.pvcam
                except:
                    print "No pvcam"
                    return
                else:
                    print "installed pvcam"
            else:
                print "installed pvcam32"
        else:
            print "installed pvcam64"

[toc] | [next] | [standalone]


#31166

FromDave Angel <d@davea.name>
Date2012-10-12 12:28 -0400
Message-ID<mailman.2092.1350059331.27098.python-list@python.org>
In reply to#31164
On 10/12/2012 11:36 AM, Wanderer wrote:
> I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?
>
>         try:
>             self.dll = windll.pvcam64
>         except:
>             print "No pvcam64"
>             try:
>                 self.dll = windll.pvcam32
>             except:
>                 print "No pvcam32"
>                 try:
>                     self.dll = windll.pvcam
>                 except:
>                     print "No pvcam"
>                     return
>                 else:
>                     print "installed pvcam"
>             else:
>                 print "installed pvcam32"
>         else:
>             print "installed pvcam64"
>

I can't help you find the dll's, because I don't run Windows.  But I
could help you write a clearer question:

"doesn't work" is thoroughly useless for describing errors.  If you're
getting an exception, show us the full traceback.  That will show which
statement got the exception that wasn't caught.  Next question is which
of the dlls is missing.  Are you getting an exception because it's
missing or because of something more fundamental, like nesting exception
handlers?

Using bare excepts is almost never a good idea.  If it "works" you get
no clues what went wrong.  For example, a typo in source code can
trigger a bare exception, as can a user typing Ctrl-C.   So when you're
using bare excepts, you have robbed the user of any way to terminate the
program.

If I were you, I'd be writing a loop so there's only one try block.  Too
much duplicated code in the way you're doing it.



-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#31172

FromWanderer <wanderer@dialup4less.com>
Date2012-10-12 10:57 -0700
Message-ID<f8610ca5-d92d-4755-96b1-4abd79de6a0f@googlegroups.com>
In reply to#31166
On Friday, October 12, 2012 12:29:02 PM UTC-4, Dave Angel wrote:
> On 10/12/2012 11:36 AM, Wanderer wrote:
> 
> > I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?
> 
> >
> 
> >         try:
> 
> >             self.dll = windll.pvcam64
> 
> >         except:
> 
> >             print "No pvcam64"
> 
> >             try:
> 
> >                 self.dll = windll.pvcam32
> 
> >             except:
> 
> >                 print "No pvcam32"
> 
> >                 try:
> 
> >                     self.dll = windll.pvcam
> 
> >                 except:
> 
> >                     print "No pvcam"
> 
> >                     return
> 
> >                 else:
> 
> >                     print "installed pvcam"
> 
> >             else:
> 
> >                 print "installed pvcam32"
> 
> >         else:
> 
> >             print "installed pvcam64"
> 
> >
> 
> 
> 
> I can't help you find the dll's, because I don't run Windows.  But I
> 
> could help you write a clearer question:
> 
> 
> 
> "doesn't work" is thoroughly useless for describing errors.  If you're
> 
> getting an exception, show us the full traceback.  That will show which
> 
> statement got the exception that wasn't caught.  Next question is which
> 
> of the dlls is missing.  Are you getting an exception because it's
> 
> missing or because of something more fundamental, like nesting exception
> 
> handlers?
> 
> 
> 
> Using bare excepts is almost never a good idea.  If it "works" you get
> 
> no clues what went wrong.  For example, a typo in source code can
> 
> trigger a bare exception, as can a user typing Ctrl-C.   So when you're
> 
> using bare excepts, you have robbed the user of any way to terminate the
> 
> program.
> 
> 
> 
> If I were you, I'd be writing a loop so there's only one try block.  Too
> 
> much duplicated code in the way you're doing it.
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

Sorry. It was a WindowsError, but the code I posted now works for me and I can't reproduce the problem. I'll be more diligent in the future.

[toc] | [prev] | [next] | [standalone]


#31173

FromWanderer <wanderer@dialup4less.com>
Date2012-10-12 10:57 -0700
Message-ID<mailman.2098.1350064633.27098.python-list@python.org>
In reply to#31166
On Friday, October 12, 2012 12:29:02 PM UTC-4, Dave Angel wrote:
> On 10/12/2012 11:36 AM, Wanderer wrote:
> 
> > I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?
> 
> >
> 
> >         try:
> 
> >             self.dll = windll.pvcam64
> 
> >         except:
> 
> >             print "No pvcam64"
> 
> >             try:
> 
> >                 self.dll = windll.pvcam32
> 
> >             except:
> 
> >                 print "No pvcam32"
> 
> >                 try:
> 
> >                     self.dll = windll.pvcam
> 
> >                 except:
> 
> >                     print "No pvcam"
> 
> >                     return
> 
> >                 else:
> 
> >                     print "installed pvcam"
> 
> >             else:
> 
> >                 print "installed pvcam32"
> 
> >         else:
> 
> >             print "installed pvcam64"
> 
> >
> 
> 
> 
> I can't help you find the dll's, because I don't run Windows.  But I
> 
> could help you write a clearer question:
> 
> 
> 
> "doesn't work" is thoroughly useless for describing errors.  If you're
> 
> getting an exception, show us the full traceback.  That will show which
> 
> statement got the exception that wasn't caught.  Next question is which
> 
> of the dlls is missing.  Are you getting an exception because it's
> 
> missing or because of something more fundamental, like nesting exception
> 
> handlers?
> 
> 
> 
> Using bare excepts is almost never a good idea.  If it "works" you get
> 
> no clues what went wrong.  For example, a typo in source code can
> 
> trigger a bare exception, as can a user typing Ctrl-C.   So when you're
> 
> using bare excepts, you have robbed the user of any way to terminate the
> 
> program.
> 
> 
> 
> If I were you, I'd be writing a loop so there's only one try block.  Too
> 
> much duplicated code in the way you're doing it.
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

Sorry. It was a WindowsError, but the code I posted now works for me and I can't reproduce the problem. I'll be more diligent in the future.

[toc] | [prev] | [next] | [standalone]


#31191

FromNobody <nobody@nowhere.com>
Date2012-10-13 12:32 +0100
Message-ID<pan.2012.10.13.11.32.15.146000@nowhere.com>
In reply to#31166
On Fri, 12 Oct 2012 12:28:17 -0400, Dave Angel wrote:

> Using bare excepts is almost never a good idea.  If it "works" you get no
> clues what went wrong.  For example, a typo in source code can trigger a
> bare exception, as can a user typing Ctrl-C.   So when you're using bare
> excepts, you have robbed the user of any way to terminate the program.

If you want to catch any error, use "except StandardError:". That covers
all errors but not things like KeyboardInterrupt (Ctrl-C) or SystemExit
(sys.exit()).

In situations such as this, where you try multiple candidates until one
succeeds, there's no reason to be any more specific than that. In any
case, Python's lack of formal interfaces makes it hard to reliably be more
specific.

However: you should bear in mind that loading the wrong DLL may just
result in an OS-level exception (e.g. segfault), which can't be caught.
It's preferable to allow the DLL to be explicitly selected e.g. in a
configuration file.

[toc] | [prev] | [next] | [standalone]


#31168

FromMRAB <python@mrabarnett.plus.com>
Date2012-10-12 17:57 +0100
Message-ID<mailman.2094.1350061025.27098.python-list@python.org>
In reply to#31164
On 2012-10-12 16:36, Wanderer wrote:
> I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?
>
>          try:
>              self.dll = windll.pvcam64
>          except:
>              print "No pvcam64"
>              try:
>                  self.dll = windll.pvcam32
>              except:
>                  print "No pvcam32"
>                  try:
>                      self.dll = windll.pvcam
>                  except:
>                      print "No pvcam"
>                      return
>                  else:
>                      print "installed pvcam"
>              else:
>                  print "installed pvcam32"
>          else:
>              print "installed pvcam64"
>
This works for me:

     for name in ("pvcam64", "pvcam32", "pvcam"):
         try:
             self.dll = getattr(windll, name)
         except OSError:
             print "No " + name
         else:
             print "Installed " + name
             return

[toc] | [prev] | [next] | [standalone]


#31170

FromWanderer <wanderer@dialup4less.com>
Date2012-10-12 10:52 -0700
Message-ID<cd188643-162a-483f-bb68-6ea48c50a503@googlegroups.com>
In reply to#31168
On Friday, October 12, 2012 12:57:06 PM UTC-4, MRAB wrote:
> On 2012-10-12 16:36, Wanderer wrote:
> 
> > I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?
> 
> >
> 
> >          try:
> 
> >              self.dll = windll.pvcam64
> 
> >          except:
> 
> >              print "No pvcam64"
> 
> >              try:
> 
> >                  self.dll = windll.pvcam32
> 
> >              except:
> 
> >                  print "No pvcam32"
> 
> >                  try:
> 
> >                      self.dll = windll.pvcam
> 
> >                  except:
> 
> >                      print "No pvcam"
> 
> >                      return
> 
> >                  else:
> 
> >                      print "installed pvcam"
> 
> >              else:
> 
> >                  print "installed pvcam32"
> 
> >          else:
> 
> >              print "installed pvcam64"
> 
> >
> 
> This works for me:
> 
> 
> 
>      for name in ("pvcam64", "pvcam32", "pvcam"):
> 
>          try:
> 
>              self.dll = getattr(windll, name)
> 
>          except OSError:
> 
>              print "No " + name
> 
>          else:
> 
>              print "Installed " + name
> 
>              return

Yes that works for me, too. Thanks

[toc] | [prev] | [next] | [standalone]


#31171

FromWanderer <wanderer@dialup4less.com>
Date2012-10-12 10:52 -0700
Message-ID<mailman.2097.1350064361.27098.python-list@python.org>
In reply to#31168
On Friday, October 12, 2012 12:57:06 PM UTC-4, MRAB wrote:
> On 2012-10-12 16:36, Wanderer wrote:
> 
> > I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?
> 
> >
> 
> >          try:
> 
> >              self.dll = windll.pvcam64
> 
> >          except:
> 
> >              print "No pvcam64"
> 
> >              try:
> 
> >                  self.dll = windll.pvcam32
> 
> >              except:
> 
> >                  print "No pvcam32"
> 
> >                  try:
> 
> >                      self.dll = windll.pvcam
> 
> >                  except:
> 
> >                      print "No pvcam"
> 
> >                      return
> 
> >                  else:
> 
> >                      print "installed pvcam"
> 
> >              else:
> 
> >                  print "installed pvcam32"
> 
> >          else:
> 
> >              print "installed pvcam64"
> 
> >
> 
> This works for me:
> 
> 
> 
>      for name in ("pvcam64", "pvcam32", "pvcam"):
> 
>          try:
> 
>              self.dll = getattr(windll, name)
> 
>          except OSError:
> 
>              print "No " + name
> 
>          else:
> 
>              print "Installed " + name
> 
>              return

Yes that works for me, too. Thanks

[toc] | [prev] | [next] | [standalone]


#31193

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-10-13 06:40 -0700
Message-ID<f50647a5-0117-4e82-aaab-1792d838ed2b@googlegroups.com>
In reply to#31164
Wanderer於 2012年10月12日星期五UTC+8下午11時36分27秒寫道:
> I'm trying to write some code that will load one of three dll depending on the one available. I've tried the code below, but it doesn't work. The try except doesn't catch the exception. Is there a way to do this?
> 
> 
> 
>         try:
> 
>             self.dll = windll.pvcam64
> 
>         except:
> 
>             print "No pvcam64"
> 
>             try:
> 
>                 self.dll = windll.pvcam32
> 
>             except:
> 
>                 print "No pvcam32"
> 
>                 try:
> 
>                     self.dll = windll.pvcam
> 
>                 except:
> 
>                     print "No pvcam"
> 
>                     return
> 
>                 else:
> 
>                     print "installed pvcam"
> 
>             else:
> 
>                 print "installed pvcam32"
> 
>         else:
> 
>             print "installed pvcam64"

In linux  there are shared libraries that could be linked in the runtime.

But for the security concerns this requres the system administrator account 
to install shared libraries.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web