Article 2 [source]

Spanish Criminal Code
English translation

1. No crime shall be punished with a penalty not provided for by law prior to its commission. Laws establishing security measures shall likewise lack retroactive effect.

2. However, penal laws that are more favorable to the accused shall have retroactive effect, even if a final judgment has been rendered and the individual is serving a sentence when the law enters into force. In case of doubt as to which law is more favorable, the accused shall be heard. Acts committed under a temporary law shall, however, be judged according to that law, unless expressly provided otherwise.