-
Notifications
You must be signed in to change notification settings - Fork 20
FIX: Closed Attribute in cursor __del__
#183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bewithgaurav
wants to merge
9
commits into
main
Choose a base branch
from
bewithgaurav/fix_cursor_cleanup_log
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2ce5a51
deleted whl
bewithgaurav c265627
added in gitignore
bewithgaurav 8b4d034
FIX: Closed Attribute in cursor
bewithgaurav 16a86f7
added a few comments and changed behaviout during interpreter shutdown
bewithgaurav 9a029ee
added a bunch of tests
bewithgaurav c81787a
deleted msvcp140.dll from mssql_python
bewithgaurav 5bcffa8
fixed tests
bewithgaurav 19b502e
refactoring
bewithgaurav 7a3c233
tests
bewithgaurav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,7 @@ build/ | |
*.swp | ||
|
||
# .DS_Store files | ||
.DS_Store | ||
.DS_Store | ||
|
||
# WHL files | ||
*.whl |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
from mssql_python.constants import ConstantsDDBC as ddbc_sql_const | ||
from mssql_python.helpers import check_error, log | ||
from mssql_python import ddbc_bindings | ||
from mssql_python.exceptions import InterfaceError | ||
from mssql_python.exceptions import ProgrammingError | ||
from .row import Row | ||
|
||
|
||
|
@@ -435,13 +435,17 @@ def _reset_cursor(self) -> None: | |
|
||
def close(self) -> None: | ||
""" | ||
Close the cursor now (rather than whenever __del__ is called). | ||
Close the connection now (rather than whenever .__del__() is called). | ||
Idempotent: subsequent calls have no effect. | ||
|
||
The idempotency here means that resources are freed only once - subsequent | ||
calls won't double-free resources or cause corruption, but will raise a | ||
predictable exception instead. | ||
|
||
Raises: | ||
Error: If any operation is attempted with the cursor after it is closed. | ||
""" | ||
if self.closed: | ||
raise Exception("Cursor is already closed.") | ||
self._check_closed() | ||
|
||
if self.hstmt: | ||
self.hstmt.free() | ||
|
@@ -457,7 +461,7 @@ def _check_closed(self): | |
Error: If the cursor is closed. | ||
""" | ||
if self.closed: | ||
raise Exception("Operation cannot be performed: the cursor is closed.") | ||
raise ProgrammingError("Cursor is already closed.", "") | ||
|
||
def _create_parameter_types_list(self, parameter, param_info, parameters_list, i): | ||
""" | ||
|
@@ -794,10 +798,16 @@ def __del__(self): | |
Destructor to ensure the cursor is closed when it is no longer needed. | ||
This is a safety net to ensure resources are cleaned up | ||
even if close() was not called explicitly. | ||
If the cursor is already closed, it will not raise an exception during cleanup. | ||
""" | ||
if "_closed" not in self.__dict__ or not self._closed: | ||
if "closed" not in self.__dict__ or not self.closed: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we define Let me know if I am missing anything wrt to dict if its used anywhere else. |
||
try: | ||
self.close() | ||
except Exception as e: | ||
# Don't raise an exception in __del__, just log it | ||
log('error', "Error during cursor cleanup in __del__: %s", e) | ||
# If interpreter is shutting down, we might not have logging set up | ||
import sys | ||
if sys and sys._is_finalizing(): | ||
# Suppress logging during interpreter shutdown | ||
return | ||
log('debug', "Exception during cursor cleanup in __del__: %s", e) |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes still keeps cursor.close() non‑idempotent while the comments claims it is idempotent.
close() now calls self._check_closed() at the start.
_check_closed() raises ProgrammingError when self.closed is True.
Therefore a second close() raises instead of being a no‑op. That is NOT idempotent.
First, the issue raised in 183 requires no-op\no error during second close of the cursor. Secondly, DB-API 2.0 convention defines calling .close() multiple times must be safe (no exception).
We can keep _check_closed() raising ProgrammingError only for operational methods (execute/fetch) after closure - NOT for close() itself.
Fix the tests to make sure double closing of cursor is silent.