views:

1012

answers:

8

All in the title :)

+1  A: 

Python runs in a virtual machine, so no.

BUT:

You could write a compiler that translates Python code to machine language. Once you've done that, you can do it.

Mark Lutton
i guessed that could not be. :( than you..
Armageddon
or you could embed a Python interpreter in the driver. Or talk Microsoft into including a Python interpreter in the kernel. Technically, there's no reason why it couldn't be done. Windows just doesn't currently offer you a lot of support for it. ;)
jalf
A: 

Never say never but eh.. no

You might be able to hack something together to run user-mode parts of drivers in python. But kernel-mode stuff can only be done in C or assembly.

Mendelt
booh :(thank you anyway :)i guess i have to learn C or assembly.. ;)
Armageddon
What about integrating the interpreter (written in C) into the driver and then executing Python script also stored in the driver as data?
Judge Maygarden
@Judge Maygarden: That might work in user-mode but kernel-mode is very restrictive on what calls can be made. Chances are your interpreter will not run there either. Kernel mode development is kind of a black art.
Mendelt
A: 

No they cannot. Windows drivers must be written in a language that can

  1. Interface with the C based API
  2. Compile down to machine code

Then again, there's nothing stopping you from writing a compiler that translates python to machine code ;)

JaredPar
hummm.. if i could be able to write this kind of compiler... i really wouldn't care of writing a driver in python :D
Armageddon
Necessary but not sufficient
MSalters
+1  A: 

I don't know the restrictions on drivers on windows (memory allocation schemes, dynamic load of libraries and all), but you may be able to embed a python interpreter in your driver, at which point you can do whatever you want. Not that I think it is a good idea :)

David Cournapeau
it will make a "fat" and "slow" driver :)
Armageddon
I was thinking the same thing. However, Lua would be a much preferable language for doing so IMHO.
Judge Maygarden
It'd be perfect for writing a driver for a 300 baud modem. ;-)
Nosredna
but slow anyway..
Armageddon
@Judge Maygarden, I can't see writing drivers in Lua either. C or assembly makes the most sense. If you don't do it in a language close to the metal, I don't see why one interpreted language would be very superior to another.
Nosredna
But you know what would be cool? A language that looks like Python but is procedural like C.
Nosredna
in what ways Python is not procedural? OOP is a subset of procedural...
Javier
+3  A: 

The definitive answer is not without embedding an interpreter in your otherwise C/assembly driver. Unless someone has a framework available, then the answer is no. Once you have the interpreter and bindings in place then the rest of the logic could be done in Python.

However, writing drivers is one of the things for which C is best suited. I imagine the resulting Python code would look a whole lot like C code and defeat the purpose of the interpreter overhead.

Judge Maygarden
+2  A: 

A good way to gain insight why this is practically impossible is by reading Microsoft's advice on the use of C++ in drivers. As a derivative of C, the use of C++ appears to be straightforward. In practice, not so.

For instance, you must decide for every function (and really every assembly instruction) whether it's in pageable or non-pageable memory. This requires extensions to C, careful use of new C++ features, or in this case a special extension to the Python language and VM. In addition, your driver-compatible VM would also have to deal with the different IRQLs - there's a hierarchy of "levels" which restrict what you can and cannot do.

MSalters
+9  A: 

Yes. You cannot create the "classic" kernel-mode drivers. However, starting with XP, Windows offers a User-Mode Driver Framework. They can't do everything, obviously - any driver used in booting the OS obviously has to be kernel-mode. But with UMDF, you only need to implement COM components.

Besides boot-time drivers, you also can't write UMDF drivers that:

  • Handle interrupts
  • Directly access hardware, such as direct memory access (DMA)
  • have strict timing loops
  • Use nonpaged pool or other resources that are reserved for kernel mode
MSalters
I was about to write the standard "Writing driver in Python = Crazy" response to the question, but I actually think a UMDF driver would work. Interesting!
Paul Betts
Interesting is the word. I'll study that..
Armageddon
A: 

Yes you can :) Just you need know deeper python and C you can do anything with python

mentalx