Skip to content

Commit

Permalink
Merge pull request #318 from CPJKU/deepcopy_score
Browse files Browse the repository at this point in the history
Fixes #304 - Workaround for fixing issues with deep copy and recursion limit
  • Loading branch information
huispaty authored Sep 22, 2023
2 parents 1262c54 + 6a2ceac commit 70daa16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 13 additions & 1 deletion partitura/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# import copy
from partitura.utils.music import MUSICAL_BEATS, INTERVALCLASSES
import warnings
import warnings, sys
import numpy as np
from scipy.interpolate import PPoly
from typing import Union, List, Optional, Iterator, Iterable as Itertype
Expand Down Expand Up @@ -4652,7 +4652,13 @@ def unfold_part_maximal(score: ScoreLike, update_ids=True, ignore_leaps=True):
"""
if isinstance(score, Score):
# Copy needs to be deep, otherwise the recursion limit will be exceeded
old_recursion_depth = sys.getrecursionlimit()
sys.setrecursionlimit(10000)
# Deep copy of score
new_score = deepcopy(score)
# Reset recursion limit to previous value to avoid side effects
sys.setrecursionlimit(old_recursion_depth)
new_partlist = list()
for score in new_score.parts:
unfolded_part = unfold_part_maximal(
Expand Down Expand Up @@ -4690,7 +4696,13 @@ def unfold_part_minimal(score: ScoreLike):
"""
if isinstance(score, Score):
# Copy needs to be deep, otherwise the recursion limit will be exceeded
old_recursion_depth = sys.getrecursionlimit()
sys.setrecursionlimit(10000)
# Deep copy of score
unfolded_score = deepcopy(score)
# Reset recursion limit to previous value to avoid side effects
sys.setrecursionlimit(old_recursion_depth)
new_partlist = list()
for part in unfolded_score.parts:
unfolded_part = unfold_part_minimal(part)
Expand Down
7 changes: 7 additions & 0 deletions partitura/utils/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,15 @@ def transpose(score: ScoreLike, interval: Interval) -> ScoreLike:
Transposed score.
"""
import partitura.score as s
import sys

# Copy needs to be deep, otherwise the recursion limit will be exceeded
old_recursion_depth = sys.getrecursionlimit()
sys.setrecursionlimit(10000)
# Deep copy of score
new_score = copy.deepcopy(score)
# Reset recursion limit to previous value to avoid side effects
sys.setrecursionlimit(old_recursion_depth)
if isinstance(score, s.Score):
for part in new_score.parts:
transpose(part, interval)
Expand Down

0 comments on commit 70daa16

Please sign in to comment.