为啥我使用 python3 遇到分段错误? [复制]

Posted

技术标签:

【中文标题】为啥我使用 python3 遇到分段错误? [复制]【英文标题】:Why am I getting a segmentation fault using python3? [duplicate]为什么我使用 python3 遇到分段错误? [复制] 【发布时间】:2020-07-16 18:53:08 【问题描述】:

我对 Python 不太熟悉,但我正在尝试创建一个程序,它接收文件名、工作表名称、每月付款、用户的额外每月付款和本金贷款金额。然后程序应该创建一个带有提交名称的 excel 文件,一个带有提交名称的工作表。在 Excel 表中,开始从余额中减去每月付款,将其写入 Excel 表并计算付款日期,然后对额外金额执行相同操作并不断重复,直到余额达到 0。我使用递归完成了此操作,但我得到以下错误

zsh: segmentation fault  python3 loanPayoff.py

以下是我的代码:

  import datetime
import xlsxwriter
import sys

sys.setrecursionlimit(10**6)

# we create the workbook
def createWorkBook(workBookName, workSheetName, monthly, principal, additional, date):
    #creating the workbook
    workbook = xlsxwriter.Workbook(workBookName + '.xlsx')
    worksheet = workbook.add_worksheet(workSheetName)
    worksheet.write('A1', 'Date')
    worksheet.write('B1', 'Mortgage principal amount')
    worksheet.write('C1', 'Regular amount against principal')
    worksheet.write('D1', 'Against principal additional')
    payments(monthly, additional, principal, worksheet, 'regular', date, 1)
    workbook.close()

# update the workbook's worksheet with the next date
def workBookUpdateDate(date, row, workSheet):
    dateColumn = 0
    workSheet.write(row, dateColumn, date)

# update the workbook's worksheet with the new balance
def workBookUpdateBalance(balance, row, workSheet):
    balanceColumn = 1
    workSheet.write(row, balanceColumn, balance)

# update the workbook's worksheet with the regular amount being paid
def workBookUpdateRegularAmount(regularAmount,row, workSheet):
    regularAmountColumn = 2
    workSheet.write(row, regularAmountColumn, regularAmount)

# update the workbook's worksheet with the additional amount being paid
def workBookUpdateAdditionalAmount(additionalAmount,row, workSheet):
    additionalAmountColumn = 3
    workSheet.write(row, additionalAmountColumn, additionalAmount)

# increment the date by two weeks
def addTwoWeeksToDate(date):
    twoWeeks = datetime.timedelta(days=14)
    newDate = date + twoWeeks
    #print("DATE ......", format(newDate))
    return newDate

def payments(monthly, additional, principal, workSheet, type, date, row):
   while principal != 0:
       if type == 'regular':
           principal = principal - monthly
           date = addTwoWeeksToDate(date)
           workBookUpdateDate(date, row, workSheet)
           workBookUpdateAdditionalAmount(0,row, workSheet)
           workBookUpdateRegularAmount(monthly, row, workSheet)
           workBookUpdateBalance(principal, row, workSheet)
           row = row + 1
           payments(0, additional, principal, workSheet, 'additional', date, row)
       elif type == 'additional':
           principal = principal - additional
           date = addTwoWeeksToDate(date)
           workBookUpdateDate(date, row, workSheet)
           workBookUpdateAdditionalAmount(additional, row, workSheet)
           workBookUpdateRegularAmount(0, row, workSheet)
           workBookUpdateBalance(principal, row, workSheet)
           row = row + 1
           payments(0, additional, principal, workSheet, 'regular', date, row)



# prompt the user for the information
fileName = input('Hello what will the file be called? ')
workSheetNameToBe = input('Ok, what will the worksheet be called? ')
monthlyPaymentAmount = float(input('How much do you pay monthly? '))
additionalPaymentAmount = float(input('How much additional can you pay monthly? '))
principalAmount = float(input('How much is the principal? '))
# set the start date for today
new_date = datetime.date.today()
createWorkBook(fileName, workSheetNameToBe, monthlyPaymentAmount, principalAmount, additionalPaymentAmount, new_date)

我目前在 Mac 10.15.4 上使用 Python 3.8.2

欢迎任何反馈。

【问题讨论】:

查看答案here。您设置的限制可能导致崩溃。 用你自己的话说,为什么payments 使用both while 循环 递归?你想用递归解决什么问题,而 while 循环还没有解决? sys.setrecursionlimit(10**6) 导致堆栈溢出和段错误。不要那样做。 【参考方案1】:

我认为你的问题是递归限制的变化

来自此链接:https://bugs.python.org/issue35542

https://docs.python.org/3/library/sys.html#sys.setrecursionlimit

可能的最高限制取决于平台。当用户有一个需要深度的程序时,他们可能需要将限制设置得更高 递归和支持更高限制的平台。这应该是 小心操作,因为过高的限制会导致崩溃。

我认为这是一个已知的情况,堆栈限制取决于 RecursionError 之前的操作系统可以在更高时引发 设置了递归限制。在我的机器(Mac 10.10.4)上,这个段错误 2.7 但在 Python 3.7.1 上引发 RecurstionError。将限制增加到 200000 会导致 3.7.1 出现段错误。

【讨论】:

【参考方案2】:

Python 在正常情况下不会“segv”。关于您的 Python 安装的某些内容已损坏。您应该尝试用全新安装替换它。

另外,以你的方式递归有点奇怪。您可以编写它以迭代运行且更清晰。

【讨论】:

sys.setrecursionlimit(10**6) 然后触发无限递归就可以了。

以上是关于为啥我使用 python3 遇到分段错误? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

为啥这个字符串反转 C 代码会导致分段错误? [复制]

为啥我在这个迭代器中遇到分段错误?

为啥我会因此遇到分段错误?

为啥我在这个 c 代码上遇到分段错误?

使用 std::copy 复制数组时出现分段错误

为啥在此 C++ 代码中出现分段错误?