If a person extinguishes a lamp on Shabbos because he is afraid of idolators, marauders or “evil spirits” (meaning mental illness), or to allow a sick person to sleep, he is exempt. If he extinguishes it to save the lamp (which could crack when the oil runs out), the oil or the wick, he is liable. Rabbi Yosi says he is exempt except when he extinguishes the lamp in order to save the wick because doing so creates charcoal (and is therefore a constructive act of labor, for which one is liable).
(When a person is in danger, his merits are recalled to see if he deserves Divine assistance. Labor is such a time of danger.) There are three sins that jeopardize a woman in labor: failure to properly observe the laws of family purity, of separating challah from the dough, and of lighting Shabbos candles.