AttributeError: 'module' object has no attribute 'fork'

Rustom Mody rustompmody at gmail.com
Fri Aug 8 01:19:27 EDT 2014


On Thursday, August 7, 2014 10:26:56 PM UTC+5:30, Steven D'Aprano wrote:
> Roy Smith wrote:

> >  Peter Otten  wrote:
> >> os.fork()
> >> Fork a child process.
> >> ...
> >> Availability: Unix.
> >> """
> >> You are using the wrong operating system ;)
> > To be honest, this could be considered a buglet in the os module.  It
> > really should raise:
> > NotImplementedError("fork() is only available on unix")
> > or perhaps even, as Peter suggests:
> > NotImplementedError("You are using the wrong operating system")
> > either of those would be better than AttributeError.

> I disagree. How would you tell if fork is implemented? With the current
> behaviour, telling whether fork is implemented or not is simple:

> is_implemented = hasattr(os, "fork")

> With your suggestion:

> try:
>     pid = os.fork()
> except NotImplementedError:
>     is_implemented = False
> else:
>     if pid == 0:
>         # In the child process.
>         os._exit(0)  # Unconditionally exit, right now, no excuses.
>     is_implemented = True

> which is not obvious, simple or cheap.

Surely I am missing something but why not check os.fork before 
checking os.fork() ?

Something along these lines

>>> try:
...   os.fork
... except AttributeError:
...   ii = False
... else:
...   ii = True


Of course more appropriate would be something along the lines:
catch AttributeError and re-raise NotImplementedError



More information about the Python-list mailing list