Unix: Disable 'crypt' command on Python >= 3.13

The module is not available anymore
This commit is contained in:
Valentin Lorentz 2024-05-05 20:41:57 +02:00 committed by Val Lorentz
parent e18332efde
commit 9ae7690484
3 changed files with 33 additions and 22 deletions

View File

@ -40,7 +40,6 @@ class InternetTestCase(PluginTestCase):
'Host not found.') 'Host not found.')
def testWhois(self): def testWhois(self):
self.assertNotError('internet whois ohio-state.edu')
self.assertNotError('internet whois microsoft.com') self.assertNotError('internet whois microsoft.com')
self.assertNotError('internet whois inria.fr') self.assertNotError('internet whois inria.fr')
self.assertNotError('internet whois slime.com.au') self.assertNotError('internet whois slime.com.au')

View File

@ -33,7 +33,6 @@ import os
import re import re
import pwd import pwd
import sys import sys
import crypt
import errno import errno
import random import random
import select import select
@ -41,6 +40,12 @@ import struct
import subprocess import subprocess
import shlex import shlex
try:
import crypt
except ImportError:
# Python >= 3.13
crypt = None
import supybot.conf as conf import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
from supybot.commands import * from supybot.commands import *
@ -119,25 +124,26 @@ class Unix(callbacks.Plugin):
irc.reply(format('%i', os.getpid()), private=True) irc.reply(format('%i', os.getpid()), private=True)
pid = wrap(pid, [('checkCapability', 'owner')]) pid = wrap(pid, [('checkCapability', 'owner')])
_cryptre = re.compile(b'[./0-9A-Za-z]') if crypt is not None: # Python < 3.13
@internationalizeDocstring _cryptre = re.compile(b'[./0-9A-Za-z]')
def crypt(self, irc, msg, args, password, salt): @internationalizeDocstring
"""<password> [<salt>] def crypt(self, irc, msg, args, password, salt):
"""<password> [<salt>]
Returns the resulting of doing a crypt() on <password>. If <salt> is Returns the resulting of doing a crypt() on <password>. If <salt> is
not given, uses a random salt. If running on a glibc2 system, not given, uses a random salt. If running on a glibc2 system,
prepending '$1$' to your salt will cause crypt to return an MD5sum prepending '$1$' to your salt will cause crypt to return an MD5sum
based crypt rather than the standard DES based crypt. based crypt rather than the standard DES based crypt.
""" """
def makeSalt(): def makeSalt():
s = b'\x00' s = b'\x00'
while self._cryptre.sub(b'', s) != b'': while self._cryptre.sub(b'', s) != b'':
s = struct.pack('<h', random.randrange(-(2**15), 2**15)) s = struct.pack('<h', random.randrange(-(2**15), 2**15))
return s return s
if not salt: if not salt:
salt = makeSalt().decode() salt = makeSalt().decode()
irc.reply(crypt.crypt(password, salt)) irc.reply(crypt.crypt(password, salt))
crypt = wrap(crypt, ['something', additional('something')]) crypt = wrap(crypt, ['something', additional('something')])
@internationalizeDocstring @internationalizeDocstring
def spell(self, irc, msg, args, word): def spell(self, irc, msg, args, word):

View File

@ -31,6 +31,11 @@
import os import os
import socket import socket
try:
import crypt
except ImportError:
crypt = None
from supybot.test import * from supybot.test import *
try: try:
@ -106,8 +111,9 @@ if os.name == 'posix':
def testProgstats(self): def testProgstats(self):
self.assertNotError('progstats') self.assertNotError('progstats')
def testCrypt(self): if crypt is not None: # Python < 3.13
self.assertNotError('crypt jemfinch') def testCrypt(self):
self.assertNotError('crypt jemfinch')
@skipUnlessFortune @skipUnlessFortune
def testFortune(self): def testFortune(self):