Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cpython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sip
Cpython
Commits
fe17d353
Unverified
Commit
fe17d353
authored
2 years ago
by
Kumar Aditya
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
GH-81057: remove static state from suggestions.c (#99411)
parent
59665d02
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Python/suggestions.c
+19
-8
19 additions, 8 deletions
Python/suggestions.c
with
19 additions
and
8 deletions
Python/suggestions.c
+
19
−
8
View file @
fe17d353
...
...
@@ -41,10 +41,8 @@ substitution_cost(char a, char b)
static
Py_ssize_t
levenshtein_distance
(
const
char
*
a
,
size_t
a_size
,
const
char
*
b
,
size_t
b_size
,
size_t
max_cost
)
size_t
max_cost
,
size_t
*
buffer
)
{
static
size_t
buffer
[
MAX_STRING_SIZE
];
// Both strings are the same (by identity)
if
(
a
==
b
)
{
return
0
;
...
...
@@ -147,12 +145,16 @@ calculate_suggestions(PyObject *dir,
if
(
name_str
==
NULL
)
{
return
NULL
;
}
size_t
*
buffer
=
PyMem_New
(
size_t
,
MAX_STRING_SIZE
);
if
(
buffer
==
NULL
)
{
return
PyErr_NoMemory
();
}
for
(
int
i
=
0
;
i
<
dir_size
;
++
i
)
{
PyObject
*
item
=
PyList_GET_ITEM
(
dir
,
i
);
Py_ssize_t
item_size
;
const
char
*
item_str
=
PyUnicode_AsUTF8AndSize
(
item
,
&
item_size
);
if
(
item_str
==
NULL
)
{
PyMem_Free
(
buffer
);
return
NULL
;
}
if
(
PyUnicode_CompareWithASCIIString
(
name
,
item_str
)
==
0
)
{
...
...
@@ -163,8 +165,8 @@ calculate_suggestions(PyObject *dir,
// Don't take matches we've already beaten.
max_distance
=
Py_MIN
(
max_distance
,
suggestion_distance
-
1
);
Py_ssize_t
current_distance
=
levenshtein_distance
(
name_str
,
name_size
,
item_str
,
item_size
,
max_distance
);
levenshtein_distance
(
name_str
,
name_size
,
item_str
,
item_size
,
max_distance
,
buffer
);
if
(
current_distance
>
max_distance
)
{
continue
;
}
...
...
@@ -173,6 +175,7 @@ calculate_suggestions(PyObject *dir,
suggestion_distance
=
current_distance
;
}
}
PyMem_Free
(
buffer
);
return
Py_XNewRef
(
suggestion
);
}
...
...
@@ -401,6 +404,14 @@ _Py_UTF8_Edit_Cost(PyObject *a, PyObject *b, Py_ssize_t max_cost)
if
(
max_cost
==
-
1
)
{
max_cost
=
MOVE_COST
*
Py_MAX
(
size_a
,
size_b
);
}
return
levenshtein_distance
(
utf8_a
,
size_a
,
utf8_b
,
size_b
,
max_cost
);
size_t
*
buffer
=
PyMem_New
(
size_t
,
MAX_STRING_SIZE
);
if
(
buffer
==
NULL
)
{
PyErr_NoMemory
();
return
-
1
;
}
Py_ssize_t
res
=
levenshtein_distance
(
utf8_a
,
size_a
,
utf8_b
,
size_b
,
max_cost
,
buffer
);
PyMem_Free
(
buffer
);
return
res
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment