Skip to content

gh-133244: TPen.pensize raises TurtleGraphicsError if called with a negative number - #135268

Open
adorilson wants to merge 10 commits into
python:mainfrom
adorilson:fix-issue-133244-pensize
Open

gh-133244: TPen.pensize raises TurtleGraphicsError if called with a negative number#135268
adorilson wants to merge 10 commits into
python:mainfrom
adorilson:fix-issue-133244-pensize

Conversation

@adorilson

@adorilson adorilson commented Jun 8, 2025

Copy link
Copy Markdown
Contributor

Fix #133244.

Before

>>> import turtle
>>> turtle.pensize(-1)
>>> turtle.forward(100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 8, in forward
  File "/home/adorilson/anaconda3/lib/python3.12/turtle.py", line 1636, in forward
    self._go(distance)
  File "/home/adorilson/anaconda3/lib/python3.12/turtle.py", line 1597, in _go
    self._goto(ende)
  File "/home/adorilson/anaconda3/lib/python3.12/turtle.py", line 3248, in _goto
    screen._drawline(self.drawingLineItem,
  File "/home/adorilson/anaconda3/lib/python3.12/turtle.py", line 543, in _drawline
    self.cv.itemconfigure(lineitem, width=width)
  File "<string>", line 1, in itemconfigure
  File "/home/adorilson/anaconda3/lib/python3.12/tkinter/__init__.py", line 2988, in itemconfigure
    return self._configure(('itemconfigure', tagOrId), cnf, kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adorilson/anaconda3/lib/python3.12/tkinter/__init__.py", line 1712, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: bad screen distance "-1"

Now

>>> import turtle
>>> turtle.pensize(-1)
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    turtle.pensize(-1)
    ~~~~~~~~~~~~~~^^^^
  File "<string>", line 8, in pensize
  File "/home/adorilson/workspace/cpython/Lib/turtle.py", line 2145, in pensize
    raise TurtleGraphicsError(f"width argument must be a positive number. It was {width}.")
turtle.TurtleGraphicsError: width argument must be a positive number. It was -1.

For now, the 0 behaviour wasn't changed, but I think it would.

If we call turtle.pensize(0) it changes the width to 0, but when the turtle draws, the real line's width is 1. It seems to be a misconception. However, change could be a backwards compatibility concern. Perhaps we can schedule a change for now with a DeprecationWarning.

vstinner and others added 5 commits June 7, 2025 12:29
…#106335)

Remove private _PyThreadState and _PyInterpreterState C API
functions: move them to the internal C API (pycore_pystate.h and
pycore_interp.h). Don't export most of these functions anymore, but
still export functions used by tests.

Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the C
API, but keep it in the stable API.
This reverts commit ebfa093.

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is user-facing, so please add a news entry. The easiest way will be with blurb-it, unless you already have blurb installed locally.

Comment thread Lib/turtle.py Outdated
@ZeroIntensity ZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jun 9, 2025
Comment thread Misc/NEWS.d/next/Library/2025-06-09-08-11-47.gh-issue-133244.GINMl-.rst Outdated
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 25, 2026
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 30, 2026
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jun 1, 2026
Comment thread Lib/turtle.py Outdated
@@ -0,0 +1 @@
:meth:`!turtle.TPen.pensize` raises an exception if called with a negative *width* argument.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about pen(pensize=-1)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

I'll address it.

Co-authored-by: Stan Ulbrych <stan@python.org>
@adorilson

Copy link
Copy Markdown
Contributor Author

For now, the 0 behaviour wasn't changed, but I think it would.

If we call turtle.pensize(0) it changes the width to 0, but when the turtle draws, the real line's width is 1. It seems to be a misconception. However, change could be a backwards compatibility concern. Perhaps we can schedule a change for now with a DeprecationWarning.

@StanFromIreland any thought about it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make turtle.pensize smarter

5 participants